pub fn Extra(comptime Item: type, comptime pool_options: Options) type arena_state: std.heap.ArenaAllocator.Statefree_list: std.SinglyLinkedListSize of the memory pool items. This is not necessarily the same as @sizeOf(Item) as the pool also uses the items for internal means.
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.
A MemoryPool containing no elements.
pub fn initCapacity(allocator: Allocator, num: usize) Allocator.Error!PoolCreates 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().
pub fn deinit(pool: *Pool, allocator: Allocator) voidDestroys the memory pool and frees all allocated memory.
pub fn addCapacity(pool: *Pool, allocator: Allocator, num: usize) Allocator.Error!voidPre-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().
pub fn reset(pool: *Pool, allocator: Allocator, mode: ResetMode) boolResets 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.
pub fn create(pool: *Pool, allocator: Allocator) Allocator.Error!ItemPtrCreates a new item and adds it to the memory pool. allocator may be undefined if pool is not growable.
pub fn destroy(pool: *Pool, ptr: ItemPtr) voidDestroys a previously created item. Only pass items to ptr that were previously created with create() of the same memory pool!
fn allocNew(pool: *Pool, allocator: Allocator) Allocator.Error!*align(item_alignment.toByteUnits()) [item_size]u829
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139pub fn Extra(comptime Item: type, comptime pool_options: Options) type {
if (pool_options.alignment) |a| {
if (a.compare(.eq, .of(Item))) {
var new_options = pool_options;
new_options.alignment = null;
return Extra(Item, new_options);
}
}
return struct {
const Pool = @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.
pub const item_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.
pub const item_alignment: Alignment = .max(pool_options.alignment orelse .of(Item), .of(Node));
const Node = std.SinglyLinkedList.Node;
const ItemPtr = *align(item_alignment.toByteUnits()) Item;
/// A MemoryPool containing no elements.
pub const empty: 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()`.
pub fn initCapacity(allocator: Allocator, num: usize) Allocator.Error!Pool {
var pool: Pool = .empty;
errdefer pool.deinit(allocator);
try pool.addCapacity(allocator, num);
return pool;
}
/// Destroys the memory pool and frees all allocated memory.
pub fn deinit(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()`.
pub fn addCapacity(pool: *Pool, allocator: Allocator, num: usize) Allocator.Error!void {
var i: usize = 0;
while (i < num) : (i += 1) {
const memory = try pool.allocNew(allocator);
pool.free_list.prepend(@ptrCast(memory));
}
}
pub const ResetMode = 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`.
pub fn reset(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.
var arena = pool.arena_state.promote(allocator);
defer pool.arena_state = arena.state;
const reset_successful = arena.reset(mode);
pool.free_list = .{};
return reset_successful;
}
/// Creates a new item and adds it to the memory pool.
/// `allocator` may be `undefined` if pool is not `growable`.
pub fn create(pool: *Pool, allocator: Allocator) Allocator.Error!ItemPtr {
const ptr: ItemPtr = if (pool.free_list.popFirst()) |node|
@ptrCast(@alignCast(node))
else if (pool_options.growable)
@ptrCast(try pool.allocNew(allocator))
else
return error.OutOfMemory;
ptr.* = undefined;
return ptr;
}
/// Destroys a previously created item.
/// Only pass items to `ptr` that were previously created with `create()` of the same memory pool!
pub fn destroy(pool: *Pool, ptr: ItemPtr) void {
ptr.* = undefined;
pool.free_list.prepend(@ptrCast(ptr));
}
fn allocNew(pool: *Pool, allocator: Allocator) Allocator.Error!*align(item_alignment.toByteUnits()) [item_size]u8 {
var arena = pool.arena_state.promote(allocator);
defer pool.arena_state = arena.state;
const memory = try arena.allocator().alignedAlloc(u8, item_alignment, item_size);
return memory[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.