DoxigAlpha

defaultQueryPageSize

The default implementation of std.options.queryPageSize. Asserts that the page size is within page_size_min and page_size_max

Type definitions in this namespace

Types

#
GeneralPurposeAllocatorConfig
Deprecated; to be removed after 0.14.0 is tagged.

Functions in this namespace

Functions

#
GeneralPurposeAllocator
Deprecated; to be removed after 0.14.0 is tagged.
pageSize
If the page size is comptime-known, return value is comptime.
defaultQueryPageSize
The default implementation of `std.options.queryPageSize`.
stackFallback
Returns a `StackFallbackAllocator` allocating using either a
StackFallbackAllocator
An allocator that attempts to allocate using a
testAllocator
This one should not try alignments that exceed what C malloc can handle.

TODO Utilize this on Windows.

Values

#
next_mmap_addr_hint
TODO Utilize this on Windows.
page_size_min
comptime-known minimum page size of the target.
page_size_max
comptime-known maximum page size of the target.
c_allocator
Supports the full Allocator interface, including alignment, and exploiting
raw_c_allocator
Asserts allocations are within `@alignOf(std.c.max_align_t)` and directly
page_allocator
On operating systems that support memory mapping, this allocator makes a
smp_allocator
= .{ .ptr = undefined, .vtable = &SmpAllocator.vtable, }
wasm_allocator
This allocator is fast, small, and specific to WebAssembly.

Source

Implementation

#
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 = switch (builtin.os.tag) {
        .linux => if (builtin.link_libc) @intCast(std.c.sysconf(@intFromEnum(std.c._SC.PAGESIZE))) else std.os.linux.getauxval(std.elf.AT_PAGESZ),
        .driverkit, .ios, .macos, .tvos, .visionos, .watchos => blk: {
            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 :blk 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,
            );
            assert(vm_info.page_size != 0);
            break :blk @intCast(vm_info.page_size);
        },
        .windows => blk: {
            var info: std.os.windows.SYSTEM_INFO = undefined;
            std.os.windows.kernel32.GetSystemInfo(&info);
            break :blk info.dwPageSize;
        },
        else => if (builtin.link_libc)
            @intCast(std.c.sysconf(@intFromEnum(std.c._SC.PAGESIZE)))
        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"),
    };

    assert(size >= page_size_min);
    assert(size <= page_size_max);
    global.cached_result.store(size, .unordered);

    return size;
}