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. Private Function
fnalloc(
_: *anyopaque,
len: usize,
alignment: Alignment,
return_address: usize,
) ?[*]u8 {
_ = return_address;
assert(len > 0);
switch (allocStrat(alignment)) {
.raw => {
// `std.c.max_align_t` isn't the whole story, because if `len` is smaller than// every C type with alignment `max_align_t`, the allocation can be less-aligned.// The implementation need only guarantee that any type of length `len` would be// suitably aligned.//// For instance, if `len == 8` and `alignment == .@"16"`, then `malloc` may not// fulfil this request, because there is necessarily no C type with 8-byte size// but 16-byte alignment.//// In theory, the resulting rule here would be target-specific, but in practice,// the smallest type with an alignment of `max_align_t` has the same size (it's// usually `c_longdouble`), so we can just extend the allocation size up to the// alignment of `max_align_t` if necessary.constactual_len = @max(len, @alignOf(std.c.max_align_t));
constptr = c.malloc(actual_len) orelsereturnnull;
assert(alignment.check(@intFromPtr(ptr)));
return@ptrCast(ptr);
},
.posix_memalign => {
// The posix_memalign only accepts alignment values that are a// multiple of the pointer sizeconsteffective_alignment = @max(alignment.toByteUnits(), @sizeOf(usize));
varaligned_ptr: ?*anyopaque = undefined;
if (c.posix_memalign(&aligned_ptr, effective_alignment, len) != 0) {
returnnull;
}
assert(alignment.check(@intFromPtr(aligned_ptr)));
return@ptrCast(aligned_ptr);
},
.manual_align => {
// Overallocate to account for alignment padding and store the original pointer// returned by `malloc` before the aligned address.constpadded_len = len + @sizeOf(usize) + alignment.toByteUnits() - 1;
constunaligned_ptr: [*]u8 = @ptrCast(c.malloc(padded_len) orelsereturnnull);
constunaligned_addr = @intFromPtr(unaligned_ptr);
constaligned_addr = alignment.forward(unaligned_addr + @sizeOf(usize));
constaligned_ptr = unaligned_ptr + (aligned_addr - unaligned_addr);
manualAlignHeader(aligned_ptr).* = unaligned_ptr;
returnaligned_ptr;
},
}
}