mmap

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2019 License: MIT Imports: 6 Imported by: 0

README

Published under MIT license with the permission of NX Studio.

Memory mapping GoDoc

This is cross-platform Golang package for memory mapped file I/O.

Currently has been tested on following architectures:

  • windows/amd64
  • linux/amd64

Installation

$ go get github.com/alexeymaximov/mmap

TODO

  • Support of all architectures which are available in Golang.
  • Working set management on Windows.
  • RLimit management on Linux.
  • Memory segment interface (accessing typical integer values by offset).

Documentation

Overview

Package mmap provides the cross-platform memory mapped file I/O. Note than all provided tools are not thread safe.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorClosed

type ErrorClosed struct{}

ErrorClosed is an error which returns when tries to access the closed mapping.

func (*ErrorClosed) Error

func (err *ErrorClosed) Error() string

Implementation of the error interface.

type ErrorIllegalOperation added in v0.2.0

type ErrorIllegalOperation struct{ Operation string }

ErrorIllegalOperation is an error which returns when tries to execute illegal operation for the mapping.

func (*ErrorIllegalOperation) Error added in v0.2.0

func (err *ErrorIllegalOperation) Error() string

Implementation of the error interface.

type ErrorInvalidLength added in v0.2.0

type ErrorInvalidLength struct{ Length uintptr }

ErrorInvalidLength is an error which returns when specified length is invalid.

func (*ErrorInvalidLength) Error added in v0.2.0

func (err *ErrorInvalidLength) Error() string

Implementation of the error interface.

type ErrorInvalidMode

type ErrorInvalidMode struct{ Mode Mode }

ErrorInvalidMode is an error which returns when specified mapping mode is invalid.

func (*ErrorInvalidMode) Error

func (err *ErrorInvalidMode) Error() string

Implementation of the error interface.

type ErrorInvalidOffset

type ErrorInvalidOffset struct{ Offset int64 }

ErrorInvalidOffset is an error which returns when specified offset is invalid.

func (*ErrorInvalidOffset) Error

func (err *ErrorInvalidOffset) Error() string

Implementation of the error interface.

type ErrorLocked added in v0.2.0

type ErrorLocked struct{}

ErrorLocked is an error which returns when mapping memory pages were already locked.

func (*ErrorLocked) Error added in v0.2.0

func (err *ErrorLocked) Error() string

Implementation of the error interface.

type ErrorPartialCommit added in v0.3.0

type ErrorPartialCommit struct{ BytesCommitted int }

ErrorPartialCommit is an error which returns when the transaction was committed partially.

func (*ErrorPartialCommit) Error added in v0.3.0

func (err *ErrorPartialCommit) Error() string

Implementation of the error interface.

type ErrorTransactionClosed added in v0.3.0

type ErrorTransactionClosed struct{}

ErrorTransactionClosed is an error which returns when tries to access the closed transaction.

func (*ErrorTransactionClosed) Error added in v0.3.0

func (err *ErrorTransactionClosed) Error() string

Implementation of the error interface.

type ErrorUnlocked added in v0.2.0

type ErrorUnlocked struct{}

ErrorUnlocked is an error which returns when mapping memory pages were not locked.

func (*ErrorUnlocked) Error added in v0.2.0

func (err *ErrorUnlocked) Error() string

Implementation of the error interface.

type Flag added in v0.2.0

type Flag int

Flags is a mapping flags.

const (
	// Mapped memory pages may be executed.
	FlagExecutable Flag = 0x1
)

type Mapping

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

Mapping is a mapping of file into the memory.

func New

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

New returns a new mapping of file into the memory. Actual offset and length may be different than specified by the reason of aligning to page size.

func (*Mapping) Address added in v0.2.0

func (m *Mapping) Address() uintptr

Address returns pointer to mapped memory.

func (*Mapping) Begin added in v0.2.0

func (m *Mapping) Begin(offset int64, length uintptr) (*Transaction, error)

Begin starts the transaction for this mapping.

func (*Mapping) Close

func (m *Mapping) Close() error

Close closes this mapping and frees all resources associated with it. Mapping will be synchronized with the underlying file and unlocked automatically. Implementation of io.Closer.

func (*Mapping) Executable added in v0.2.0

func (m *Mapping) Executable() bool

Executable returns true if mapped memory pages may be executed.

func (*Mapping) Length added in v0.2.0

func (m *Mapping) Length() uintptr

Length returns mapped memory length in bytes.

func (*Mapping) Lock added in v0.2.0

func (m *Mapping) Lock() error

Lock locks mapped memory pages. All pages that contain a part of 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 added in v0.2.0

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

Memory returns mapped memory as a byte slice.

func (*Mapping) ReadAt

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

Read reads len(buf) bytes at given offset from mapped memory. Implementation of io.ReaderAt.

func (*Mapping) Sync

func (m *Mapping) Sync() error

Sync synchronizes mapping with the underlying file.

func (*Mapping) Unlock added in v0.2.0

func (m *Mapping) Unlock() error

Unlock unlocks mapped memory pages.

func (*Mapping) Writable added in v0.2.0

func (m *Mapping) Writable() bool

Writable returns true if mapped memory pages may be written.

func (*Mapping) WriteAt

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

Write writes len(buf) bytes at given offset to mapped memory. Implementation of io.WriterAt.

type Mode

type Mode int

Mode is a mapping mode.

const (
	// Share this mapping and allow 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 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
)

type Transaction added in v0.3.0

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

Transaction is a transaction over the mapping. The transaction is not valid if parent mapping is closed.

func NewTransaction added in v0.3.0

func NewTransaction(m *Mapping, offset int64, length uintptr) (*Transaction, error)

NewTransaction returns a new transaction over the specified mapping. Transaction snapshot allocating into the heap starts at specified offset and has specified length.

func (*Transaction) Commit added in v0.3.0

func (tx *Transaction) Commit() error

Commit flushes snapshot to mapped memory, closes this transaction and frees all resources associated with it.

func (*Transaction) Flush added in v0.3.0

func (tx *Transaction) Flush() error

Flush commits this transaction and synchronize parent mapping with the underlying file.

func (*Transaction) Length added in v0.3.0

func (tx *Transaction) Length() uintptr

Length returns snapshot length in bytes.

func (*Transaction) Offset added in v0.3.0

func (tx *Transaction) Offset() int64

Offset returns starting offset of this transaction.

func (*Transaction) ReadAt added in v0.3.0

func (tx *Transaction) ReadAt(buf []byte, offset int64) (int, error)

Read reads len(buf) bytes at given offset relatively to parent mapping address from the snapshot. Implementation of io.ReaderAt.

func (*Transaction) Rollback added in v0.3.0

func (tx *Transaction) Rollback() error

Rollback closes this transaction and frees all resources associated with it.

func (*Transaction) WriteAt added in v0.3.0

func (tx *Transaction) WriteAt(buf []byte, offset int64) (int, error)

Write writes len(buf) bytes at given offset relatively to parent mapping address to the snapshot. Implementation of io.WriterAt.

Jump to

Keyboard shortcuts

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