mmap

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2022 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const MaxInt = int(^uint(0) >> 1)

MaxInt is the maximum platform dependent signed integer.

Variables

View Source
var (
	ErrBadMode     = fmt.Errorf("mmap: bad mode")
	ErrBadOffset   = fmt.Errorf("mmap: bad offset")
	ErrBadLength   = fmt.Errorf("mmap: bad length")
	ErrClosed      = fmt.Errorf("mmap: mapping closed")
	ErrLocked      = fmt.Errorf("mmap: mapping already locked")
	ErrNotLocked   = fmt.Errorf("mmap: mapping is not locked")
	ErrOutOfBounds = fmt.Errorf("mmap: out of bounds")
	ErrReadOnly    = fmt.Errorf("mmap: mapping is read only")
	ErrSeekWhence  = errors.New("mmap: invalid seek whence")
	ErrSeekOffset  = errors.New("mmap: invalid seek offset")
)

Functions

func OpenFile

func OpenFile(path string) (*os.File, error)

func UnsafeBytesToString

func UnsafeBytesToString(bytes []byte) string

UnsafeBytesToString converts bytes to string saving allocations

func UnsafeStringToBytes

func UnsafeStringToBytes(s string) []byte

UnsafeStringToBytes converts bytes to string saving allocations by re-using

Types

type Flag

type Flag int

Flag is a mapping flag

const (
	// Mapped memory pages may be executed
	FlagExecutable Flag = 1 << iota
)

type Mapping

type Mapping struct {
	// contains filtered or unexported fields
}

Mapping is a mapping of the file into the memory.

func Open

func Open(fd uintptr, offset int64, length uintptr, mode Mode, flags Flag) (*Mapping, error)

Open opens and returns a new mapping of the given file into the memory. The given file descriptor will be duplicated. It means that if the parent file will be closed the mapping will still be valid. Actual offset and length may be different than the given by the reason of aligning to the memory page size.

func OpenFileMapping

func OpenFileMapping(name string, perm os.FileMode, size uintptr, flags Flag, init func(mf *Mapping) error) (*Mapping, error)

OpenMappedFile prepares a file, calls the initializer if file was just created and returns a new mapping of the prepared file into the memory.

func (*Mapping) Address

func (m *Mapping) Address() uintptr

Address returns the pointer to the mapped memory.

func (*Mapping) Close

func (m *Mapping) Close() error

Close closes this mapping and frees all resources associated with it. Mapped memory will be synchronized with the underlying file and unlocked automatically. Close implements the io.Closer interface.

func (*Mapping) Executable

func (m *Mapping) Executable() bool

Executable returns true if the mapped memory pages may be executed.

func (*Mapping) Length

func (m *Mapping) Length() uintptr

Length returns the mapped memory length in bytes.

func (*Mapping) Lock

func (m *Mapping) Lock() error

Lock locks the mapped memory pages. All pages that contain a part of the mapping address range are guaranteed to be resident in RAM when the call returns successfully. The pages are guaranteed to stay in RAM until later unlocked. It may need to increase process memory limits for operation success. See working set on Windows and rlimit on Linux for details.

func (*Mapping) Memory

func (m *Mapping) Memory() []byte

Memory returns the byte slice which wraps the mapped memory.

func (*Mapping) Read

func (m *Mapping) Read(buf []byte) (int, error)

Read reads len(buf) bytes from the internal offset from the mapped memory. If the offset is out of the available bounds or there are not enough bytes to read the ErrOutOfBounds error will be returned. Otherwise len(buf) will be returned with no errors. Read implements the io.Reader interface.

func (*Mapping) ReadAt

func (m *Mapping) ReadAt(buf []byte, offset int64) (int, error)

ReadAt reads len(buf) bytes at the given offset from start of the mapped memory from the mapped memory. If the given offset is out of the available bounds or there are not enough bytes to read the ErrOutOfBounds error will be returned. Otherwise len(buf) will be returned with no errors. ReadAt implements the io.ReaderAt interface.

func (*Mapping) Seek

func (m *Mapping) Seek(offset int64, whence int) (int64, error)

Seek sets the offset for the next Read or Write to offset, interpreted according to whence: SeekStart means relative to the start of the file, SeekCurrent means relative to the current offset, and SeekEnd means relative to the end. Seek returns the new offset relative to the start of the file and an error, if any. Seeking to an offset before the start of the file is an error. Seeking to any positive offset is legal, but the behavior of subsequent I/O operations on the underlying object is implementation-dependent.

func (*Mapping) Segment

func (m *Mapping) Segment() *segment.Segment

Segment returns the data segment on top of the mapped memory.

func (*Mapping) Sync

func (m *Mapping) Sync() error

Sync synchronizes the mapped memory with the underlying file.

func (*Mapping) Unlock

func (m *Mapping) Unlock() error

Unlock unlocks the previously locked mapped memory pages.

func (*Mapping) Writable

func (m *Mapping) Writable() bool

Writable returns true if the mapped memory pages may be written.

func (*Mapping) Write

func (m *Mapping) Write(buf []byte) (int, error)

Write writes len(buf) bytes from the internal offset into the mapped memory. If the offset is out of the available bounds or there are not enough space to write all given bytes the ErrOutOfBounds error will be returned. Otherwise len(buf) will be returned with no errors. Write implements the io.Writer interface.

func (*Mapping) WriteAt

func (m *Mapping) WriteAt(buf []byte, offset int64) (int, error)

WriteAt writes len(buf) bytes at the given offset from start of the mapped memory into the mapped memory. If the given offset is out of the available bounds or there are not enough space to write all given bytes the ErrOutOfBounds error will be returned. Otherwise len(buf) will be returned with no errors. WriteAt implements the io.WriterAt interface.

type Mode

type Mode int

Mode is a mapping mode.

const (
	// Share this mapping and allow the read-only access
	ModeReadOnly Mode = iota

	// Share this mapping
	// Updates to the mapping are visible to other processes
	// mapping the same region, and are carried through to the underlying file.
	// To precisely control when updates are carried through to the underlying file
	// requires the use of Mapping.Sync.
	ModeReadWrite

	// Create a private copy-on-write mapping.
	// Updates to the mapping are not visible to other processes
	// mapping the same region, and are not carried through to the underlying file.
	// It is unspecified whether changes made to the file are visible in the mapped region
	ModeWriteCopy
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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