Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.112.0

overview

Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinga local clone.

std.mmfile

Read and write memory mapped files.
Memory mapped files are a mechanism in operating systems that allows file access through virtual memory. After opening a file withMmFile, the contents can be read from or written to with standard slice / pointer operations. Changes to the memory are automatically reflected in the underlying file.
Memory mapping can increase I/O performance of large files, compared to buffered read / write operations fromstd.file andstd.stdio. However, I/O errors are not handled as safely: when for example the disk that the file is on gets removed, reading from it may result in a segfault.
License:
Boost License 1.0.
Authors:
Walter Bright, Matthew Wilson

Referenceshttps://en.wikipedia.org/wiki/Memory-mapped_file

Sourcestd/mmfile.d

classMmFile;
MmFile objects control the memory mapped file resource.
Examples:
Read an existing file
import std.file;std.file.write(deleteme,"hello");// deleteme is a temporary filenamescope(exit) remove(deleteme);// Use a scope class so the file will be closed at the end of this functionscope mmfile =newMmFile(deleteme);writeln(mmfile.length);// "hello".length// Access file contents with the slice operator// This is typed as `void[]`, so cast to `char[]` or `ubyte[]` to use itconst 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 contentswriteln(data[0 .. 5]);// "hello"
Examples:
Write a new file
import std.file;scope(exit) remove(deleteme);scope mmfile =newMmFile(deleteme,MmFile.Mode.readWriteNew, 5,null);writeln(mmfile.length);// 5auto data =cast(ubyte[]) mmfile[];// This write to memory will be reflected in the file contentsdata[] = '\n';mmfile.flush();writeln(std.file.read(deleteme));// "\n\n\n\n\n"
enumMode: int;
The mode the memory mapped file is opened with.
read
Read existing file
readWriteNew
Delete existing file, write new file
readWrite
Read/Write existing file, create if not existing
readCopyOnWrite
Read/Write existing file, copy on write
this(stringfilename) scope;
Open memory mapped file filename for reading. File is closed when the object instance is deleted.
this(stringfilename, Modemode, ulongsize, void*address, size_twindow = 0) scope;
Open memory mapped file filename in mode. File is closed when the object instance is deleted.
Parameters:
stringfilenamename of the file. If null, an anonymous file mapping is created.
Modemodeaccess mode defined above.
ulongsizethe size of the file. If 0, it is taken to be the size of the existing file.
void*addressthe preferred address to map the file to, although the system is not required to honor it. If null, the system selects the most convenient address.
size_twindowpreferred block size of the amount of data to map at one time with 0 meaning map the entire file. The window size must be a multiple of the memory allocation page size.
@property ulonglength() const;
Gives size in bytes of the memory mapped file.
aliasopDollar = length;
Forwardslength.
Modemode();
Read-only property returning the file mode.
void[]opSlice();
Returns entire file contents as an array.
void[]opSlice(ulongi1, ulongi2);
Returns slice of file contents as an array.
ubyteopIndex(ulongi);
Returns byte at index i in file.
ubyteopIndexAssign(ubytevalue, ulongi);
Sets and returns byte at index i in file to value.
Copyright © 1999-2026 by theD Language Foundation | Page generated byDdoc on Sat Feb 21 00:07:04 2026

[8]ページ先頭

©2009-2026 Movatter.jp