From b9a6febd71cf04ec90db2a0aa059743738fd98a5 Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Thu, 3 Apr 2025 21:16:43 -0400 Subject: Move dba to top level scope This makes it so references to it are lazily compiled, so I can throw a compileError if it is referenced in the wrong mode. --- src/main.zig | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main.zig b/src/main.zig index 9a0b8a4..dd6abd7 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,6 +1,11 @@ const is_debug = builtin.mode == .Debug; const base64Enc = std.base64.Base64Encoder.init(std.base64.standard_alphabet_chars, '='); const base64Dec = std.base64.Base64Decoder.init(std.base64.standard_alphabet_chars, '='); +var dba: DebugAllocator = + if (is_debug) + DebugAllocator.init + else + @compileError("Should not use debug allocator in release mode"); /// Type tag for SaprusMessage union. /// This is the first value in the actual packet sent over the network. @@ -148,12 +153,11 @@ const SaprusMessage = union(SaprusPacketType) { }; pub fn main() !void { - var dba: ?DebugAllocator = if (comptime is_debug) DebugAllocator.init else null; - defer if (dba) |*d| { - _ = d.deinit(); + defer if (is_debug) { + _ = dba.deinit(); }; - var gpa = if (dba) |*d| d.allocator() else std.heap.smp_allocator; + var gpa = if (is_debug) dba.allocator() else std.heap.smp_allocator; const msg = SaprusMessage{ .relay = .{ -- cgit