diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-06-27 18:21:00 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-27 18:21:00 -0400 |
| commit | 0cfe8e5d6ff06eed0cde6aed0c009a58ceffc395 (patch) | |
| tree | d0dd3f43e534528d5c99ae28506c846a7d9063d0 /lib/std/testing/leak_count_allocator.zig | |
| parent | 626b5eccab7264e579ce58f56be5fbc3aa42efc4 (diff) | |
| parent | a728436992415d1bce44b0c63938f6443a4e9a11 (diff) | |
| download | zig-0cfe8e5d6ff06eed0cde6aed0c009a58ceffc395.tar.gz zig-0cfe8e5d6ff06eed0cde6aed0c009a58ceffc395.zip | |
Merge pull request #5064 from marler8997/newAllocator
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 { |
