Skip to content

Commit 6ddaf51

Browse files
committed
refactor(mmap): change from posix.mmap to Io.File.MemoryMap
1 parent eb75a2a commit 6ddaf51

2 files changed

Lines changed: 20 additions & 22 deletions

File tree

assets/src/01-02.zig

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ const std = @import("std");
33
const filename = "/tmp/zig-cookbook-01-02.txt";
44

55
pub fn main(init: std.process.Init) !void {
6-
if (.windows == @import("builtin").os.tag) {
7-
std.debug.print("MMap is not supported in Windows\n", .{});
8-
return;
9-
}
10-
116
const io = init.io;
127
const file = try std.Io.Dir.cwd().createFile(io, filename, .{
138
.read = true,
@@ -17,25 +12,22 @@ pub fn main(init: std.process.Init) !void {
1712
defer file.close(io);
1813
const content_to_write = "hello zig cookbook";
1914

20-
// Before mmap, we need to ensure file isn't empty
15+
// Before mapping the memory, we need to ensure file isn't empty
2116
try file.setLength(io, content_to_write.len);
2217

23-
const md = try file.stat(io);
24-
try std.testing.expectEqual(md.size, content_to_write.len);
18+
const length = try file.length(io);
19+
try std.testing.expectEqual(length, content_to_write.len);
2520

26-
const ptr = try std.posix.mmap(
27-
null,
28-
content_to_write.len,
29-
.{ .READ = true, .WRITE = true },
30-
.{ .TYPE = .SHARED },
31-
file.handle,
32-
0,
33-
);
34-
defer std.posix.munmap(ptr);
21+
var mm = try file.createMemoryMap(io, .{ .len = content_to_write.len });
22+
defer mm.destroy(io);
3523

36-
// Write file via mmap
37-
std.mem.copyForwards(u8, ptr, content_to_write);
24+
// Write file via mapped memory
25+
std.mem.copyForwards(u8, mm.memory, content_to_write);
26+
// Synchronize the mapped memory with the file (store it to the file)
27+
try mm.write(io);
3828

39-
// Read file via mmap
40-
try std.testing.expectEqualStrings(content_to_write, ptr);
29+
// Synchronize memory with contents of file
30+
try mm.read(io);
31+
// Read via mapped memory
32+
try std.testing.expectEqualStrings(content_to_write, mm.memory);
4133
}

src/en-US/01-02-mmap-file.smd

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
.layout = "section.shtml",
66
---
77

8-
Creates a memory map of a file using [mmap](https://man7.org/linux/man-pages/man2/mmap.2.html) and simulates some non-sequential reads from the file. Using a memory map means you just index into a slice rather than having to deal with seek to navigate a file.
8+
Creates a memory map of a file and simulates some non-sequential reads from the file. Using a memory
9+
map means you just index into a slice rather than having to deal with seek to navigate a file.
10+
11+
This uses [NtCreateSection](https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/nf-ntifs-ntcreatesection)
12+
and [NtMapViewOfSection](https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-zwmapviewofsection)
13+
on Windows, and [mmap](https://man7.org/linux/man-pages/man2/mmap.2.html) on other operating
14+
systems that support it (When using [`std.Io.Threaded`](https://codeberg.org/ziglang/zig/src/commit/9908b10d928a12731a288bd59068151d386efdac/lib/std/Io/Threaded.zig#L18222-L18355)).
915

1016
[]($code.siteAsset('src/01-02.zig').language('zig'))

0 commit comments

Comments
 (0)