MmFile

MmFile objects control the memory mapped file resource.

Constructors

this
this(string filename)

Open memory mapped file filename for reading. File is closed when the object instance is deleted.

this
this(string filename, Mode mode, ulong size, void* address, size_t window)

Open memory mapped file filename in mode. File is closed when the object instance is deleted.

Destructor

~this
~this()

Flushes pending output and closes the memory mapped file.

Members

Aliases

opDollar
alias opDollar = length

Forwards length.

Enums

Mode
enum Mode

The mode the memory mapped file is opened with.

Functions

mode
Mode mode()

Read-only property returning the file mode.

opIndex
ubyte opIndex(ulong i)

Returns byte at index i in file.

opIndexAssign
ubyte opIndexAssign(ubyte value, ulong i)

Sets and returns byte at index i in file to value.

opSlice
void[] opSlice()

Returns entire file contents as an array.

opSlice
void[] opSlice(ulong i1, ulong i2)

Returns slice of file contents as an array.

Properties

length
ulong length [@property getter]

Gives size in bytes of the memory mapped file.

Examples

Read an existing file

import std.file;
std.file.write(deleteme, "hello"); // deleteme is a temporary filename
scope(exit) remove(deleteme);

// Use a scope class so the file will be closed at the end of this function
scope mmfile = new MmFile(deleteme);

assert(mmfile.length == "hello".length);

// Access file contents with the slice operator
// This is typed as `void[]`, so cast to `char[]` or `ubyte[]` to use it
const data = cast(const(char)[]) mmfile[];

// At this point, the file content may not have been read yet.
// In that case, the following memory access will intentionally
// trigger a page fault, causing the kernel to load the file contents
assert(data[0 .. 5] == "hello");

Write a new file

import std.file;
scope(exit) remove(deleteme);

scope mmfile = new MmFile(deleteme, MmFile.Mode.readWriteNew, 5, null);
assert(mmfile.length == 5);

auto data = cast(ubyte[]) mmfile[];

// This write to memory will be reflected in the file contents
data[] = '\n';

mmfile.flush();

assert(std.file.read(deleteme) == "\n\n\n\n\n");

Meta