summaryrefslogtreecommitdiff
path: root/src/zisp/gc.zig
diff options
context:
space:
mode:
authorTaylan Kammer <taylan.kammer@gmail.com>2025-03-30 18:34:00 +0200
committerTaylan Kammer <taylan.kammer@gmail.com>2025-03-30 18:34:00 +0200
commit3d05c94b9d8aa964e4ff848c95d5999cec170e04 (patch)
tree2864753404e8c9cb9fa8bce537772a29b90d1cbd /src/zisp/gc.zig
parent9340f3c44ca0d9d4fdee905fc6e8b428824b9185 (diff)
Big cleanup.
Diffstat (limited to 'src/zisp/gc.zig')
-rw-r--r--src/zisp/gc.zig47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/zisp/gc.zig b/src/zisp/gc.zig
new file mode 100644
index 0000000..d778b77
--- /dev/null
+++ b/src/zisp/gc.zig
@@ -0,0 +1,47 @@
+const builtin = @import("builtin");
+const std = @import("std");
+
+const value = @import("value.zig");
+
+const seq = value.seq;
+
+const Value = value.Value;
+
+/// A "heap value" that could be a Value or object header.
+pub const Hval = union {
+ value: Value,
+ seq_header: seq.Header,
+};
+
+pub const alloc = std.heap.smp_allocator;
+
+// Cons cells
+
+var cons_pool = std.heap.MemoryPool([2]Value).init(alloc);
+
+pub fn cons(v1: Value, v2: Value) *[2]Value {
+ const mem = cons_pool.create() catch @panic("OOM");
+ mem[0] = v1;
+ mem[1] = v2;
+ return mem;
+}
+
+// Interned strings
+
+var istr_pool = std.hash_map.StringHashMap(void).init(alloc);
+
+pub fn intern(header: seq.Header, str: []const u8) *seq.Header {
+ const hs = @sizeOf(seq.Header);
+ const size = str.len + hs;
+ const copy = alloc.alloc(u8, size) catch @panic("OOM");
+ const header_bytes: [hs]u8 = @bitCast(header);
+ @memcpy(copy[0..hs], &header_bytes);
+ @memcpy(copy[hs..size], str);
+ const entry = istr_pool.getOrPutValue(copy, {}) catch @panic("OOM");
+ return @ptrCast(entry.key_ptr);
+}
+
+pub fn istrHeader(ptr: *Hval) *seq.Header {
+ const entry_key: *[]u8 = @ptrCast(ptr);
+ return @alignCast(@ptrCast(entry_key.ptr));
+}