authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-04-10 02:02:55 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-04-10 02:02:55 -07:00
log602bb5704999acd385f39f2dd9cd2b9afa1480df
tree04f74d896e788988858348122b56cc54f561fa67
parenteca4d5fd5a69e269da7d3dc883bc6a7618c0c380
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

add mmap and munmap


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

mod.zig+68
......@@ -2074,6 +2074,10 @@ pub const libc = struct {
20742074 /// https://pubs.opengroup.org/onlinepubs/9699919799.orig/functions/mlockall.html
20752075 pub extern fn mlockall(flags: c_int) c_int;
20762076
2077 /// void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);
2078 /// https://pubs.opengroup.org/onlinepubs/9699919799.orig/functions/mmap.html
2079 pub extern fn mmap(addr: ?*anyopaque, len: usize, prot: c_int, flags: c_int, fd: c_int, offset: off_t) *allowzero anyopaque;
2080
20772081 /// int mq_unlink(const char *name);
20782082 /// https://pubs.opengroup.org/onlinepubs/9699919799.orig/functions/mq_unlink.html
20792083 pub extern fn mq_unlink(name: [*:0]const u8) c_int;
......@@ -2086,6 +2090,10 @@ pub const libc = struct {
20862090 /// https://pubs.opengroup.org/onlinepubs/9699919799.orig/functions/munlockall.html
20872091 pub extern fn munlockall() void;
20882092
2093 /// int munmap(void *addr, size_t len);
2094 /// https://pubs.opengroup.org/onlinepubs/9699919799.orig/functions/munmap.html
2095 pub extern fn munmap(addr: *const anyopaque, len: usize) c_int;
2096
20892097 /// double nearbyint(double x);
20902098 /// https://pubs.opengroup.org/onlinepubs/9699919799.orig/functions/nearbyint.html
20912099 pub extern fn nearbyint(x: f64) f64;
......@@ -2854,6 +2862,55 @@ pub const CLOCK = struct {
28542862 pub const TAI = 11;
28552863};
28562864
2865pub const MAP = struct {
2866 pub const FAILED = -1;
2867
2868 pub const SHARED = 0x01;
2869 pub const PRIVATE = 0x02;
2870 pub const SHARED_VALIDATE = 0x03;
2871 pub const TYPE = 0x0f;
2872 pub const FIXED = 0x10;
2873 pub const ANON = 0x20;
2874 pub const ANONYMOUS = ANON;
2875 pub const NORESERVE = 0x4000;
2876 pub const GROWSDOWN = 0x0100;
2877 pub const DENYWRITE = 0x0800;
2878 pub const EXECUTABLE = 0x1000;
2879 pub const LOCKED = 0x2000;
2880 pub const POPULATE = 0x8000;
2881 pub const NONBLOCK = 0x10000;
2882 pub const STACK = 0x20000;
2883 pub const HUGETLB = 0x40000;
2884 pub const SYNC = 0x80000;
2885 pub const FIXED_NOREPLACE = 0x100000;
2886 pub const FILE = 0;
2887
2888 pub const MAP_HUGE_SHIFT = 26;
2889 pub const MAP_HUGE_MASK = 0x3f;
2890 pub const MAP_HUGE_16KB = 14 << 26;
2891 pub const MAP_HUGE_64KB = 16 << 26;
2892 pub const MAP_HUGE_512KB = 19 << 26;
2893 pub const MAP_HUGE_1MB = 20 << 26;
2894 pub const MAP_HUGE_2MB = 21 << 26;
2895 pub const MAP_HUGE_8MB = 23 << 26;
2896 pub const MAP_HUGE_16MB = 24 << 26;
2897 pub const MAP_HUGE_32MB = 25 << 26;
2898 pub const MAP_HUGE_256MB = 28 << 26;
2899 pub const MAP_HUGE_512MB = 29 << 26;
2900 pub const MAP_HUGE_1GB = 30 << 26;
2901 pub const MAP_HUGE_2GB = 31 << 26;
2902 pub const MAP_HUGE_16GB = 34 << 26;
2903};
2904
2905pub const PROT = struct {
2906 pub const NONE = 0;
2907 pub const READ = 1;
2908 pub const WRITE = 2;
2909 pub const EXEC = 4;
2910 pub const GROWSDOWN = 0x01000000;
2911 pub const GROWSUP = 0x02000000;
2912};
2913
28572914pub const NAME_MAX = 255;
28582915pub const PATH_MAX = 4096;
28592916pub const NGROUPS_MAX = 32;
......@@ -3217,3 +3274,14 @@ pub fn renameat(oldfd: c_int, old: [*:0]const u8, newfd: c_int, new: [*:0]const
32173274 if (rc == -1) return errno.fromInt(errno.fromLibC());
32183275 std.debug.assert(rc == 0);
32193276}
3277pub fn mmap(addr: ?*anyopaque, len: usize, prot: c_int, flags: c_int, fd: c_int, offset: off_t) errno.Error![]u8 {
3278 const rc = libc.mmap(addr, len, prot, flags, fd, offset);
3279 if (@intFromPtr(rc) == MAP.FAILED) return errno.fromInt(errno.fromLibC());
3280 const ptr: [*]u8 = @ptrCast(rc);
3281 return ptr[0..len];
3282}
3283pub fn munmap(addr: *const anyopaque, len: usize) errno.Error!void {
3284 const rc = libc.munmap(addr, len);
3285 if (rc == -1) return errno.fromInt(errno.fromLibC());
3286 std.debug.assert(rc == 0);
3287}
test.zig+2
......@@ -36,4 +36,6 @@ test {
3636 _ = &linux.sendfile;
3737 _ = &linux.getdents;
3838 _ = &linux.renameat;
39 _ = &linux.mmap;
40 _ = &linux.munmap;
3941}