diff --git a/std/debug/failing_allocator.zig b/std/debug/failing_allocator.zig index 7a89460513a3f69006e5096573b9863882a5d4a7..5776d23194cbfc6545f9816fbdb746198dc2a165 100644 --- a/std/debug/failing_allocator.zig +++ b/std/debug/failing_allocator.zig @@ -10,6 +10,7 @@ pub const FailingAllocator = struct { internal_allocator: *mem.Allocator, allocated_bytes: usize, freed_bytes: usize, + allocations: usize, deallocations: usize, pub fn init(allocator: *mem.Allocator, fail_index: usize) FailingAllocator { @@ -19,6 +20,7 @@ pub const FailingAllocator = struct { .index = 0, .allocated_bytes = 0, .freed_bytes = 0, + .allocations = 0, .deallocations = 0, .allocator = mem.Allocator{ .reallocFn = realloc, @@ -39,19 +41,25 @@ pub const FailingAllocator = struct { new_size, new_align, ); - if (new_size <= old_mem.len) { + if (new_size < old_mem.len) { self.freed_bytes += old_mem.len - new_size; - } else { + if (new_size == 0) + self.deallocations += 1; + } else if (new_size > old_mem.len) { self.allocated_bytes += new_size - old_mem.len; + if (old_mem.len == 0) + self.allocations += 1; } - self.deallocations += 1; self.index += 1; return result; } fn shrink(allocator: *mem.Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 { const self = @fieldParentPtr(FailingAllocator, "allocator", allocator); - self.freed_bytes += old_mem.len - new_size; - return self.internal_allocator.shrinkFn(self.internal_allocator, old_mem, old_align, new_size, new_align); + const r = self.internal_allocator.shrinkFn(self.internal_allocator, old_mem, old_align, new_size, new_align); + self.freed_bytes += old_mem.len - r.len; + if (new_size == 0) + self.deallocations += 1; + return r; } }; diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig index f2e80a9257e2c8863e23aa61a43f0faaa12571aa..6977e4e6b1683d656641bf990144c08689b6311c 100644 --- a/std/zig/parser_test.zig +++ b/std/zig/parser_test.zig @@ -2215,7 +2215,7 @@ fn testTransform(source: []const u8, expected_source: []const u8) !void { needed_alloc_count, failing_allocator.allocated_bytes, failing_allocator.freed_bytes, - failing_allocator.index, + failing_allocator.allocations, failing_allocator.deallocations, ); return error.MemoryLeakDetected;