mirror of
https://github.com/ziglang/zig.git
synced 2024-11-27 15:42:49 +00:00
allocgate: update code to use new interface
This commit is contained in:
parent
02e5e0ba1f
commit
23866b1f81
@ -1192,7 +1192,7 @@ test "bug 9995 fix, large allocs count requested size not backing size" {
|
|||||||
// with AtLeast, buffer likely to be larger than requested, especially when shrinking
|
// with AtLeast, buffer likely to be larger than requested, especially when shrinking
|
||||||
var gpa = GeneralPurposeAllocator(.{ .enable_memory_limit = true }){};
|
var gpa = GeneralPurposeAllocator(.{ .enable_memory_limit = true }){};
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var buf = try allocator.allocAdvanced(u8, 1, page_size + 1, .at_least);
|
var buf = try allocator.allocAdvanced(u8, 1, page_size + 1, .at_least);
|
||||||
try std.testing.expect(gpa.total_requested_bytes == page_size + 1);
|
try std.testing.expect(gpa.total_requested_bytes == page_size + 1);
|
||||||
buf = try allocator.reallocAtLeast(buf, 1);
|
buf = try allocator.reallocAtLeast(buf, 1);
|
||||||
|
@ -109,11 +109,14 @@ pub fn tracyAllocator(allocator: std.mem.Allocator) TracyAllocator(null) {
|
|||||||
|
|
||||||
pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
|
pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
|
||||||
return struct {
|
return struct {
|
||||||
allocator: std.mem.Allocator,
|
|
||||||
parent_allocator: std.mem.Allocator,
|
parent_allocator: std.mem.Allocator,
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
|
pub fn allocator(self: *Self) std.mem.Allocator {
|
||||||
|
return std.mem.Allocator.init(self, allocFn, resizeFn);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn init(allocator: std.mem.Allocator) Self {
|
pub fn init(allocator: std.mem.Allocator) Self {
|
||||||
return .{
|
return .{
|
||||||
.parent_allocator = allocator,
|
.parent_allocator = allocator,
|
||||||
@ -124,8 +127,7 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn allocFn(allocator: std.mem.Allocator, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) std.mem.Allocator.Error![]u8 {
|
fn allocFn(self: *Self, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) std.mem.Allocator.Error![]u8 {
|
||||||
const self = @fieldParentPtr(Self, "allocator", allocator);
|
|
||||||
const result = self.parent_allocator.allocFn(self.parent_allocator, len, ptr_align, len_align, ret_addr);
|
const result = self.parent_allocator.allocFn(self.parent_allocator, len, ptr_align, len_align, ret_addr);
|
||||||
if (result) |data| {
|
if (result) |data| {
|
||||||
if (data.len != 0) {
|
if (data.len != 0) {
|
||||||
@ -141,9 +143,7 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resizeFn(allocator: std.mem.Allocator, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) std.mem.Allocator.Error!usize {
|
fn resizeFn(self: *Self, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) std.mem.Allocator.Error!usize {
|
||||||
const self = @fieldParentPtr(Self, "allocator", allocator);
|
|
||||||
|
|
||||||
if (self.parent_allocator.resizeFn(self.parent_allocator, buf, buf_align, new_len, len_align, ret_addr)) |resized_len| {
|
if (self.parent_allocator.resizeFn(self.parent_allocator, buf, buf_align, new_len, len_align, ret_addr)) |resized_len| {
|
||||||
// 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{}`
|
||||||
|
Loading…
Reference in New Issue
Block a user