std: the failing allocator didn't actually count allocations

Add a field '.allocations' to actually track the number of allocations.

Additionally, only increment '.deallocations' when memory is freed
master
daurnimator 2019-05-05 23:17:23 +10:00 committed by Andrew Kelley
parent 6756e545f4
commit 3d93c89fc5
2 changed files with 14 additions and 6 deletions

View File

@ -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 {
self.allocated_bytes += new_size - old_mem.len;
}
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.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;
}
};

View File

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