From 45fa195eab3ab745a33e63d7250090da2979d6ad Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Sun, 28 Jun 2020 01:41:53 +0200 Subject: [PATCH] Adapt ProfilingAllocator to new allocator interface --- generator/main.zig | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/generator/main.zig b/generator/main.zig index 13741c5..bb4956b 100644 --- a/generator/main.zig +++ b/generator/main.zig @@ -76,8 +76,8 @@ const ProfilingAllocator = struct { fn init(parent_allocator: *Allocator) ProfilingAllocator { return ProfilingAllocator{ .allocator = Allocator{ - .reallocFn = realloc, - .shrinkFn = shrink, + .allocFn = alloc, + .resizeFn = resize, }, .parent_allocator = parent_allocator, .max_usage = 0, @@ -85,19 +85,26 @@ const ProfilingAllocator = struct { }; } - fn realloc(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 { + fn alloc(allocator: *Allocator, len: usize, ptr_align: u29, len_align: u29) ![]u8 { const self = @fieldParentPtr(ProfilingAllocator, "allocator", allocator); - self.current_usage = self.current_usage - old_mem.len + new_size; + const buf = try self.parent_allocator.allocFn(self.parent_allocator, len, ptr_align, len_align); + self.current_usage += buf.len; if (self.current_usage > self.max_usage) { self.max_usage = self.current_usage; } - return self.parent_allocator.reallocFn(self.parent_allocator, old_mem, old_align, new_size, new_align); + return buf; } - fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 { + fn resize(allocator: *Allocator, buf: []u8, new_len: usize, len_align: u29) !usize { const self = @fieldParentPtr(ProfilingAllocator, "allocator", allocator); - return self.parent_allocator.shrinkFn(self.parent_allocator, old_mem, old_align, new_size, new_align); + const actual_new_len = try self.parent_allocator.resizeFn(self.parent_allocator, buf, new_len, len_align); + self.current_usage = self.current_usage - buf.len + actual_new_len; + if (self.current_usage > self.max_usage) { + self.max_usage = self.current_usage; + } + + return actual_new_len; } };