blob: 6efa8fe7ae9a9361a12d0a7813e9ec41832742c6 (
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
76
77
78
79
80
81
82
83
84
85
86
87
|
const builtin = @import("builtin");
const std = @import("std");
const Alloc = std.mem.Allocator;
const value = @import("value.zig");
pub const ListPool = @import("gc/ListPool.zig");
pub const IstrPool = @import("gc/IstrPool.zig");
const Value = value.Value;
const HeapPtr = value.HeapPtr;
const HeapType = value.HeapType;
const i32max = std.math.maxInt(i32);
var heap: *[2 * i32max]u64 = undefined;
var watermark: i32 = 0;
threadlocal var tl_watermark: i32 = 0;
var main_list_pool: ListPool = undefined;
var main_istr_pool: IstrPool = undefined;
pub fn ptrFromIdx(idx: u32) [*]Value {
return &heap[idx];
}
pub fn idxFromPtr(ptr: [*]Value) u32 {
return @intCast(ptr - heap);
}
// TODO: Actually allocate within the boundaries of the various heaps
pub fn allocIstr(len: u8) [*]const u8 {
return @ptrCast(main_alloc.alloc(u8, len).ptr);
}
pub fn allocHeap(comptime typ: HeapType, len: usize) !typ.PtrType() {
const aln = std.mem.Alignment.of(HeapPtr);
return @ptrCast(main_alloc.alignedAlloc(u8, aln, len));
}
pub fn createHeap(comptime typ: HeapType) !typ.PtrType() {
return @ptrCast(main_alloc.create(typ.ObjType()));
}
pub fn packHeap(ptr: anytype) Value {
const idx = heapIdxFromPtr(ptr);
const typ = value.HeapType.of(ptr);
return .{ .hptr = .{ .idx = idx, .typ = typ } };
}
pub fn unpackHeap(comptime typ: HeapType, v: Value) typ.PtrType() {
if (!v.isHptrTyp(typ)) @panic("Wrong type.");
return heapPtrFromIdx(typ, v.hptr.idx);
}
// init
var init_done = false;
pub fn init() !void {
if (init_done) return;
defer init_done = true;
main_alloc = std.heap.smp_allocator;
main_list_pool = try ListPool.init(main_alloc);
main_istr_pool = 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_pool;
}
|