summaryrefslogtreecommitdiff
path: root/src/libzisp/gc.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/libzisp/gc.zig')
-rw-r--r--src/libzisp/gc.zig40
1 files changed, 26 insertions, 14 deletions
diff --git a/src/libzisp/gc.zig b/src/libzisp/gc.zig
index 46ac091..059be74 100644
--- a/src/libzisp/gc.zig
+++ b/src/libzisp/gc.zig
@@ -1,16 +1,30 @@
+const builtin = @import("builtin");
const std = @import("std");
const value = @import("value.zig");
+const seq = value.seq;
+
const Value = value.Value;
-const Hval = value.Hval;
-var _gpa = std.heap.GeneralPurposeAllocator(.{}).init;
-const gpa = _gpa.allocator();
+/// A "heap value" that could be a Value or object header.
+pub const Hval = union {
+ value: Value,
+ seq_header: seq.Header,
+};
+
+var _debugAlloc = switch (builtin.mode) {
+ .Debug => std.heap.DebugAllocator(.{}).init,
+ else => undefined,
+};
+const alloc = switch (builtin.mode) {
+ .Debug => _debugAlloc.allocator(),
+ else => std.heap.smp_allocator,
+};
// Cons cells
-var cons_pool = std.heap.MemoryPool([2]Value).init(gpa);
+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");
@@ -21,22 +35,20 @@ pub fn cons(v1: Value, v2: Value) *[2]Value {
// Interned strings
-var istr_pool = std.hash_map.StringHashMap(void).init(gpa);
+var istr_pool = std.hash_map.StringHashMap(void).init(alloc);
-pub fn intern(header: value.seq.Header, str: []const u8) [*]Hval {
- comptime {
- std.debug.assert(@sizeOf(value.seq.Header) == 8);
- }
- const size = str.len + 8;
- const copy = gpa.alloc(u8, size) catch @panic("OOM");
+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: [8]u8 = @bitCast(header);
- @memcpy(copy[0..8], &header_bytes);
- @memcpy(copy[8..size], str);
+ @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) *value.seq.Header {
+pub fn istrHeader(ptr: *Hval) *seq.Header {
const entry_key: *[]u8 = @ptrCast(ptr);
return @alignCast(@ptrCast(entry_key.ptr));
}