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