| ... | @@ -86,18 +86,26 @@ pub fn MemoryPoolExtra(comptime Item: type, comptime pool_options: Options) type | ... | @@ -86,18 +86,26 @@ pub fn MemoryPoolExtra(comptime Item: type, comptime pool_options: Options) type |
| 86 | pool.* = undefined; | 86 | pool.* = undefined; |
| 87 | } | 87 | } |
| 88 | | 88 | |
| | 89 | pub const ResetMode = std.heap.ArenaAllocator.ResetMode; |
| | 90 | |
| 89 | /// Resets the memory pool and destroys all allocated items. | 91 | /// Resets the memory pool and destroys all allocated items. |
| 90 | /// This can be used to batch-destroy all objects without invalidating the memory pool. | 92 | /// 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 { |
| 92 | // TODO: Potentially store all allocated objects in a list as well, allowing to | 101 | // TODO: Potentially store all allocated objects in a list as well, allowing to |
| 93 | // just move them into the free list instead of actually releasing the memory. | 102 | // just move them into the free list instead of actually releasing the memory. |
| 94 | const allocator = pool.arena.child_allocator; | | |
| 95 | | 103 | |
| 96 | // TODO: Replace with "pool.arena.reset()" when implemented. | 104 | const reset_successful = pool.arena.reset(mode); |
| 97 | pool.arena.deinit(); | | |
| 98 | pool.arena = std.heap.ArenaAllocator.init(allocator); | | |
| 99 | | 105 | |
| 100 | pool.free_list = null; | 106 | pool.free_list = null; |
| | 107 | |
| | 108 | return reset_successful; |
| 101 | } | 109 | } |
| 102 | | 110 | |
| 103 | /// Creates a new item and adds it to the memory pool. | 111 | /// Creates a new item and adds it to the memory pool. |