summaryrefslogtreecommitdiff
path: root/src/zisp/gc/PairPool.zig
blob: 4a77acc058fbeb93aac3c0361014b8604ed03839 (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
const std = @import("std");

const Alloc = std.mem.Allocator;
const AlignedPool = std.heap.memory_pool.Aligned;

const value = @import("../value.zig");

const Value = value.Value;
const PairPtr = value.pair.PairPtr;

const PairPool = @This();

alloc: Alloc,
pool: AlignedPool(value.pair.Pair, @enumFromInt(@alignOf(value.Zptr))),

const default_init_cap = 1024;

pub fn init(alloc: Alloc) !PairPool {
    return initCustom(alloc, default_init_cap);
}

pub fn initCustom(alloc: Alloc, init_cap: usize) !PairPool {
    return .{
        .alloc = alloc,
        .pool = try .initCapacity(alloc, init_cap),
    };
}

pub fn cons(self: *PairPool, car: Value, cdr: Value) !PairPtr {
    var p = try self.pool.create(self.alloc);
    p.car = car;
    p.cdr = cdr;
    return p;
}