summaryrefslogtreecommitdiff
path: root/src/zisp/gc.zig
blob: 1be43047e133dc7a2fe3e67e26e2e5007f930962 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const builtin = @import("builtin");
const std = @import("std");

const value = @import("value.zig");

const IstrSet = @import("gc/IstrSet.zig");

const istr = value.istr;
const ptr = value.ptr;

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;

pub fn cons(v1: Value, v2: Value) *[2]Value {
    const mem = cons_pool.create(alloc) catch @panic("OOM");
    mem[0] = v1;
    mem[1] = v2;
    return mem;
}

// Interned strings

var istr_set: IstrSet = undefined;

pub fn internString(s: []const u8) !Value {
    const p = istr_set.add(alloc, s) catch @panic("OOM");
    return ptr.pack(p, .istr);
}

// Init

pub fn init() void {
    cons_pool = ConsPool.initCapacity(alloc, 64) catch @panic("OOM");
    istr_set = IstrSet.init(alloc) catch @panic("OOM");
}