diff --git a/File.zig b/File.zig index d826958f36445c13569cf5cb7ef1e9943fb4fc11..216b2c5252291c297a083a4ec045067cf1e5e655 100644 --- a/File.zig +++ b/File.zig @@ -170,3 +170,16 @@ pub const Kind = enum { unix_socket, unknown, }; + +/// Maps file content into memory with a single syscall. +/// If length is null it will also call stat. +pub fn mmap(self: File) ![]const u8 { + return sys.mmap( + null, + (try self.stat()).size, + sys.PROT.READ, + sys.MAP.PRIVATE, + @intFromEnum(self.fd), + 0, + ); +} diff --git a/nfs.zig b/nfs.zig index 3c18dff389718e9075dd57bc2c0ee77881533e67..7447f5932affa2abe758c2e8534d43bdf1c9cdc6 100644 --- a/nfs.zig +++ b/nfs.zig @@ -36,3 +36,9 @@ pub fn stderr() File { pub fn memfd_create(name: [*:0]const u8, flags: c_uint) !File { return .{ .fd = @enumFromInt(try sys.memfd_create(name, flags)) }; } + +/// Free a region of memory allocated with mmap. +/// Any error calling munmap is ignored. +pub fn munmap(region: []const u8) void { + return sys.munmap(region.ptr, region.len) catch {}; +}