Skip to content

Commit 50af613

Browse files
committed
feat: enhance memory allocation strategy with conditional debug allocator
1 parent f3975ce commit 50af613

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

cmd/fun/main.zig

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
const std = @import("std");
22
const heap = std.heap;
3+
const builtin = @import("builtin");
34
const token = @import("lexer").token;
45
const codegen = @import("codegen");
56
const lexer = @import("lexer");
67
const parser = @import("parser");
78
const utils = @import("utils");
89
const cli = @import("cli");
910

11+
var debug_allocator: heap.DebugAllocator(.{}) = .init;
12+
1013
fn print_error_and_exit(err: anyerror) noreturn {
1114
const stderr = std.io.getStdErr().writer();
1215

@@ -38,9 +41,17 @@ fn print_error_and_exit(err: anyerror) noreturn {
3841
}
3942

4043
pub fn main() void {
41-
var gpa = heap.DebugAllocator(.{ .thread_safe = true, .safety = true }){};
42-
defer _ = gpa.deinit();
43-
var arena = heap.ArenaAllocator.init(gpa.allocator());
44+
const gpa, const is_debug = blk: {
45+
if (builtin.target.os.tag == .wasi) break :blk .{ heap.wasm_allocator, false };
46+
break :blk switch (builtin.mode) {
47+
.Debug, .ReleaseSafe => .{ debug_allocator.allocator(), true },
48+
.ReleaseFast, .ReleaseSmall => .{ heap.smp_allocator, false },
49+
};
50+
};
51+
defer if (is_debug) {
52+
_ = debug_allocator.deinit();
53+
};
54+
var arena = heap.ArenaAllocator.init(gpa);
4455
defer arena.deinit();
4556
const global_allocator = arena.allocator();
4657

0 commit comments

Comments
 (0)