summaryrefslogtreecommitdiff
path: root/src/zisp/gc.zig
blob: 0e26aa9920d64f646a6f7777e280bc09df3056e6 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const builtin = @import("builtin");
const std = @import("std");

const Alloc = std.mem.Allocator;

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

pub const CodePool = @import("gc/CodePool.zig");
pub const IstrPool = @import("gc/IstrPool.zig");

const HeapPtr = value.HeapPtr;

var main_alloc: Alloc = undefined;

var code_heap_start: usize = undefined;
var main_heap_start: usize = undefined;
var istr_heap_start: usize = undefined;

var main_list_pool: ListPool = undefined;
var main_istr_pool: IstrPool = undefined;

pub fn codePtrFromIdx(idx: u32) [*]Value {
    return @ptrFromInt(code_heap_start + idx * 8);
}

pub fn heapPtrFromIdx(idx: u32) HeapPtr {
    return @ptrFromInt(main_heap_start + idx * 8);
}

pub fn istrPtrFromIdx(idx: u32) [*]const u8 {
    return @ptrFromInt(istr_heap_start + idx);
}

pub fn codeIdxFromPtr(ptr: [*]Value) u32 {
    return @intCast(@intFromPtr(ptr - code_heap_start) / 8);
}

pub fn heapIdxFromPtr(ptr: HeapPtr) u32 {
    return @intCast(@intFromPtr(ptr - main_heap_start) / 8);
}

pub fn istrIdxFromPtr(ptr: [*]const u8) u32 {
    return @intCast(@intFromPtr(ptr - istr_heap_start));
}

// init

var init_done = false;

pub fn init() !void {
    if (init_done) return;
    defer init_done = true;

    main_alloc = std.heap.smp_allocator;

    //code_heap_start = ...;

    main_list_pool = try ListPool.init(main_alloc);
    main_istr_set = try IstrPool.init(main_alloc);
}

pub fn mainAlloc() Alloc {
    init() catch @panic("OOM"); // TODO this is only here for the test suite
    return main_alloc;
}

pub fn mainListPool() *ListPool {
    init() catch @panic("OOM"); // TODO this is only here for the test suite
    return &main_list_pool;
}

pub fn mainIstrPool() *IstrPool {
    init() catch @panic("OOM"); // TODO this is only here for the test suite
    return &main_istr_set;
}