1 #include <kiba/renderer/vulkan/allocator.h>
10 const char *vulkan_allocation_scope_names[] = {
11 "VK_SYSTEM_ALLOCATION_SCOPE_COMMAND",
12 "VK_SYSTEM_ALLOCATION_SCOPE_OBJECT",
13 "VK_SYSTEM_ALLOCATION_SCOPE_CACHE",
14 "VK_SYSTEM_ALLOCATION_SCOPE_DEVICE",
15 "VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE",
18 void *vulkan_allocation_function(
void *pUserData,
21 VkSystemAllocationScope allocationScope);
23 void vulkan_free_function(
void *pUserData,
void *pMemory);
25 void vulkan_internal_allocation_notification(
void *pUserData,
27 VkInternalAllocationType allocationType,
28 VkSystemAllocationScope allocationScope);
30 void vulkan_internal_free_notification(
void *pUserData,
32 VkInternalAllocationType allocationType,
33 VkSystemAllocationScope allocationScope);
35 void *vulkan_reallocation_function(
void *pUserData,
39 VkSystemAllocationScope allocationScope);
43 KB_ERROR(
"could not create kiba allocator for vulkan allocator");
47 alloc->vulkan_callbacks.pUserData = alloc;
48 alloc->vulkan_callbacks.pfnAllocation = vulkan_allocation_function;
49 alloc->vulkan_callbacks.pfnFree = vulkan_free_function;
50 alloc->vulkan_callbacks.pfnInternalAllocation = vulkan_internal_allocation_notification;
51 alloc->vulkan_callbacks.pfnInternalFree = vulkan_internal_free_notification;
52 alloc->vulkan_callbacks.pfnReallocation = vulkan_reallocation_function;
57 alloc->vulkan_callbacks.pUserData =
KB_NULL;
58 alloc->vulkan_callbacks.pfnAllocation =
KB_NULL;
59 alloc->vulkan_callbacks.pfnFree =
KB_NULL;
60 alloc->vulkan_callbacks.pfnInternalAllocation =
KB_NULL;
61 alloc->vulkan_callbacks.pfnInternalFree =
KB_NULL;
62 alloc->vulkan_callbacks.pfnReallocation =
KB_NULL;
66 void *vulkan_allocation_function(
void *pUserData,
69 VkSystemAllocationScope allocationScope) {
71 KB_TRACE(
"vulkan allocating {usize} bytes aligned to {usize} with scope {raw_string}",
74 vulkan_allocation_scope_names[allocationScope]);
77 uptr misalignment = (uptr) ret % alignment;
78 KB_ASSERT(misalignment == 0,
"allcation must be aligned as requested");
82 void vulkan_free_function(
void *pUserData,
void *pMemory) {
83 KB_TRACE(
"vulkan freeing {pointer}", pMemory);
90 void vulkan_internal_allocation_notification(
void *pUserData,
92 VkInternalAllocationType allocationType,
93 VkSystemAllocationScope allocationScope) {
98 KB_DEBUG(
"vulkan allocating {usize} bytes of type {u32} with scope {raw_string}",
101 vulkan_allocation_scope_names[allocationScope]);
104 void vulkan_internal_free_notification(
void *pUserData,
106 VkInternalAllocationType allocationType,
107 VkSystemAllocationScope allocationScope) {
112 KB_DEBUG(
"vulkan freeing {usize} bytes of type {u32} with scope {raw_string}",
115 vulkan_allocation_scope_names[allocationScope]);
118 void *vulkan_reallocation_function(
void *pUserData,
122 VkSystemAllocationScope allocationScope) {
124 KB_TRACE(
"vulkan reallocating {pointer} to {usize} bytes aligned to {usize} with scope {raw_string}",
128 vulkan_allocation_scope_names[allocationScope]);
132 return vulkan_allocation_function(pUserData, size, alignment, allocationScope);
135 vulkan_free_function(pUserData, pOriginal);
139 uptr misalignment = (uptr) ret % alignment;
140 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.
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.