Commit bfd051a53c

Andrew Kelley <andrew@ziglang.org>
2021-05-19 19:26:50
stage2: use c_allocator not raw_c_allocator
when the raw C allocator alignment is not sufficient. closes #8835
1 parent 1273bc2
Changed files (1)
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);