Skip to content

add std.heap.AutoAllocator #23432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 37 additions & 1 deletion lib/std/heap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,30 @@ pub fn StackFallbackAllocator(comptime size: usize) type {
};
}

pub const DefaultAllocator = struct {
var debug_allocator: DebugAllocator(.{}) = .init;

pub const allocator = impl.gpa;
const impl: struct {
gpa: std.mem.Allocator,
debug: bool,
} = if (builtin.os.tag == .wasi)
.{ .gpa = wasm_allocator, .debug = false }
else if (builtin.link_libc)
.{ .gpa = c_allocator, .debug = false }
else switch (builtin.mode) {
.Debug => .{ .gpa = debug_allocator.allocator(), .debug = true },
.ReleaseSafe, .ReleaseFast, .ReleaseSmall => if (builtin.single_threaded)
.{ .gpa = debug_allocator.allocator(), .debug = true }
else
.{ .gpa = smp_allocator, .debug = false },
};

pub fn deinit() Check {
return if (impl.debug) debug_allocator.deinit() else .ok;
}
};

test c_allocator {
if (builtin.link_libc) {
try testAllocator(c_allocator);
Expand Down Expand Up @@ -524,6 +548,18 @@ test PageAllocator {
}
}

test DefaultAllocator {
try testAllocator(DefaultAllocator.allocator);
try testAllocatorAligned(DefaultAllocator.allocator);
if (!builtin.target.cpu.arch.isWasm()) {
try testAllocatorLargeAlignment(DefaultAllocator.allocator);
try testAllocatorAlignedShrink(DefaultAllocator.allocator);
}
// Only ensure deinit is semantically analyzed since calling it would modify global state and
// unintentionally break other tests using DefaultAllocator.
_ = DefaultAllocator.deinit;
}

test ArenaAllocator {
var arena_allocator = ArenaAllocator.init(page_allocator);
defer arena_allocator.deinit();
Expand All @@ -535,7 +571,7 @@ test ArenaAllocator {
try testAllocatorAlignedShrink(allocator);
}

test "StackFallbackAllocator" {
test StackFallbackAllocator {
{
var stack_allocator = stackFallback(4096, std.testing.allocator);
try testAllocator(stack_allocator.get());
Expand Down
21 changes: 2 additions & 19 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -175,25 +175,8 @@ var debug_allocator: std.heap.DebugAllocator(.{
pub fn main() anyerror!void {
crash_report.initialize();

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 switch (builtin.mode) {
.Debug, .ReleaseSafe => .{ debug_allocator.allocator(), true },
.ReleaseFast, .ReleaseSmall => .{ std.heap.smp_allocator, false },
};
};
defer if (is_debug) {
_ = debug_allocator.deinit();
};
const gpa = std.heap.DefaultAllocator.allocator;
defer _ = std.heap.DefaultAllocator.deinit();
var arena_instance = std.heap.ArenaAllocator.init(gpa);
defer arena_instance.deinit();
const arena = arena_instance.allocator();
Expand Down
Loading