authorgravatar for contact@kaij.techKai Jellinghaus <contact@kaij.tech> 2023-11-01 13:45:35+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-11-01 14:45:35+02:00
log084a7cf0285a17b94ccf7822a4b3496d401e6cd8
tree2d39e7ce8c828900db1ce6908da862a2e2afb2c1
parent725f765c376dda291d3d5afe5446221a97c189f7
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Use ArenaAllocator.reset in MemoryPool


1 files changed, 13 insertions(+), 5 deletions(-)

lib/std/heap/memory_pool.zig+13-5
......@@ -86,18 +86,26 @@ pub fn MemoryPoolExtra(comptime Item: type, comptime pool_options: Options) type
8686 pool.* = undefined;
8787 }
8888
89 pub const ResetMode = std.heap.ArenaAllocator.ResetMode;
90
8991 /// Resets the memory pool and destroys all allocated items.
9092 /// This can be used to batch-destroy all objects without invalidating the memory pool.
91 pub fn reset(pool: *Pool) void {
93 ///
94 /// The function will return whether the reset operation was successful or not.
95 /// If the reallocation failed `false` is returned. The pool will still be fully
96 /// functional in that case, all memory is released. Future allocations just might
97 /// be slower.
98 ///
99 /// NOTE: If `mode` is `free_all`, the function will always return `true`.
100 pub fn reset(pool: *Pool, mode: ResetMode) bool {
92101 // TODO: Potentially store all allocated objects in a list as well, allowing to
93102 // just move them into the free list instead of actually releasing the memory.
94 const allocator = pool.arena.child_allocator;
95103
96 // TODO: Replace with "pool.arena.reset()" when implemented.
97 pool.arena.deinit();
98 pool.arena = std.heap.ArenaAllocator.init(allocator);
104 const reset_successful = pool.arena.reset(mode);
99105
100106 pool.free_list = null;
107
108 return reset_successful;
101109 }
102110
103111 /// Creates a new item and adds it to the memory pool.