make gpa.deinit work with stage2

This commit is contained in:
Veikka Tuominen 2022-02-28 10:20:29 +02:00 committed by Andrew Kelley
parent dfeffcfbf8
commit 2682b41da5
3 changed files with 24 additions and 10 deletions

View File

@ -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 {

View File

@ -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;

View File

@ -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;
}
}