summaryrefslogtreecommitdiff
path: root/src/zisp/gc.zig
diff options
context:
space:
mode:
authorTaylan Kammer <taylan.kammer@gmail.com>2026-05-31 01:02:58 +0200
committerTaylan Kammer <taylan.kammer@gmail.com>2026-05-31 01:02:58 +0200
commit79f4d5ae23c44924f413f9d7a1113d2280611ec5 (patch)
treed00735af778cfa3a52959ebd51eb1161a20a0565 /src/zisp/gc.zig
parenta21c485635f74c6fb86db630110fa4ddebc8c0c0 (diff)
Make the code compile.
Diffstat (limited to 'src/zisp/gc.zig')
-rw-r--r--src/zisp/gc.zig55
1 files changed, 14 insertions, 41 deletions
diff --git a/src/zisp/gc.zig b/src/zisp/gc.zig
index 4fc2c90..1be4304 100644
--- a/src/zisp/gc.zig
+++ b/src/zisp/gc.zig
@@ -3,25 +3,24 @@ const std = @import("std");
const value = @import("value.zig");
+const IstrSet = @import("gc/IstrSet.zig");
+
+const istr = value.istr;
const ptr = value.ptr;
-const seq = value.seq;
const Value = value.Value;
const Zptr = value.Zptr;
+const Istr = istr.Istr;
+
pub const alloc = std.heap.smp_allocator;
// Cons cells
const ConsPool = std.heap.MemoryPool([2]Value);
var cons_pool: ConsPool = undefined;
-var need_init = true;
pub fn cons(v1: Value, v2: Value) *[2]Value {
- if (need_init) {
- cons_pool = ConsPool.initCapacity(alloc, 64) catch @panic("OOM");
- need_init = false;
- }
const mem = cons_pool.create(alloc) catch @panic("OOM");
mem[0] = v1;
mem[1] = v2;
@@ -30,42 +29,16 @@ pub fn cons(v1: Value, v2: Value) *[2]Value {
// Interned strings
-const istr_ctx = std.hash_map.StringContext{};
-var istr_pool = std.hash_map.StringHashMap(*seq.Header).init(alloc);
-
-pub fn internString(str: []const u8) Value {
- const gop = istr_pool.getOrPutAdapted(
- str,
- istr_ctx,
- ) catch @panic("OOM");
- if (gop.found_existing) {
- const p: *seq.Header = gop.value_ptr.*;
- return ptr.pack(p, .seq);
- }
-
- std.debug.assert(str.len <= std.math.maxInt(u48));
+var istr_set: IstrSet = undefined;
- const header: seq.Header = .{
- .type = .string,
- .info = .{ .string = .{
- .enc = .utf8,
- .interned = true,
- } },
- .size = @intCast(str.len),
- };
-
- const h_align: std.mem.Alignment = @enumFromInt(@alignOf(seq.Header));
- const h_size = @sizeOf(seq.Header);
- const size = str.len + h_size;
-
- const mem = alloc.alignedAlloc(u8, h_align, size) catch @panic("OOM");
-
- const h_bytes: [h_size]u8 = @bitCast(header);
- @memcpy(mem[0..h_size], &h_bytes);
- @memcpy(mem[h_size..size], str);
+pub fn internString(s: []const u8) !Value {
+ const p = istr_set.add(alloc, s) catch @panic("OOM");
+ return ptr.pack(p, .istr);
+}
- gop.key_ptr.* = alloc.dupe(u8, str) catch @panic("OOM");
- gop.value_ptr.* = @ptrCast(mem.ptr);
+// Init
- return ptr.pack(mem.ptr, .seq);
+pub fn init() void {
+ cons_pool = ConsPool.initCapacity(alloc, 64) catch @panic("OOM");
+ istr_set = IstrSet.init(alloc) catch @panic("OOM");
}