mmf

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: May 7, 2019 License: Apache-2.0 Imports: 9 Imported by: 5

Documentation

Overview

Package mmf implements privitives for mapping files into memory.

Index

Examples

Constants

View Source
const (
	MEM_READ_ONLY     = 0x00000001
	MEM_READ_PRIVATE  = 0x00000002
	MEM_READWRITE     = 0x00000004
	MEM_COPY_ON_WRITE = 0x00000008
)

Constants for memory regions.

Variables

This section is empty.

Functions

func UseMemoryRegion

func UseMemoryRegion(region *MemoryRegion)

UseMemoryRegion ensures, that the object is still alive at the moment of the call. The usecase is when you use memory region's Data() and don't use the region itself anymore. In this case the region can be gc'ed, the memory mapping destroyed and you can get segfault. It can be used like the following:

region := NewMemoryRegion(...)
defer UseMemoryRegion(region)
data := region.Data()
{ work with data }

However, it is better to use MemoryRegionReader/Writer. This function could be removed in future releases.

Types

type Mappable

type Mappable interface {
	Fd() uintptr
}

Mappable is a named object, which can return a handle, that can be used as a file descriptor for mmap.

type MemoryRegion

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

MemoryRegion is a mmapped area of a memory object. Warning. The internal object has a finalizer set, so the region will be unmapped during the gc. Thus, you should be careful getting internal data. For example, the following code may crash:

func f() {
	region := NewMemoryRegion(...)
	return g(region.Data())
}

region may be gc'ed while its data is used by g(). To avoid this, you can use UseMemoryRegion() or region readers/writers.

Example
// this example shows how to copy a file using mmf.

// open source file for reading.
inFile, err := os.Open("in.dat")
if err != nil {
	panic("open file")
}
stat, err := inFile.Stat()
if err != nil {
	panic("stat")
}
// open destination file for writing.
outFile, err := os.Create("out.dat")
if err != nil {
	panic("create file")
}
// then, mmap contents of both files.
inRegion, err := NewMemoryRegion(inFile, MEM_READ_ONLY, 0, 0)
if err != nil {
	panic("in region")
}
defer inRegion.Close()
outRegion, err := NewMemoryRegion(outFile, MEM_READWRITE, 0, 0)
if err != nil {
	panic("out region")
}
defer outRegion.Close()

// copy file contents.
rd := NewMemoryRegionReader(inRegion)
wr := NewMemoryRegionWriter(outRegion)
written, err := io.Copy(wr, rd)

if err != nil || written != stat.Size() {
	panic("copy")
}

// ensure the data has been written.
if err := outRegion.Flush(false); err != nil {
	panic("flush")
}
Output:

func NewMemoryRegion

func NewMemoryRegion(object Mappable, flag int, offset int64, size int) (*MemoryRegion, error)

NewMemoryRegion creates a new shared memory region.

object - an object to mmap.
flag - open flags. see MEM_* constants.
offset - offset in bytes from the beginning of the mmaped file.
size - mapping size.

func (*MemoryRegion) Close

func (region *MemoryRegion) Close() error

Close unmaps the regions so that it cannot be longer used.

func (*MemoryRegion) Data

func (region *MemoryRegion) Data() []byte

Data returns region's mapped data. This function can be dangerous and could be removed in future releases.

func (*MemoryRegion) Flush

func (region *MemoryRegion) Flush(async bool) error

Flush syncs mapped content with the file data.

func (*MemoryRegion) Size

func (region *MemoryRegion) Size() int

Size returns mapping size.

type MemoryRegionReader

type MemoryRegionReader struct {
	*bytes.Reader
	// contains filtered or unexported fields
}

MemoryRegionReader is a reader for safe operations over a shared memory region. It holds a reference to the region, so the former can't be gc'ed.

func NewMemoryRegionReader

func NewMemoryRegionReader(region *MemoryRegion) *MemoryRegionReader

NewMemoryRegionReader creates a new reader for the given region.

type MemoryRegionWriter

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

MemoryRegionWriter is a writer for safe operations over a shared memory region. It holds a reference to the region, so the former can't be gc'ed.

func NewMemoryRegionWriter

func NewMemoryRegionWriter(region *MemoryRegion) *MemoryRegionWriter

NewMemoryRegionWriter creates a new writer for the given region.

func (*MemoryRegionWriter) Write

func (w *MemoryRegionWriter) Write(p []byte) (n int, err error)

Write is to implement io.Writer.

func (*MemoryRegionWriter) WriteAt

func (w *MemoryRegionWriter) WriteAt(p []byte, off int64) (n int, err error)

WriteAt is to implement io.WriterAt.

type SizedObject added in v0.2.0

type SizedObject interface {
	Size() int64
}

SizedObject is an object, which allows to obtain its size.

Jump to

Keyboard shortcuts

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