summaryrefslogtreecommitdiff
path: root/src/zisp/gc.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/zisp/gc.zig')
-rw-r--r--src/zisp/gc.zig39
1 files changed, 30 insertions, 9 deletions
diff --git a/src/zisp/gc.zig b/src/zisp/gc.zig
index 0e26aa9..b36628f 100644
--- a/src/zisp/gc.zig
+++ b/src/zisp/gc.zig
@@ -5,25 +5,26 @@ const Alloc = std.mem.Allocator;
const value = @import("value.zig");
-pub const CodePool = @import("gc/CodePool.zig");
+pub const ListPool = @import("gc/ListPool.zig");
pub const IstrPool = @import("gc/IstrPool.zig");
const HeapPtr = value.HeapPtr;
+const HeapType = value.HeapType;
var main_alloc: Alloc = undefined;
-var code_heap_start: usize = undefined;
+var list_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 listPtrFromIdx(idx: u32) [*]Value {
+ return @ptrFromInt(list_heap_start + idx * 8);
}
-pub fn heapPtrFromIdx(idx: u32) HeapPtr {
+pub fn heapPtrFromIdx(comptime typ: HeapType, idx: u32) typ.PtrType() {
return @ptrFromInt(main_heap_start + idx * 8);
}
@@ -31,8 +32,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 listIdxFromPtr(ptr: [*]Value) u32 {
+ return @intCast(@intFromPtr(ptr - list_heap_start) / 8);
}
pub fn heapIdxFromPtr(ptr: HeapPtr) u32 {
@@ -43,6 +44,26 @@ pub fn istrIdxFromPtr(ptr: [*]const u8) u32 {
return @intCast(@intFromPtr(ptr - istr_heap_start));
}
+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;
@@ -53,10 +74,10 @@ pub fn init() !void {
main_alloc = std.heap.smp_allocator;
- //code_heap_start = ...;
+ //list_heap_start = ...;
main_list_pool = try ListPool.init(main_alloc);
- main_istr_set = try IstrPool.init(main_alloc);
+ main_istr_pool = try IstrPool.init(main_alloc);
}
pub fn mainAlloc() Alloc {