mmap

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: GPL-2.0 Imports: 5 Imported by: 1

README

What is this

This is an OS independent mmap(2) abstractions.

How can I use it?

Here is an example program:


    // error checking omitted

    fd, _ := os.Open(filename)
    
    map := mmap.New(fd)

    // map the whole file as a RO mapping for sequential I/O
    p, err := map.Map(0, 0, mmap.PROT_READ, mmap.F_READAHEAD)
    if err != nil {
        ...
    }

    // p now represents the mapping of the entire file

    // buf represents the file contents as a byte slice
    buf := p.Bytes()
    ...

    p.Unmap()
    fd.Close()

Documentation

Overview

Package mmap provides an OS independent interface for memory mapped files

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Reader

func Reader(fd *os.File, fp func(buf []byte) error) (int64, error)

Reader mmap's chunks of the file and calls the given closure with successive chunks of the file contents until EOF. If the closure returns non-nil error, it breaks the iteration and the error is propogated back to the caller. Reader returns the number of bytes of read.

Types

type Flag

type Flag uint

Flag describes additional properties for a given mapping

const (
	F_COW Flag = 1 << iota
	F_HUGETLB
	F_READAHEAD
)

type Mapping

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

func (*Mapping) Bytes

func (p *Mapping) Bytes() []byte

Bytes returns a byte slice corresponding to the mapping

func (*Mapping) Flush

func (p *Mapping) Flush() error

Flush flushes any changes to the backing disk (or swap for anon mappings)

func (*Mapping) Lock

func (p *Mapping) Lock() error

Lock locks the given mappings in memory (prevents page out)

func (*Mapping) Unlock

func (p *Mapping) Unlock() error

Unlock unlocks the given mappings (enable page out as needed)

func (*Mapping) Unmap

func (p *Mapping) Unmap() error

Unmap unmaps the given mapping

type Mmap

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

Mmap describes mappings for a file backed object

func New

func New(fd *os.File) *Mmap

New creates a new memory map object for the given file. It is a runtime error for a file to be opened in RO mode while asking for a PROT_WRITE mapping.

func NewAnon

func NewAnon() *Mmap

NewAnon creates a mmemory map object suitable for anon mappings.

func (*Mmap) Map

func (m *Mmap) Map(sz, off int64, prot Prot, flags Flag) (*Mapping, error)

Map creates a memory mapping at offset 'off' for 'sz' bytes.

func (*Mmap) Unmap

func (m *Mmap) Unmap(p *Mapping) error

Unmap unmaps a given mapping

type Prot

type Prot uint

Prot describes the protections for a mapping

const (
	PROT_READ Prot = 1 << iota
	PROT_WRITE
	PROT_EXEC
)

Jump to

Keyboard shortcuts

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