From e26e81aa33dc1ecc2f26de26b1d420d288a28493 Mon Sep 17 00:00:00 2001 From: Taylan Kammer Date: Sun, 16 Aug 2026 10:01:12 +0200 Subject: Implemented meta allocator. --- src/zisp/value/hptr.zig | 33 +++++++++++++++++++++++++++++++++ src/zisp/value/istr.zig | 30 +++++++++--------------------- src/zisp/value/ptr.zig | 33 --------------------------------- 3 files changed, 42 insertions(+), 54 deletions(-) create mode 100644 src/zisp/value/hptr.zig delete mode 100644 src/zisp/value/ptr.zig (limited to 'src/zisp/value') diff --git a/src/zisp/value/hptr.zig b/src/zisp/value/hptr.zig new file mode 100644 index 0000000..8a3ca18 --- /dev/null +++ b/src/zisp/value/hptr.zig @@ -0,0 +1,33 @@ +const std = @import("std"); + +const gc = @import("../gc.zig"); +const value = @import("../value.zig"); + +const HeapType = value.HeapType; +const Value = value.Value; +const Zptr = value.Zptr; + +const max = std.math.maxInt; + +// Zig API + +pub fn check(v: Value) bool { + return v.isPtrAny(); +} + +pub fn assert(v: Value) void { + if (!check(v)) { + v.dump(); + @panic("not pointer"); + } +} + +pub fn pack(comptime ht: HeapType, ptr: ht.PtrType()) Value { + const index: u44 = @intCast(@intFromPtr(ptr) >> 4); + return .{ .ptr = .{ .index = index, .htype = ht } }; +} + +pub fn unpack(v: Value) struct { HeapType, ?Zptr } { + assert(v); + return v.getPtrAny() orelse unreachable; +} diff --git a/src/zisp/value/istr.zig b/src/zisp/value/istr.zig index 528280a..c0928ef 100644 --- a/src/zisp/value/istr.zig +++ b/src/zisp/value/istr.zig @@ -38,35 +38,24 @@ fn assertValidIstr(s: []const u8) void { } } -pub fn new(alloc: Alloc, s: []const u8) !IstrPtr { - const algn = std.mem.Alignment.of(IstrPtr); - const size = @sizeOf(IstrHead) + s.len; - const istr: IstrPtr = @ptrCast(try alloc.alignedAlloc(u8, algn, size)); - istr.putStr(s); - return istr; +pub fn new(s: []const u8) !Value { + const istr = gc.allocIstr(s.len); + @memcpy(istr) } -pub fn getOrNew(s: []const u8) !IstrPtr { +pub fn getOrNew(s: []const u8) !Value { return getOrNewInPool(gc.mainIstrPool(), s); } -pub fn getOrNewInPool(pool: *IstrPool, s: []const u8) !IstrPtr { +pub fn getOrNewInPool(pool: *IstrPool, s: []const u8) !Value { assertValidIstr(s); return try pool.getOrNew(s); } -pub fn pack(istr: IstrPtr) Value { - const ptr = @intFromPtr(istr); - return .{ .istr = .{ .ptr = @intCast(ptr) } }; -} - -pub fn unpack(v: Value) IstrPtr { - return @ptrFromInt(v.istr.ptr); -} - pub fn getBytes(v: Value) []const u8 { assert(v); - return unpack(v).bytes(); + const ptr = gc.istrPtrFromIdx(v.istr.idx); + return ptr[0..v.istr.len]; } // Zisp API @@ -77,8 +66,7 @@ pub fn pred(v: Value) Value { pub fn getLen(v: Value) Value { assert(v); - const istr: IstrPtr = @ptrFromInt(v.istr.ptr); - return value.fixnum.pack(istr.len); + return value.fixnum.pack(v.istr.len); } // TODO: Zisp representation of IstrPool & ability to intern in given IstrPool @@ -86,5 +74,5 @@ pub fn getLen(v: Value) Value { pub fn intern(v: Value) Value { const istr = assert(v); const pool = gc.mainIstrPool(); - return pack(pool.getOrPut(istr)); + return pool.getOrPut(istr); } diff --git a/src/zisp/value/ptr.zig b/src/zisp/value/ptr.zig deleted file mode 100644 index 8a3ca18..0000000 --- a/src/zisp/value/ptr.zig +++ /dev/null @@ -1,33 +0,0 @@ -const std = @import("std"); - -const gc = @import("../gc.zig"); -const value = @import("../value.zig"); - -const HeapType = value.HeapType; -const Value = value.Value; -const Zptr = value.Zptr; - -const max = std.math.maxInt; - -// Zig API - -pub fn check(v: Value) bool { - return v.isPtrAny(); -} - -pub fn assert(v: Value) void { - if (!check(v)) { - v.dump(); - @panic("not pointer"); - } -} - -pub fn pack(comptime ht: HeapType, ptr: ht.PtrType()) Value { - const index: u44 = @intCast(@intFromPtr(ptr) >> 4); - return .{ .ptr = .{ .index = index, .htype = ht } }; -} - -pub fn unpack(v: Value) struct { HeapType, ?Zptr } { - assert(v); - return v.getPtrAny() orelse unreachable; -} -- cgit v1.2.3