class Memmap::MapFile

Overview

A memory mapped buffer backed by a specified file.

The safest way to access the data mapped is through the #value getter, which returns a Slice(UInt8). Any access through the raw pointer interface can cause segmentation faults or undefined behavior unless you're really careful, while accessing the buffer through a Slice allows you to reap the potential benefits of using mmap without shooting yourself in the foot because of its bound checks.

Defined in:

memmap/unix.cr

Constant Summary

DEFAULT_PERM = File::Permissions.new(420)
PAGE_SIZE = (LibC.sysconf(LibC::SC_PAGESIZE)).to_u64

Constructors

Instance Method Summary

Constructor Detail

def self.new(filepath : String, mode = "r", offset : LibC::SizeT = 0) #

Create an instance of MapFile.


[View source]

Instance Method Detail

def <<(appendix : Bytes) #

[View source]
def as_ptr : Pointer(UInt8) #

Returns the buffer as a raw Pointer(UInt8). This is unsafe, obviously.


[View source]
def close #

Force the map to close before the GC runs finalize


[View source]
def flush #

Flush changes made in the map back into the filesystem. Synchronous/blocking version.


[View source]
def make_read_only #

Call mprotect to change the 'prot' flags to be read-only


[View source]
def make_writable #

Call mprotect to change the 'prot' flags to allow reading and writing


[View source]
def push(appendix : Bytes) #

Append a Slice(UInt8)/Bytes to a mapped file by calling ftruncate on the mapped file's fdesc, lseeking to the 'old' end of the file, writing the Bytes to the file, and either calling mremap if we're on Linux or munmap and then mmap if we're on macOS/FreeBSD/whatever.


[View source]
def read(slice : Bytes) #

[View source]
def seek(offset, whence : Seek = IO::Seek::Set) #

Move the seek pointer for the mapped fdesc


[View source]
def to_s #

[View source]
def value : Slice(UInt8) #

[View source]
def write(filepath : String, appendix : Bytes) : Symbol #

Append a Slice(UInt8)/Bytes to a mapped file by writing the already-mapped buffer concatenated with the new bytes to a newly allocated mapped buffer backed by a new file. This relies on ftruncate and some pointer arithmetic to work correctly, so tread carefully. If you point it in the wrong direction it will eat your data.


[View source]