class RemiLib::MmappedFile
- RemiLib::MmappedFile
- Reference
- Object
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.crConstructors
-
.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.
Class Method Summary
-
.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.
Instance Method Summary
-
#close : Nil
Unmaps the file from memory.
-
#fd : Int64
The file descriptor this mapping was created from.
- #finalize
-
#getBytes(count : Int32, position : LibC::SizeT) : Bytes
Gets count bytes from the given position, then returns a new
Bytes
slice containing those bytes. -
#getInt128(position : LibC::SizeT) : Int128
Returns an
Int128
from the file at the given position. -
#getInt16(position : LibC::SizeT) : Int16
Returns an
Int16
from the file at the given position. -
#getInt24(position : LibC::SizeT) : Int32
Gets three bytes from position and returns an
Int32
. -
#getInt32(position : LibC::SizeT) : Int32
Returns an
Int32
from the file at the given position. -
#getInt64(position : LibC::SizeT) : Int64
Returns an
Int64
from the file at the given position. -
#getInt8(position : LibC::SizeT) : Int8
Returns an
Int8
from the file at the given position. -
#getString(count : Int32, position : LibC::SizeT) : String
Gets a
String
of count bytes long at the given position, then returns the newString
. -
#getUInt128(position : LibC::SizeT) : UInt128
Returns a
UInt128
from the file at the given position. -
#getUInt16(position : LibC::SizeT) : UInt16
Returns a
UInt16
from the file at the given position. -
#getUInt24(position : LibC::SizeT) : UInt32
Gets three bytes from position and returns a
UInt32
. -
#getUInt32(position : LibC::SizeT) : UInt32
Returns a
UInt32
from the file at the given position. -
#getUInt64(position : LibC::SizeT) : UInt64
Returns a
UInt64
from the file at the given position. -
#getUInt8(position : LibC::SizeT) : UInt8
Returns a
UInt8
from the file at the given position. -
#len : LibC::SizeT
Length of the file.
-
#mapping : Mapping
The
Mapping
this instance was created with. -
#mode : Mode
The
Mode
this instance was opened with. -
#open? : Bool
Returns
true
if this instance is open, orfalse
otherwise. -
#pos : LibC::SizeT
Current read position.
- #pos=(position : LibC::SizeT) : Nil
-
#putBytes(data : Bytes, position : LibC::SizeT) : Nil
Writes a
Bytes
slice to the file at the given position. -
#putInt128(val : Int128, position : LibC::SizeT) : Nil
Writes an
Int128
to the file at the given position. -
#putInt16(val : Int16, position : LibC::SizeT) : Nil
Writes an
Int16
to the file at the given position. -
#putInt24(val : Int32, position : LibC::SizeT) : Nil
Writes a 24-bit integer stored in a
UInt32
to the file at the given position. -
#putInt32(val : Int32, position : LibC::SizeT) : Nil
Writes an
Int32
to the file at the given position. -
#putInt64(val : Int64, position : LibC::SizeT) : Nil
Writes an
Int64
to the file at the given position. -
#putInt8(val : Int8, position : LibC::SizeT) : Nil
Writes an
Int8
to the file at the given position. -
#putString(str : String, position : LibC::SizeT) : Nil
Writes a
String
as raw bytes to the file at the given position. -
#putUInt128(val : UInt128, position : LibC::SizeT) : Nil
Writes a
UInt128
to the file at the given position. -
#putUInt16(val : UInt16, position : LibC::SizeT) : Nil
Writes a
UInt16
to the file at the given position. -
#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. -
#putUInt32(val : UInt32, position : LibC::SizeT) : Nil
Writes a
UInt32
to the file at the given position. -
#putUInt64(val : UInt64, position : LibC::SizeT) : Nil
Writes a
UInt64
to the file at the given position. -
#putUInt8(val : UInt8, position : LibC::SizeT) : Nil
Writes a
UInt8
to the file at the given position. -
#readBytes(count : Int32) : Bytes
Reads count bytes, then returns a new
Bytes
slice containing those bytes. -
#readInt128 : Int128
Reads an
Int128
from the file at the current position. -
#readInt16 : Int16
Reads a
Int16
from the file at the current position. -
#readInt24 : Int32
Reads three bytes from the file at the current position and returns a 24-bit integer as an
Int32
. -
#readInt32 : Int32
Reads an
Int32
from the file at the current position. -
#readInt64 : Int64
Reads an
Int64
from the file at the current position. -
#readInt8 : Int8
Reads a
Int8
from the file at the current position. -
#readString(count : Int32) : String
Reads a
String
of count bytes long, then returns the newString
. -
#readUInt128 : UInt128
Reads a
UInt128
from the file at the current position. -
#readUInt16 : UInt16
Reads a
UInt16
from the file at the current position. -
#readUInt24 : UInt32
Reads three bytes from the file at the current position and returns a 24-bit integer as a
UInt32
. -
#readUInt32 : UInt32
Reads a
UInt32
from the file at the current position. -
#readUInt64 : UInt64
Reads a
UInt64
from the file at the current position. -
#readUInt8 : UInt8
Reads a
UInt8
from the file at the current position. - #rewind : Nil
- #sync : Nil
- #syncMode : MsyncFlags
- #syncMode=(syncMode : MsyncFlags)
-
#writeBytes(data : Bytes) : Nil
Writes a
Bytes
slice to the file at the current position. -
#writeInt128(val : Int128) : Nil
Writes an
Int128
to the file at the current position. -
#writeInt16(val : Int16) : Nil
Writes an
Int16
to the file at the current position. -
#writeInt24(val : Int32) : Nil
Writes a 24-bit integer stored in an
Int32
to the file at the current position. -
#writeInt32(val : Int32) : Nil
Writes an
Int32
to the file at the current position. -
#writeInt64(val : Int64) : Nil
Writes an
Int64
to the file at the current position. -
#writeInt8(val : Int8) : Nil
Writes an
Int8
to the file at the current position. -
#writeString(str : String) : Nil
Writes a
String
as raw bytes to the file at the current position. -
#writeUInt128(val : UInt128) : Nil
Writes a
UInt128
to the file at the current position. -
#writeUInt16(val : UInt16) : Nil
Writes a
UInt16
to the file at the current position. -
#writeUInt24(val : UInt32, littleEndian? : Bool = true) : Nil
Writes a 24-bit integer stored in a
UInt32
to the file at the current position. -
#writeUInt32(val : UInt32) : Nil
Writes a
UInt32
to the file at the current position. -
#writeUInt64(val : UInt64) : Nil
Writes a
UInt64
to the file at the current position. -
#writeUInt8(val : UInt8) : Nil
Writes a
UInt8
to the file at the current position.
Constructor Detail
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.
Class Method Detail
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.
Instance Method Detail
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
).
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
).
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
).
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
).
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
).
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
).
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
).
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
).
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
).
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
).
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
).
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
).
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
).
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
).
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
).
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
).
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
).
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
.
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
).
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
).
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
).
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
).
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
).
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
).
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
.
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
).
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
).
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
).
Reads count bytes, then returns a new Bytes
slice containing those
bytes.
Reads an Int128
from the file at the current position. Raises an
MmappedFileEofError
if there are not enough bytes to read an Int128
.
Reads a Int16
from the file at the current position. Raises an
MmappedFileEofError
if there are not enough bytes to read a Int16
.
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.
Reads an Int32
from the file at the current position. Raises an
MmappedFileEofError
if there are not enough bytes to read an Int32
.
Reads an Int64
from the file at the current position. Raises an
MmappedFileEofError
if there are not enough bytes to read an Int64
.
Reads a Int8
from the file at the current position. Raises an
MmappedFileEofError
if there are not enough bytes to read a Int8
.
Reads a String
of count bytes long, then returns the new String
.
Reads a UInt128
from the file at the current position. Raises an
MmappedFileEofError
if there are not enough bytes to read a UInt128
.
Reads a UInt16
from the file at the current position. Raises an
MmappedFileEofError
if there are not enough bytes to read a UInt16
.
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.
Reads a UInt32
from the file at the current position. Raises an
MmappedFileEofError
if there are not enough bytes to read a UInt32
.
Reads a UInt64
from the file at the current position. Raises an
MmappedFileEofError
if there are not enough bytes to read a UInt64
.
Reads a UInt8
from the file at the current position. Raises an
MmappedFileEofError
if there are not enough bytes to read a UInt8
.
Specifies the behavior when #sync
or #close
are called and this
instance is writeable. If the instance isn't writeable, this is ignored.
Specifies the behavior when #sync
or #close
are called and this
instance is writeable. If the instance isn't writeable, this is ignored.
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
.
Writes an Int128
to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError
.
Writes an Int16
to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError
.
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
.
Writes an Int32
to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError
.
Writes an Int64
to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError
.
Writes an Int8
to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError
.
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
.
Writes a UInt128
to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError
.
Writes a UInt16
to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError
.
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
.
Writes a UInt32
to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError
.
Writes a UInt64
to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError
.
Writes a UInt8
to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError
.