Skip to content

Commit be20f97

Browse files
silversquirlalexrp
authored andcommitted
Update TracyAllocator to new allocator API
1 parent 9a8f1f6 commit be20f97

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

src/tracy.zig

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,21 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
120120
.vtable = &.{
121121
.alloc = allocFn,
122122
.resize = resizeFn,
123+
.remap = remapFn,
123124
.free = freeFn,
124125
},
125126
};
126127
}
127128

128-
fn allocFn(ptr: *anyopaque, len: usize, ptr_align: u8, ret_addr: usize) ?[*]u8 {
129+
fn allocFn(ptr: *anyopaque, len: usize, alignment: std.mem.Alignment, ret_addr: usize) ?[*]u8 {
129130
const self: *Self = @ptrCast(@alignCast(ptr));
130-
const result = self.parent_allocator.rawAlloc(len, ptr_align, ret_addr);
131-
if (result) |data| {
131+
const result = self.parent_allocator.rawAlloc(len, alignment, ret_addr);
132+
if (result) |memory| {
132133
if (len != 0) {
133134
if (name) |n| {
134-
allocNamed(data, len, n);
135+
allocNamed(memory, len, n);
135136
} else {
136-
alloc(data, len);
137+
alloc(memory, len);
137138
}
138139
}
139140
} else {
@@ -142,15 +143,15 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
142143
return result;
143144
}
144145

145-
fn resizeFn(ptr: *anyopaque, buf: []u8, buf_align: u8, new_len: usize, ret_addr: usize) bool {
146+
fn resizeFn(ptr: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize) bool {
146147
const self: *Self = @ptrCast(@alignCast(ptr));
147-
if (self.parent_allocator.rawResize(buf, buf_align, new_len, ret_addr)) {
148+
if (self.parent_allocator.rawResize(memory, alignment, new_len, ret_addr)) {
148149
if (name) |n| {
149-
freeNamed(buf.ptr, n);
150-
allocNamed(buf.ptr, new_len, n);
150+
freeNamed(memory.ptr, n);
151+
allocNamed(memory.ptr, new_len, n);
151152
} else {
152-
free(buf.ptr);
153-
alloc(buf.ptr, new_len);
153+
free(memory.ptr);
154+
alloc(memory.ptr, new_len);
154155
}
155156

156157
return true;
@@ -161,16 +162,33 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
161162
return false;
162163
}
163164

164-
fn freeFn(ptr: *anyopaque, buf: []u8, buf_align: u8, ret_addr: usize) void {
165+
fn remapFn(ptr: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize) ?[*]u8 {
165166
const self: *Self = @ptrCast(@alignCast(ptr));
166-
self.parent_allocator.rawFree(buf, buf_align, ret_addr);
167+
if (self.parent_allocator.rawRemap(memory, alignment, new_len, ret_addr)) |new_memory| {
168+
if (name) |n| {
169+
freeNamed(memory.ptr, n);
170+
allocNamed(new_memory, new_len, n);
171+
} else {
172+
free(memory.ptr);
173+
alloc(new_memory, new_len);
174+
}
175+
return new_memory;
176+
} else {
177+
messageColor("reallocation failed", 0xFF0000);
178+
return null;
179+
}
180+
}
181+
182+
fn freeFn(ptr: *anyopaque, memory: []u8, alignment: std.mem.Alignment, ret_addr: usize) void {
183+
const self: *Self = @ptrCast(@alignCast(ptr));
184+
self.parent_allocator.rawFree(memory, alignment, ret_addr);
167185
// this condition is to handle free being called on an empty slice that was never even allocated
168186
// example case: `std.process.getSelfExeSharedLibPaths` can return `&[_][:0]u8{}`
169-
if (buf.len != 0) {
187+
if (memory.len != 0) {
170188
if (name) |n| {
171-
freeNamed(buf.ptr, n);
189+
freeNamed(memory.ptr, n);
172190
} else {
173-
free(buf.ptr);
191+
free(memory.ptr);
174192
}
175193
}
176194
}

0 commit comments

Comments
 (0)