From 23b1ae89d351aabbdc8b7d6dfa362cc31165501e Mon Sep 17 00:00:00 2001 From: Taylan Kammer Date: Wed, 1 Jul 2026 01:26:15 +0200 Subject: Big changes. Incomplete. Won't compile yet. --- src/zisp/gc/ListPool.zig | 110 ----------------------------------------------- 1 file changed, 110 deletions(-) delete mode 100644 src/zisp/gc/ListPool.zig (limited to 'src/zisp/gc/ListPool.zig') diff --git a/src/zisp/gc/ListPool.zig b/src/zisp/gc/ListPool.zig deleted file mode 100644 index 668735d..0000000 --- a/src/zisp/gc/ListPool.zig +++ /dev/null @@ -1,110 +0,0 @@ -//! List array pool -//! -//! Strategy: To ensure list arrays are allocated sequentially without any -//! padding, we allocate memory in large blocks, and whenever a list array with -//! a certain length is to be allocated, we check if the current block still has -//! enough room; otherwise we allocate the next block. -//! -//! This is only used for lists up to 7 elements. Starting at 8, they can't -//! share a cache line with other list elements anyway. - -const std = @import("std"); - -const Alloc = std.mem.Allocator; -const ArrayList = std.ArrayListUnmanaged; - -const value = @import("../value.zig"); -const seg_stack = @import("../lib/seg_stack.zig"); - -const Value = value.Value; -const SegStack = seg_stack.SegStack; - -const Self = @This(); - -// 4 KiB blocks fit 512 Value elements, which should be good. -const Block = [4096 / @sizeOf(Value)]Value; - -alloc: Alloc, - -current_block: *Block, -current_index: usize = 0, - -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), - .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); -} - -fn newBlock(self: *Self) !void { - try self.full_list.push(self.alloc, self.current_block); - self.current_block = try self.alloc.create(Block); - self.current_index = 0; -} - -fn curBlockPtr(self: *Self) [*]Value { - return @ptrCast(&self.current_block[self.current_index]); -} - -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 > self.current_block.len - self.current_index) { - try self.newBlock(); - } - - defer self.current_index += len; - return self.curBlockPtr(); -} - -pub fn freeVals(self: *Self, len: u3, ptr: [*]Value) !void { - std.debug.assert(len != 0); - - // Is it simply the last allocation? If so, just undo it. - if (self.current_index >= len) { - const ptr_val = @intFromPtr(ptr); - const last_ptr_val = @intFromPtr(self.curBlockPtr() - len); - if (ptr_val == last_ptr_val) { - self.current_index -= len; - return; - } - } - - 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); -} -- cgit v1.2.3