@@ -10,16 +10,67 @@ const testing = std.testing;
10
10
const Endian = std .builtin .Endian ;
11
11
const native_endian = builtin .cpu .arch .endian ();
12
12
13
+ pub const PageSizeCheck = enum {
14
+ /// Does nothing if the page size is invalid
15
+ unsafe ,
16
+ /// Throws an error if the page size is invalid
17
+ throw ,
18
+ /// Asserts if the page size is invalid
19
+ assert ,
20
+ /// Panics if the page size is invalid
21
+ panic ,
22
+
23
+ pub fn makeType (comptime self : PageSizeCheck ) type {
24
+ return switch (self ) {
25
+ .assert , .unsafe , .panic = > usize ,
26
+ .throw = > error {InvalidPageSize }! usize ,
27
+ };
28
+ }
29
+ };
30
+
31
+ pub const PageSizeOptions = struct {
32
+ /// Value sanity check method
33
+ check : PageSizeCheck = .assert ,
34
+ /// Use the comptime version of page size if the OS does not report it.
35
+ comptime_fallback : bool = true ,
36
+ };
37
+
38
+ /// Runtime known minimum page size.
39
+ pub fn getPageSize (comptime options : PageSizeOptions ) PageSizeCheck.makeType (options .check ) {
40
+ const value : usize = switch (builtin .os .tag ) {
41
+ .linux = > if (builtin .link_libc ) @intCast (std .c .sysconf (std .os .linux .SC .PAGESIZE )) else std .os .linux .getauxval (std .elf .AT_PAGESZ ),
42
+ else = > if (options .comptime_fallback ) page_size else 0 ,
43
+ };
44
+
45
+ return switch (options .check ) {
46
+ .unsafe = > value ,
47
+ .assert = > {
48
+ assert (value != 0 );
49
+ return value ;
50
+ },
51
+ .panic = > {
52
+ if (value == 0 ) @panic ("Invalid page size" );
53
+ return value ;
54
+ },
55
+ .throw = > if (value == 0 ) return error .InvalidPageSize else value ,
56
+ };
57
+ }
58
+
13
59
/// Compile time known minimum page size.
14
60
/// https://github.com/ziglang/zig/issues/4082
15
- pub const page_size = switch (builtin .cpu .arch ) {
16
- .wasm32 , .wasm64 = > 64 * 1024 ,
17
- .aarch64 = > switch (builtin .os .tag ) {
18
- .macos , .ios , .watchos , .tvos = > 16 * 1024 ,
61
+ pub const page_size = blk : {
62
+ const value = builtin .target .page_size orelse switch (builtin .cpu .arch ) {
63
+ .wasm32 , .wasm64 = > 64 * 1024 ,
64
+ .aarch64 = > switch (builtin .os .tag ) {
65
+ .macos , .ios , .watchos , .tvos = > 16 * 1024 ,
66
+ else = > 4 * 1024 ,
67
+ },
68
+ .sparc64 = > 8 * 1024 ,
19
69
else = > 4 * 1024 ,
20
- },
21
- .sparc64 = > 8 * 1024 ,
22
- else = > 4 * 1024 ,
70
+ };
71
+
72
+ if (value == 0 ) @compileError ("Page size is not valid" );
73
+ break :blk value ;
23
74
};
24
75
25
76
/// The standard library currently thoroughly depends on byte size
0 commit comments