diff --git a/src/main.zig b/src/main.zig index 09b0ea1e4..c801ef2d4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -693,3 +693,8 @@ test "abc" { refAllDeclsRecursiveExceptCImports(@This()); _ = @import("zon.zig"); } + +test "ThreadLocals in test" { + initThreadLocals(); + deinitThreadLocals(); +} diff --git a/src/utils/heap.zig b/src/utils/heap.zig index da4117724..8c4bdc0a6 100644 --- a/src/utils/heap.zig +++ b/src/utils/heap.zig @@ -1,3 +1,4 @@ +const builtin = @import("builtin"); const std = @import("std"); const Allocator = std.mem.Allocator; @@ -9,8 +10,8 @@ var testingErrorHandlingAllocator = ErrorHandlingAllocator.init(std.testing.allo pub const testingAllocator = testingErrorHandlingAllocator.allocator(); pub const allocators = struct { // MARK: allocators - pub var globalGpa = std.heap.GeneralPurposeAllocator(.{.thread_safe = true}){}; - pub var handledGpa = ErrorHandlingAllocator.init(globalGpa.allocator()); + pub var globalGpa: std.heap.GeneralPurposeAllocator(.{.thread_safe = true}) = if(builtin.is_test) undefined else .init; + pub var handledGpa = ErrorHandlingAllocator.init(if(builtin.is_test) std.testing.allocator else globalGpa.allocator()); pub var globalArenaAllocator: ThreadSafeAllocator(NeverFailingArenaAllocator) = .init(.init(handledGpa.allocator())); pub var worldArenaAllocator: ThreadSafeAllocator(NeverFailingArenaAllocator) = undefined; var worldArenaOpenCount: usize = 0; @@ -20,10 +21,12 @@ pub const allocators = struct { // MARK: allocators std.log.info("Clearing global arena with {} MiB", .{globalArenaAllocator.child.arena.queryCapacity() >> 20}); globalArenaAllocator.deinit(); globalArenaAllocator = undefined; - if(globalGpa.deinit() == .leak) { - std.log.err("Memory leak", .{}); + if(!builtin.is_test) { + if(globalGpa.deinit() == .leak) { + std.log.err("Memory leak", .{}); + } + globalGpa = undefined; } - globalGpa = undefined; } pub fn createWorldArena() void {