class Memmap::MapFile
- Memmap::MapFile
- Reference
- Object
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.crConstant Summary
-
DEFAULT_PERM =
File::Permissions.new(420)
-
PAGE_SIZE =
(LibC.sysconf(LibC::SC_PAGESIZE)).to_u64
Constructors
-
.new(filepath : String, mode = "r", offset : LibC::SizeT = 0)
Create an instance of
MapFile
.
Instance Method Summary
- #<<(appendix : Bytes)
-
#as_ptr : Pointer(UInt8)
Returns the buffer as a raw
Pointer(UInt8)
. -
#close
Force the map to close before the GC runs
finalize
-
#flush
Flush changes made in the map back into the filesystem.
-
#make_read_only
Call
mprotect
to change the 'prot' flags to be read-only -
#make_writable
Call
mprotect
to change the 'prot' flags to allow reading and writing -
#push(appendix : Bytes)
Append a
Slice(UInt8)
/Bytes
to a mapped file by callingftruncate
on the mapped file's fdesc,lseek
ing to the 'old' end of the file, writing theBytes
to the file, and either callingmremap
if we're on Linux ormunmap
and thenmmap
if we're on macOS/FreeBSD/whatever. - #read(slice : Bytes)
-
#seek(offset, whence : Seek = IO::Seek::Set)
Move the seek pointer for the mapped fdesc
- #to_s
- #value : Slice(UInt8)
-
#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.
Constructor Detail
Create an instance of MapFile
.
Instance Method Detail
Returns the buffer as a raw Pointer(UInt8)
. This is unsafe, obviously.
Append a Slice(UInt8)
/Bytes
to a mapped file by calling ftruncate
on the mapped file's
fdesc, lseek
ing 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.
Move the seek pointer for the mapped fdesc
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.