zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 2682b41da54e9b652dc570140d52f85418d6b89d (tree)
parent dfeffcfbf825c29d89ccec281ab95dd2383317ac
Author: Veikka Tuominen <git@vexu.eu>
Date:   Mon, 28 Feb 2022 10:20:29 +0200

make gpa.deinit work with stage2

Diffstat:
Mlib/std/hash_map.zig | 4++--
Mlib/std/heap/general_purpose_allocator.zig | 25+++++++++++++++++++------
Mlib/std/special/test_runner.zig | 5+++--
3 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/lib/std/hash_map.zig b/lib/std/hash_map.zig @@ -738,7 +738,7 @@ pub fn HashMapUnmanaged( value: V, }; - const Header = packed struct { + const Header = struct { values: [*]V, keys: [*]K, capacity: Size, @@ -932,7 +932,7 @@ pub fn HashMapUnmanaged( } fn header(self: *const Self) *Header { - return @ptrCast(*Header, @ptrCast([*]Header, self.metadata.?) - 1); + return @ptrCast(*Header, @ptrCast([*]Header, @alignCast(@alignOf(Header), self.metadata.?)) - 1); } fn keys(self: *const Self) [*]K { diff --git a/lib/std/heap/general_purpose_allocator.zig b/lib/std/heap/general_purpose_allocator.zig @@ -341,9 +341,15 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type { const slot_index = @intCast(SlotIndex, used_bits_byte * 8 + bit_index); const stack_trace = bucketStackTrace(bucket, size_class, slot_index, .alloc); const addr = bucket.page + slot_index * size_class; - log.err("memory address 0x{x} leaked: {s}", .{ - @ptrToInt(addr), stack_trace, - }); + if (builtin.zig_backend == .stage1) { + log.err("memory address 0x{x} leaked: {s}", .{ + @ptrToInt(addr), stack_trace, + }); + } else { // TODO + log.err("memory address 0x{x} leaked", .{ + @ptrToInt(addr), + }); + } leaks = true; } if (bit_index == math.maxInt(u3)) @@ -372,9 +378,16 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type { var it = self.large_allocations.valueIterator(); while (it.next()) |large_alloc| { if (config.retain_metadata and large_alloc.freed) continue; - log.err("memory address 0x{x} leaked: {s}", .{ - @ptrToInt(large_alloc.bytes.ptr), large_alloc.getStackTrace(.alloc), - }); + const stack_trace = large_alloc.getStackTrace(.alloc); + if (builtin.zig_backend == .stage1) { + log.err("memory address 0x{x} leaked: {s}", .{ + @ptrToInt(large_alloc.bytes.ptr), stack_trace, + }); + } else { // TODO + log.err("memory address 0x{x} leaked", .{ + @ptrToInt(large_alloc.bytes.ptr), + }); + } leaks = true; } return leaks; diff --git a/lib/std/special/test_runner.zig b/lib/std/special/test_runner.zig @@ -46,9 +46,10 @@ pub fn main() void { var leaks: usize = 0; for (test_fn_list) |test_fn, i| { - if (builtin.zig_backend != .stage2_llvm) std.testing.allocator_instance = .{}; + const gpa_works = builtin.zig_backend == .stage1 or builtin.os.tag != .macos; + if (gpa_works) std.testing.allocator_instance = .{}; defer { - if (builtin.zig_backend != .stage2_llvm and std.testing.allocator_instance.deinit()) { + if (gpa_works and std.testing.allocator_instance.deinit()) { leaks += 1; } }