authorgravatar for leecannon@leecannon.xyzLee Cannon <leecannon@leecannon.xyz> 2022-12-08 19:17:01+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-11 15:02:44-05:00
log4efdbd304488207490e9c686aae98053d258ebd4
treec019a4259cfa5c36640f7f62bebb7fb4fad626c9
parent15a6336bb40d413a7c7f140528268bb0397fdb41

update TracyAllocator for new Allocator changes


1 files changed, 24 insertions(+), 14 deletions(-)

src/tracy.zig+24-14
......@@ -121,44 +121,54 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
121121 }
122122
123123 pub fn allocator(self: *Self) std.mem.Allocator {
124 return std.mem.Allocator.init(self, allocFn, resizeFn, freeFn);
124 return .{
125 .ptr = self,
126 .vtable = &.{
127 .alloc = allocFn,
128 .resize = resizeFn,
129 .free = freeFn,
130 },
131 };
125132 }
126133
127 fn allocFn(self: *Self, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) std.mem.Allocator.Error![]u8 {
128 const result = self.parent_allocator.rawAlloc(len, ptr_align, len_align, ret_addr);
134 fn allocFn(ptr: *anyopaque, len: usize, ptr_align: u8, ret_addr: usize) ?[*]u8 {
135 const self = @ptrCast(*Self, @alignCast(@alignOf(Self), ptr));
136 const result = self.parent_allocator.rawAlloc(len, ptr_align, ret_addr);
129137 if (result) |data| {
130 if (data.len != 0) {
138 if (len != 0) {
131139 if (name) |n| {
132 allocNamed(data.ptr, data.len, n);
140 allocNamed(data, len, n);
133141 } else {
134 alloc(data.ptr, data.len);
142 alloc(data, len);
135143 }
136144 }
137 } else |_| {
145 } else {
138146 messageColor("allocation failed", 0xFF0000);
139147 }
140148 return result;
141149 }
142150
143 fn resizeFn(self: *Self, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) ?usize {
144 if (self.parent_allocator.rawResize(buf, buf_align, new_len, len_align, ret_addr)) |resized_len| {
151 fn resizeFn(ptr: *anyopaque, buf: []u8, buf_align: u8, new_len: usize, ret_addr: usize) bool {
152 const self = @ptrCast(*Self, @alignCast(@alignOf(Self), ptr));
153 if (self.parent_allocator.rawResize(buf, buf_align, new_len, ret_addr)) {
145154 if (name) |n| {
146155 freeNamed(buf.ptr, n);
147 allocNamed(buf.ptr, resized_len, n);
156 allocNamed(buf.ptr, new_len, n);
148157 } else {
149158 free(buf.ptr);
150 alloc(buf.ptr, resized_len);
159 alloc(buf.ptr, new_len);
151160 }
152161
153 return resized_len;
162 return true;
154163 }
155164
156165 // during normal operation the compiler hits this case thousands of times due to this
157166 // emitting messages for it is both slow and causes clutter
158 return null;
167 return false;
159168 }
160169
161 fn freeFn(self: *Self, buf: []u8, buf_align: u29, ret_addr: usize) void {
170 fn freeFn(ptr: *anyopaque, buf: []u8, buf_align: u8, ret_addr: usize) void {
171 const self = @ptrCast(*Self, @alignCast(@alignOf(Self), ptr));
162172 self.parent_allocator.rawFree(buf, buf_align, ret_addr);
163173 // this condition is to handle free being called on an empty slice that was never even allocated
164174 // example case: `std.process.getSelfExeSharedLibPaths` can return `&[_][:0]u8{}`