Commit 884b1423a4
Changed files (1)
lib
std
heap
lib/std/heap/memory_pool.zig
@@ -69,9 +69,22 @@ pub fn MemoryPoolExtra(comptime Item: type, comptime pool_options: Options) type
pub fn initPreheated(allocator: std.mem.Allocator, initial_size: usize) MemoryPoolError!Pool {
var pool = init(allocator);
errdefer pool.deinit();
+ try pool.preheat(initial_size);
+ return pool;
+ }
+
+ /// Destroys the memory pool and frees all allocated memory.
+ pub fn deinit(pool: *Pool) void {
+ pool.arena.deinit();
+ pool.* = undefined;
+ }
+ /// Preheats the memory pool by pre-allocating `size` items.
+ /// This allows up to `size` active allocations before an
+ /// `OutOfMemory` error might happen when calling `create()`.
+ pub fn preheat(pool: *Pool, size: usize) MemoryPoolError!void {
var i: usize = 0;
- while (i < initial_size) : (i += 1) {
+ while (i < size) : (i += 1) {
const raw_mem = try pool.allocNew();
const free_node = @as(NodePtr, @ptrCast(raw_mem));
free_node.* = Node{
@@ -79,14 +92,6 @@ pub fn MemoryPoolExtra(comptime Item: type, comptime pool_options: Options) type
};
pool.free_list = free_node;
}
-
- return pool;
- }
-
- /// Destroys the memory pool and frees all allocated memory.
- pub fn deinit(pool: *Pool) void {
- pool.arena.deinit();
- pool.* = undefined;
}
pub const ResetMode = std.heap.ArenaAllocator.ResetMode;