From be20f97eb97d4712bfd9be85ed2e41acbb7718de Mon Sep 17 00:00:00 2001 From: Silver Date: Fri, 4 Jul 2025 15:02:50 +0100 Subject: [PATCH] Update TracyAllocator to new allocator API --- src/tracy.zig | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/src/tracy.zig b/src/tracy.zig index e473088922..50d1b2e1a7 100644 --- a/src/tracy.zig +++ b/src/tracy.zig @@ -120,20 +120,21 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type { .vtable = &.{ .alloc = allocFn, .resize = resizeFn, + .remap = remapFn, .free = freeFn, }, }; } - fn allocFn(ptr: *anyopaque, len: usize, ptr_align: u8, ret_addr: usize) ?[*]u8 { + fn allocFn(ptr: *anyopaque, len: usize, alignment: std.mem.Alignment, ret_addr: usize) ?[*]u8 { const self: *Self = @ptrCast(@alignCast(ptr)); - const result = self.parent_allocator.rawAlloc(len, ptr_align, ret_addr); - if (result) |data| { + const result = self.parent_allocator.rawAlloc(len, alignment, ret_addr); + if (result) |memory| { if (len != 0) { if (name) |n| { - allocNamed(data, len, n); + allocNamed(memory, len, n); } else { - alloc(data, len); + alloc(memory, len); } } } else { @@ -142,15 +143,15 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type { return result; } - fn resizeFn(ptr: *anyopaque, buf: []u8, buf_align: u8, new_len: usize, ret_addr: usize) bool { + fn resizeFn(ptr: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize) bool { const self: *Self = @ptrCast(@alignCast(ptr)); - if (self.parent_allocator.rawResize(buf, buf_align, new_len, ret_addr)) { + if (self.parent_allocator.rawResize(memory, alignment, new_len, ret_addr)) { if (name) |n| { - freeNamed(buf.ptr, n); - allocNamed(buf.ptr, new_len, n); + freeNamed(memory.ptr, n); + allocNamed(memory.ptr, new_len, n); } else { - free(buf.ptr); - alloc(buf.ptr, new_len); + free(memory.ptr); + alloc(memory.ptr, new_len); } return true; @@ -161,16 +162,33 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type { return false; } - fn freeFn(ptr: *anyopaque, buf: []u8, buf_align: u8, ret_addr: usize) void { + fn remapFn(ptr: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize) ?[*]u8 { const self: *Self = @ptrCast(@alignCast(ptr)); - self.parent_allocator.rawFree(buf, buf_align, ret_addr); + if (self.parent_allocator.rawRemap(memory, alignment, new_len, ret_addr)) |new_memory| { + if (name) |n| { + freeNamed(memory.ptr, n); + allocNamed(new_memory, new_len, n); + } else { + free(memory.ptr); + alloc(new_memory, new_len); + } + return new_memory; + } else { + messageColor("reallocation failed", 0xFF0000); + return null; + } + } + + fn freeFn(ptr: *anyopaque, memory: []u8, alignment: std.mem.Alignment, ret_addr: usize) void { + const self: *Self = @ptrCast(@alignCast(ptr)); + self.parent_allocator.rawFree(memory, alignment, ret_addr); // this condition is to handle free being called on an empty slice that was never even allocated // example case: `std.process.getSelfExeSharedLibPaths` can return `&[_][:0]u8{}` - if (buf.len != 0) { + if (memory.len != 0) { if (name) |n| { - freeNamed(buf.ptr, n); + freeNamed(memory.ptr, n); } else { - free(buf.ptr); + free(memory.ptr); } } }