fmap

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2019 License: GPL-2.0 Imports: 7 Imported by: 7

Documentation

Overview

File memory MAP

The fmap package implements a nice way to memory map a file for block oriented operations. It's interface is block oriented and allows you to allocate and free blocks inside of the file. To support resizing the underlying file outstanding pointers are tracked and are expected to be released. This is done through run time checking.

Index

Constants

View Source
const BLOCKSIZE = 4096

Variables

The flag used when creating the file

The flag used when opening the file

Functions

func MemClr

func MemClr(bytes []byte)

Zero the bytes of the passed in slice. It uses the length not the capacity of the slice. It uses the libc function memset under the hood to do this. Go has an implementation of this function in assembly in the runtime package but I could not find a nice way to expose it. So here is the libc version exposed (via several method calls and the cgo interface).

Types

type BlockFile

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

A BlockFile represents the memory mapped file. It has a blocksize all operations are done as block aligned operations.

func Anonymous

func Anonymous(blksize uint32) (*BlockFile, error)

Create an anonymous blockfile. There is no backing file. The blocksize must be divisible by 4096 as usual

func CreateBlockFile

func CreateBlockFile(path string) (*BlockFile, error)

Create a blockfile with the standard block size (4096 which is normally the OS page size).

func CreateBlockFileCustomBlockSize

func CreateBlockFileCustomBlockSize(path string, blksize uint32) (*BlockFile, error)

Create a blockfile with a custom blocksize. Note, the size must be a multiple of 4096.

func OpenBlockFile

func OpenBlockFile(path string) (*BlockFile, error)

Open a previously created BlockFile. This will fail if you didn't use the creation functions to create the file (or at least have undefined effects).

func (*BlockFile) Address

func (self *BlockFile) Address() uintptr

What is the address of the file in the address space of the program. Use this at your own risk!

func (*BlockFile) Allocate

func (self *BlockFile) Allocate() (offset uint64, err error)

Allocate 1 block and return its offset.

func (*BlockFile) AllocateBlocks

func (self *BlockFile) AllocateBlocks(n int) (offset uint64, err error)

Allocate n blocks. Return the offset of the first block. These are guarranteed to be sequential. This always causes a file resize at the moment.

func (*BlockFile) BlockSize

func (self *BlockFile) BlockSize() int

The blocksize for this file.

func (*BlockFile) Close

func (self *BlockFile) Close() error

Close the file. Unmaps the region. There must be no outstanding blocks.

func (*BlockFile) ControlData

func (self *BlockFile) ControlData() (data []byte, err error)

Get the "control data" this free form data which is stored in the control block file. You can put whatever you want in here.

func (*BlockFile) Do

func (self *BlockFile) Do(offset, blocks uint64, do func([]byte) error) error

Load the blocks at the give offset then call the callback, `do`, passing in the loaded bytes. This function releases those bytes after your callback is done. This is the recommended interface to the contents of the memory mapped region.

func (*BlockFile) Free

func (self *BlockFile) Free(offset uint64) error

Free the block at the given offset. The offset is in bytes from the start of the file.

func (*BlockFile) Get

func (self *BlockFile) Get(offset, blocks uint64) ([]byte, error)

Get the bytes at the offset and block count. You probably want to use Do instead. You must call Release() on the bytes when done.

func (*BlockFile) Path

func (self *BlockFile) Path() string

The file system path to this file.

func (*BlockFile) Release

func (self *BlockFile) Release(bytes []byte) error

Release() bytes aquired with Get(). Should error if the bytes where not allocated from the mapping. But why take chances, you probably want to use the Do interface instead.

func (*BlockFile) Remove

func (self *BlockFile) Remove() error

Remove the underlying file. (must be already closed).

func (*BlockFile) SetControlData

func (self *BlockFile) SetControlData(data []byte) (err error)

Put user data into the control block of the file.

func (*BlockFile) SetControlDataNoSync

func (self *BlockFile) SetControlDataNoSync(data []byte) (err error)

Same as SetControlData but does not call Sync() at the end.

func (*BlockFile) Size

func (self *BlockFile) Size() (uint64, error)

func (*BlockFile) Sync

func (self *BlockFile) Sync() error

Sync the mmap'ed changes to disk. This uses the async interface (via the MS_ASYNC flag) so the changes may not be written by the time this method returns. However, they will be written soon.

func (*BlockFile) Valid

func (self *BlockFile) Valid(address uintptr) bool

Is the address given still the address of the memory map?

Jump to

Keyboard shortcuts

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