mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
Update TracyAllocator to new allocator API
This commit is contained in:
parent
9a8f1f675b
commit
be20f97eb9
1 changed files with 34 additions and 16 deletions
|
|
@ -120,20 +120,21 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
|
||||||
.vtable = &.{
|
.vtable = &.{
|
||||||
.alloc = allocFn,
|
.alloc = allocFn,
|
||||||
.resize = resizeFn,
|
.resize = resizeFn,
|
||||||
|
.remap = remapFn,
|
||||||
.free = freeFn,
|
.free = freeFn,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn allocFn(ptr: *anyopaque, len: usize, ptr_align: u8, ret_addr: usize) ?[*]u8 {
|
fn allocFn(ptr: *anyopaque, len: usize, alignment: std.mem.Alignment, ret_addr: usize) ?[*]u8 {
|
||||||
const self: *Self = @ptrCast(@alignCast(ptr));
|
const self: *Self = @ptrCast(@alignCast(ptr));
|
||||||
const result = self.parent_allocator.rawAlloc(len, ptr_align, ret_addr);
|
const result = self.parent_allocator.rawAlloc(len, alignment, ret_addr);
|
||||||
if (result) |data| {
|
if (result) |memory| {
|
||||||
if (len != 0) {
|
if (len != 0) {
|
||||||
if (name) |n| {
|
if (name) |n| {
|
||||||
allocNamed(data, len, n);
|
allocNamed(memory, len, n);
|
||||||
} else {
|
} else {
|
||||||
alloc(data, len);
|
alloc(memory, len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -142,15 +143,15 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resizeFn(ptr: *anyopaque, buf: []u8, buf_align: u8, new_len: usize, ret_addr: usize) bool {
|
fn resizeFn(ptr: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize) bool {
|
||||||
const self: *Self = @ptrCast(@alignCast(ptr));
|
const self: *Self = @ptrCast(@alignCast(ptr));
|
||||||
if (self.parent_allocator.rawResize(buf, buf_align, new_len, ret_addr)) {
|
if (self.parent_allocator.rawResize(memory, alignment, new_len, ret_addr)) {
|
||||||
if (name) |n| {
|
if (name) |n| {
|
||||||
freeNamed(buf.ptr, n);
|
freeNamed(memory.ptr, n);
|
||||||
allocNamed(buf.ptr, new_len, n);
|
allocNamed(memory.ptr, new_len, n);
|
||||||
} else {
|
} else {
|
||||||
free(buf.ptr);
|
free(memory.ptr);
|
||||||
alloc(buf.ptr, new_len);
|
alloc(memory.ptr, new_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -161,16 +162,33 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn freeFn(ptr: *anyopaque, buf: []u8, buf_align: u8, ret_addr: usize) void {
|
fn remapFn(ptr: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize) ?[*]u8 {
|
||||||
const self: *Self = @ptrCast(@alignCast(ptr));
|
const self: *Self = @ptrCast(@alignCast(ptr));
|
||||||
self.parent_allocator.rawFree(buf, buf_align, ret_addr);
|
if (self.parent_allocator.rawRemap(memory, alignment, new_len, ret_addr)) |new_memory| {
|
||||||
|
if (name) |n| {
|
||||||
|
freeNamed(memory.ptr, n);
|
||||||
|
allocNamed(new_memory, new_len, n);
|
||||||
|
} else {
|
||||||
|
free(memory.ptr);
|
||||||
|
alloc(new_memory, new_len);
|
||||||
|
}
|
||||||
|
return new_memory;
|
||||||
|
} else {
|
||||||
|
messageColor("reallocation failed", 0xFF0000);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn freeFn(ptr: *anyopaque, memory: []u8, alignment: std.mem.Alignment, ret_addr: usize) void {
|
||||||
|
const self: *Self = @ptrCast(@alignCast(ptr));
|
||||||
|
self.parent_allocator.rawFree(memory, alignment, ret_addr);
|
||||||
// this condition is to handle free being called on an empty slice that was never even allocated
|
// this condition is to handle free being called on an empty slice that was never even allocated
|
||||||
// example case: `std.process.getSelfExeSharedLibPaths` can return `&[_][:0]u8{}`
|
// example case: `std.process.getSelfExeSharedLibPaths` can return `&[_][:0]u8{}`
|
||||||
if (buf.len != 0) {
|
if (memory.len != 0) {
|
||||||
if (name) |n| {
|
if (name) |n| {
|
||||||
freeNamed(buf.ptr, n);
|
freeNamed(memory.ptr, n);
|
||||||
} else {
|
} else {
|
||||||
free(buf.ptr);
|
free(memory.ptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue