Skip to content

Commit ea2e63c

Browse files
memory: use a GPA in Debug mode and an Arena (page based) on Release
Signed-off-by: Francis Bouvier <[email protected]>
1 parent 57918e9 commit ea2e63c

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/main.zig

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,27 @@ fn printUsageExit(execname: []const u8, res: u8) void {
158158
pub fn main() !void {
159159

160160
// allocator
161-
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
162-
defer arena.deinit();
163-
const alloc = arena.allocator();
161+
// - in Debug mode we use the General Purpose Allocator to detect memory leaks
162+
// - in Release mode we use the page allocator
163+
var alloc: std.mem.Allocator = undefined;
164+
var _gpa: ?std.heap.GeneralPurposeAllocator(.{}) = null;
165+
if (builtin.mode == .Debug) {
166+
_gpa = std.heap.GeneralPurposeAllocator(.{}){};
167+
alloc = _gpa.?.allocator();
168+
} else {
169+
alloc = std.heap.page_allocator;
170+
}
171+
defer {
172+
if (_gpa) |*gpa| {
173+
switch (gpa.deinit()) {
174+
.ok => std.debug.print("No memory leaks\n", .{}),
175+
.leak => @panic("Memory leak"),
176+
}
177+
}
178+
}
164179

165180
// args
166-
var args = try std.process.argsWithAllocator(arena.allocator());
181+
var args = try std.process.argsWithAllocator(alloc);
167182
defer args.deinit();
168183

169184
const execname = args.next().?;
@@ -263,7 +278,7 @@ pub fn main() !void {
263278
std.log.info("Listening on: {s}:{d}...", .{ host, port });
264279

265280
// loop
266-
var loop = try jsruntime.Loop.init(arena.allocator());
281+
var loop = try jsruntime.Loop.init(alloc);
267282
defer loop.deinit();
268283

269284
// listen
@@ -279,7 +294,7 @@ pub fn main() !void {
279294
const vm = jsruntime.VM.init();
280295
defer vm.deinit();
281296

282-
var loop = try jsruntime.Loop.init(arena.allocator());
297+
var loop = try jsruntime.Loop.init(alloc);
283298
defer loop.deinit();
284299

285300
var browser = Browser{};

0 commit comments

Comments
 (0)