class RemiLib::MmappedFile

Overview

The MmappedFile class provides a convenient way to read from and write to files using mmap(). This is only supported on POSIX systems, and is not meant to be a complete interface to everything you can do with mmap().

TODO This should eventually inherit from ::IO.

Defined in:

remilib/mmappedfile.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(path : Path | String, size : Int, *, mode : Mode = Mode::Read, mapping : Mapping = Mapping::Private, syncMode : MsyncFlags = MsyncFlags::Async) #

Creates a new MmappedFile instance by mapping the file at path into the memory space.

If mode is Mode::Read, then the file must already exist.

If mode is Mode:ReadWrite, then the file is always overwritten if it exists and is recreated with the requested size. If it does not exist, it is created with the requested size.

In all cases, size must be at least 1.

When you are finished with the instance, you should call #close to unmap the file and release the resources. This can also happen during finalization.


[View source]

Class Method Detail

def self.open(path : Path | String, size : Int, *, mode : Mode = Mode::Read, mapping : Mapping = Mapping::Private, &) #

Creates a new MmappedFile instance by mapping the file at path into the memory spacem, then yields that instance and executes the block. The file must already exist.

If size is provided, then it must be less than or equal to the size of the file on disk.

This will automatically call #close at the end.


[View source]

Instance Method Detail

def close : Nil #

Unmaps the file from memory.


[View source]
def fd : Int64 #

The file descriptor this mapping was created from.


[View source]
def finalize #

[View source]
def getBytes(count : Int32, position : LibC::SizeT) : Bytes #

Gets count bytes from the given position, then returns a new Bytes slice containing those bytes. This does not change the internal position of the internal cursor (#pos).


[View source]
def getInt128(position : LibC::SizeT) : Int128 #

Returns an Int128 from the file at the given position. Raises an MmappedFileEofError if there are not enough bytes to read an Int128 at the given position. This does not change the internal position of the internal cursor (#pos).


[View source]
def getInt16(position : LibC::SizeT) : Int16 #

Returns an Int16 from the file at the given position. Raises an MmappedFileEofError if there are not enough bytes to read an Int16 at the given position. This does not change the internal position of the internal cursor (#pos).


[View source]
def getInt24(position : LibC::SizeT) : Int32 #

Gets three bytes from position and returns an Int32. MmappedFileEofError if there are not enough bytes to read a 24-bit integer at the given position. This does not change the internal position of the internal cursor (#pos).


[View source]
def getInt32(position : LibC::SizeT) : Int32 #

Returns an Int32 from the file at the given position. Raises an MmappedFileEofError if there are not enough bytes to read an Int32 at the given position. This does not change the internal position of the internal cursor (#pos).


[View source]
def getInt64(position : LibC::SizeT) : Int64 #

Returns an Int64 from the file at the given position. Raises an MmappedFileEofError if there are not enough bytes to read an Int64 at the given position. This does not change the internal position of the internal cursor (#pos).


[View source]
def getInt8(position : LibC::SizeT) : Int8 #

Returns an Int8 from the file at the given position. Raises an MmappedFileEofError if there are not enough bytes to read an Int8 at the given position. This does not change the internal position of the internal cursor (#pos).


[View source]
def getString(count : Int32, position : LibC::SizeT) : String #

Gets a String of count bytes long at the given position, then returns the new String. This does not change the internal position of the internal cursor (#pos).


[View source]
def getUInt128(position : LibC::SizeT) : UInt128 #

Returns a UInt128 from the file at the given position. Raises an MmappedFileEofError if there are not enough bytes to read a UInt128 at the given position. This does not change the internal position of the internal cursor (#pos).


[View source]
def getUInt16(position : LibC::SizeT) : UInt16 #

Returns a UInt16 from the file at the given position. Raises an MmappedFileEofError if there are not enough bytes to read a UInt16 at the given position. This does not change the internal position of the internal cursor (#pos).


[View source]
def getUInt24(position : LibC::SizeT) : UInt32 #

Gets three bytes from position and returns a UInt32. MmappedFileEofError if there are not enough bytes to read a 24-bit number at the given position. This does not change the internal position of the internal cursor (#pos).


[View source]
def getUInt32(position : LibC::SizeT) : UInt32 #

Returns a UInt32 from the file at the given position. Raises an MmappedFileEofError if there are not enough bytes to read a UInt32 at the given position. This does not change the internal position of the internal cursor (#pos).


[View source]
def getUInt64(position : LibC::SizeT) : UInt64 #

Returns a UInt64 from the file at the given position. Raises an MmappedFileEofError if there are not enough bytes to read a UInt64 at the given position. This does not change the internal position of the internal cursor (#pos).


[View source]
def getUInt8(position : LibC::SizeT) : UInt8 #

Returns a UInt8 from the file at the given position. Raises an MmappedFileEofError if there are not enough bytes to read a UInt8 at the given position. This does not change the internal position of the internal cursor (#pos).


[View source]
def len : LibC::SizeT #

Length of the file.


[View source]
def mapping : Mapping #

The Mapping this instance was created with.


[View source]
def mode : Mode #

The Mode this instance was opened with.


[View source]
def open? : Bool #

Returns true if this instance is open, or false otherwise.


[View source]
def pos : LibC::SizeT #

Current read position.


[View source]
def pos=(position : LibC::SizeT) : Nil #

[View source]
def putBytes(data : Bytes, position : LibC::SizeT) : Nil #

Writes a Bytes slice to the file at the given position. If there is not enough space to write the value, then this raises a MmappedFileEofError. This does not change the internal position of the internal cursor (#pos).


[View source]
def putInt128(val : Int128, position : LibC::SizeT) : Nil #

Writes an Int128 to the file at the given position. If there is no space to write the value, then this raises a MmappedFileEofError. This does not change the internal position of the internal cursor (#pos).


[View source]
def putInt16(val : Int16, position : LibC::SizeT) : Nil #

Writes an Int16 to the file at the given position. If there is no space to write the value, then this raises a MmappedFileEofError. This does not change the internal position of the internal cursor (#pos).


[View source]
def putInt24(val : Int32, position : LibC::SizeT) : Nil #

Writes a 24-bit integer stored in a UInt32 to the file at the given position. If there is no space to write the value, then this raises a MmappedFileEofError. This does not change the internal position of the internal cursor (#pos).

If val does not contain a 24-bit integer, this raises an ArgumentError.


[View source]
def putInt32(val : Int32, position : LibC::SizeT) : Nil #

Writes an Int32 to the file at the given position. If there is no space to write the value, then this raises a MmappedFileEofError. This does not change the internal position of the internal cursor (#pos).


[View source]
def putInt64(val : Int64, position : LibC::SizeT) : Nil #

Writes an Int64 to the file at the given position. If there is no space to write the value, then this raises a MmappedFileEofError. This does not change the internal position of the internal cursor (#pos).


[View source]
def putInt8(val : Int8, position : LibC::SizeT) : Nil #

Writes an Int8 to the file at the given position. If there is no space to write the value, then this raises a MmappedFileEofError. This does not change the internal position of the internal cursor (#pos).


[View source]
def putString(str : String, position : LibC::SizeT) : Nil #

Writes a String as raw bytes to the file at the given position. If there is not enough space to write the value, then this raises a MmappedFileEofError. This does not change the internal position of the internal cursor (#pos).


[View source]
def putUInt128(val : UInt128, position : LibC::SizeT) : Nil #

Writes a UInt128 to the file at the given position. If there is no space to write the value, then this raises a MmappedFileEofError. This does not change the internal position of the internal cursor (#pos).


[View source]
def putUInt16(val : UInt16, position : LibC::SizeT) : Nil #

Writes a UInt16 to the file at the given position. If there is no space to write the value, then this raises a MmappedFileEofError. This does not change the internal position of the internal cursor (#pos).


[View source]
def putUInt24(val : UInt32, position : LibC::SizeT, littleEndian? : Bool = true) : Nil #

Writes a 24-bit integer stored in a UInt32 to the file at the given position. If there is no space to write the value, then this raises a MmappedFileEofError. This does not change the internal position of the internal cursor (#pos).

If val does not contain a 24-bit integer, this raises an ArgumentError.


[View source]
def putUInt32(val : UInt32, position : LibC::SizeT) : Nil #

Writes a UInt32 to the file at the given position. If there is no space to write the value, then this raises a MmappedFileEofError. This does not change the internal position of the internal cursor (#pos).


[View source]
def putUInt64(val : UInt64, position : LibC::SizeT) : Nil #

Writes a UInt64 to the file at the given position. If there is no space to write the value, then this raises a MmappedFileEofError. This does not change the internal position of the internal cursor (#pos).


[View source]
def putUInt8(val : UInt8, position : LibC::SizeT) : Nil #

Writes a UInt8 to the file at the given position. If there is no space to write the value, then this raises a MmappedFileEofError. This does not change the internal position of the internal cursor (#pos).


[View source]
def readBytes(count : Int32) : Bytes #

Reads count bytes, then returns a new Bytes slice containing those bytes.


[View source]
def readInt128 : Int128 #

Reads an Int128 from the file at the current position. Raises an MmappedFileEofError if there are not enough bytes to read an Int128.


[View source]
def readInt16 : Int16 #

Reads a Int16 from the file at the current position. Raises an MmappedFileEofError if there are not enough bytes to read a Int16.


[View source]
def readInt24 : Int32 #

Reads three bytes from the file at the current position and returns a 24-bit integer as an Int32. Raises an MmappedFileEofError if there are not enough bytes to read a 24-bit integer.


[View source]
def readInt32 : Int32 #

Reads an Int32 from the file at the current position. Raises an MmappedFileEofError if there are not enough bytes to read an Int32.


[View source]
def readInt64 : Int64 #

Reads an Int64 from the file at the current position. Raises an MmappedFileEofError if there are not enough bytes to read an Int64.


[View source]
def readInt8 : Int8 #

Reads a Int8 from the file at the current position. Raises an MmappedFileEofError if there are not enough bytes to read a Int8.


[View source]
def readString(count : Int32) : String #

Reads a String of count bytes long, then returns the new String.


[View source]
def readUInt128 : UInt128 #

Reads a UInt128 from the file at the current position. Raises an MmappedFileEofError if there are not enough bytes to read a UInt128.


[View source]
def readUInt16 : UInt16 #

Reads a UInt16 from the file at the current position. Raises an MmappedFileEofError if there are not enough bytes to read a UInt16.


[View source]
def readUInt24 : UInt32 #

Reads three bytes from the file at the current position and returns a 24-bit integer as a UInt32. Raises an MmappedFileEofError if there are not enough bytes to read a 24-bit unsigned integer.


[View source]
def readUInt32 : UInt32 #

Reads a UInt32 from the file at the current position. Raises an MmappedFileEofError if there are not enough bytes to read a UInt32.


[View source]
def readUInt64 : UInt64 #

Reads a UInt64 from the file at the current position. Raises an MmappedFileEofError if there are not enough bytes to read a UInt64.


[View source]
def readUInt8 : UInt8 #

Reads a UInt8 from the file at the current position. Raises an MmappedFileEofError if there are not enough bytes to read a UInt8.


[View source]
def rewind : Nil #

[View source]
def sync : Nil #

[View source]
def syncMode : MsyncFlags #

Specifies the behavior when #sync or #close are called and this instance is writeable. If the instance isn't writeable, this is ignored.


[View source]
def syncMode=(syncMode : MsyncFlags) #

Specifies the behavior when #sync or #close are called and this instance is writeable. If the instance isn't writeable, this is ignored.


[View source]
def writeBytes(data : Bytes) : Nil #

Writes a Bytes slice to the file at the current position. If there is not enough space to write the value, then this raises a MmappedFileEofError.


[View source]
def writeInt128(val : Int128) : Nil #

Writes an Int128 to the file at the current position. If there is no space to write the value, then this raises a MmappedFileEofError.


[View source]
def writeInt16(val : Int16) : Nil #

Writes an Int16 to the file at the current position. If there is no space to write the value, then this raises a MmappedFileEofError.


[View source]
def writeInt24(val : Int32) : Nil #

Writes a 24-bit integer stored in an Int32 to the file at the current position. If there is no space to write the value, then this raises a MmappedFileEofError.


[View source]
def writeInt32(val : Int32) : Nil #

Writes an Int32 to the file at the current position. If there is no space to write the value, then this raises a MmappedFileEofError.


[View source]
def writeInt64(val : Int64) : Nil #

Writes an Int64 to the file at the current position. If there is no space to write the value, then this raises a MmappedFileEofError.


[View source]
def writeInt8(val : Int8) : Nil #

Writes an Int8 to the file at the current position. If there is no space to write the value, then this raises a MmappedFileEofError.


[View source]
def writeString(str : String) : Nil #

Writes a String as raw bytes to the file at the current position. If there is not enough space to write the value, then this raises a MmappedFileEofError.


[View source]
def writeUInt128(val : UInt128) : Nil #

Writes a UInt128 to the file at the current position. If there is no space to write the value, then this raises a MmappedFileEofError.


[View source]
def writeUInt16(val : UInt16) : Nil #

Writes a UInt16 to the file at the current position. If there is no space to write the value, then this raises a MmappedFileEofError.


[View source]
def writeUInt24(val : UInt32, littleEndian? : Bool = true) : Nil #

Writes a 24-bit integer stored in a UInt32 to the file at the current position. If there is no space to write the value, then this raises a MmappedFileEofError.

If val does not contain a 24-bit integer, this raises an ArgumentError.


[View source]
def writeUInt32(val : UInt32) : Nil #

Writes a UInt32 to the file at the current position. If there is no space to write the value, then this raises a MmappedFileEofError.


[View source]
def writeUInt64(val : UInt64) : Nil #

Writes a UInt64 to the file at the current position. If there is no space to write the value, then this raises a MmappedFileEofError.


[View source]
def writeUInt8(val : UInt8) : Nil #

Writes a UInt8 to the file at the current position. If there is no space to write the value, then this raises a MmappedFileEofError.


[View source]