aboutsummaryrefslogtreecommitdiff
path: root/libphobos/src/std/mmfile.d
diff options
context:
space:
mode:
authorIain Buclaw <ibuclaw@gdcproject.org>2024-01-18 02:39:20 +0100
committerIain Buclaw <ibuclaw@gdcproject.org>2024-02-03 00:49:46 +0100
commitf204359931866b917856fc959c70dbf55f28c14d (patch)
treeba1c671045e384fa49a6381f79abf7c1b84a55ea /libphobos/src/std/mmfile.d
parent5470a9b176c2b3030ff3891c7e9403db2b0685b8 (diff)
downloadgcc-f204359931866b917856fc959c70dbf55f28c14d.zip
gcc-f204359931866b917856fc959c70dbf55f28c14d.tar.gz
gcc-f204359931866b917856fc959c70dbf55f28c14d.tar.bz2
d: Merge dmd, druntime bce5c1f7b5, phobos e4d0dd513.
D front-end changes: - Import latest changes from dmd v2.107.0-beta.1. - Keywords like `__FILE__' are now always evaluated at the callsite. D runtime changes: - Import latest changes from druntime v2.107.0-beta.1. - Added `nameSig' field to TypeInfo_Class in object.d. Phobos changes: - Import latest changes from phobos v2.107.0-beta.1. gcc/d/ChangeLog: * dmd/MERGE: Merge upstream dmd bce5c1f7b5. * d-attribs.cc (build_attributes): Update for new front-end interface. * d-lang.cc (d_parse_file): Likewise. * decl.cc (DeclVisitor::visit (VarDeclaration *)): Likewise. * expr.cc (build_lambda_tree): New function. (ExprVisitor::visit (FuncExp *)): Use build_lambda_tree. (ExprVisitor::visit (SymOffExp *)): Likewise. (ExprVisitor::visit (VarExp *)): Likewise. * typeinfo.cc (create_tinfo_types): Add two ulong fields to internal TypeInfo representation. (TypeInfoVisitor::visit (TypeInfoClassDeclaration *)): Emit stub data for TypeInfo_Class.nameSig. (TypeInfoVisitor::visit (TypeInfoStructDeclaration *)): Update for new front-end interface. libphobos/ChangeLog: * libdruntime/MERGE: Merge upstream druntime bce5c1f7b5. * src/MERGE: Merge upstream phobos e4d0dd513.
Diffstat (limited to 'libphobos/src/std/mmfile.d')
-rw-r--r--libphobos/src/std/mmfile.d68
1 files changed, 52 insertions, 16 deletions
diff --git a/libphobos/src/std/mmfile.d b/libphobos/src/std/mmfile.d
index f8f8a90..b2cab31 100644
--- a/libphobos/src/std/mmfile.d
+++ b/libphobos/src/std/mmfile.d
@@ -2,10 +2,22 @@
/**
* 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 with `MmFile`,
+ * 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 from `std.file` and `std.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.
+ *
* Copyright: Copyright The D Language Foundation 2004 - 2009.
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: $(HTTP digitalmars.com, Walter Bright),
* Matthew Wilson
+ * References: $(LINK https://en.wikipedia.org/wiki/Memory-mapped_file)
* Source: $(PHOBOSSRC std/mmfile.d)
*
* $(SCRIPT inhibitQuickIndex = 1;)
@@ -612,23 +624,47 @@ private:
{
static assert(0);
}
+}
+
+/// Read an existing file
+@system unittest
+{
+ 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
+@system unittest
+{
+ 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();
- // Report error, where errno gives the error number
- // void errNo()
- // {
- // version (Windows)
- // {
- // throw new FileException(filename, GetLastError());
- // }
- // else version (linux)
- // {
- // throw new FileException(filename, errno);
- // }
- // else
- // {
- // static assert(0);
- // }
- // }
+ assert(std.file.read(deleteme) == "\n\n\n\n\n");
}
@system unittest