Skip to content

Commit

Permalink
polish mmap
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed Apr 15, 2024
1 parent a21e388 commit a082b09
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion book-src/01-02-mmap-file.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Mmap file

Creates a memory map of a file using mmap and simulates some non-sequential reads from the file. Using a memory map means you just index into a slice rather than dealing with seek to navigate a File.
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 dealing with seek to navigate a File.

```zig
{{#include ../src/01-02.zig }}
Expand Down
2 changes: 1 addition & 1 deletion book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ git-repository-url = "https://github.com/zigcc/zig-cookbook"
edit-url-template = "https://github.com/zigcc/zig-cookbook/edit/main/{path}"
additional-js = ["static/zig-hl.js"]
additional-css = ["static/last-changed.css"]
site-url = "zig-cookbook"
# site-url = "zig-cookbook"

[preprocessor.last-changed]
command = "mdbook-last-changed"
Expand Down
21 changes: 10 additions & 11 deletions src/01-02.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const print = std.debug.print;
const is_zig_11 = @import("builtin").zig_version.minor == 11;

const filename = "/tmp/zig-cookbook-01-02.txt";
const file_size = 4096;

pub fn main() !void {
if (.windows == @import("builtin").os.tag) {
Expand All @@ -18,42 +17,42 @@ pub fn main() !void {
.exclusive = false, // Set to true will ensure this file is created by us
});
defer file.close();
const content_to_write = "hello zig cookbook";

// Before mmap, we need to ensure file isn't empty
try file.setEndPos(file_size);
try file.setEndPos(content_to_write.len);

const md = try file.metadata();
try std.testing.expectEqual(md.size(), file_size);
try std.testing.expectEqual(md.size(), content_to_write.len);

const ptr = if (is_zig_11)
try std.os.mmap(
null,
20,
content_to_write.len,
std.os.PROT.READ | std.os.PROT.WRITE,
std.os.MAP.PRIVATE,
std.os.MAP.SHARED,
file.handle,
0,
)
else
try std.posix.mmap(
null,
20,
content_to_write.len,
std.posix.PROT.READ | std.posix.PROT.WRITE,
.{ .TYPE = .PRIVATE },
.{ .TYPE = .SHARED },
file.handle,
0,
);

defer if (is_zig_11) {
std.os.munmap(ptr);
} else {
defer std.posix.munmap(ptr);
std.posix.munmap(ptr);
};

// Write file via mmap
const body = "hello zig cookbook";
std.mem.copyForwards(u8, ptr, body);
std.mem.copyForwards(u8, ptr, content_to_write);

// Read file via mmap
try std.testing.expectEqualStrings(body, ptr[0..body.len]);
try std.testing.expectEqualStrings(content_to_write, ptr);
}

0 comments on commit a082b09

Please sign in to comment.