From 084a7cf0285a17b94ccf7822a4b3496d401e6cd8 Mon Sep 17 00:00:00 2001 From: Kai Jellinghaus Date: Wed, 1 Nov 2023 13:45:35 +0100 Subject: [PATCH] Use ArenaAllocator.reset in MemoryPool --- lib/std/heap/memory_pool.zig | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/std/heap/memory_pool.zig b/lib/std/heap/memory_pool.zig index 764198d36d..722bdd8f3e 100644 --- a/lib/std/heap/memory_pool.zig +++ b/lib/std/heap/memory_pool.zig @@ -86,18 +86,26 @@ pub fn MemoryPoolExtra(comptime Item: type, comptime pool_options: Options) type pool.* = undefined; } + 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. - pub fn reset(pool: *Pool) void { + /// + /// 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, 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. - const allocator = pool.arena.child_allocator; - // TODO: Replace with "pool.arena.reset()" when implemented. - pool.arena.deinit(); - pool.arena = std.heap.ArenaAllocator.init(allocator); + const reset_successful = pool.arena.reset(mode); pool.free_list = null; + + return reset_successful; } /// Creates a new item and adds it to the memory pool.