diff options
| author | Jonathan Marler <johnnymarler@gmail.com> | 2020-04-17 14:15:36 -0600 |
|---|---|---|
| committer | Jonathan Marler <johnnymarler@gmail.com> | 2020-06-26 13:34:48 -0600 |
| commit | dc9648f868ed8ad08f040753767c03976bbcf3b7 (patch) | |
| tree | 56025ae541deb21028ae9d96124bad9226987896 /lib/std/testing/leak_count_allocator.zig | |
| parent | 129a4fb251f8eab22eacf219fbf81006baec3251 (diff) | |
| download | zig-dc9648f868ed8ad08f040753767c03976bbcf3b7.tar.gz zig-dc9648f868ed8ad08f040753767c03976bbcf3b7.zip | |
new allocator interface
Diffstat (limited to 'lib/std/testing/leak_count_allocator.zig')
| -rw-r--r-- | lib/std/testing/leak_count_allocator.zig | 21 |
1 files changed, 11 insertions, 10 deletions
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 { |
