| ... | ... | @@ -10,6 +10,7 @@ pub const FailingAllocator = struct { |
| 10 | 10 | internal_allocator: *mem.Allocator, |
| 11 | 11 | allocated_bytes: usize, |
| 12 | 12 | freed_bytes: usize, |
| 13 | allocations: usize, |
| 13 | 14 | deallocations: usize, |
| 14 | 15 | |
| 15 | 16 | pub fn init(allocator: *mem.Allocator, fail_index: usize) FailingAllocator { |
| ... | ... | @@ -19,6 +20,7 @@ pub const FailingAllocator = struct { |
| 19 | 20 | .index = 0, |
| 20 | 21 | .allocated_bytes = 0, |
| 21 | 22 | .freed_bytes = 0, |
| 23 | .allocations = 0, |
| 22 | 24 | .deallocations = 0, |
| 23 | 25 | .allocator = mem.Allocator{ |
| 24 | 26 | .reallocFn = realloc, |
| ... | ... | @@ -39,19 +41,25 @@ pub const FailingAllocator = struct { |
| 39 | 41 | new_size, |
| 40 | 42 | new_align, |
| 41 | 43 | ); |
| 42 | | if (new_size <= old_mem.len) { |
| 44 | if (new_size < old_mem.len) { |
| 43 | 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 | 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 | 53 | self.index += 1; |
| 49 | 54 | return result; |
| 50 | 55 | } |
| 51 | 56 | |
| 52 | 57 | fn shrink(allocator: *mem.Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 { |
| 53 | 58 | const self = @fieldParentPtr(FailingAllocator, "allocator", allocator); |
| 54 | | self.freed_bytes += old_mem.len - new_size; |
| 55 | | return self.internal_allocator.shrinkFn(self.internal_allocator, old_mem, old_align, new_size, new_align); |
| 59 | const r = 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 | }; |