authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2019-05-05 23:17:23+10:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-11 09:55:41-04:00
log3d93c89fc55a428171bbfa779fb604801d84421d
tree3f01ec200b694e8facc4a27abc17998f3819f50a
parent6756e545f40dc7de373186a6581594e07bfd8200

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

2 files changed, 14 insertions(+), 6 deletions(-)

std/debug/failing_allocator.zig+13-5
...@@ -10,6 +10,7 @@ pub const FailingAllocator = struct {...@@ -10,6 +10,7 @@ pub const FailingAllocator = struct {
10 internal_allocator: *mem.Allocator,10 internal_allocator: *mem.Allocator,
11 allocated_bytes: usize,11 allocated_bytes: usize,
12 freed_bytes: usize,12 freed_bytes: usize,
13 allocations: usize,
13 deallocations: usize,14 deallocations: usize,
1415
15 pub fn init(allocator: *mem.Allocator, fail_index: usize) FailingAllocator {16 pub fn init(allocator: *mem.Allocator, fail_index: usize) FailingAllocator {
...@@ -19,6 +20,7 @@ pub const FailingAllocator = struct {...@@ -19,6 +20,7 @@ pub const FailingAllocator = struct {
19 .index = 0,20 .index = 0,
20 .allocated_bytes = 0,21 .allocated_bytes = 0,
21 .freed_bytes = 0,22 .freed_bytes = 0,
23 .allocations = 0,
22 .deallocations = 0,24 .deallocations = 0,
23 .allocator = mem.Allocator{25 .allocator = mem.Allocator{
24 .reallocFn = realloc,26 .reallocFn = realloc,
...@@ -39,19 +41,25 @@ pub const FailingAllocator = struct {...@@ -39,19 +41,25 @@ pub const FailingAllocator = struct {
39 new_size,41 new_size,
40 new_align,42 new_align,
41 );43 );
42 if (new_size <= old_mem.len) {44 if (new_size < old_mem.len) {
43 self.freed_bytes += old_mem.len - new_size;45 self.freed_bytes += old_mem.len - new_size;
44 } else {46 if (new_size == 0)
47 self.deallocations += 1;
48 } else if (new_size > old_mem.len) {
45 self.allocated_bytes += new_size - old_mem.len;49 self.allocated_bytes += new_size - old_mem.len;
50 if (old_mem.len == 0)
51 self.allocations += 1;
46 }52 }
47 self.deallocations += 1;
48 self.index += 1;53 self.index += 1;
49 return result;54 return result;
50 }55 }
5156
52 fn shrink(allocator: *mem.Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {57 fn shrink(allocator: *mem.Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {
53 const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);58 const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
54 self.freed_bytes += old_mem.len - new_size;59 const r = self.internal_allocator.shrinkFn(self.internal_allocator, old_mem, old_align, new_size, new_align);
55 return self.internal_allocator.shrinkFn(self.internal_allocator, old_mem, old_align, new_size, new_align);60 self.freed_bytes += old_mem.len - r.len;
61 if (new_size == 0)
62 self.deallocations += 1;
63 return r;
56 }64 }
57};65};
std/zig/parser_test.zig+1-1
...@@ -2215,7 +2215,7 @@ fn testTransform(source: []const u8, expected_source: []const u8) !void {...@@ -2215,7 +2215,7 @@ fn testTransform(source: []const u8, expected_source: []const u8) !void {
2215 needed_alloc_count,2215 needed_alloc_count,
2216 failing_allocator.allocated_bytes,2216 failing_allocator.allocated_bytes,
2217 failing_allocator.freed_bytes,2217 failing_allocator.freed_bytes,
2218 failing_allocator.index,2218 failing_allocator.allocations,
2219 failing_allocator.deallocations,2219 failing_allocator.deallocations,
2220 );2220 );
2221 return error.MemoryLeakDetected;2221 return error.MemoryLeakDetected;