The default implementation of std.options.queryPageSize.
Asserts that the page size is within page_size_min and page_size_max
pub fn defaultQueryPageSize() usize
pub fn defaultQueryPageSize() usize {
const global = struct {
var cached_result: std.atomic.Value(usize) = .init(0);
};
var size = global.cached_result.load(.unordered);
if (size > 0) return size;
size = size: switch (builtin.os.tag) {
.linux => if (builtin.link_libc)
@max(std.c.sysconf(@backingInt(std.c._SC.PAGESIZE)), 0)
else
std.os.linux.getauxval(std.elf.AT_PAGESZ),
.driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos => {
const task_port = std.c.mach_task_self();
// mach_task_self may fail "if there are any resource failures or other errors".
if (task_port == std.c.TASK.NULL) break :size 0;
var info_count = std.c.TASK.VM.INFO_COUNT;
var vm_info: std.c.task_vm_info_data_t = undefined;
vm_info.page_size = 0;
_ = std.c.task_info(
task_port,
std.c.TASK.VM.INFO,
@as(std.c.task_info_t, @ptrCast(&vm_info)),
&info_count,
);
break :size @intCast(vm_info.page_size);
},
.windows => {
var sbi: windows.SYSTEM.BASIC_INFORMATION = undefined;
switch (windows.ntdll.NtQuerySystemInformation(
.Basic,
&sbi,
@sizeOf(windows.SYSTEM.BASIC_INFORMATION),
null,
)) {
.SUCCESS => break :size sbi.PageSize,
else => break :size 0,
}
},
else => if (builtin.link_libc)
@max(std.c.sysconf(@backingInt(std.c._SC.PAGESIZE)), 0)
else if (builtin.os.tag == .freestanding or builtin.os.tag == .other)
@compileError("unsupported target: freestanding/other")
else
@compileError("pageSize on " ++ @tagName(builtin.cpu.arch) ++ "-" ++ @tagName(builtin.os.tag) ++ " is not supported without linking libc, using the default implementation"),
};
if (size == 0) size = page_size_max;
assert(size >= page_size_min);
assert(size <= page_size_max);
global.cached_result.store(size, .unordered);
return size;
}