authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-04-10 02:01:53 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-04-10 02:01:53 -07:00
logd2059f907b037d0495c820290b1062df3781567f
treef5d619186ab73aa563de221fdd08738fcda14c03
parent09b8f3eb5f661e682495dd5800db9f9b4237227d
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

File: add mmap and munmap


2 files changed, 19 insertions(+), 0 deletions(-)

File.zig+13
......@@ -170,3 +170,16 @@ pub const Kind = enum {
170170 unix_socket,
171171 unknown,
172172};
173
174/// Maps file content into memory with a single syscall.
175/// If length is null it will also call stat.
176pub fn mmap(self: File) ![]const u8 {
177 return sys.mmap(
178 null,
179 (try self.stat()).size,
180 sys.PROT.READ,
181 sys.MAP.PRIVATE,
182 @intFromEnum(self.fd),
183 0,
184 );
185}
nfs.zig+6
......@@ -36,3 +36,9 @@ pub fn stderr() File {
3636pub fn memfd_create(name: [*:0]const u8, flags: c_uint) !File {
3737 return .{ .fd = @enumFromInt(try sys.memfd_create(name, flags)) };
3838}
39
40/// Free a region of memory allocated with mmap.
41/// Any error calling munmap is ignored.
42pub fn munmap(region: []const u8) void {
43 return sys.munmap(region.ptr, region.len) catch {};
44}