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 standard library.
std › heap › FixedBufferAllocator › threadSafeAlloc Private Function threadSafeAlloc Doc Source Private Function std.heap.FixedBufferAllocator.threadSafeAlloc
fn threadSafeAlloc (ctx : *anyopaque , n : usize , alignment : mem .Alignment , ret_addr : usize ) ?[*]u8 Params ctx : *anyopaque n : usize alignment : mem .Alignment ret_addr : usize File heap/FixedBufferAllocator.zig: 130
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 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 ;
}
}