Bump arena- new checkpointing system

This commit is contained in:
Peterino2 2026-10-24 16:18:43 -07:00
parent 1a07a5388c
commit 14df85a638
1 changed files with 244 additions and 216 deletions

View File

@ -7,7 +7,19 @@ const stats = @import("stats.zig");
// memory comes from a linked list of blocks taken from the backing allocator.
// blocks are never returned to the backing allocator until reset(.free_all) or
// deinit, rewound and reset blocks go to free lists and get reused.
pub const BumpArenaAdvanced = struct {
pub const BumpArenaOptions = struct {
/// most checkpoints that can be live at once, taking more panics.
max_checkpoints: usize = 256,
/// tracks live checkpoints and asserts on rewinding an invalidated one.
validate_checkpoints: bool = std.debug.runtime_safety,
};
pub const BumpArenaAdvanced = BumpArenaAdvancedOptions(.{});
pub fn BumpArenaAdvancedOptions(comptime options: BumpArenaOptions) type {
return struct {
const Self = @This();
/// usable bytes in a standard block. allocations that do not fit get a
/// power of two sized block of their own.
pub const BlockSize = 8192 * 2;
@ -42,8 +54,11 @@ pub const BumpArenaAdvanced = struct {
current: ?*Block = null,
// free lists indexed by size class, sizeClass(block.size)
free_blocks: [size_classes]?*Block = @splat(null),
// live checkpoints, used to assert they are rewound in LIFO order.
depth: u32 = 0,
// ids of the checkpoints still on the timeline, ascending. rewinding drops
// every id newer than the target, those checkpoints are invalid.
live: [if (options.validate_checkpoints) options.max_checkpoints else 0]u64 = undefined,
live_len: usize = 0,
next_id: u64 = 0,
allocatedBytes: usize = 0,
allocCount: usize = 0,
@ -85,22 +100,32 @@ pub const BumpArenaAdvanced = struct {
/// Saves the current write position. Rewind with `defer checkpoint.rewind()`;
/// memory allocated after the checkpoint is invalid once it is rewound.
/// Checkpoints must be rewound in the reverse order they were taken.
pub fn checkpoint(self: *@This()) BumpCheckpoint {
self.depth += 1;
/// Checkpoints may be rewound in any order and more than once. Rewinding one
/// invalidates every checkpoint taken after it, and reset invalidates all.
/// Checkpoints that are never rewound are simply dropped.
pub fn checkpoint(self: *Self) Checkpoint {
self.next_id += 1;
if (options.validate_checkpoints) {
if (self.live_len == options.max_checkpoints) @panic("BumpArenaAdvanced: too many live checkpoints, raise max_checkpoints");
self.live[self.live_len] = self.next_id;
self.live_len += 1;
}
return .{
.arena = self,
.block = self.current,
.used = if (self.current) |block| block.used else 0,
.allocatedBytes = self.allocatedBytes,
.allocCount = self.allocCount,
.depth = self.depth,
.id = self.next_id,
};
}
fn rewind(self: *@This(), cp: BumpCheckpoint) void {
std.debug.assert(self.depth == cp.depth); // checkpoints rewound out of order
self.depth -= 1;
fn rewind(self: *Self, cp: Checkpoint) void {
if (options.validate_checkpoints) {
while (self.live_len > 0 and self.live[self.live_len - 1] > cp.id) self.live_len -= 1;
// the checkpoint was invalidated by rewinding an older one, or by reset.
std.debug.assert(self.live_len > 0 and self.live[self.live_len - 1] == cp.id);
}
while (self.current != cp.block) {
const block = self.current.?;
@ -221,10 +246,10 @@ pub const BumpArenaAdvanced = struct {
return .{ .ptr = self, .vtable = &vtable };
}
/// Releases every allocation. `.retain_capacity` keeps the blocks for reuse,
/// `.free_all` returns them to the backing allocator. No checkpoints may be live.
/// Releases every allocation and invalidates every checkpoint. `.retain_capacity`
/// keeps the blocks for reuse, `.free_all` returns them to the backing allocator.
pub fn reset(self: *@This(), mode: ResetMode) void {
std.debug.assert(self.depth == 0); // reset with a live checkpoint
self.live_len = 0;
while (self.current) |block| {
self.current = block.prev;
@ -239,21 +264,24 @@ pub const BumpArenaAdvanced = struct {
self.allocatedBytes = 0;
self.allocCount = 0;
}
};
/// A saved write position of a BumpArenaAdvanced, see BumpArenaAdvanced.checkpoint.
pub const BumpCheckpoint = struct {
arena: *BumpArenaAdvanced,
block: ?*BumpArenaAdvanced.Block,
/// A saved write position, see checkpoint.
pub const Checkpoint = struct {
arena: *Self,
block: ?*Block,
used: usize,
allocatedBytes: usize,
allocCount: usize,
depth: u32,
id: u64,
pub fn rewind(self: BumpCheckpoint) void {
pub fn rewind(self: Checkpoint) void {
self.arena.rewind(self);
}
};
};
};
}
pub const BumpCheckpoint = BumpArenaAdvanced.Checkpoint;
// mutex based, threadsafe BumpArenaAdvanced. does not support checkpoints,
// rewinding would release other threads' allocations.