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 {...@@ -121,44 +121,54 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
121 }121 }
122122
123 pub fn allocator(self: *Self) std.mem.Allocator {123 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 };
125 }132 }
126133
127 fn allocFn(self: *Self, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) std.mem.Allocator.Error![]u8 {134 fn allocFn(ptr: *anyopaque, len: usize, ptr_align: u8, ret_addr: usize) ?[*]u8 {
128 const result = self.parent_allocator.rawAlloc(len, ptr_align, len_align, ret_addr);135 const self = @ptrCast(*Self, @alignCast(@alignOf(Self), ptr));
136 const result = self.parent_allocator.rawAlloc(len, ptr_align, ret_addr);
129 if (result) |data| {137 if (result) |data| {
130 if (data.len != 0) {138 if (len != 0) {
131 if (name) |n| {139 if (name) |n| {
132 allocNamed(data.ptr, data.len, n);140 allocNamed(data, len, n);
133 } else {141 } else {
134 alloc(data.ptr, data.len);142 alloc(data, len);
135 }143 }
136 }144 }
137 } else |_| {145 } else {
138 messageColor("allocation failed", 0xFF0000);146 messageColor("allocation failed", 0xFF0000);
139 }147 }
140 return result;148 return result;
141 }149 }
142150
143 fn resizeFn(self: *Self, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) ?usize {151 fn resizeFn(ptr: *anyopaque, buf: []u8, buf_align: u8, new_len: usize, ret_addr: usize) bool {
144 if (self.parent_allocator.rawResize(buf, buf_align, new_len, len_align, ret_addr)) |resized_len| {152 const self = @ptrCast(*Self, @alignCast(@alignOf(Self), ptr));
153 if (self.parent_allocator.rawResize(buf, buf_align, new_len, ret_addr)) {
145 if (name) |n| {154 if (name) |n| {
146 freeNamed(buf.ptr, n);155 freeNamed(buf.ptr, n);
147 allocNamed(buf.ptr, resized_len, n);156 allocNamed(buf.ptr, new_len, n);
148 } else {157 } else {
149 free(buf.ptr);158 free(buf.ptr);
150 alloc(buf.ptr, resized_len);159 alloc(buf.ptr, new_len);
151 }160 }
152161
153 return resized_len;162 return true;
154 }163 }
155164
156 // during normal operation the compiler hits this case thousands of times due to this165 // during normal operation the compiler hits this case thousands of times due to this
157 // emitting messages for it is both slow and causes clutter166 // emitting messages for it is both slow and causes clutter
158 return null;167 return false;
159 }168 }
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));
162 self.parent_allocator.rawFree(buf, buf_align, ret_addr);172 self.parent_allocator.rawFree(buf, buf_align, ret_addr);
163 // this condition is to handle free being called on an empty slice that was never even allocated173 // this condition is to handle free being called on an empty slice that was never even allocated
164 // example case: `std.process.getSelfExeSharedLibPaths` can return `&[_][:0]u8{}`174 // example case: `std.process.getSelfExeSharedLibPaths` can return `&[_][:0]u8{}`