update TracyAllocator for new Allocator changes (#812)

* update TracyAllocator for new Allocator changes

* Add build with Tracy to CI to catch regressions

* disable Tracy CI step on macos
This commit is contained in:
Lee Cannon 2022-12-08 19:32:50 +00:00 committed by GitHub
parent ddf3ef1389
commit 9e658cdbb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 15 deletions

View File

@ -22,7 +22,7 @@ jobs:
with: with:
fetch-depth: 0 fetch-depth: 0
submodules: true submodules: true
- uses: goto-bus-stop/setup-zig@v1 - uses: goto-bus-stop/setup-zig@v2
with: with:
version: master version: master
@ -32,6 +32,10 @@ jobs:
- name: Build - name: Build
run: zig build run: zig build
- name: Build with Tracy
if: ${{ matrix.os != 'macos-latest' }}
run: zig build -Denable_tracy -Denable_tracy_allocation
- name: Run Tests - name: Run Tests
run: zig build test run: zig build test

View File

@ -146,44 +146,54 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
} }
pub fn allocator(self: *Self) std.mem.Allocator { 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 { fn allocFn(ptr: *anyopaque, len: usize, ptr_align: u8, ret_addr: usize) ?[*]u8 {
const result = self.parent_allocator.rawAlloc(len, ptr_align, len_align, ret_addr); const self = @ptrCast(*Self, @alignCast(@alignOf(Self), ptr));
const result = self.parent_allocator.rawAlloc(len, ptr_align, ret_addr);
if (result) |data| { if (result) |data| {
if (data.len != 0) { if (len != 0) {
if (name) |n| { if (name) |n| {
allocNamed(data.ptr, data.len, n); allocNamed(data, len, n);
} else { } else {
alloc(data.ptr, data.len); alloc(data, len);
} }
} }
} else |_| { } else {
messageColor("allocation failed", 0xFF0000); messageColor("allocation failed", 0xFF0000);
} }
return result; return result;
} }
fn resizeFn(self: *Self, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) ?usize { fn resizeFn(ptr: *anyopaque, buf: []u8, buf_align: u8, new_len: usize, ret_addr: usize) bool {
if (self.parent_allocator.rawResize(buf, buf_align, new_len, len_align, ret_addr)) |resized_len| { const self = @ptrCast(*Self, @alignCast(@alignOf(Self), ptr));
if (self.parent_allocator.rawResize(buf, buf_align, new_len, ret_addr)) {
if (name) |n| { if (name) |n| {
freeNamed(buf.ptr, n); freeNamed(buf.ptr, n);
allocNamed(buf.ptr, resized_len, n); allocNamed(buf.ptr, new_len, n);
} else { } else {
free(buf.ptr); 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 // during normal operation the compiler hits this case thousands of times due to this
// emitting messages for it is both slow and causes clutter // 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); 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 // 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{}`