-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpool.zig
More file actions
36 lines (30 loc) · 942 Bytes
/
pool.zig
File metadata and controls
36 lines (30 loc) · 942 Bytes
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
const std = @import("std");
const js = @import("zapi:zapi").js;
const Node = @import("persistent_merkle_tree").Node;
pub const State = struct {
pool: Node.Pool = undefined,
initialized: bool = false,
pub fn init(self: *State) !void {
if (self.initialized) return;
self.pool = try Node.Pool.init(.{});
self.initialized = true;
}
pub fn deinit(self: *State) void {
if (!self.initialized) return;
self.pool.deinit();
self.initialized = false;
}
};
pub var state: State = .{};
/// JS: pool.ensureCapacity(newSize)
pub fn ensureCapacity(new_size: js.Number) !void {
if (!state.initialized) {
return error.PoolNotInitialized;
}
const requested = new_size.assertU32();
const old_size = state.pool.nodes.capacity;
if (requested <= old_size) {
return;
}
try state.pool.preheat(@intCast(requested - state.pool.nodes.capacity));
}