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.

threadSafeAlloc

FixedBufferAllocator.threadSafeAlloc
fn threadSafeAlloc(ctx: *anyopaque, n: usize, alignment: mem.Alignment, ret_addr: usize) ?[*]u8

File

lib/std/heap/FixedBufferAllocator.zig:130

Code

fn threadSafeAlloc(ctx: *anyopaque, n: usize, alignment: mem.Alignment, ret_addr: usize) ?[*]u8 {
    const self: *FixedBufferAllocator = @ptrCast(@alignCast(ctx));
    _ = ret_addr;
    const ptr_align = alignment.toByteUnits();
    var cur_end_index = @atomicLoad(usize, &self.end_index, .monotonic);
    while (true) {
        const adjust_off = mem.alignPointerOffset(self.buffer.ptr + cur_end_index, ptr_align) orelse return null;
        const adjusted_index = cur_end_index + adjust_off;
        const new_end_index = adjusted_index + n;
        if (new_end_index > self.buffer.len) return null;
        cur_end_index = @cmpxchgWeak(
            usize,
            &self.end_index,
            cur_end_index,
            new_end_index,
            .acquire, // acquire any memory that may have been freed
            .monotonic,
        ) orelse
            return self.buffer[adjusted_index..new_end_index].ptr;
    }
}