diff options
| author | hryx <codroid@gmail.com> | 2019-05-12 02:00:49 -0700 |
|---|---|---|
| committer | hryx <codroid@gmail.com> | 2019-05-12 02:00:49 -0700 |
| commit | 3787f3428625e830fd852a8f5a40c7d8a2d429f6 (patch) | |
| tree | 23fb493b9d2f07c7abe57955874682959936319a /std/debug/failing_allocator.zig | |
| parent | 16aee1f58a80295f7599a8290d764a5c7040c373 (diff) | |
| parent | edcc7c72d1a684a8a16ca23ad26689f2cce4e803 (diff) | |
| download | zig-3787f3428625e830fd852a8f5a40c7d8a2d429f6.tar.gz zig-3787f3428625e830fd852a8f5a40c7d8a2d429f6.zip | |
Merge branch 'master' into rebased
Diffstat (limited to 'std/debug/failing_allocator.zig')
| -rw-r--r-- | std/debug/failing_allocator.zig | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/std/debug/failing_allocator.zig b/std/debug/failing_allocator.zig index 7a89460513..5776d23194 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; } }; |
