Skip to content

Upgrade allocator wrapper for zig 0.14.0 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/ztracy.zig
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ const tracy_full = struct {
.vtable = &.{
.alloc = alloc,
.resize = resize,
.remap = remap,
.free = free,
},
};
Expand All @@ -576,11 +577,11 @@ const tracy_full = struct {
fn alloc(
ctx: *anyopaque,
len: usize,
log2_ptr_align: u8,
alignment: std.mem.Alignment,
ra: usize,
) ?[*]u8 {
const self: *TracyAllocator = @ptrCast(@alignCast(ctx));
const result = self.child_allocator.rawAlloc(len, log2_ptr_align, ra);
const result = self.child_allocator.rawAlloc(len, alignment, ra);
if (result) |addr| {
Alloc(addr, len);
} else {
Expand All @@ -594,12 +595,12 @@ const tracy_full = struct {
fn resize(
ctx: *anyopaque,
buf: []u8,
log2_ptr_align: u8,
alignment: std.mem.Alignment,
new_len: usize,
ra: usize,
) bool {
const self: *TracyAllocator = @ptrCast(@alignCast(ctx));
const result = self.child_allocator.rawResize(buf, log2_ptr_align, new_len, ra);
const result = self.child_allocator.rawResize(buf, alignment, new_len, ra);
if (result) {
Free(buf.ptr);
Alloc(buf.ptr, new_len);
Expand All @@ -611,14 +612,34 @@ const tracy_full = struct {
return result;
}

fn remap(
ctx: *anyopaque,
buf: []u8,
alignment: std.mem.Alignment,
new_len: usize,
ra: usize,
) ?[*]u8 {
const self: *TracyAllocator = @ptrCast(@alignCast(ctx));
const result = self.child_allocator.rawRemap(buf, alignment, new_len, ra);
if (result) |data| {
Free(buf.ptr);
Alloc(data, new_len);
} else {
var buffer: [128]u8 = undefined;
const msg = std.fmt.bufPrint(&buffer, "remap failed requesting {d} -> {d}", .{ buf.len, new_len }) catch return result;
Message(msg);
}
return result;
}

fn free(
ctx: *anyopaque,
buf: []u8,
log2_ptr_align: u8,
alignment: std.mem.Alignment,
ra: usize,
) void {
const self: *TracyAllocator = @ptrCast(@alignCast(ctx));
self.child_allocator.rawFree(buf, log2_ptr_align, ra);
self.child_allocator.rawFree(buf, alignment, ra);
Free(buf.ptr);
}
};
Expand Down
Loading