util

package
v0.0.0-...-0a843ca Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FileInfo

func FileInfo(name string) (bool, int64)

func IntegerHash

func IntegerHash(k uint64) uint64

func UpperPowerOfTwo

func UpperPowerOfTwo(v uint64) uint64

Types

type MmapMalloc

type MmapMalloc struct {
	Path         string `capid:"0"`
	File         *os.File
	Fd           int         `capid:"1"`
	FileBytesLen int64       `capid:"2"`
	BytesAlloc   int64       `capid:"3"`
	MMap         gommap.MMap // equiv to Mem, just avoids casts everywhere.
	Mem          []byte      `capid:"4"` // equiv to Mmap

	ReadOnly bool
}

The MmapMalloc struct represents either an anonymous, private region of memory (if path was "", or a memory mapped file if path was supplied to Malloc() at creation.

Malloc() creates and returns an MmapMalloc struct, which can then be later Free()-ed. Malloc() calls request memory directly from the kernel via mmap(). Memory can optionally be backed by a file for simplicity/efficiency of saving to disk.

For use when the Go GC overhead is too large, and you need to move the hash table off-heap.

func Malloc

func Malloc(numBytes int64, path string) *MmapMalloc

Malloc() creates a new memory region that is provided directly by OS via the mmap() call, and is thus not scanned by the Go garbage collector.

If path is not empty then we memory map to the given path. Otherwise it is just like a call to malloc(): an anonymous memory allocation, outside the realm of the Go Garbage Collector. If numBytes is -1, then we take the size from the path file's size. Otherwise the file is expanded or truncated to be numBytes in size. If numBytes is -1 then a path must be provided; otherwise we have no way of knowing the size to allocate, and the function will panic.

The returned value's .Mem member holds a []byte pointing to the returned memory (as does .MMap, for use in other gommap calls).

func MallocReadOnly

func MallocReadOnly(path string) (mm *MmapMalloc, err error)

MallocReadOnly creates new read-only memory region from existing file

func (*MmapMalloc) BackgrounSync

func (mm *MmapMalloc) BackgrounSync()

BackgroundSync() schedules a sync to disk, but may return before it is done. Without a call to either BackgroundSync() or BlockUntilSync(), there is no guarantee that file has ever been written to disk at any point before the munmap() call that happens during Free(). See the man pages msync(2) and mmap(2) for details.

func (*MmapMalloc) BlockUntilSync

func (mm *MmapMalloc) BlockUntilSync()

BlockUntilSync() returns only once the file is synced to disk.

func (*MmapMalloc) Free

func (mm *MmapMalloc) Free()

Free eleases the memory allocation back to the OS by removing the (possibly anonymous and private) memroy mapped file that was backing it. Warning: any pointers still remaining will crash the program if dereferenced.

func (*MmapMalloc) TruncateTo

func (mm *MmapMalloc) TruncateTo(newSize int64)

TruncateTo enlarges or shortens the file backing the memory map to be size newSize bytes. It only impacts the file underlying the mapping, not the mapping itself at this point.

Jump to

Keyboard shortcuts

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