blob: 46ac0915c2677f4e48e95b107fa1ab57a9219484 (
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
|
const std = @import("std");
const value = @import("value.zig");
const Value = value.Value;
const Hval = value.Hval;
var _gpa = std.heap.GeneralPurposeAllocator(.{}).init;
const gpa = _gpa.allocator();
// Cons cells
var cons_pool = std.heap.MemoryPool([2]Value).init(gpa);
pub fn cons(v1: Value, v2: Value) *[2]Value {
const mem = cons_pool.create() catch @panic("OOM");
mem[0] = v1;
mem[1] = v2;
return mem;
}
// Interned strings
var istr_pool = std.hash_map.StringHashMap(void).init(gpa);
pub fn intern(header: value.seq.Header, str: []const u8) [*]Hval {
comptime {
std.debug.assert(@sizeOf(value.seq.Header) == 8);
}
const size = str.len + 8;
const copy = gpa.alloc(u8, size) catch @panic("OOM");
const header_bytes: [8]u8 = @bitCast(header);
@memcpy(copy[0..8], &header_bytes);
@memcpy(copy[8..size], str);
const entry = istr_pool.getOrPutValue(copy, {}) catch @panic("OOM");
return @ptrCast(entry.key_ptr);
}
pub fn istrHeader(ptr: [*]Hval) *value.seq.Header {
const entry_key: *[]u8 = @ptrCast(ptr);
return @alignCast(@ptrCast(entry_key.ptr));
}
|