summaryrefslogtreecommitdiff
path: root/src/libzisp/value
diff options
context:
space:
mode:
authorTaylan Kammer <taylan.kammer@gmail.com>2025-03-19 08:49:52 +0100
committerTaylan Kammer <taylan.kammer@gmail.com>2025-03-19 08:49:52 +0100
commitd1e0c8f3a928247d9e2576fddd8143f5d6cf4646 (patch)
tree50d5fdc71c992433da4b6572f2c5a09eaca7737e /src/libzisp/value
parent7b67144bc3bda3b92b5ba599e5198d16c0cf4d1f (diff)
Code cleanup & use SMP Allocator.
Diffstat (limited to 'src/libzisp/value')
-rw-r--r--src/libzisp/value/istr.zig9
-rw-r--r--src/libzisp/value/ptr.zig20
-rw-r--r--src/libzisp/value/seq.zig5
3 files changed, 20 insertions, 14 deletions
diff --git a/src/libzisp/value/istr.zig b/src/libzisp/value/istr.zig
index 8056d98..9834716 100644
--- a/src/libzisp/value/istr.zig
+++ b/src/libzisp/value/istr.zig
@@ -6,6 +6,8 @@ const gc = @import("../gc.zig");
const ptr = @import("ptr.zig");
const seq = @import("seq.zig");
+const Hval = gc.Hval;
+
const Value = value.Value;
// Zig API
@@ -34,13 +36,14 @@ pub fn intern(str: []const u8, quoted: bool) Value {
} },
.size = @intCast(str.len),
};
- const bytes_ptr = gc.intern(header, str);
- return ptr.pack(bytes_ptr, .seq);
+ const header_ptr = gc.intern(header, str);
+ return ptr.pack(@ptrCast(header_ptr), .seq);
}
pub fn getHeader(v: Value) *seq.Header {
assert(v);
- return gc.istrHeader(ptr.unpack(v).@"0");
+ const header_ptr, _ = ptr.unpack(v);
+ return gc.istrHeader(header_ptr);
}
// Zisp API
diff --git a/src/libzisp/value/ptr.zig b/src/libzisp/value/ptr.zig
index b07acc4..2ed3765 100644
--- a/src/libzisp/value/ptr.zig
+++ b/src/libzisp/value/ptr.zig
@@ -1,9 +1,11 @@
const std = @import("std");
-const value = @import("../value.zig");
+
const gc = @import("../gc.zig");
+const value = @import("../value.zig");
+
+const Hval = gc.Hval;
const Value = value.Value;
-const Hval = value.Hval;
// Zig API
@@ -86,23 +88,23 @@ pub fn assertStrong(v: Value) void {
}
}
-pub fn packZisp(ptr: [*]Hval, tag: Tag, is_weak: bool) Value {
+pub fn packZisp(ptr: *Hval, tag: Tag, is_weak: bool) Value {
return .{ .zptr = .{
.tagged_value = tagPtr(ptr, tag),
.is_weak = is_weak,
} };
}
-pub fn pack(ptr: [*]Hval, tag: Tag) Value {
+pub fn pack(ptr: *Hval, tag: Tag) Value {
return packZisp(ptr, tag, false);
}
-pub fn packWeak(ptr: [*]Hval, tag: Tag) Value {
+pub fn packWeak(ptr: *Hval, tag: Tag) Value {
return packZisp(ptr, tag, true);
}
// Unpacks weak as well; no need for a separate fn.
-pub fn unpack(v: Value) struct { [*]Hval, Tag } {
+pub fn unpack(v: Value) struct { *Hval, Tag } {
assertZisp(v);
return untagPtr(v.zptr.tagged_value);
}
@@ -117,15 +119,15 @@ pub fn isWeakNull(v: Value) bool {
return v.zptr.tagged_value == 0;
}
-fn tagPtr(ptr: [*]Hval, tag: Tag) u48 {
+fn tagPtr(ptr: *Hval, tag: Tag) u48 {
const int: usize = @intFromPtr(ptr);
const untagged: u48 = @intCast(int);
return untagged | @intFromEnum(tag);
}
-fn untagPtr(tagged: u48) struct { [*]Hval, Tag } {
+fn untagPtr(tagged: u48) struct { *Hval, Tag } {
const untagged: u48 = tagged & 0xfffffffffff8;
- const ptr: [*]Hval = @ptrFromInt(untagged);
+ const ptr: *Hval = @ptrFromInt(untagged);
const int: u3 = @truncate(tagged);
const tag: Tag = @enumFromInt(int);
return .{ ptr, tag };
diff --git a/src/libzisp/value/seq.zig b/src/libzisp/value/seq.zig
index 5382a7e..3418a5a 100644
--- a/src/libzisp/value/seq.zig
+++ b/src/libzisp/value/seq.zig
@@ -49,8 +49,9 @@ pub const Header = packed struct(u64) {
size: u48,
pub fn bytes(self: *Header) []u8 {
+ const hs = @sizeOf(Header);
const ptr: [*]u8 = @ptrCast(self);
- const end = 8 + self.size;
- return ptr[8..end];
+ const end = hs + self.size;
+ return ptr[hs..end];
}
};