Skip to content

Commit

Permalink
std.os: add getPageSize() with smoke tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matu3ba committed Jul 31, 2023
1 parent 282cb5e commit 086d8cd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/std/c/linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ pub const user_desc = linux.user_desc;
pub const utsname = linux.utsname;
pub const PR = linux.PR;

pub const _SC = struct {
pub const PAGESIZE = 30;
};

pub const _errno = switch (native_abi) {
.android => struct {
extern fn __errno() *c_int;
Expand Down
11 changes: 6 additions & 5 deletions lib/std/c/solaris.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ const timezone = std.c.timezone;
extern "c" fn ___errno() *c_int;
pub const _errno = ___errno;

// TODO: Add sysconf numbers when the other OSs do.
pub const _SC = struct {
pub const NPROCESSORS_ONLN = 15;
// TODO PAGESIZE
};

pub const dl_iterate_phdr_callback = *const fn (info: *dl_phdr_info, size: usize, data: ?*anyopaque) callconv(.C) c_int;
pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*anyopaque) c_int;

Expand Down Expand Up @@ -1685,11 +1691,6 @@ pub const AF_SUN = struct {
pub const NOPLM = 0x00000004;
};

// TODO: Add sysconf numbers when the other OSs do.
pub const _SC = struct {
pub const NPROCESSORS_ONLN = 15;
};

pub const procfs = struct {
pub const misc_header = extern struct {
size: u32,
Expand Down
29 changes: 29 additions & 0 deletions lib/std/os.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4354,6 +4354,35 @@ pub fn fork() ForkError!pid_t {
}
}

pub fn getPageSize() usize {
switch (builtin.cpu.arch) { // archs with fixed size page size
.wasm32, .wasm64 => 64 * 1024,
else => {},
}

switch (builtin.os.tag) { // kernel chooses the page size
.linux => {
if (builtin.link_libc) {
return std.c.sysconf(std.c._SC.PAGESIZE);
} else {
return linux.getauxval(std.elf.AT_PAGESZ);
}
},
.windows => {
var si: windows.SYSTEM_INFO = undefined;
windows.kernel32.GetSystemInfo(&si);
return si.dwPageSize;
},
.macos, .ios, .watchos, .tvos => return darwin.getPageSize() catch unreachable,
// zig fmt: off
// TODO add constants + validate
// .dragonfly, .emscripten, freebsd, .fuchsia, haiku,
// .hermit, .minix, netbsd, openbsd, solaris, => return std.c.sysconf(std.c._SC.PAGESIZE),
// zig fmt: on
else => unreachable,
}
}

pub const MMapError = error{
/// The underlying filesystem of the specified file does not support memory mapping.
MemoryMappingNotSupported,
Expand Down
6 changes: 6 additions & 0 deletions lib/std/os/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1219,3 +1219,9 @@ test "fchmodat smoke test" {
const st = try os.fstatat(tmp.dir.fd, "foo.txt", 0);
try expectEqual(@as(os.mode_t, 0o755), st.mode & 0b111_111_111);
}

test "getPageSize smoke test" {
const page_size = os.getPageSize();
// debug, if syscalls work or anything breaks
try std.testing.expectEqual(page_size, 100); // this intentionally breaks
}

0 comments on commit 086d8cd

Please sign in to comment.