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/istr.zig | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) (limited to 'src/zisp/value/istr.zig') 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); } -- cgit v1.2.3