aboutsummaryrefslogtreecommitdiff
path: root/lib/std/testing
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/testing')
-rw-r--r--lib/std/testing/failing_allocator.zig41
-rw-r--r--lib/std/testing/leak_count_allocator.zig21
2 files changed, 29 insertions, 33 deletions
diff --git a/lib/std/testing/failing_allocator.zig b/lib/std/testing/failing_allocator.zig
index 081a29cd97..ade3e9d85a 100644
--- a/lib/std/testing/failing_allocator.zig
+++ b/lib/std/testing/failing_allocator.zig
@@ -39,43 +39,38 @@ pub const FailingAllocator = struct {
.allocations = 0,
.deallocations = 0,
.allocator = mem.Allocator{
- .reallocFn = realloc,
- .shrinkFn = shrink,
+ .allocFn = alloc,
+ .resizeFn = resize,
},
};
}
- fn realloc(allocator: *mem.Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 {
+ fn alloc(allocator: *std.mem.Allocator, len: usize, ptr_align: u29, len_align: u29) error{OutOfMemory}![]u8 {
const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
if (self.index == self.fail_index) {
return error.OutOfMemory;
}
- const result = try self.internal_allocator.reallocFn(
- self.internal_allocator,
- old_mem,
- old_align,
- new_size,
- new_align,
- );
- if (new_size < old_mem.len) {
- self.freed_bytes += old_mem.len - new_size;
- 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;
- }
+ const result = try self.internal_allocator.callAllocFn(len, ptr_align, len_align);
+ self.allocated_bytes += result.len;
+ 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 {
+ fn resize(allocator: *std.mem.Allocator, old_mem: []u8, new_len: usize, len_align: u29) error{OutOfMemory}!usize {
const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
- 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)
+ const r = self.internal_allocator.callResizeFn(old_mem, new_len, len_align) catch |e| {
+ std.debug.assert(new_len > old_mem.len);
+ return e;
+ };
+ if (new_len == 0) {
self.deallocations += 1;
+ self.freed_bytes += old_mem.len;
+ } else if (r < old_mem.len) {
+ self.freed_bytes += old_mem.len - r;
+ } else {
+ self.allocated_bytes += r - old_mem.len;
+ }
return r;
}
};
diff --git a/lib/std/testing/leak_count_allocator.zig b/lib/std/testing/leak_count_allocator.zig
index 65244e529b..87564aeea7 100644
--- a/lib/std/testing/leak_count_allocator.zig
+++ b/lib/std/testing/leak_count_allocator.zig
@@ -14,23 +14,21 @@ pub const LeakCountAllocator = struct {
return .{
.count = 0,
.allocator = .{
- .reallocFn = realloc,
- .shrinkFn = shrink,
+ .allocFn = alloc,
+ .resizeFn = resize,
},
.internal_allocator = allocator,
};
}
- fn realloc(allocator: *std.mem.Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 {
+ fn alloc(allocator: *std.mem.Allocator, len: usize, ptr_align: u29, len_align: u29) error{OutOfMemory}![]u8 {
const self = @fieldParentPtr(LeakCountAllocator, "allocator", allocator);
- var data = try self.internal_allocator.reallocFn(self.internal_allocator, old_mem, old_align, new_size, new_align);
- if (old_mem.len == 0) {
- self.count += 1;
- }
- return data;
+ const ptr = try self.internal_allocator.callAllocFn(len, ptr_align, len_align);
+ self.count += 1;
+ return ptr;
}
- fn shrink(allocator: *std.mem.Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {
+ fn resize(allocator: *std.mem.Allocator, old_mem: []u8, new_size: usize, len_align: u29) error{OutOfMemory}!usize {
const self = @fieldParentPtr(LeakCountAllocator, "allocator", allocator);
if (new_size == 0) {
if (self.count == 0) {
@@ -38,7 +36,10 @@ pub const LeakCountAllocator = struct {
}
self.count -= 1;
}
- return self.internal_allocator.shrinkFn(self.internal_allocator, old_mem, old_align, new_size, new_align);
+ return self.internal_allocator.callResizeFn(old_mem, new_size, len_align) catch |e| {
+ std.debug.assert(new_size > old_mem.len);
+ return e;
+ };
}
pub fn validate(self: LeakCountAllocator) !void {