| ... | ... | @@ -69,9 +69,22 @@ pub fn MemoryPoolExtra(comptime Item: type, comptime pool_options: Options) type |
| 69 | 69 | pub fn initPreheated(allocator: std.mem.Allocator, initial_size: usize) MemoryPoolError!Pool { |
| 70 | 70 | var pool = init(allocator); |
| 71 | 71 | errdefer pool.deinit(); |
| 72 | try pool.preheat(initial_size); |
| 73 | return pool; |
| 74 | } |
| 75 | |
| 76 | /// Destroys the memory pool and frees all allocated memory. |
| 77 | pub fn deinit(pool: *Pool) void { |
| 78 | pool.arena.deinit(); |
| 79 | pool.* = undefined; |
| 80 | } |
| 72 | 81 | |
| 82 | /// Preheats the memory pool by pre-allocating `size` items. |
| 83 | /// This allows up to `size` active allocations before an |
| 84 | /// `OutOfMemory` error might happen when calling `create()`. |
| 85 | pub fn preheat(pool: *Pool, size: usize) MemoryPoolError!void { |
| 73 | 86 | var i: usize = 0; |
| 74 | | while (i < initial_size) : (i += 1) { |
| 87 | while (i < size) : (i += 1) { |
| 75 | 88 | const raw_mem = try pool.allocNew(); |
| 76 | 89 | const free_node = @as(NodePtr, @ptrCast(raw_mem)); |
| 77 | 90 | free_node.* = Node{ |
| ... | ... | @@ -79,14 +92,6 @@ pub fn MemoryPoolExtra(comptime Item: type, comptime pool_options: Options) type |
| 79 | 92 | }; |
| 80 | 93 | pool.free_list = free_node; |
| 81 | 94 | } |
| 82 | | |
| 83 | | return pool; |
| 84 | | } |
| 85 | | |
| 86 | | /// Destroys the memory pool and frees all allocated memory. |
| 87 | | pub fn deinit(pool: *Pool) void { |
| 88 | | pool.arena.deinit(); |
| 89 | | pool.* = undefined; |
| 90 | 95 | } |
| 91 | 96 | |
| 92 | 97 | pub const ResetMode = std.heap.ArenaAllocator.ResetMode; |