Commit a60f219660

Andrew Kelley <andrew@ziglang.org>
2024-02-07 06:06:01
std.c.MAP: use a packed struct
Same as previous commit, but for the libc interface.
1 parent 9f31655
lib/std/c/darwin.zig
@@ -1310,26 +1310,6 @@ pub const PROT = struct {
     pub const COPY: vm_prot_t = 0x10;
 };
 
-pub const MAP = struct {
-    /// allocated from memory, swap space
-    pub const ANONYMOUS = 0x1000;
-    /// map from file (default)
-    pub const FILE = 0x0000;
-    /// interpret addr exactly
-    pub const FIXED = 0x0010;
-    /// region may contain semaphores
-    pub const HASSEMAPHORE = 0x0200;
-    /// changes are private
-    pub const PRIVATE = 0x0002;
-    /// share changes
-    pub const SHARED = 0x0001;
-    /// don't cache pages for this mapping
-    pub const NOCACHE = 0x0400;
-    /// don't reserve needed swap area
-    pub const NORESERVE = 0x0040;
-    pub const FAILED = @as(*anyopaque, @ptrFromInt(maxInt(usize)));
-};
-
 pub const MSF = struct {
     pub const ASYNC = 0x1;
     pub const INVALIDATE = 0x2;
lib/std/c/dragonfly.zig
@@ -223,28 +223,6 @@ pub const PROT = struct {
     pub const EXEC = 4;
 };
 
-pub const MAP = struct {
-    pub const FILE = 0;
-    pub const FAILED = @as(*anyopaque, @ptrFromInt(maxInt(usize)));
-    pub const ANONYMOUS = ANON;
-    pub const COPY = PRIVATE;
-    pub const SHARED = 1;
-    pub const PRIVATE = 2;
-    pub const FIXED = 16;
-    pub const RENAME = 32;
-    pub const NORESERVE = 64;
-    pub const INHERIT = 128;
-    pub const NOEXTEND = 256;
-    pub const HASSEMAPHORE = 512;
-    pub const STACK = 1024;
-    pub const NOSYNC = 2048;
-    pub const ANON = 4096;
-    pub const VPAGETABLE = 8192;
-    pub const TRYFIXED = 65536;
-    pub const NOCORE = 131072;
-    pub const SIZEALIGN = 262144;
-};
-
 pub const MSF = struct {
     pub const ASYNC = 1;
     pub const INVALIDATE = 2;
lib/std/c/emscripten.zig
@@ -16,11 +16,6 @@ pub const IOV_MAX = emscripten.IOV_MAX;
 pub const IPPROTO = emscripten.IPPROTO;
 pub const LOCK = emscripten.LOCK;
 pub const MADV = emscripten.MADV;
-pub const MAP = struct {
-    pub usingnamespace emscripten.MAP;
-    /// Only used by libc to communicate failure.
-    pub const FAILED = @as(*anyopaque, @ptrFromInt(maxInt(usize)));
-};
 pub const MSF = emscripten.MSF;
 pub const MSG = emscripten.MSG;
 pub const NAME_MAX = emscripten.NAME_MAX;
lib/std/c/freebsd.zig
@@ -606,29 +606,6 @@ pub const CLOCK = struct {
     pub const PROCESS_CPUTIME_ID = 15;
 };
 
-pub const MAP = struct {
-    pub const FAILED = @as(*anyopaque, @ptrFromInt(maxInt(usize)));
-    pub const SHARED = 0x0001;
-    pub const PRIVATE = 0x0002;
-    pub const FIXED = 0x0010;
-    pub const STACK = 0x0400;
-    pub const NOSYNC = 0x0800;
-    pub const ANON = 0x1000;
-    pub const ANONYMOUS = ANON;
-    pub const FILE = 0;
-
-    pub const GUARD = 0x00002000;
-    pub const EXCL = 0x00004000;
-    pub const NOCORE = 0x00020000;
-    pub const PREFAULT_READ = 0x00040000;
-    pub const @"32BIT" = 0x00080000;
-
-    pub fn ALIGNED(alignment: u32) u32 {
-        return alignment << 24;
-    }
-    pub const ALIGNED_SUPER = ALIGNED(1);
-};
-
 pub const MADV = struct {
     pub const NORMAL = 0;
     pub const RANDOM = 1;
lib/std/c/haiku.zig
@@ -408,22 +408,6 @@ pub const CLOCK = struct {
     pub const THREAD_CPUTIME_ID = -3;
 };
 
-pub const MAP = struct {
-    /// mmap() error return code
-    pub const FAILED = @as(*anyopaque, @ptrFromInt(maxInt(usize)));
-    /// changes are seen by others
-    pub const SHARED = 0x01;
-    /// changes are only seen by caller
-    pub const PRIVATE = 0x02;
-    /// require mapping to specified addr
-    pub const FIXED = 0x04;
-    /// no underlying object
-    pub const ANONYMOUS = 0x0008;
-    pub const ANON = ANONYMOUS;
-    /// don't commit memory
-    pub const NORESERVE = 0x10;
-};
-
 pub const MSF = struct {
     pub const ASYNC = 1;
     pub const INVALIDATE = 2;
lib/std/c/linux.zig
@@ -1,6 +1,5 @@
 const std = @import("../std.zig");
 const builtin = @import("builtin");
-const maxInt = std.math.maxInt;
 const native_abi = builtin.abi;
 const native_arch = builtin.cpu.arch;
 const linux = std.os.linux;
@@ -25,11 +24,6 @@ pub const IOV_MAX = linux.IOV_MAX;
 pub const IPPROTO = linux.IPPROTO;
 pub const LOCK = linux.LOCK;
 pub const MADV = linux.MADV;
-pub const MAP = struct {
-    pub usingnamespace linux.MAP;
-    /// Only used by libc to communicate failure.
-    pub const FAILED = @as(*anyopaque, @ptrFromInt(maxInt(usize)));
-};
 pub const MSF = linux.MSF;
 pub const MMAP2_UNIT = linux.MMAP2_UNIT;
 pub const MSG = linux.MSG;
lib/std/c/netbsd.zig
@@ -573,26 +573,6 @@ pub const CLOCK = struct {
     pub const PROCESS_CPUTIME_ID = 0x40000000;
 };
 
-pub const MAP = struct {
-    pub const FAILED = @as(*anyopaque, @ptrFromInt(maxInt(usize)));
-    pub const SHARED = 0x0001;
-    pub const PRIVATE = 0x0002;
-    pub const REMAPDUP = 0x0004;
-    pub const FIXED = 0x0010;
-    pub const RENAME = 0x0020;
-    pub const NORESERVE = 0x0040;
-    pub const INHERIT = 0x0080;
-    pub const HASSEMAPHORE = 0x0200;
-    pub const TRYFIXED = 0x0400;
-    pub const WIRED = 0x0800;
-
-    pub const FILE = 0x0000;
-    pub const NOSYNC = 0x0800;
-    pub const ANON = 0x1000;
-    pub const ANONYMOUS = ANON;
-    pub const STACK = 0x2000;
-};
-
 pub const MSF = struct {
     pub const ASYNC = 1;
     pub const INVALIDATE = 2;
lib/std/c/openbsd.zig
@@ -436,24 +436,6 @@ pub const CLOCK = struct {
     pub const THREAD_CPUTIME_ID = 4;
 };
 
-pub const MAP = struct {
-    pub const FAILED = @as(*anyopaque, @ptrFromInt(maxInt(usize)));
-    pub const SHARED = 0x0001;
-    pub const PRIVATE = 0x0002;
-    pub const FIXED = 0x0010;
-    pub const RENAME = 0;
-    pub const NORESERVE = 0;
-    pub const INHERIT = 0;
-    pub const HASSEMAPHORE = 0;
-    pub const TRYFIXED = 0;
-
-    pub const FILE = 0;
-    pub const ANON = 0x1000;
-    pub const ANONYMOUS = ANON;
-    pub const STACK = 0x4000;
-    pub const CONCEAL = 0x8000;
-};
-
 pub const MSF = struct {
     pub const ASYNC = 1;
     pub const INVALIDATE = 2;
lib/std/c/solaris.zig
@@ -523,27 +523,6 @@ pub const CLOCK = struct {
     pub const PROF = THREAD_CPUTIME_ID;
 };
 
-pub const MAP = struct {
-    pub const FAILED = @as(*anyopaque, @ptrFromInt(maxInt(usize)));
-    pub const SHARED = 0x0001;
-    pub const PRIVATE = 0x0002;
-    pub const TYPE = 0x000f;
-
-    pub const FILE = 0x0000;
-    pub const FIXED = 0x0010;
-    // Unimplemented
-    pub const RENAME = 0x0020;
-    pub const NORESERVE = 0x0040;
-    /// Force mapping in lower 4G address space
-    pub const @"32BIT" = 0x0080;
-
-    pub const ANON = 0x0100;
-    pub const ANONYMOUS = ANON;
-    pub const ALIGN = 0x0200;
-    pub const TEXT = 0x0400;
-    pub const INITDATA = 0x0800;
-};
-
 pub const MSF = struct {
     pub const ASYNC = 1;
     pub const INVALIDATE = 2;
lib/std/os/emscripten.zig
@@ -449,27 +449,6 @@ pub const MADV = struct {
     pub const SOFT_OFFLINE = 101;
 };
 
-pub const MAP = struct {
-    pub const SHARED = 0x01;
-    pub const PRIVATE = 0x02;
-    pub const SHARED_VALIDATE = 0x03;
-    pub const TYPE = 0x0f;
-    pub const FIXED = 0x10;
-    pub const ANON = 0x20;
-    pub const ANONYMOUS = ANON;
-    pub const NORESERVE = 0x4000;
-    pub const GROWSDOWN = 0x0100;
-    pub const DENYWRITE = 0x0800;
-    pub const EXECUTABLE = 0x1000;
-    pub const LOCKED = 0x2000;
-    pub const POPULATE = 0x8000;
-    pub const NONBLOCK = 0x10000;
-    pub const STACK = 0x20000;
-    pub const HUGETLB = 0x40000;
-    pub const SYNC = 0x80000;
-    pub const FIXED_NOREPLACE = 0x100000;
-};
-
 pub const MSF = struct {
     pub const ASYNC = 1;
     pub const INVALIDATE = 2;
lib/std/c.zig
@@ -45,6 +45,148 @@ pub usingnamespace switch (builtin.os.tag) {
     else => struct {},
 };
 
+pub const MAP = switch (builtin.os.tag) {
+    .linux => std.os.linux.MAP,
+    .emscripten => packed struct(u32) {
+        TYPE: enum(u4) {
+            SHARED = 0x01,
+            PRIVATE = 0x02,
+            SHARED_VALIDATE = 0x03,
+        },
+        FIXED: bool = false,
+        ANONYMOUS: bool = false,
+        _6: u2 = 0,
+        GROWSDOWN: bool = false,
+        _9: u2 = 0,
+        DENYWRITE: bool = false,
+        EXECUTABLE: bool = false,
+        LOCKED: bool = false,
+        NORESERVE: bool = false,
+        POPULATE: bool = false,
+        NONBLOCK: bool = false,
+        STACK: bool = false,
+        HUGETLB: bool = false,
+        SYNC: bool = false,
+        FIXED_NOREPLACE: bool = false,
+        _: u11 = 0,
+    },
+    .solaris, .illumos => packed struct(u32) {
+        TYPE: enum(u4) {
+            SHARED = 0x01,
+            PRIVATE = 0x02,
+        },
+        FIXED: bool = false,
+        RENAME: bool = false,
+        NORESERVE: bool = false,
+        @"32BIT": bool = false,
+        ANONYMOUS: bool = false,
+        ALIGN: bool = false,
+        TEXT: bool = false,
+        INITDATA: bool = false,
+        _: u20 = 0,
+    },
+    .netbsd => packed struct(u32) {
+        TYPE: enum(u2) {
+            SHARED = 0x01,
+            PRIVATE = 0x02,
+        },
+        REMAPDUP: bool = false,
+        _3: u1 = 0,
+        FIXED: bool = false,
+        RENAME: bool = false,
+        NORESERVE: bool = false,
+        INHERIT: bool = false,
+        _8: u1 = 0,
+        HASSEMAPHORE: bool = false,
+        TRYFIXED: bool = false,
+        WIRED: bool = false,
+        ANONYMOUS: bool = false,
+        STACK: bool = false,
+        _: u18 = 0,
+    },
+    .openbsd => packed struct(u32) {
+        TYPE: enum(u4) {
+            SHARED = 0x01,
+            PRIVATE = 0x02,
+        },
+        FIXED: bool = false,
+        _5: u7 = 0,
+        ANONYMOUS: bool = false,
+        _13: u1 = 0,
+        STACK: bool = false,
+        CONCEAL: bool = false,
+        _: u16 = 0,
+    },
+    .haiku => packed struct(u32) {
+        TYPE: enum(u2) {
+            SHARED = 0x01,
+            PRIVATE = 0x02,
+        },
+        FIXED: bool = false,
+        ANONYMOUS: bool = false,
+        NORESERVE: bool = false,
+        _: u27 = 0,
+    },
+    .macos, .ios, .tvos, .watchos => packed struct(u32) {
+        TYPE: enum(u4) {
+            SHARED = 0x01,
+            PRIVATE = 0x02,
+        },
+        FIXED: bool = false,
+        _5: u1 = 0,
+        NORESERVE: bool = false,
+        _7: u2 = 0,
+        HASSEMAPHORE: bool = false,
+        NOCACHE: bool = false,
+        _11: u1 = 0,
+        ANONYMOUS: bool = false,
+        _: u19 = 0,
+    },
+    .dragonfly => packed struct(u32) {
+        TYPE: enum(u4) {
+            SHARED = 0x01,
+            PRIVATE = 0x02,
+        },
+        FIXED: bool = false,
+        RENAME: bool = false,
+        NORESERVE: bool = false,
+        INHERIT: bool = false,
+        NOEXTEND: bool = false,
+        HASSEMAPHORE: bool = false,
+        STACK: bool = false,
+        NOSYNC: bool = false,
+        ANONYMOUS: bool = false,
+        VPAGETABLE: bool = false,
+        _14: u2 = 0,
+        TRYFIXED: bool = false,
+        NOCORE: bool = false,
+        SIZEALIGN: bool = false,
+        _: u13 = 0,
+    },
+    .freebsd => packed struct(u32) {
+        TYPE: enum(u4) {
+            SHARED = 0x01,
+            PRIVATE = 0x02,
+        },
+        FIXED: bool = false,
+        _5: u5 = 0,
+        STACK: bool = false,
+        NOSYNC: bool = false,
+        ANONYMOUS: bool = false,
+        GUARD: bool = false,
+        EXCL: bool = false,
+        _15: u2 = 0,
+        NOCORE: bool = false,
+        PREFAULT_READ: bool = false,
+        @"32BIT": bool = false,
+        _: u12 = 0,
+    },
+    else => @compileError("target libc does not have MAP"),
+};
+
+/// Used by libc to communicate failure. Not actually part of the underlying syscall.
+pub const MAP_FAILED: *anyopaque = @ptrFromInt(std.math.maxInt(usize));
+
 pub const whence_t = if (builtin.os.tag == .wasi) std.os.wasi.whence_t else c_int;
 
 // Unix-like systems
lib/std/os.zig
@@ -4666,7 +4666,7 @@ pub fn mmap(
     const ioffset: i64 = @bitCast(offset); // the OS treats this as unsigned
     const rc = mmap_sym(ptr, length, prot, @bitCast(flags), fd, ioffset);
     const err = if (builtin.link_libc) blk: {
-        if (rc != std.c.MAP.FAILED) return @as([*]align(mem.page_size) u8, @ptrCast(@alignCast(rc)))[0..length];
+        if (rc != std.c.MAP_FAILED) return @as([*]align(mem.page_size) u8, @ptrCast(@alignCast(rc)))[0..length];
         break :blk @as(E, @enumFromInt(system._errno().*));
     } else blk: {
         const err = errno(rc);