mmap

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2018 License: MIT Imports: 6 Imported by: 0

README

mmap

GoDoc

Package mmap provides an interface to memory mapped files.

Memory maps can be opened as read-only with Read or read-write with Write. To specify additional flags and a file mode use Open.

There are two different ways to work with memory maps. They cannot be used simultaneously. Creating a Direct accessor will fail if any Readers or Writers are open. Creating Reader or Writer will fail if any Direct accessors are open.

The first is through direct access via a Direct, which is a pointer to a byte slice. This lets you write directly to the mapped memory, but you will need to manage access between go routines.

The second is through Readers and Writers. These implement the standard interfaces from the io package and can be treated like files while still benefitting from the improved performance of memory mapping.

You can have multiple Readers and Writers. The map will ensure that writes don't conflict with reads. That is, the underlying map won't change during the middle of a read.

When the map is resized via Truncate, all open Direct, Reader, and Writer objects are closed.

Documentation

Overview

Package mmap provides an interface to memory mapped files.

Memory maps can be opened as read-only with Read or read-write with Write. To specify additional flags and a file mode use Open.

There are two different ways to work with memory maps. They cannot be used simultaneously. Creating a Direct accessor will fail if any Readers or Writers are open. Creating Reader or Writer will fail if any Direct accessors are open.

The first is through direct access via a Direct, which is a pointer to a byte slice. This lets you write directly to the mapped memory, but you will need to manage access between go routines.

The second is through Readers and Writers. These implement the standard interfaces from the io package and can be treated like files while still benefitting from the improved performance of memory mapping.

You can have multiple Readers and Writers. The map will ensure that writes don't conflict with reads. That is, the underlying map won't change during the middle of a read.

When the map is resized via Truncate, all open Direct, Reader, and Writer objects are closed.

Index

Constants

View Source
const (
	ReadOnly  int = os.O_RDONLY // open the file read-only
	ReadWrite int = os.O_RDWR   // open the file read-write
	Create    int = os.O_CREATE // create a new file if none exists
	Exclusive int = os.O_EXCL   // used with O_CREATE, file must not exist
	Sync      int = os.O_SYNC   // calls Sync() after each write
	Truncate  int = os.O_TRUNC  // if possible, truncate file when opened
)

Exactly one of ReadOnly or ReadWrite must be specified. The remaining values may be or'ed in to control behavior.

View Source
const (
	SeekStart   int = 0 // seek relative to the start of the file
	SeekCurrent int = 1 // seek relative to the current offset
	SeekEnd     int = 2 // seek relative to the end
)

Constants used for whence in Seek.

Variables

This section is empty.

Functions

This section is empty.

Types

type Direct

type Direct *[]byte

Direct is a pointer to a byte slice that directly accesses the memory map. Multiple Direct pointers can be created, but only if there are no open Readers or Writers on the Map.

As a pointer, the value will have to be dereferenced when used.

b, _ := memmap.Direct()
n := len(*b)
for i := range *b {
        *(b)[i] = i % 256
}

Warning: Do not copy or assign the Direct to another variable. The copy won't be released when the original is and will become invalid if the map is closed or resized.

b, _ := memmap.Direct()
x := *b // Do not do this!

type Map

type Map struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Map represents a file on disk that has been mapped into memory.

func Open

func Open(name string, flags int, mode os.FileMode) (*Map, error)

Open opens a file as a memory map using the given flags. It does not support the O_WRONLY or O_APPEND flags. It will create the file if it doesn't exist. If the file is empty or O_TRUNC is specified, the file will be resized to the size of a memory page as returned by os.Getpagesize() and the bytes will be zeroed out.

func Read

func Read(name string) (*Map, error)

Read opens a file as a read-only memory map.

func Write

func Write(name string) (*Map, error)

Write opens a file as a writeable memory map. It will create the file if it doesn't exist with FileMode 0600. It does not truncate the file, however if the file size is 0, it will resize the file to be size of a memory page as returned by os.Getpagesize(). If a different size is needed, call Truncate after opening.

func (*Map) Close

func (m *Map) Close() error

Close closes the memory map and returns an error if any.

func (*Map) Closed

func (m *Map) Closed() bool

Closed indicates if the map is closed.

func (*Map) Direct

func (m *Map) Direct() (Direct, error)

Direct creates a Direct slice to the entire memory map.

func (*Map) DirectAt

func (m *Map) DirectAt(offset int, size int) (Direct, error)

DirectAt creates a Direct slice to a region of the memory map specified by offset and size.

func (*Map) Free

func (m *Map) Free(direct Direct) error

Free releases a Direct slice

func (*Map) Name

func (m *Map) Name() string

Name returns the name of the backing file.

func (*Map) Reader

func (m *Map) Reader() (*Reader, error)

Reader returns a new Reader for the map.

func (*Map) Size

func (m *Map) Size() int

Size returns the size of the map.

func (*Map) Sync

func (m *Map) Sync(wait bool) error

Sync flushes all changes to the map out to the backing file.

func (*Map) Truncate

func (m *Map) Truncate(size int64) error

Truncate resizes the backing file and the memory map to the requested size. Any open Direct, Readers, and Writers are closed.

func (*Map) WriteSync

func (m *Map) WriteSync() bool

WriteSync indicates if the map uses synchronous writes.

func (*Map) Writeable

func (m *Map) Writeable() bool

Writeable indicates if the map is writeable.

func (*Map) Writer

func (m *Map) Writer() (*Writer, error)

Writer returns a new Writer for the map.

type Reader

type Reader struct {
	*Map
	// contains filtered or unexported fields
}

Reader reads from a map. It implements the following interfaces from the io standard package:

  • Reader (Read)
  • ReaderAt (ReadAt)
  • ByteReader (ReadByte)
  • Seeker (Seek)
  • Closer (Close)
  • ReadCloser (Read, Close)
  • ReadSeeker (Read, Seek)

func (*Reader) Close

func (r *Reader) Close() error

Close closes the Reader.

func (*Reader) Peek

func (r *Reader) Peek(offset int) (byte, error)

Peek returns the value of the byte at offset.

func (*Reader) Read

func (r *Reader) Read(b []byte) (n int, err error)

Read reads up to len(b) bytes from the map Reader. It returns the number of bytes read and any error encountered. At end of map, Read returns 0, io.EOF.

func (*Reader) ReadAt

func (r *Reader) ReadAt(b []byte, offset int64) (n int, err error)

ReadAt reads len(b) bytes from the Map starting at offset. It returns the number of bytes read and the error, if any. ReadAt returns io.EOF when n < len(b).

func (*Reader) ReadByte

func (r *Reader) ReadByte() (byte, error)

ReadByte reads and returns the next byte from the map. It returns io.EOF if the Reader is at the end of the file.

func (*Reader) Seek

func (r *Reader) Seek(offset int64, whence int) (ret int64, err error)

Seek sets the offset for the next Read or Write on Reader to offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an error, if any.

type Writer

type Writer struct {
	*Reader
}

Writer reads from and writes to a map. In addition to the methods of mmap.Reader, it also implements the following interfaces from the io standard package:

  • Writer (Write)
  • WriterAt (WriteAt)
  • ByteWriter (WriteByte)
  • WriteSeeker (Write, Seek)
  • WriteCloser (Write, Close)
  • ReadWriter (Read, Write)
  • ReadWriteSeeker (Read, Write, Seek)
  • ReadWriteCloser (Read, Write, Close)

func (*Writer) Poke

func (w *Writer) Poke(b byte, offset int) error

Poke sets the byte at offset.

func (*Writer) Write

func (w *Writer) Write(b []byte) (n int, err error)

Write writes len(b) bytes to the File. It returns the number of bytes written and an error, if any. Write returns io.ErrShortWrite when n < len(b).

func (*Writer) WriteAt

func (w *Writer) WriteAt(b []byte, offset int64) (n int, err error)

WriteAt writes len(b) bytes to the File starting at byte offset off. It returns the number of bytes written and an error, if any. WriteAt returns io.ErrShortWrite when n < len(b).

func (*Writer) WriteByte

func (w *Writer) WriteByte(b byte) error

WriteByte a byte to the map. It returns io.EOF if the writer is at the end of the file.

func (*Writer) WriteString

func (w *Writer) WriteString(s string) (n int, err error)

WriteString is like Write, but writes the contents of string s rather than a slice of bytes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL