authorgravatar for leecannon@leecannon.xyzLee Cannon <leecannon@leecannon.xyz> 2021-12-07 00:05:35+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-06 16:09:12-08:00
log7d1f47313d798ce42f519b770edb2f1dae17199e
treeb88f3c6762a1362153133bfd9bf09994fade5175
parenta3d9cd1c1d9f1acbc9715a46bee76282e340e294

stage2: fix TracyAllocator bugs


1 files changed, 13 insertions(+), 17 deletions(-)

src/tracy.zig+13-17
...@@ -124,7 +124,7 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {...@@ -124,7 +124,7 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
124 }124 }
125125
126 fn allocFn(self: *Self, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) std.mem.Allocator.Error![]u8 {126 fn allocFn(self: *Self, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) std.mem.Allocator.Error![]u8 {
127 const result = self.parent_allocator.allocFn(self.parent_allocator, len, ptr_align, len_align, ret_addr);127 const result = self.parent_allocator.rawAlloc(len, ptr_align, len_align, ret_addr);
128 if (result) |data| {128 if (result) |data| {
129 if (data.len != 0) {129 if (data.len != 0) {
130 if (name) |n| {130 if (name) |n| {
...@@ -139,22 +139,14 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {...@@ -139,22 +139,14 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
139 return result;139 return result;
140 }140 }
141141
142 fn resizeFn(self: *Self, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) std.mem.Allocator.Error!usize {142 fn resizeFn(self: *Self, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) ?usize {
143 if (self.parent_allocator.resizeFn(self.parent_allocator, buf, buf_align, new_len, len_align, ret_addr)) |resized_len| {143 if (self.parent_allocator.rawResize(buf, buf_align, new_len, len_align, ret_addr)) |resized_len| {
144 // this condition is to handle free being called on an empty slice that was never even allocated
145 // example case: `std.process.getSelfExeSharedLibPaths` can return `&[_][:0]u8{}`
146 if (buf.len != 0) {
147 if (name) |n| {
148 freeNamed(buf.ptr, n);
149 } else {
150 free(buf.ptr);
151 }
152 }
153
154 if (name) |n| {144 if (name) |n| {
145 freeNamed(buf.ptr, n);
155 allocNamed(buf.ptr, resized_len, n);146 allocNamed(buf.ptr, resized_len, n);
156 } else {147 } else {
157 alloc(buf.ptr, resized_len);148 alloc(buf.ptr, resized_len);
149 free(buf.ptr);
158 }150 }
159151
160 return resized_len;152 return resized_len;
...@@ -167,10 +159,14 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {...@@ -167,10 +159,14 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
167159
168 fn freeFn(self: *Self, buf: []u8, buf_align: u29, ret_addr: usize) void {160 fn freeFn(self: *Self, buf: []u8, buf_align: u29, ret_addr: usize) void {
169 self.parent_allocator.rawFree(buf, buf_align, ret_addr);161 self.parent_allocator.rawFree(buf, buf_align, ret_addr);
170 if (name) |n| {162 // this condition is to handle free being called on an empty slice that was never even allocated
171 freeNamed(buf.ptr, n);163 // example case: `std.process.getSelfExeSharedLibPaths` can return `&[_][:0]u8{}`
172 } else {164 if (buf.len != 0) {
173 free(buf.ptr);165 if (name) |n| {
166 freeNamed(buf.ptr, n);
167 } else {
168 free(buf.ptr);
169 }
174 }170 }
175 }171 }
176 };172 };