authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-06 22:06:01-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-06 22:06:01-07:00
loga60f219660c5973c12987149c48ce9febf3c2b24
tree6e652532fde77daea4735dc20e7352a349158c30
parent9f3165540ed0e2b1fec0ab2eb16d4c06d899c86a

std.c.MAP: use a packed struct

Same as previous commit, but for the libc interface.

12 files changed, 143 insertions(+), 173 deletions(-)

lib/std/c.zig+142
......@@ -45,6 +45,148 @@ pub usingnamespace switch (builtin.os.tag) {
4545 else => struct {},
4646};
4747
48pub const MAP = switch (builtin.os.tag) {
49 .linux => std.os.linux.MAP,
50 .emscripten => packed struct(u32) {
51 TYPE: enum(u4) {
52 SHARED = 0x01,
53 PRIVATE = 0x02,
54 SHARED_VALIDATE = 0x03,
55 },
56 FIXED: bool = false,
57 ANONYMOUS: bool = false,
58 _6: u2 = 0,
59 GROWSDOWN: bool = false,
60 _9: u2 = 0,
61 DENYWRITE: bool = false,
62 EXECUTABLE: bool = false,
63 LOCKED: bool = false,
64 NORESERVE: bool = false,
65 POPULATE: bool = false,
66 NONBLOCK: bool = false,
67 STACK: bool = false,
68 HUGETLB: bool = false,
69 SYNC: bool = false,
70 FIXED_NOREPLACE: bool = false,
71 _: u11 = 0,
72 },
73 .solaris, .illumos => packed struct(u32) {
74 TYPE: enum(u4) {
75 SHARED = 0x01,
76 PRIVATE = 0x02,
77 },
78 FIXED: bool = false,
79 RENAME: bool = false,
80 NORESERVE: bool = false,
81 @"32BIT": bool = false,
82 ANONYMOUS: bool = false,
83 ALIGN: bool = false,
84 TEXT: bool = false,
85 INITDATA: bool = false,
86 _: u20 = 0,
87 },
88 .netbsd => packed struct(u32) {
89 TYPE: enum(u2) {
90 SHARED = 0x01,
91 PRIVATE = 0x02,
92 },
93 REMAPDUP: bool = false,
94 _3: u1 = 0,
95 FIXED: bool = false,
96 RENAME: bool = false,
97 NORESERVE: bool = false,
98 INHERIT: bool = false,
99 _8: u1 = 0,
100 HASSEMAPHORE: bool = false,
101 TRYFIXED: bool = false,
102 WIRED: bool = false,
103 ANONYMOUS: bool = false,
104 STACK: bool = false,
105 _: u18 = 0,
106 },
107 .openbsd => packed struct(u32) {
108 TYPE: enum(u4) {
109 SHARED = 0x01,
110 PRIVATE = 0x02,
111 },
112 FIXED: bool = false,
113 _5: u7 = 0,
114 ANONYMOUS: bool = false,
115 _13: u1 = 0,
116 STACK: bool = false,
117 CONCEAL: bool = false,
118 _: u16 = 0,
119 },
120 .haiku => packed struct(u32) {
121 TYPE: enum(u2) {
122 SHARED = 0x01,
123 PRIVATE = 0x02,
124 },
125 FIXED: bool = false,
126 ANONYMOUS: bool = false,
127 NORESERVE: bool = false,
128 _: u27 = 0,
129 },
130 .macos, .ios, .tvos, .watchos => packed struct(u32) {
131 TYPE: enum(u4) {
132 SHARED = 0x01,
133 PRIVATE = 0x02,
134 },
135 FIXED: bool = false,
136 _5: u1 = 0,
137 NORESERVE: bool = false,
138 _7: u2 = 0,
139 HASSEMAPHORE: bool = false,
140 NOCACHE: bool = false,
141 _11: u1 = 0,
142 ANONYMOUS: bool = false,
143 _: u19 = 0,
144 },
145 .dragonfly => packed struct(u32) {
146 TYPE: enum(u4) {
147 SHARED = 0x01,
148 PRIVATE = 0x02,
149 },
150 FIXED: bool = false,
151 RENAME: bool = false,
152 NORESERVE: bool = false,
153 INHERIT: bool = false,
154 NOEXTEND: bool = false,
155 HASSEMAPHORE: bool = false,
156 STACK: bool = false,
157 NOSYNC: bool = false,
158 ANONYMOUS: bool = false,
159 VPAGETABLE: bool = false,
160 _14: u2 = 0,
161 TRYFIXED: bool = false,
162 NOCORE: bool = false,
163 SIZEALIGN: bool = false,
164 _: u13 = 0,
165 },
166 .freebsd => packed struct(u32) {
167 TYPE: enum(u4) {
168 SHARED = 0x01,
169 PRIVATE = 0x02,
170 },
171 FIXED: bool = false,
172 _5: u5 = 0,
173 STACK: bool = false,
174 NOSYNC: bool = false,
175 ANONYMOUS: bool = false,
176 GUARD: bool = false,
177 EXCL: bool = false,
178 _15: u2 = 0,
179 NOCORE: bool = false,
180 PREFAULT_READ: bool = false,
181 @"32BIT": bool = false,
182 _: u12 = 0,
183 },
184 else => @compileError("target libc does not have MAP"),
185};
186
187/// Used by libc to communicate failure. Not actually part of the underlying syscall.
188pub const MAP_FAILED: *anyopaque = @ptrFromInt(std.math.maxInt(usize));
189
48190pub const whence_t = if (builtin.os.tag == .wasi) std.os.wasi.whence_t else c_int;
49191
50192// Unix-like systems
lib/std/c/darwin.zig-20
......@@ -1310,26 +1310,6 @@ pub const PROT = struct {
13101310 pub const COPY: vm_prot_t = 0x10;
13111311};
13121312
1313pub const MAP = struct {
1314 /// allocated from memory, swap space
1315 pub const ANONYMOUS = 0x1000;
1316 /// map from file (default)
1317 pub const FILE = 0x0000;
1318 /// interpret addr exactly
1319 pub const FIXED = 0x0010;
1320 /// region may contain semaphores
1321 pub const HASSEMAPHORE = 0x0200;
1322 /// changes are private
1323 pub const PRIVATE = 0x0002;
1324 /// share changes
1325 pub const SHARED = 0x0001;
1326 /// don't cache pages for this mapping
1327 pub const NOCACHE = 0x0400;
1328 /// don't reserve needed swap area
1329 pub const NORESERVE = 0x0040;
1330 pub const FAILED = @as(*anyopaque, @ptrFromInt(maxInt(usize)));
1331};
1332
13331313pub const MSF = struct {
13341314 pub const ASYNC = 0x1;
13351315 pub const INVALIDATE = 0x2;
lib/std/c/dragonfly.zig-22
......@@ -223,28 +223,6 @@ pub const PROT = struct {
223223 pub const EXEC = 4;
224224};
225225
226pub const MAP = struct {
227 pub const FILE = 0;
228 pub const FAILED = @as(*anyopaque, @ptrFromInt(maxInt(usize)));
229 pub const ANONYMOUS = ANON;
230 pub const COPY = PRIVATE;
231 pub const SHARED = 1;
232 pub const PRIVATE = 2;
233 pub const FIXED = 16;
234 pub const RENAME = 32;
235 pub const NORESERVE = 64;
236 pub const INHERIT = 128;
237 pub const NOEXTEND = 256;
238 pub const HASSEMAPHORE = 512;
239 pub const STACK = 1024;
240 pub const NOSYNC = 2048;
241 pub const ANON = 4096;
242 pub const VPAGETABLE = 8192;
243 pub const TRYFIXED = 65536;
244 pub const NOCORE = 131072;
245 pub const SIZEALIGN = 262144;
246};
247
248226pub const MSF = struct {
249227 pub const ASYNC = 1;
250228 pub const INVALIDATE = 2;
lib/std/c/emscripten.zig-5
......@@ -16,11 +16,6 @@ pub const IOV_MAX = emscripten.IOV_MAX;
1616pub const IPPROTO = emscripten.IPPROTO;
1717pub const LOCK = emscripten.LOCK;
1818pub const MADV = emscripten.MADV;
19pub const MAP = struct {
20 pub usingnamespace emscripten.MAP;
21 /// Only used by libc to communicate failure.
22 pub const FAILED = @as(*anyopaque, @ptrFromInt(maxInt(usize)));
23};
2419pub const MSF = emscripten.MSF;
2520pub const MSG = emscripten.MSG;
2621pub const NAME_MAX = emscripten.NAME_MAX;
lib/std/c/freebsd.zig-23
......@@ -606,29 +606,6 @@ pub const CLOCK = struct {
606606 pub const PROCESS_CPUTIME_ID = 15;
607607};
608608
609pub const MAP = struct {
610 pub const FAILED = @as(*anyopaque, @ptrFromInt(maxInt(usize)));
611 pub const SHARED = 0x0001;
612 pub const PRIVATE = 0x0002;
613 pub const FIXED = 0x0010;
614 pub const STACK = 0x0400;
615 pub const NOSYNC = 0x0800;
616 pub const ANON = 0x1000;
617 pub const ANONYMOUS = ANON;
618 pub const FILE = 0;
619
620 pub const GUARD = 0x00002000;
621 pub const EXCL = 0x00004000;
622 pub const NOCORE = 0x00020000;
623 pub const PREFAULT_READ = 0x00040000;
624 pub const @"32BIT" = 0x00080000;
625
626 pub fn ALIGNED(alignment: u32) u32 {
627 return alignment << 24;
628 }
629 pub const ALIGNED_SUPER = ALIGNED(1);
630};
631
632609pub const MADV = struct {
633610 pub const NORMAL = 0;
634611 pub const RANDOM = 1;
lib/std/c/haiku.zig-16
......@@ -408,22 +408,6 @@ pub const CLOCK = struct {
408408 pub const THREAD_CPUTIME_ID = -3;
409409};
410410
411pub const MAP = struct {
412 /// mmap() error return code
413 pub const FAILED = @as(*anyopaque, @ptrFromInt(maxInt(usize)));
414 /// changes are seen by others
415 pub const SHARED = 0x01;
416 /// changes are only seen by caller
417 pub const PRIVATE = 0x02;
418 /// require mapping to specified addr
419 pub const FIXED = 0x04;
420 /// no underlying object
421 pub const ANONYMOUS = 0x0008;
422 pub const ANON = ANONYMOUS;
423 /// don't commit memory
424 pub const NORESERVE = 0x10;
425};
426
427411pub const MSF = struct {
428412 pub const ASYNC = 1;
429413 pub const INVALIDATE = 2;
lib/std/c/linux.zig-6
......@@ -1,6 +1,5 @@
11const std = @import("../std.zig");
22const builtin = @import("builtin");
3const maxInt = std.math.maxInt;
43const native_abi = builtin.abi;
54const native_arch = builtin.cpu.arch;
65const linux = std.os.linux;
......@@ -25,11 +24,6 @@ pub const IOV_MAX = linux.IOV_MAX;
2524pub const IPPROTO = linux.IPPROTO;
2625pub const LOCK = linux.LOCK;
2726pub const MADV = linux.MADV;
28pub const MAP = struct {
29 pub usingnamespace linux.MAP;
30 /// Only used by libc to communicate failure.
31 pub const FAILED = @as(*anyopaque, @ptrFromInt(maxInt(usize)));
32};
3327pub const MSF = linux.MSF;
3428pub const MMAP2_UNIT = linux.MMAP2_UNIT;
3529pub const MSG = linux.MSG;
lib/std/c/netbsd.zig-20
......@@ -573,26 +573,6 @@ pub const CLOCK = struct {
573573 pub const PROCESS_CPUTIME_ID = 0x40000000;
574574};
575575
576pub const MAP = struct {
577 pub const FAILED = @as(*anyopaque, @ptrFromInt(maxInt(usize)));
578 pub const SHARED = 0x0001;
579 pub const PRIVATE = 0x0002;
580 pub const REMAPDUP = 0x0004;
581 pub const FIXED = 0x0010;
582 pub const RENAME = 0x0020;
583 pub const NORESERVE = 0x0040;
584 pub const INHERIT = 0x0080;
585 pub const HASSEMAPHORE = 0x0200;
586 pub const TRYFIXED = 0x0400;
587 pub const WIRED = 0x0800;
588
589 pub const FILE = 0x0000;
590 pub const NOSYNC = 0x0800;
591 pub const ANON = 0x1000;
592 pub const ANONYMOUS = ANON;
593 pub const STACK = 0x2000;
594};
595
596576pub const MSF = struct {
597577 pub const ASYNC = 1;
598578 pub const INVALIDATE = 2;
lib/std/c/openbsd.zig-18
......@@ -436,24 +436,6 @@ pub const CLOCK = struct {
436436 pub const THREAD_CPUTIME_ID = 4;
437437};
438438
439pub const MAP = struct {
440 pub const FAILED = @as(*anyopaque, @ptrFromInt(maxInt(usize)));
441 pub const SHARED = 0x0001;
442 pub const PRIVATE = 0x0002;
443 pub const FIXED = 0x0010;
444 pub const RENAME = 0;
445 pub const NORESERVE = 0;
446 pub const INHERIT = 0;
447 pub const HASSEMAPHORE = 0;
448 pub const TRYFIXED = 0;
449
450 pub const FILE = 0;
451 pub const ANON = 0x1000;
452 pub const ANONYMOUS = ANON;
453 pub const STACK = 0x4000;
454 pub const CONCEAL = 0x8000;
455};
456
457439pub const MSF = struct {
458440 pub const ASYNC = 1;
459441 pub const INVALIDATE = 2;
lib/std/c/solaris.zig-21
......@@ -523,27 +523,6 @@ pub const CLOCK = struct {
523523 pub const PROF = THREAD_CPUTIME_ID;
524524};
525525
526pub const MAP = struct {
527 pub const FAILED = @as(*anyopaque, @ptrFromInt(maxInt(usize)));
528 pub const SHARED = 0x0001;
529 pub const PRIVATE = 0x0002;
530 pub const TYPE = 0x000f;
531
532 pub const FILE = 0x0000;
533 pub const FIXED = 0x0010;
534 // Unimplemented
535 pub const RENAME = 0x0020;
536 pub const NORESERVE = 0x0040;
537 /// Force mapping in lower 4G address space
538 pub const @"32BIT" = 0x0080;
539
540 pub const ANON = 0x0100;
541 pub const ANONYMOUS = ANON;
542 pub const ALIGN = 0x0200;
543 pub const TEXT = 0x0400;
544 pub const INITDATA = 0x0800;
545};
546
547526pub const MSF = struct {
548527 pub const ASYNC = 1;
549528 pub const INVALIDATE = 2;
lib/std/os.zig+1-1
......@@ -4666,7 +4666,7 @@ pub fn mmap(
46664666 const ioffset: i64 = @bitCast(offset); // the OS treats this as unsigned
46674667 const rc = mmap_sym(ptr, length, prot, @bitCast(flags), fd, ioffset);
46684668 const err = if (builtin.link_libc) blk: {
4669 if (rc != std.c.MAP.FAILED) return @as([*]align(mem.page_size) u8, @ptrCast(@alignCast(rc)))[0..length];
4669 if (rc != std.c.MAP_FAILED) return @as([*]align(mem.page_size) u8, @ptrCast(@alignCast(rc)))[0..length];
46704670 break :blk @as(E, @enumFromInt(system._errno().*));
46714671 } else blk: {
46724672 const err = errno(rc);
lib/std/os/emscripten.zig-21
......@@ -449,27 +449,6 @@ pub const MADV = struct {
449449 pub const SOFT_OFFLINE = 101;
450450};
451451
452pub const MAP = struct {
453 pub const SHARED = 0x01;
454 pub const PRIVATE = 0x02;
455 pub const SHARED_VALIDATE = 0x03;
456 pub const TYPE = 0x0f;
457 pub const FIXED = 0x10;
458 pub const ANON = 0x20;
459 pub const ANONYMOUS = ANON;
460 pub const NORESERVE = 0x4000;
461 pub const GROWSDOWN = 0x0100;
462 pub const DENYWRITE = 0x0800;
463 pub const EXECUTABLE = 0x1000;
464 pub const LOCKED = 0x2000;
465 pub const POPULATE = 0x8000;
466 pub const NONBLOCK = 0x10000;
467 pub const STACK = 0x20000;
468 pub const HUGETLB = 0x40000;
469 pub const SYNC = 0x80000;
470 pub const FIXED_NOREPLACE = 0x100000;
471};
472
473452pub const MSF = struct {
474453 pub const ASYNC = 1;
475454 pub const INVALIDATE = 2;