Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -693,3 +693,8 @@ test "abc" {
refAllDeclsRecursiveExceptCImports(@This());
_ = @import("zon.zig");
}

test "ThreadLocals in test" {
initThreadLocals();
deinitThreadLocals();
}
13 changes: 8 additions & 5 deletions src/utils/heap.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const builtin = @import("builtin");
const std = @import("std");
const Allocator = std.mem.Allocator;

Expand All @@ -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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion this should be done at the declaration of the interface. Instead I'd propose to have a globally accessible testing allocator that is std.testing.allocator wrapped in the error handling allocator.

pub var globalArenaAllocator: ThreadSafeAllocator(NeverFailingArenaAllocator) = .init(.init(handledGpa.allocator()));
pub var worldArenaAllocator: ThreadSafeAllocator(NeverFailingArenaAllocator) = undefined;
var worldArenaOpenCount: usize = 0;
Expand All @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function shouldn't be called during testing.

if(globalGpa.deinit() == .leak) {
std.log.err("Memory leak", .{});
}
globalGpa = undefined;
}
globalGpa = undefined;
}

pub fn createWorldArena() void {
Expand Down