1 #include <kiba/gpu/vulkan/allocator.h>
12 const char *vk_allocation_scope_names[] = {
13 "VK_SYSTEM_ALLOCATION_SCOPE_COMMAND",
14 "VK_SYSTEM_ALLOCATION_SCOPE_OBJECT",
15 "VK_SYSTEM_ALLOCATION_SCOPE_CACHE",
16 "VK_SYSTEM_ALLOCATION_SCOPE_DEVICE",
17 "VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE",
20 void *vk_allocation_function(
void *pUserData,
size_t size,
size_t alignment, VkSystemAllocationScope allocationScope);
22 void vk_free_function(
void *pUserData,
void *pMemory);
24 void vk_internal_allocation_notification(
void *pUserData,
26 VkInternalAllocationType allocationType,
27 VkSystemAllocationScope allocationScope);
29 void vk_internal_free_notification(
void *pUserData,
31 VkInternalAllocationType allocationType,
32 VkSystemAllocationScope allocationScope);
34 void *vk_reallocation_function(
void *pUserData,
38 VkSystemAllocationScope allocationScope);
40 b8 vk_allocator_initialize(
void) {
42 KB_ERROR(
"could not create kiba allocator for vulkan allocator");
47 vk_alloc.vulkan_callbacks.pfnAllocation = vk_allocation_function;
48 vk_alloc.vulkan_callbacks.pfnFree = vk_free_function;
49 vk_alloc.vulkan_callbacks.pfnInternalAllocation = vk_internal_allocation_notification;
50 vk_alloc.vulkan_callbacks.pfnInternalFree = vk_internal_free_notification;
51 vk_alloc.vulkan_callbacks.pfnReallocation = vk_reallocation_function;
55 void vk_allocator_shutdown(
void) {
66 void *vk_allocation_function(
void *pUserData,
size_t size,
size_t alignment, VkSystemAllocationScope allocationScope) {
68 KB_TRACE(
"vulkan allocating {usize} bytes aligned to {usize} with scope {raw_string}",
71 vk_allocation_scope_names[allocationScope]);
74 uptr misalignment = (uptr) ret % alignment;
75 KB_ASSERT(misalignment == 0,
"allcation must be aligned as requested");
79 void vk_free_function(
void *pUserData,
void *pMemory) {
80 KB_TRACE(
"vulkan freeing {pointer}", pMemory);
87 void vk_internal_allocation_notification(
void *pUserData,
89 VkInternalAllocationType allocationType,
90 VkSystemAllocationScope allocationScope) {
95 KB_DEBUG(
"vulkan allocating {usize} bytes of type {u32} with scope {raw_string}",
98 vk_allocation_scope_names[allocationScope]);
101 void vk_internal_free_notification(
void *pUserData,
103 VkInternalAllocationType allocationType,
104 VkSystemAllocationScope allocationScope) {
109 KB_DEBUG(
"vulkan freeing {usize} bytes of type {u32} with scope {raw_string}",
112 vk_allocation_scope_names[allocationScope]);
115 void *vk_reallocation_function(
void *pUserData,
119 VkSystemAllocationScope allocationScope) {
121 KB_TRACE(
"vulkan reallocating {pointer} to {usize} bytes aligned to {usize} with scope {raw_string}",
125 vk_allocation_scope_names[allocationScope]);
129 return vk_allocation_function(pUserData, size, alignment, allocationScope);
132 vk_free_function(pUserData, pOriginal);
136 uptr misalignment = (uptr) ret % alignment;
137 KB_ASSERT(misalignment == 0,
"reallcation must be aligned as requested");
void * allocator_allocate_aligned(allocator *alloc, usize size, usize alignment)
Allocate aligned memory.
void allocator_free(allocator *alloc, void *mem)
Give back memory to the allocator.
b8 allocator_create(allocator *alloc, allocator_type type, usize size)
Create an allocator of a specific type.
void allocator_destroy(allocator *alloc)
Destroy an allocator.
void * allocator_reallocate_aligned(allocator *alloc, void *original, usize new_size, usize alignment)
Rellocate aligned memory.
void * memory_zero(void *mem, usize size)
Zero out memory.
Lightweight layer between platform and other engine components to enable tracing/monitoring.
#define UNUSED(x)
Mark parameter as unused.
#define KB_NULL
Value of an invalid ptr (nullptr).
#define KB_DEBUG(...)
Log entry with debug log level.
#define KB_ASSERT(expr,...)
Perform runtime assertion and log failures.
#define KB_ERROR(...)
Log entry with error log level.
#define KB_TRACE(...)
Log entry with trace log level.