From a5c93975394af6638e04f6a8a8c4f060bc4887d1 Mon Sep 17 00:00:00 2001 From: Layne Gustafson Date: Wed, 15 Jan 2020 18:12:32 -0500 Subject: [PATCH] No allocations for n.t. empty strings --- src-self-hosted/stage1.zig | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src-self-hosted/stage1.zig b/src-self-hosted/stage1.zig index 1d08616ae2..db6d281fea 100644 --- a/src-self-hosted/stage1.zig +++ b/src-self-hosted/stage1.zig @@ -617,6 +617,8 @@ fn printCpusForArch(arch_name: []const u8, show_dependencies: bool) !void { } } +const null_terminated_empty_string = (&[_]u8 { 0 })[0..0 :0]; + fn toNullTerminatedStringAlloc(allocator: *std.mem.Allocator, str: []const u8) ![:0]const u8 { var buffer = try std.Buffer.init(allocator, str); @@ -653,7 +655,7 @@ const Stage2TargetDetails = struct { .cpu = cpu, }, .llvm_cpu_str = try toNullTerminatedStringAlloc(allocator, cpu.name), - .llvm_features_str = try toNullTerminatedStringAlloc(allocator, ""), + .llvm_features_str = null_terminated_empty_string, .builtin_str = builtin_str_buffer.toOwnedSlice(), }; } @@ -697,7 +699,7 @@ const Stage2TargetDetails = struct { .target_details = std.target.TargetDetails{ .features = features, }, - .llvm_cpu_str = try toNullTerminatedStringAlloc(allocator, ""), + .llvm_cpu_str = null_terminated_empty_string, .llvm_features_str = llvm_features_buffer.toOwnedSlice()[0..llvm_features_buffer_len :0], .builtin_str = builtin_str_buffer.toOwnedSlice(), }; @@ -801,7 +803,7 @@ export fn stage2_target_details_get_cache_str(target_details: ?*const Stage2Targ }); } - return @as([*:0]const u8, ""); + return @as([*:0]const u8, null_terminated_empty_string); } // ABI warning @@ -810,7 +812,7 @@ export fn stage2_target_details_get_llvm_cpu(target_details: ?*const Stage2Targe return @as([*:0]const u8, td.llvm_cpu_str); } - return @as([*:0]const u8, ""); + return @as([*:0]const u8, null_terminated_empty_string); } // ABI warning @@ -819,7 +821,7 @@ export fn stage2_target_details_get_llvm_features(target_details: ?*const Stage2 return @as([*:0]const u8, td.llvm_features_str); } - return @as([*:0]const u8, ""); + return @as([*:0]const u8, null_terminated_empty_string); } // ABI warning @@ -828,7 +830,7 @@ export fn stage2_target_details_get_builtin_str(target_details: ?*const Stage2Ta return @as([*:0]const u8, td.builtin_str); } - return @as([*:0]const u8, ""); + return @as([*:0]const u8, null_terminated_empty_string); } const riscv_default_features: []*const std.target.Feature = &[_]*const std.target.Feature {