Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

allocBigPages

BrkAllocator.allocBigPages
fn allocBigPages(n: usize) usize

File

lib/std/heap/BrkAllocator.zig:156

Code

fn allocBigPages(n: usize) usize {
    const pow2_pages = math.ceilPowerOfTwoAssert(usize, n);
    const slot_size_bytes = pow2_pages * bigpage_size;
    const class = math.log2(pow2_pages);

    const top_free_ptr = global.big_frees[class];
    if (top_free_ptr != 0) {
        const node: *usize = @ptrFromInt(top_free_ptr + (slot_size_bytes - @sizeOf(usize)));
        global.big_frees[class] = node.*;
        return top_free_ptr;
    }

    if (builtin.cpu.arch.isWasm()) {
        comptime assert(std.heap.page_size_max == std.heap.page_size_min);
        const page_size = std.heap.page_size_max;
        const pages_per_bigpage = bigpage_size / page_size;
        const page_index = @wasmMemoryGrow(0, pow2_pages * pages_per_bigpage);
        if (page_index == -1) return 0;
        return @as(usize, @intCast(page_index)) * page_size;
    } else if (builtin.os.tag == .linux) {
        const prev_brk = global.prev_brk;
        const start_brk = if (prev_brk == 0)
            std.mem.alignForward(usize, std.os.linux.brk(0), bigpage_size)
        else
            prev_brk;
        const end_brk = start_brk + pow2_pages * bigpage_size;
        const new_prev_brk = std.os.linux.brk(end_brk);
        global.prev_brk = new_prev_brk;
        if (new_prev_brk != end_brk) return 0;
        return start_brk;
    } else {
        @compileError("no sbrk-like OS primitive available");
    }
}