From bfd051a53c9d1db7a7f8743cd981646a6ac37114 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 19 May 2021 10:26:50 -0700 Subject: [PATCH] stage2: use c_allocator not raw_c_allocator when the raw C allocator alignment is not sufficient. closes #8835 --- src/main.zig | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main.zig b/src/main.zig index 5b0b62a886..b388329508 100644 --- a/src/main.zig +++ b/src/main.zig @@ -126,11 +126,20 @@ var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{ }){}; pub fn main() anyerror!void { - const gpa = if (std.builtin.link_libc) - std.heap.raw_c_allocator - else - &general_purpose_allocator.allocator; - defer if (!std.builtin.link_libc) { + var gpa_need_deinit = false; + const gpa = gpa: { + if (!std.builtin.link_libc) { + gpa_need_deinit = true; + 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) < @alignOf(i128)) { + break :gpa std.heap.c_allocator; + } + break :gpa std.heap.raw_c_allocator; + }; + defer if (gpa_need_deinit) { _ = general_purpose_allocator.deinit(); }; var arena_instance = std.heap.ArenaAllocator.init(gpa);