From 79f4d5ae23c44924f413f9d7a1113d2280611ec5 Mon Sep 17 00:00:00 2001 From: Taylan Kammer Date: Sun, 31 May 2026 01:02:58 +0200 Subject: Make the code compile. --- src/zisp/gc.zig | 55 ++++++++++++++----------------------------------------- 1 file changed, 14 insertions(+), 41 deletions(-) (limited to 'src/zisp/gc.zig') 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"); } -- cgit v1.2.3