Fix reset.

This commit is contained in:
Said Kadrioski 2025-11-25 17:33:04 +01:00
parent 74b9139bc3
commit 50b16ef544

View file

@ -126,19 +126,18 @@ pub fn Extra(comptime Item: type, comptime pool_options: Options) type {
/// ///
/// NOTE: If `mode` is `free_all`, the function will always return `true`. /// NOTE: If `mode` is `free_all`, the function will always return `true`.
pub fn reset(self: *Pool, allocator: Allocator, mode: ResetMode) bool { pub fn reset(self: *Pool, allocator: Allocator, mode: ResetMode) bool {
self.free_list = .{};
var arena = self.arena_state.promote(allocator); var arena = self.arena_state.promote(allocator);
defer self.arena_state = arena.state; const arena_mode: std.heap.ArenaAllocator.ResetMode = switch (mode) {
const ArenaResetMode = std.heap.ArenaAllocator.ResetMode;
const arena_mode = switch (mode) {
.free_all => .free_all, .free_all => .free_all,
.retain_capacity => .retain_capacity, .retain_capacity => .retain_capacity,
.retain_with_limit => |limit| ArenaResetMode{ .retain_with_limit = limit * item_size }, .retain_with_limit => |limit| .{ .retain_with_limit = limit * item_size },
}; };
self.free_list = .{}; const arena_result = arena.reset(arena_mode);
if (!arena.reset(arena_mode)) return false; self.arena_state = arena.state;
if (!arena_result) return false;
// When the backing arena allocator is being reset to // When the backing arena allocator is being reset to
// a capacity greater than 0, then its internals consists // a capacity greater than 0, then its internals consist
// of a *single* buffer node of said capacity. This means, // of a *single* buffer node of said capacity. This means,
// we can safely pre-heat without causing additional allocations. // we can safely pre-heat without causing additional allocations.
const arena_capacity = arena.queryCapacity() / item_size; const arena_capacity = arena.queryCapacity() / item_size;