aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2025-02-06 17:41:50 -0800
committerAndrew Kelley <andrew@ziglang.org>2025-02-07 14:41:49 -0800
commit7360be19a461175d1a50dfb1d7c086bcc650b3c1 (patch)
treee2c3d5c04b48c4c14597e9ffc1cd4b6152067756 /src/main.zig
parent84bf7a6701d5f51a8307736d310d4049c9158921 (diff)
downloadzig-7360be19a461175d1a50dfb1d7c086bcc650b3c1.tar.gz
zig-7360be19a461175d1a50dfb1d7c086bcc650b3c1.zip
compiler: use std.heap.smp_allocator
In main, now this allocator is chosen by default when compiling without libc in ReleaseFast or ReleaseSmall, and not targeting WebAssembly.
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig35
1 files changed, 18 insertions, 17 deletions
diff --git a/src/main.zig b/src/main.zig
index a00261cc30..401f6b2296 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -171,30 +171,31 @@ pub fn log(
std.debug.print(prefix1 ++ prefix2 ++ format ++ "\n", args);
}
-var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{
+var debug_allocator: std.heap.DebugAllocator(.{
.stack_trace_frames = build_options.mem_leak_frames,
-}){};
+}) = .init;
pub fn main() anyerror!void {
crash_report.initialize();
- const use_gpa = (build_options.force_gpa or !builtin.link_libc) and native_os != .wasi;
- const gpa = gpa: {
- if (native_os == .wasi) {
- break :gpa std.heap.wasm_allocator;
- }
- if (use_gpa) {
- break :gpa general_purpose_allocator.allocator();
- }
- // We would prefer to use raw libc allocator here, but cannot
- // use it if it won't support the alignment we need.
- if (@alignOf(std.c.max_align_t) < @max(@alignOf(i128), std.atomic.cache_line)) {
- break :gpa std.heap.c_allocator;
+ const gpa, const is_debug = gpa: {
+ if (build_options.debug_gpa) break :gpa .{ debug_allocator.allocator(), true };
+ if (native_os == .wasi) break :gpa .{ std.heap.wasm_allocator, false };
+ if (builtin.link_libc) {
+ // We would prefer to use raw libc allocator here, but cannot use
+ // it if it won't support the alignment we need.
+ if (@alignOf(std.c.max_align_t) < @max(@alignOf(i128), std.atomic.cache_line)) {
+ break :gpa .{ std.heap.c_allocator, false };
+ }
+ break :gpa .{ std.heap.raw_c_allocator, false };
}
- break :gpa std.heap.raw_c_allocator;
+ break :gpa switch (builtin.mode) {
+ .Debug, .ReleaseSafe => .{ debug_allocator.allocator(), true },
+ .ReleaseFast, .ReleaseSmall => .{ std.heap.smp_allocator, false },
+ };
};
- defer if (use_gpa) {
- _ = general_purpose_allocator.deinit();
+ defer if (is_debug) {
+ _ = debug_allocator.deinit();
};
var arena_instance = std.heap.ArenaAllocator.init(gpa);
defer arena_instance.deinit();