summaryrefslogtreecommitdiff
path: root/src/zisp/gc/ListPool.zig
diff options
context:
space:
mode:
authorTaylan Kammer <taylan.kammer@gmail.com>2026-06-21 23:31:08 +0200
committerTaylan Kammer <taylan.kammer@gmail.com>2026-06-21 23:31:08 +0200
commitaf42ac0a082c90b6dfced76cd1ecdf5091e22c7e (patch)
tree936dda4883ceb21e5e48c067edda440bf10693d6 /src/zisp/gc/ListPool.zig
parentb84ed4f563b3536365f7d3cc4d068407e98685b3 (diff)
yippie
Diffstat (limited to 'src/zisp/gc/ListPool.zig')
-rw-r--r--src/zisp/gc/ListPool.zig58
1 files changed, 44 insertions, 14 deletions
diff --git a/src/zisp/gc/ListPool.zig b/src/zisp/gc/ListPool.zig
index 6c34074..a843ae5 100644
--- a/src/zisp/gc/ListPool.zig
+++ b/src/zisp/gc/ListPool.zig
@@ -14,52 +14,70 @@ const Alloc = std.mem.Allocator;
const ArrayList = std.ArrayListUnmanaged;
const value = @import("../value.zig");
+const seg_stack = @import("seg_stack.zig");
const Value = value.Value;
+const SegStack = seg_stack.SegStack;
const Self = @This();
-// How many Value elements fit in a block. A good value should be 512, since
-// each Value is 8 bytes: 512 * 8 = 4 KiB
+/// How many Value elements fit in a block. A good value should be 512, since
+/// each Value is 8 bytes: 512 * 8 = 4 KiB
const block_value_cap = 512;
-// Initial capacity of ArrayList of pointers to filled blocks. Also 512, since
-// pointers are 8 bytes as well.
-const def_full_blocks_cap = 512;
-
const Block = [block_value_cap]Value;
alloc: Alloc,
current_block: *Block,
-current_index: usize,
+current_index: usize = 0,
-full_blocks: ArrayList(*Block),
+full_list: SegStack(*Block, 4096),
+free_lists: [7]SegStack([*]Value, 512),
pub fn init(alloc: Alloc) !Self {
return .{
.alloc = alloc,
.current_block = try alloc.create(Block),
- .current_index = 0,
- .full_blocks = try .initCapacity(alloc, def_full_blocks_cap),
+ .full_list = try .init(alloc),
+ .free_lists = .{
+ try .init(alloc),
+ try .init(alloc),
+ try .init(alloc),
+ try .init(alloc),
+ try .init(alloc),
+ try .init(alloc),
+ try .init(alloc),
+ },
};
}
pub fn deinit(self: *Self) void {
+ for (self.free_lists) |l| l.deinit(self.alloc);
+ while (self.full_list.pop(self.alloc)) |block| self.alloc.destroy(block);
+ self.full_list.deinit(self.alloc);
self.alloc.destroy(self.current_block);
- for (self.full_blocks) |block| self.alloc.destroy(block);
- self.full_blocks.deinit(self.alloc);
}
fn newBlock(self: *Self) !void {
- try self.full_blocks.append(self.alloc, self.current_block);
+ try self.full_list.push(self.alloc, self.current_block);
self.current_block = try self.alloc.create(Block);
self.current_index = 0;
}
-pub fn create(self: *Self, len: u3) ![*]Value {
+fn popFree(self: *Self, len: u3) ?[*]Value {
+ return self.free_lists[len - 1].pop(self.alloc);
+}
+
+fn pushFree(self: *Self, len: u3, ptr: [*]Value) !void {
+ try self.free_lists[len - 1].push(self.alloc, ptr);
+}
+
+pub fn allocVals(self: *Self, len: u3) ![*]Value {
std.debug.assert(len != 0);
+ if (self.popFree(len)) |ptr| return ptr;
+
if (len > block_value_cap - self.current_index) {
try self.newBlock();
}
@@ -67,3 +85,15 @@ pub fn create(self: *Self, len: u3) ![*]Value {
defer self.current_index += len;
return @ptrCast(&self.current_block[self.current_index]);
}
+
+pub fn freeVals(self: *Self, len: u3, ptr: [*]Value) !void {
+ std.debug.assert(len != 0);
+
+ try self.pushFree(len, ptr);
+}
+
+pub fn freeList(self: *Self, list: Value) !void {
+ const len = value.list.getLenTag(list);
+ const ptr = value.list.getValPtr(list);
+ try self.freeVals(len, ptr);
+}