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 Type Function
Pool
Private Type Function
std.heap.memory_pool.Extra.Pool
pubfnExtra(comptimeItem: type, comptimepool_options: Options) type
A memory pool that can allocate objects of a single type very quickly. Use this when you need to allocate a lot of objects of the same type, because it outperforms general purpose allocators. Functions that potentially allocate memory accept an Allocator parameter.
pubfnExtra(comptimeItem: type, comptimepool_options: Options) type {
if (pool_options.alignment) |a| {
if (a.compare(.eq, .of(Item))) {
varnew_options = pool_options;
new_options.alignment = null;
returnExtra(Item, new_options);
}
}
returnstruct {
constPool = @This();
arena_state: std.heap.ArenaAllocator.State,
free_list: std.SinglyLinkedList,
/// Size of the memory pool items. This is not necessarily the same/// as `@sizeOf(Item)` as the pool also uses the items for internal means.pubconstitem_size = @max(@sizeOf(Node), @sizeOf(Item));
/// Alignment of the memory pool items. This is not necessarily the same/// as `@alignOf(Item)` as the pool also uses the items for internal means.pubconstitem_alignment: Alignment = .max(pool_options.alignmentorelse .of(Item), .of(Node));
constNode = std.SinglyLinkedList.Node;
constItemPtr = *align(item_alignment.toByteUnits()) Item;
/// A MemoryPool containing no elements.pubconstempty: Pool = .{
.arena_state = .{},
.free_list = .{},
};
/// Creates a new memory pool and pre-allocates `num` items./// This allows up to `num` active allocations before an/// `OutOfMemory` error might happen when calling `create()`.pubfninitCapacity(allocator: Allocator, num: usize) Allocator.Error!Pool {
varpool: Pool = .empty;
errdeferpool.deinit(allocator);
trypool.addCapacity(allocator, num);
returnpool;
}
/// Destroys the memory pool and frees all allocated memory.pubfndeinit(pool: *Pool, allocator: Allocator) void {
pool.arena_state.promote(allocator).deinit();
pool.* = undefined;
}
/// Pre-allocates `num` items and adds them to the memory pool./// This allows at least `num` active allocations before an/// `OutOfMemory` error might happen when calling `create()`.pubfnaddCapacity(pool: *Pool, allocator: Allocator, num: usize) Allocator.Error!void {
vari: usize = 0;
while (i < num) : (i += 1) {
constmemory = trypool.allocNew(allocator);
pool.free_list.prepend(@ptrCast(memory));
}
}
pubconstResetMode = std.heap.ArenaAllocator.ResetMode;
/// Resets the memory pool and destroys all allocated items./// This can be used to batch-destroy all objects without invalidating the memory pool.////// The function will return whether the reset operation was successful or not./// If the reallocation failed `false` is returned. The pool will still be fully/// functional in that case, all memory is released. Future allocations just might/// be slower.////// NOTE: If `mode` is `free_all`, the function will always return `true`.pubfnreset(pool: *Pool, allocator: Allocator, mode: ResetMode) bool {
// TODO: Potentially store all allocated objects in a list as well, allowing to// just move them into the free list instead of actually releasing the memory.vararena = pool.arena_state.promote(allocator);
deferpool.arena_state = arena.state;
constreset_successful = arena.reset(mode);
pool.free_list = .{};
returnreset_successful;
}
/// Creates a new item and adds it to the memory pool./// `allocator` may be `undefined` if pool is not `growable`.pubfncreate(pool: *Pool, allocator: Allocator) Allocator.Error!ItemPtr {
constptr: ItemPtr = if (pool.free_list.popFirst()) |node|
@ptrCast(@alignCast(node))
elseif (pool_options.growable)
@ptrCast(trypool.allocNew(allocator))
elsereturnerror.OutOfMemory;
ptr.* = undefined;
returnptr;
}
/// Destroys a previously created item./// Only pass items to `ptr` that were previously created with `create()` of the same memory pool!pubfndestroy(pool: *Pool, ptr: ItemPtr) void {
ptr.* = undefined;
pool.free_list.prepend(@ptrCast(ptr));
}
fnallocNew(pool: *Pool, allocator: Allocator) Allocator.Error!*align(item_alignment.toByteUnits()) [item_size]u8 {
vararena = pool.arena_state.promote(allocator);
deferpool.arena_state = arena.state;
constmemory = tryarena.allocator().alignedAlloc(u8, item_alignment, item_size);
returnmemory[0..item_size];
}
};
}
A memory pool that can allocate objects of a single type very quickly. Use this when you need to allocate a lot of objects of the same type, because it outperforms general purpose allocators. Functions that potentially allocate memory accept an
Allocatorparameter.