| ... | @@ -109,11 +109,14 @@ pub fn tracyAllocator(allocator: std.mem.Allocator) TracyAllocator(null) { | ... | @@ -109,11 +109,14 @@ pub fn tracyAllocator(allocator: std.mem.Allocator) TracyAllocator(null) { |
| 109 | | 109 | |
| 110 | pub fn TracyAllocator(comptime name: ?[:0]const u8) type { | 110 | pub fn TracyAllocator(comptime name: ?[:0]const u8) type { |
| 111 | return struct { | 111 | return struct { |
| 112 | allocator: std.mem.Allocator, | | |
| 113 | parent_allocator: std.mem.Allocator, | 112 | parent_allocator: std.mem.Allocator, |
| 114 | | 113 | |
| 115 | const Self = @This(); | 114 | const Self = @This(); |
| 116 | | 115 | |
| | 116 | pub fn allocator(self: *Self) std.mem.Allocator { |
| | 117 | return std.mem.Allocator.init(self, allocFn, resizeFn); |
| | 118 | } |
| | 119 | |
| 117 | pub fn init(allocator: std.mem.Allocator) Self { | 120 | pub fn init(allocator: std.mem.Allocator) Self { |
| 118 | return .{ | 121 | return .{ |
| 119 | .parent_allocator = allocator, | 122 | .parent_allocator = allocator, |
| ... | @@ -124,8 +127,7 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type { | ... | @@ -124,8 +127,7 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type { |
| 124 | }; | 127 | }; |
| 125 | } | 128 | } |
| 126 | | 129 | |
| 127 | fn allocFn(allocator: std.mem.Allocator, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) std.mem.Allocator.Error![]u8 { | 130 | fn allocFn(self: *Self, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) std.mem.Allocator.Error![]u8 { |
| 128 | const self = @fieldParentPtr(Self, "allocator", allocator); | | |
| 129 | const result = self.parent_allocator.allocFn(self.parent_allocator, len, ptr_align, len_align, ret_addr); | 131 | const result = self.parent_allocator.allocFn(self.parent_allocator, len, ptr_align, len_align, ret_addr); |
| 130 | if (result) |data| { | 132 | if (result) |data| { |
| 131 | if (data.len != 0) { | 133 | if (data.len != 0) { |
| ... | @@ -141,9 +143,7 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type { | ... | @@ -141,9 +143,7 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type { |
| 141 | return result; | 143 | return result; |
| 142 | } | 144 | } |
| 143 | | 145 | |
| 144 | fn resizeFn(allocator: std.mem.Allocator, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) std.mem.Allocator.Error!usize { | 146 | fn resizeFn(self: *Self, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) std.mem.Allocator.Error!usize { |
| 145 | const self = @fieldParentPtr(Self, "allocator", allocator); | | |
| 146 | | | |
| 147 | if (self.parent_allocator.resizeFn(self.parent_allocator, buf, buf_align, new_len, len_align, ret_addr)) |resized_len| { | 147 | if (self.parent_allocator.resizeFn(self.parent_allocator, buf, buf_align, new_len, len_align, ret_addr)) |resized_len| { |
| 148 | // this condition is to handle free being called on an empty slice that was never even allocated | 148 | // this condition is to handle free being called on an empty slice that was never even allocated |
| 149 | // example case: `std.process.getSelfExeSharedLibPaths` can return `&[_][:0]u8{}` | 149 | // example case: `std.process.getSelfExeSharedLibPaths` can return `&[_][:0]u8{}` |