| ... | @@ -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, |
| 14 | | 15 | |
| 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 | } |
| 51 | | 56 | |
| 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 | }; |