feature. See also
. The project being documented here (as the example) is the Zig library itself.
File
Code
const std = @import("../std.zig");
const Allocator = std.mem.Allocator;
const Alignment = std.mem.Alignment;
const MemoryPool = std.heap.MemoryPool;
pub fn Aligned(comptime Item: type, comptime alignment: Alignment) type {
return Extra(Item, .{ .alignment = alignment });
}
pub const Options = struct {
alignment: ?Alignment = null,
growable: bool = true,
};
pub fn Extra(comptime Item: type, comptime pool_options: Options) type {
if (pool_options.alignment) |a| {
if (a.compare(.eq, .of(Item))) {
var new_options = pool_options;
new_options.alignment = null;
return Extra(Item, new_options);
}
}
return struct {
const Pool = @This();
arena_state: std.heap.ArenaAllocator.State,
free_list: std.SinglyLinkedList,
pub const item_size = @max(@sizeOf(Node), @sizeOf(Item));
pub const item_alignment: Alignment = .max(pool_options.alignment orelse .of(Item), .of(Node));
const Node = std.SinglyLinkedList.Node;
const ItemPtr = *align(item_alignment.toByteUnits()) Item;
pub const empty: Pool = .{
.arena_state = .{},
.free_list = .{},
};
pub fn initCapacity(allocator: Allocator, num: usize) Allocator.Error!Pool {
var pool: Pool = .empty;
errdefer pool.deinit(allocator);
try pool.addCapacity(allocator, num);
return pool;
}
pub fn deinit(pool: *Pool, allocator: Allocator) void {
pool.arena_state.promote(allocator).deinit();
pool.* = undefined;
}
pub fn addCapacity(pool: *Pool, allocator: Allocator, num: usize) Allocator.Error!void {
var i: usize = 0;
while (i < num) : (i += 1) {
const memory = try pool.allocNew(allocator);
pool.free_list.prepend(@ptrCast(memory));
}
}
pub const ResetMode = std.heap.ArenaAllocator.ResetMode;
pub fn reset(pool: *Pool, allocator: Allocator, mode: ResetMode) bool {
// just move them into the free list instead of actually releasing the memory.
var arena = pool.arena_state.promote(allocator);
defer pool.arena_state = arena.state;
const reset_successful = arena.reset(mode);
pool.free_list = .{};
return reset_successful;
}
pub fn create(pool: *Pool, allocator: Allocator) Allocator.Error!ItemPtr {
const ptr: ItemPtr = if (pool.free_list.popFirst()) |node|
@ptrCast(@alignCast(node))
else if (pool_options.growable)
@ptrCast(try pool.allocNew(allocator))
else
return error.OutOfMemory;
ptr.* = undefined;
return ptr;
}
pub fn destroy(pool: *Pool, ptr: ItemPtr) void {
ptr.* = undefined;
pool.free_list.prepend(@ptrCast(ptr));
}
fn allocNew(pool: *Pool, allocator: Allocator) Allocator.Error!*align(item_alignment.toByteUnits()) [item_size]u8 {
var arena = pool.arena_state.promote(allocator);
defer pool.arena_state = arena.state;
const memory = try arena.allocator().alignedAlloc(u8, item_alignment, item_size);
return memory[0..item_size];
}
};
}
test "basic" {
const a = std.testing.allocator;
var pool: MemoryPool(u32) = .empty;
defer pool.deinit(a);
const p1 = try pool.create(a);
const p2 = try pool.create(a);
const p3 = try pool.create(a);
try std.testing.expect(p1 != p2);
try std.testing.expect(p1 != p3);
try std.testing.expect(p2 != p3);
pool.destroy(p2);
const p4 = try pool.create(a);
try std.testing.expect(p2 == p4);
}
test "initCapacity (success)" {
const a = std.testing.allocator;
var pool: MemoryPool(u32) = try .initCapacity(a, 4);
defer pool.deinit(a);
_ = try pool.create(a);
_ = try pool.create(a);
_ = try pool.create(a);
}
test "initCapacity (failure)" {
const failer = std.testing.failing_allocator;
try std.testing.expectError(error.OutOfMemory, MemoryPool(u32).initCapacity(failer, 5));
}
test "growable" {
const a = std.testing.allocator;
var pool: Extra(u32, .{ .growable = false }) = try .initCapacity(a, 4);
defer pool.deinit(a);
_ = try pool.create(a);
_ = try pool.create(a);
_ = try pool.create(a);
_ = try pool.create(a);
try std.testing.expectError(error.OutOfMemory, pool.create(a));
}
test "greater than pointer default alignment" {
const Foo = struct {
data: u64 align(16),
};
const a = std.testing.allocator;
var pool: MemoryPool(Foo) = .empty;
defer pool.deinit(a);
const foo: *Foo = try pool.create(a);
pool.destroy(foo);
}
test "greater than pointer manual alignment" {
const Foo = struct {
data: u64,
};
const a = std.testing.allocator;
var pool: Aligned(Foo, .@"16") = .empty;
defer pool.deinit(a);
const foo: *align(16) Foo = try pool.create(a);
pool.destroy(foo);
}