reworking vulkan allocator
This commit is contained in:
@@ -26,18 +26,54 @@ pub fn init(callbacks: ?*const vk.AllocationCallbacks, scope: vk.SystemAllocatio
|
||||
}
|
||||
|
||||
pub fn allocator(self: *const Self) Allocator {
|
||||
if (self.callbacks != null) {
|
||||
return .{
|
||||
.ptr = undefined,
|
||||
.vtable = &.{
|
||||
.alloc = alloc,
|
||||
.resize = resize,
|
||||
.remap = remap,
|
||||
.free = free,
|
||||
},
|
||||
};
|
||||
}
|
||||
return .{
|
||||
.ptr = @ptrCast(@constCast(self)), // Ugly const cast for convenience
|
||||
.vtable = &.{
|
||||
.alloc = alloc,
|
||||
.resize = resize,
|
||||
.remap = remap,
|
||||
.free = free,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
fn alloc(context: *anyopaque, len: usize, alignment: Alignment, ret_addr: usize) ?[*]u8 {
|
||||
const self: *Self = @ptrCast(@alignCast(context));
|
||||
if (self.callbacks.?.pfn_allocation) |pfn_allocation| {
|
||||
return @ptrCast(pfn_allocation(self.callbacks.?.p_user_data, len, alignment.toByteUnits(), self.scope));
|
||||
} else {
|
||||
return getFallbackAllocator().rawAlloc(len, alignment, ret_addr);
|
||||
}
|
||||
}
|
||||
|
||||
fn resize(context: *anyopaque, ptr: []u8, alignment: Alignment, new_len: usize, ret_addr: usize) bool {
|
||||
const self: *Self = @ptrCast(@alignCast(context));
|
||||
if (self.callbacks != null) {
|
||||
return new_len <= ptr.len;
|
||||
} else {
|
||||
return getFallbackAllocator().rawResize(ptr, alignment, new_len, ret_addr);
|
||||
}
|
||||
}
|
||||
|
||||
fn remap(context: *anyopaque, ptr: []u8, alignment: Alignment, new_len: usize, ret_addr: usize) ?[*]u8 {
|
||||
const self: *Self = @ptrCast(@alignCast(context));
|
||||
if (self.callbacks.?.pfn_reallocation) |pfn_reallocation| {
|
||||
return @ptrCast(pfn_reallocation(self.callbacks.?.p_user_data, ptr.ptr, new_len, alignment.toByteUnits(), self.scope));
|
||||
} else {
|
||||
return getFallbackAllocator().rawRemap(ptr, alignment, new_len, ret_addr);
|
||||
}
|
||||
}
|
||||
|
||||
fn free(context: *anyopaque, ptr: []u8, alignment: Alignment, ret_addr: usize) void {
|
||||
const self: *Self = @ptrCast(@alignCast(context));
|
||||
if (self.callbacks.?.pfn_free) |pfn_free| {
|
||||
return pfn_free(self.callbacks.?.p_user_data, ptr.ptr);
|
||||
} else {
|
||||
return getFallbackAllocator().rawFree(ptr, alignment, ret_addr);
|
||||
}
|
||||
}
|
||||
|
||||
inline fn getFallbackAllocator() std.mem.Allocator {
|
||||
if (std.process.hasEnvVarConstant(DRIVER_DEBUG_ALLOCATOR_ENV_NAME) or builtin.mode == std.builtin.OptimizeMode.Debug) {
|
||||
@branchHint(.unlikely);
|
||||
return debug_allocator.allocator();
|
||||
@@ -45,34 +81,3 @@ pub fn allocator(self: *const Self) Allocator {
|
||||
return std.heap.c_allocator;
|
||||
}
|
||||
}
|
||||
|
||||
fn alloc(context: *anyopaque, len: usize, alignment: Alignment, _: usize) ?[*]u8 {
|
||||
const self: *Self = @ptrCast(@alignCast(context));
|
||||
if (self.callbacks.?.pfn_allocation) |pfn_allocation| {
|
||||
return @ptrCast(pfn_allocation(self.callbacks.?.p_user_data, len, alignment.toByteUnits(), self.scope));
|
||||
}
|
||||
@panic("Null PFN_vkAllocationFunction passed to VkAllocationCallbacks");
|
||||
}
|
||||
|
||||
fn resize(context: *anyopaque, ptr: []u8, alignment: Alignment, new_len: usize, _: usize) bool {
|
||||
_ = alignment;
|
||||
_ = context;
|
||||
return new_len <= ptr.len;
|
||||
}
|
||||
|
||||
fn remap(context: *anyopaque, ptr: []u8, alignment: Alignment, new_len: usize, _: usize) ?[*]u8 {
|
||||
const self: *Self = @ptrCast(@alignCast(context));
|
||||
if (self.callbacks.?.pfn_reallocation) |pfn_reallocation| {
|
||||
return @ptrCast(pfn_reallocation(self.callbacks.?.p_user_data, ptr.ptr, new_len, alignment.toByteUnits(), self.scope));
|
||||
}
|
||||
@panic("Null PFN_vkReallocationFunction passed to VkAllocationCallbacks");
|
||||
}
|
||||
|
||||
fn free(context: *anyopaque, ptr: []u8, alignment: Alignment, _: usize) void {
|
||||
_ = alignment;
|
||||
const self: *Self = @ptrCast(@alignCast(context));
|
||||
if (self.callbacks.?.pfn_free) |pfn_free| {
|
||||
return pfn_free(self.callbacks.?.p_user_data, ptr.ptr);
|
||||
}
|
||||
@panic("Null PFN_vkFreeFunction passed to VkAllocationCallbacks");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user