diff options
| author | Taylan Kammer <taylan.kammer@gmail.com> | 2026-06-23 19:26:57 +0200 |
|---|---|---|
| committer | Taylan Kammer <taylan.kammer@gmail.com> | 2026-06-23 19:26:57 +0200 |
| commit | f6385a0b3cae394aa8e21d7899fd22f0176c43e4 (patch) | |
| tree | 9935c610ae4e1079d3e82d34815085058c320c74 /src/zisp/gc | |
| parent | d19ad26fb451c4bf10b20d90483ac424a4dd9a57 (diff) | |
Move seg_stack to lib.
Diffstat (limited to 'src/zisp/gc')
| -rw-r--r-- | src/zisp/gc/ListPool.zig | 2 | ||||
| -rw-r--r-- | src/zisp/gc/seg_stack.zig | 84 |
2 files changed, 1 insertions, 85 deletions
diff --git a/src/zisp/gc/ListPool.zig b/src/zisp/gc/ListPool.zig index 0735582..668735d 100644 --- a/src/zisp/gc/ListPool.zig +++ b/src/zisp/gc/ListPool.zig @@ -14,7 +14,7 @@ const Alloc = std.mem.Allocator; const ArrayList = std.ArrayListUnmanaged; const value = @import("../value.zig"); -const seg_stack = @import("seg_stack.zig"); +const seg_stack = @import("../lib/seg_stack.zig"); const Value = value.Value; const SegStack = seg_stack.SegStack; diff --git a/src/zisp/gc/seg_stack.zig b/src/zisp/gc/seg_stack.zig deleted file mode 100644 index 0867892..0000000 --- a/src/zisp/gc/seg_stack.zig +++ /dev/null @@ -1,84 +0,0 @@ -//! Segmented linked list backed stack - -const std = @import("std"); - -const Alloc = std.mem.Allocator; - -/// Segmented linked list backed stack -pub fn SegStack(T: type, seg_max_bytes: usize) type { - const seg_size: usize = (seg_max_bytes - @sizeOf(usize)) / @sizeOf(T); - - comptime { - if (seg_size < 4) { - @panic("Surely you didn't want to have segments this small?"); - } - } - - const Node = struct { - const Self = @This(); - - prev: ?*Self = null, - elts: [seg_size]T = undefined, - - pub fn new(alloc: Alloc) !*Self { - const self = try alloc.create(Self); - self.* = .{}; - return self; - } - }; - - return struct { - const Self = @This(); - - head: *Node, - idx: usize = 0, - - // To prevent "thrashing" we don't immediately deallocate the current - // node when it's emptied but rather save it aside. - aside: ?*Node = null, - - pub fn init(alloc: Alloc) !Self { - return .{ .head = try .new(alloc) }; - } - - pub fn deinit(self: *Self, alloc: Alloc) void { - if (self.aside) |aside| alloc.destroy(aside); - var node: ?Node = self.head; - while (node) |n| { - alloc.destroy(n); - node = n.prev; - } - } - - pub fn push(self: *Self, alloc: Alloc, elt: T) !void { - if (self.idx == seg_size) { - if (self.aside) |aside| { - self.head = aside; - self.aside = null; - } else { - const prev = self.head; - self.head = try .new(alloc); - self.head.prev = prev; - } - self.idx = 0; - } - self.head.elts[self.idx] = elt; - self.idx += 1; - } - - pub fn pop(self: *Self, alloc: Alloc) ?T { - if (self.idx == 0) { - if (self.head.prev) |prev| { - if (self.aside) |aside| alloc.destroy(aside); - self.aside = self.head; - self.head = prev; - self.idx = seg_size; - } else { - return null; - } - } - self.idx -= 1; - return self.head.elts[self.idx]; - } - }; -} |
