diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4f9a76e..21f7ebd 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -22,7 +22,7 @@ jobs: with: fetch-depth: 0 submodules: true - - uses: goto-bus-stop/setup-zig@v1 + - uses: goto-bus-stop/setup-zig@v2 with: version: master @@ -32,6 +32,10 @@ jobs: - name: Build run: zig build + - name: Build with Tracy + if: ${{ matrix.os != 'macos-latest' }} + run: zig build -Denable_tracy -Denable_tracy_allocation + - name: Run Tests run: zig build test diff --git a/src/tracy.zig b/src/tracy.zig index 07553bc..f933edd 100644 --- a/src/tracy.zig +++ b/src/tracy.zig @@ -146,44 +146,54 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type { } pub fn allocator(self: *Self) std.mem.Allocator { - return std.mem.Allocator.init(self, allocFn, resizeFn, freeFn); + return .{ + .ptr = self, + .vtable = &.{ + .alloc = allocFn, + .resize = resizeFn, + .free = freeFn, + }, + }; } - fn allocFn(self: *Self, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) std.mem.Allocator.Error![]u8 { - const result = self.parent_allocator.rawAlloc(len, ptr_align, len_align, ret_addr); + fn allocFn(ptr: *anyopaque, len: usize, ptr_align: u8, ret_addr: usize) ?[*]u8 { + const self = @ptrCast(*Self, @alignCast(@alignOf(Self), ptr)); + const result = self.parent_allocator.rawAlloc(len, ptr_align, ret_addr); if (result) |data| { - if (data.len != 0) { + if (len != 0) { if (name) |n| { - allocNamed(data.ptr, data.len, n); + allocNamed(data, len, n); } else { - alloc(data.ptr, data.len); + alloc(data, len); } } - } else |_| { + } else { messageColor("allocation failed", 0xFF0000); } return result; } - fn resizeFn(self: *Self, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) ?usize { - if (self.parent_allocator.rawResize(buf, buf_align, new_len, len_align, ret_addr)) |resized_len| { + fn resizeFn(ptr: *anyopaque, buf: []u8, buf_align: u8, new_len: usize, ret_addr: usize) bool { + const self = @ptrCast(*Self, @alignCast(@alignOf(Self), ptr)); + if (self.parent_allocator.rawResize(buf, buf_align, new_len, ret_addr)) { if (name) |n| { freeNamed(buf.ptr, n); - allocNamed(buf.ptr, resized_len, n); + allocNamed(buf.ptr, new_len, n); } else { free(buf.ptr); - alloc(buf.ptr, resized_len); + alloc(buf.ptr, new_len); } - return resized_len; + return true; } // during normal operation the compiler hits this case thousands of times due to this // emitting messages for it is both slow and causes clutter - return null; + return false; } - fn freeFn(self: *Self, buf: []u8, buf_align: u29, ret_addr: usize) void { + fn freeFn(ptr: *anyopaque, buf: []u8, buf_align: u8, ret_addr: usize) void { + const self = @ptrCast(*Self, @alignCast(@alignOf(Self), ptr)); self.parent_allocator.rawFree(buf, buf_align, ret_addr); // 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{}`