Bump arena- new checkpointing system
This commit is contained in:
parent
1a07a5388c
commit
14df85a638
|
|
@ -7,7 +7,19 @@ const stats = @import("stats.zig");
|
||||||
// memory comes from a linked list of blocks taken from the backing allocator.
|
// 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
|
// 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.
|
// 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
|
/// usable bytes in a standard block. allocations that do not fit get a
|
||||||
/// power of two sized block of their own.
|
/// power of two sized block of their own.
|
||||||
pub const BlockSize = 8192 * 2;
|
pub const BlockSize = 8192 * 2;
|
||||||
|
|
@ -42,8 +54,11 @@ pub const BumpArenaAdvanced = struct {
|
||||||
current: ?*Block = null,
|
current: ?*Block = null,
|
||||||
// free lists indexed by size class, sizeClass(block.size)
|
// free lists indexed by size class, sizeClass(block.size)
|
||||||
free_blocks: [size_classes]?*Block = @splat(null),
|
free_blocks: [size_classes]?*Block = @splat(null),
|
||||||
// live checkpoints, used to assert they are rewound in LIFO order.
|
// ids of the checkpoints still on the timeline, ascending. rewinding drops
|
||||||
depth: u32 = 0,
|
// 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,
|
allocatedBytes: usize = 0,
|
||||||
allocCount: usize = 0,
|
allocCount: usize = 0,
|
||||||
|
|
@ -85,22 +100,32 @@ pub const BumpArenaAdvanced = struct {
|
||||||
|
|
||||||
/// Saves the current write position. Rewind with `defer checkpoint.rewind()`;
|
/// Saves the current write position. Rewind with `defer checkpoint.rewind()`;
|
||||||
/// memory allocated after the checkpoint is invalid once it is rewound.
|
/// memory allocated after the checkpoint is invalid once it is rewound.
|
||||||
/// Checkpoints must be rewound in the reverse order they were taken.
|
/// Checkpoints may be rewound in any order and more than once. Rewinding one
|
||||||
pub fn checkpoint(self: *@This()) BumpCheckpoint {
|
/// invalidates every checkpoint taken after it, and reset invalidates all.
|
||||||
self.depth += 1;
|
/// 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 .{
|
return .{
|
||||||
.arena = self,
|
.arena = self,
|
||||||
.block = self.current,
|
.block = self.current,
|
||||||
.used = if (self.current) |block| block.used else 0,
|
.used = if (self.current) |block| block.used else 0,
|
||||||
.allocatedBytes = self.allocatedBytes,
|
.allocatedBytes = self.allocatedBytes,
|
||||||
.allocCount = self.allocCount,
|
.allocCount = self.allocCount,
|
||||||
.depth = self.depth,
|
.id = self.next_id,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rewind(self: *@This(), cp: BumpCheckpoint) void {
|
fn rewind(self: *Self, cp: Checkpoint) void {
|
||||||
std.debug.assert(self.depth == cp.depth); // checkpoints rewound out of order
|
if (options.validate_checkpoints) {
|
||||||
self.depth -= 1;
|
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) {
|
while (self.current != cp.block) {
|
||||||
const block = self.current.?;
|
const block = self.current.?;
|
||||||
|
|
@ -221,10 +246,10 @@ pub const BumpArenaAdvanced = struct {
|
||||||
return .{ .ptr = self, .vtable = &vtable };
|
return .{ .ptr = self, .vtable = &vtable };
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Releases every allocation. `.retain_capacity` keeps the blocks for reuse,
|
/// Releases every allocation and invalidates every checkpoint. `.retain_capacity`
|
||||||
/// `.free_all` returns them to the backing allocator. No checkpoints may be live.
|
/// keeps the blocks for reuse, `.free_all` returns them to the backing allocator.
|
||||||
pub fn reset(self: *@This(), mode: ResetMode) void {
|
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| {
|
while (self.current) |block| {
|
||||||
self.current = block.prev;
|
self.current = block.prev;
|
||||||
|
|
@ -239,21 +264,24 @@ pub const BumpArenaAdvanced = struct {
|
||||||
self.allocatedBytes = 0;
|
self.allocatedBytes = 0;
|
||||||
self.allocCount = 0;
|
self.allocCount = 0;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
/// A saved write position of a BumpArenaAdvanced, see BumpArenaAdvanced.checkpoint.
|
/// A saved write position, see checkpoint.
|
||||||
pub const BumpCheckpoint = struct {
|
pub const Checkpoint = struct {
|
||||||
arena: *BumpArenaAdvanced,
|
arena: *Self,
|
||||||
block: ?*BumpArenaAdvanced.Block,
|
block: ?*Block,
|
||||||
used: usize,
|
used: usize,
|
||||||
allocatedBytes: usize,
|
allocatedBytes: usize,
|
||||||
allocCount: usize,
|
allocCount: usize,
|
||||||
depth: u32,
|
id: u64,
|
||||||
|
|
||||||
pub fn rewind(self: BumpCheckpoint) void {
|
pub fn rewind(self: Checkpoint) void {
|
||||||
self.arena.rewind(self);
|
self.arena.rewind(self);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const BumpCheckpoint = BumpArenaAdvanced.Checkpoint;
|
||||||
|
|
||||||
// mutex based, threadsafe BumpArenaAdvanced. does not support checkpoints,
|
// mutex based, threadsafe BumpArenaAdvanced. does not support checkpoints,
|
||||||
// rewinding would release other threads' allocations.
|
// rewinding would release other threads' allocations.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue