Have gotten far enough in zig that I'm trying to do things like "module level parsed data".
For example I've got this thing:
const Patch = struct {
stride: usize,
width: usize,
data: []Cell, // an enum fwiw, not relevant
const Self = @This();
pub fn parse(allocator: Allocator, s: []const u8) !Self {
var data = try allocator.alloc(Cell, s.len);
errdefer allocator.free(data);
// this does the "obvious" thing and parses u8 codes into Cell data
return Self.parseInto(data, s);
}
// other methods elided for brevity
};
Later on I want to have some compiled in static patches:
const init_patch = Patch.parse(std.heap.page_allocator,
\\ |.......|
\\ |.......|
\\ |.......|
\\ |.......|
\\ +-------+
) catch @compileError("must parse init_patch");
But this fails with a more or less reasonable:
/home/jcorbin/.local/zig-linux-x86_64-0.11.0-dev.829+68d2f68ed/lib/std/mem/Allocator.zig:128:54: error: unable to evaluate comptime expression
return self.allocAdvancedWithRetAddr(T, null, n, @returnAddress());
^~~~~~~~~~~~~~~~
day17/main.zig:322:39: note: called from here
var data = try allocator.alloc(Cell, s.len);
~~~~~~~~~~~~~~~^~~~~~~~~~~~~
day17/main.zig:354:35: note: called from here
const init_patch = Patch.parse(std.heap.page_allocator,
~~~~~~~~~~~^
day17/main.zig:297:34: error: member function expected 2 argument(s), found 1
.data = try allocator.dupe(self.data),
~~~~~~~~~^~~~~
/home/jcorbin/.local/zig-linux-x86_64-0.11.0-dev.829+68d2f68ed/lib/std/mem/Allocator.zig:307:5: note: function declared here
pub fn dupe(allocator: Allocator, comptime T: type, m: []const T) ![]T {
Which makes sense, since I had misgivings in the first place when choosinng "Heap Allocator" for something at module level.
So is there an allocator that I can use here? Maybe this will work:
var static_slab = [_]u8{8} ** (64 * 1024); // should be enough ;-)
var static_buffer = std.heap.FixedBufferAllocator.init(&static_slab);
// then we pass static_buffer.allocator() later
But maybe there's already some sort of copiler assisted static allocator?