summaryrefslogtreecommitdiff
path: root/src/zisp/gc/seg_stack.zig
diff options
context:
space:
mode:
authorTaylan Kammer <taylan.kammer@gmail.com>2026-06-23 19:26:57 +0200
committerTaylan Kammer <taylan.kammer@gmail.com>2026-06-23 19:26:57 +0200
commitf6385a0b3cae394aa8e21d7899fd22f0176c43e4 (patch)
tree9935c610ae4e1079d3e82d34815085058c320c74 /src/zisp/gc/seg_stack.zig
parentd19ad26fb451c4bf10b20d90483ac424a4dd9a57 (diff)
Move seg_stack to lib.
Diffstat (limited to 'src/zisp/gc/seg_stack.zig')
-rw-r--r--src/zisp/gc/seg_stack.zig84
1 files changed, 0 insertions, 84 deletions
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];
- }
- };
-}