aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2025-02-08 04:54:38 -0800
committerGitHub <noreply@github.com>2025-02-08 04:54:38 -0800
commitea1ce2df9b20b5c91278eb0ed99a9cd0b0949e1a (patch)
treea3456166f71ca3a8dc56c99da9452ef3ffb4aa25 /src
parent3fe981e1ad746f9e3dfa2006fc69c907c92ddce6 (diff)
parent975cd9fc4ff8c12ae1f54e470b72be04d26e0837 (diff)
downloadzig-ea1ce2df9b20b5c91278eb0ed99a9cd0b0949e1a.tar.gz
zig-ea1ce2df9b20b5c91278eb0ed99a9cd0b0949e1a.zip
Merge pull request #22808 from ziglang/fast-gpa
introduce std.heap.SmpAllocator
Diffstat (limited to 'src')
-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();