Skip to content

Commit 7d1f473

Browse files
leecannonandrewrk
authored andcommitted
stage2: fix TracyAllocator bugs
1 parent a3d9cd1 commit 7d1f473

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

src/tracy.zig

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
124124
}
125125

126126
fn allocFn(self: *Self, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) std.mem.Allocator.Error![]u8 {
127-
const result = self.parent_allocator.allocFn(self.parent_allocator, len, ptr_align, len_align, ret_addr);
127+
const result = self.parent_allocator.rawAlloc(len, ptr_align, len_align, ret_addr);
128128
if (result) |data| {
129129
if (data.len != 0) {
130130
if (name) |n| {
@@ -139,22 +139,14 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
139139
return result;
140140
}
141141

142-
fn resizeFn(self: *Self, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) std.mem.Allocator.Error!usize {
143-
if (self.parent_allocator.resizeFn(self.parent_allocator, buf, buf_align, new_len, len_align, ret_addr)) |resized_len| {
144-
// this condition is to handle free being called on an empty slice that was never even allocated
145-
// example case: `std.process.getSelfExeSharedLibPaths` can return `&[_][:0]u8{}`
146-
if (buf.len != 0) {
147-
if (name) |n| {
148-
freeNamed(buf.ptr, n);
149-
} else {
150-
free(buf.ptr);
151-
}
152-
}
153-
142+
fn resizeFn(self: *Self, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) ?usize {
143+
if (self.parent_allocator.rawResize(buf, buf_align, new_len, len_align, ret_addr)) |resized_len| {
154144
if (name) |n| {
145+
freeNamed(buf.ptr, n);
155146
allocNamed(buf.ptr, resized_len, n);
156147
} else {
157148
alloc(buf.ptr, resized_len);
149+
free(buf.ptr);
158150
}
159151

160152
return resized_len;
@@ -167,10 +159,14 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
167159

168160
fn freeFn(self: *Self, buf: []u8, buf_align: u29, ret_addr: usize) void {
169161
self.parent_allocator.rawFree(buf, buf_align, ret_addr);
170-
if (name) |n| {
171-
freeNamed(buf.ptr, n);
172-
} else {
173-
free(buf.ptr);
162+
// this condition is to handle free being called on an empty slice that was never even allocated
163+
// example case: `std.process.getSelfExeSharedLibPaths` can return `&[_][:0]u8{}`
164+
if (buf.len != 0) {
165+
if (name) |n| {
166+
freeNamed(buf.ptr, n);
167+
} else {
168+
free(buf.ptr);
169+
}
174170
}
175171
}
176172
};

0 commit comments

Comments
 (0)