diff options
author | Charles Giessen <charles@lunarg.com> | 2022-05-21 03:40:30 +0300 |
---|---|---|
committer | Charles Giessen <46324611+charles-lunarg@users.noreply.github.com> | 2022-05-24 23:18:26 +0300 |
commit | e65a8bcaeb8f1405c08463da88e8137fd87b7c3a (patch) | |
tree | 769c9acacce94e4e37a50ecbd1eff9fb1969c637 | |
parent | 2e5d77b6ef57619a23ab448a4c1b5c58cd1903ea (diff) |
Refactor loader allocation functionality
Created wrapper functions loader_alloc, loader_calloc, loader_free,
and loader_realloc. Made the existing loader allocation functions use
them. Replaced manual usage of VkAllocatorCallbacks with the new
wrapper functions.
-rw-r--r-- | loader/allocation.c | 130 | ||||
-rw-r--r-- | loader/allocation.h | 27 | ||||
-rw-r--r-- | loader/debug_utils.c | 297 | ||||
-rw-r--r-- | loader/get_environment.c | 19 | ||||
-rw-r--r-- | loader/loader.c | 18 | ||||
-rw-r--r-- | loader/trampoline.c | 12 | ||||
-rw-r--r-- | loader/vk_loader_platform.h | 3 |
7 files changed, 166 insertions, 340 deletions
diff --git a/loader/allocation.c b/loader/allocation.c index 0841c967b..3397ef992 100644 --- a/loader/allocation.c +++ b/loader/allocation.c @@ -28,15 +28,18 @@ #include <stdlib.h> -void *loader_instance_heap_alloc(const struct loader_instance *instance, size_t size, VkSystemAllocationScope alloc_scope) { +// A debug option to disable allocators at compile time to investigate future issues. +#define DEBUG_DISABLE_APP_ALLOCATORS 0 + +void *loader_alloc(const VkAllocationCallbacks *pAllocator, size_t size, VkSystemAllocationScope allocation_scope) { void *pMemory = NULL; #if (DEBUG_DISABLE_APP_ALLOCATORS == 1) { #else - if (instance && instance->alloc_callbacks.pfnAllocation) { + if (pAllocator && pAllocator->pfnAllocation) { // These are internal structures, so it's best to align everything to // the largest unit size which is the size of a uint64_t. - pMemory = instance->alloc_callbacks.pfnAllocation(instance->alloc_callbacks.pUserData, size, sizeof(uint64_t), alloc_scope); + pMemory = pAllocator->pfnAllocation(pAllocator->pUserData, size, sizeof(uint64_t), allocation_scope); } else { #endif pMemory = malloc(size); @@ -45,64 +48,33 @@ void *loader_instance_heap_alloc(const struct loader_instance *instance, size_t return pMemory; } -void loader_instance_heap_free(const struct loader_instance *instance, void *pMemory) { - if (pMemory != NULL) { -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (instance && instance->alloc_callbacks.pfnFree) { - instance->alloc_callbacks.pfnFree(instance->alloc_callbacks.pUserData, pMemory); - } else { -#endif - free(pMemory); - } - } -} - -void *loader_instance_heap_realloc(const struct loader_instance *instance, void *pMemory, size_t orig_size, size_t size, - VkSystemAllocationScope alloc_scope) { - void *pNewMem = NULL; - if (pMemory == NULL || orig_size == 0) { - pNewMem = loader_instance_heap_alloc(instance, size, alloc_scope); - } else if (size == 0) { - loader_instance_heap_free(instance, pMemory); -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) -#else - } else if (instance && instance->alloc_callbacks.pfnReallocation) { - // These are internal structures, so it's best to align everything to - // the largest unit size which is the size of a uint64_t. - pNewMem = instance->alloc_callbacks.pfnReallocation(instance->alloc_callbacks.pUserData, pMemory, size, sizeof(uint64_t), - alloc_scope); -#endif - } else { - pNewMem = realloc(pMemory, size); - } - return pNewMem; -} - -void *loader_device_heap_alloc(const struct loader_device *device, size_t size, VkSystemAllocationScope alloc_scope) { +void *loader_calloc(const VkAllocationCallbacks *pAllocator, size_t size, VkSystemAllocationScope allocation_scope) { void *pMemory = NULL; #if (DEBUG_DISABLE_APP_ALLOCATORS == 1) { #else - if (device && device->alloc_callbacks.pfnAllocation) { + if (pAllocator && pAllocator->pfnAllocation) { // These are internal structures, so it's best to align everything to // the largest unit size which is the size of a uint64_t. - pMemory = device->alloc_callbacks.pfnAllocation(device->alloc_callbacks.pUserData, size, sizeof(uint64_t), alloc_scope); + pMemory = pAllocator->pfnAllocation(pAllocator->pUserData, size, sizeof(uint64_t), allocation_scope); + if (pMemory) { + memset(pMemory, 0, size); + } } else { #endif - pMemory = malloc(size); + pMemory = calloc(1, size); } + return pMemory; } -void loader_device_heap_free(const struct loader_device *device, void *pMemory) { +void loader_free(const VkAllocationCallbacks *pAllocator, void *pMemory) { if (pMemory != NULL) { #if (DEBUG_DISABLE_APP_ALLOCATORS == 1) { #else - if (device && device->alloc_callbacks.pfnFree) { - device->alloc_callbacks.pfnFree(device->alloc_callbacks.pUserData, pMemory); + if (pAllocator && pAllocator->pfnFree) { + pAllocator->pfnFree(pAllocator->pUserData, pMemory); } else { #endif free(pMemory); @@ -110,23 +82,75 @@ void loader_device_heap_free(const struct loader_device *device, void *pMemory) } } -void *loader_device_heap_realloc(const struct loader_device *device, void *pMemory, size_t orig_size, size_t size, - VkSystemAllocationScope alloc_scope) { +void *loader_realloc(const VkAllocationCallbacks *pAllocator, void *pMemory, size_t orig_size, size_t size, + VkSystemAllocationScope allocation_scope) { void *pNewMem = NULL; if (pMemory == NULL || orig_size == 0) { - pNewMem = loader_device_heap_alloc(device, size, alloc_scope); + pNewMem = loader_alloc(pAllocator, size, allocation_scope); } else if (size == 0) { - loader_device_heap_free(device, pMemory); + loader_free(pAllocator, pMemory); #if (DEBUG_DISABLE_APP_ALLOCATORS == 1) #else - } else if (device && device->alloc_callbacks.pfnReallocation) { + } else if (pAllocator && pAllocator->pfnReallocation) { // These are internal structures, so it's best to align everything to // the largest unit size which is the size of a uint64_t. - pNewMem = device->alloc_callbacks.pfnReallocation(device->alloc_callbacks.pUserData, pMemory, size, sizeof(uint64_t), - alloc_scope); + pNewMem = pAllocator->pfnReallocation(pAllocator->pUserData, pMemory, size, sizeof(uint64_t), allocation_scope); #endif } else { pNewMem = realloc(pMemory, size); } return pNewMem; -}
\ No newline at end of file +} + +void *loader_instance_heap_alloc(const struct loader_instance *inst, size_t size, VkSystemAllocationScope allocation_scope) { + return loader_alloc(inst ? &inst->alloc_callbacks : NULL, size, allocation_scope); +} + +void *loader_instance_heap_calloc(const struct loader_instance *inst, size_t size, VkSystemAllocationScope allocation_scope) { + return loader_calloc(inst ? &inst->alloc_callbacks : NULL, size, allocation_scope); +} + +void loader_instance_heap_free(const struct loader_instance *inst, void *pMemory) { + loader_free(inst ? &inst->alloc_callbacks : NULL, pMemory); +} +void *loader_instance_heap_realloc(const struct loader_instance *inst, void *pMemory, size_t orig_size, size_t size, + VkSystemAllocationScope allocation_scope) { + return loader_realloc(inst ? &inst->alloc_callbacks : NULL, pMemory, orig_size, size, allocation_scope); +} + +void *loader_device_heap_alloc(const struct loader_device *dev, size_t size, VkSystemAllocationScope allocation_scope) { + return loader_alloc(dev ? &dev->alloc_callbacks : NULL, size, allocation_scope); +} + +void *loader_device_heap_calloc(const struct loader_device *dev, size_t size, VkSystemAllocationScope allocation_scope) { + return loader_calloc(dev ? &dev->alloc_callbacks : NULL, size, allocation_scope); +} + +void loader_device_heap_free(const struct loader_device *dev, void *pMemory) { + loader_free(dev ? &dev->alloc_callbacks : NULL, pMemory); +} +void *loader_device_heap_realloc(const struct loader_device *dev, void *pMemory, size_t orig_size, size_t size, + VkSystemAllocationScope allocation_scope) { + return loader_realloc(dev ? &dev->alloc_callbacks : NULL, pMemory, orig_size, size, allocation_scope); +} + +void *loader_alloc_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *inst, size_t size, + VkSystemAllocationScope allocation_scope) { + return loader_alloc(NULL != pAllocator ? pAllocator : &inst->alloc_callbacks, size, allocation_scope); +} + +void *loader_calloc_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *instance, + size_t size, VkSystemAllocationScope allocation_scope) { + return loader_calloc(NULL != pAllocator ? pAllocator : &instance->alloc_callbacks, size, allocation_scope); +} + +void loader_free_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *instance, + void *pMemory) { + loader_free(NULL != pAllocator ? pAllocator : &instance->alloc_callbacks, pMemory); +} + +void *loader_realloc_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *instance, + void *pMemory, size_t orig_size, size_t size, + VkSystemAllocationScope allocation_scope) { + return loader_realloc(NULL != pAllocator ? pAllocator : &instance->alloc_callbacks, pMemory, orig_size, size, allocation_scope); +} diff --git a/loader/allocation.h b/loader/allocation.h index 6c84050f5..3f272d925 100644 --- a/loader/allocation.h +++ b/loader/allocation.h @@ -30,17 +30,38 @@ #include "loader_common.h" -void *loader_instance_heap_alloc(const struct loader_instance *instance, size_t size, VkSystemAllocationScope allocationScope); +void *loader_instance_heap_alloc(const struct loader_instance *instance, size_t size, VkSystemAllocationScope allocation_scope); +void *loader_instance_heap_calloc(const struct loader_instance *instance, size_t size, VkSystemAllocationScope allocation_scope); void loader_instance_heap_free(const struct loader_instance *instance, void *pMemory); void *loader_instance_heap_realloc(const struct loader_instance *instance, void *pMemory, size_t orig_size, size_t size, - VkSystemAllocationScope alloc_scope); + VkSystemAllocationScope allocation_scope); + void *loader_device_heap_alloc(const struct loader_device *device, size_t size, VkSystemAllocationScope allocationScope); +void *loader_device_heap_calloc(const struct loader_device *device, size_t size, VkSystemAllocationScope allocationScope); void loader_device_heap_free(const struct loader_device *device, void *pMemory); void *loader_device_heap_realloc(const struct loader_device *device, void *pMemory, size_t orig_size, size_t size, VkSystemAllocationScope alloc_scope); +// Wrappers around various memory functions. The loader will use the VkAllocationCallbacks functions if pAllocator is not NULL, +// otherwise use the system functions +void *loader_alloc(const VkAllocationCallbacks *pAllocator, size_t size, VkSystemAllocationScope allocation_scope); +void *loader_calloc(const VkAllocationCallbacks *pAllocator, size_t size, VkSystemAllocationScope allocation_scope); +void loader_free(const VkAllocationCallbacks *pAllocator, void *pMemory); +void *loader_realloc(const VkAllocationCallbacks *pAllocator, void *pMemory, size_t orig_size, size_t size, + VkSystemAllocationScope allocation_scope); + +// helper allocation functions that will use pAllocator and if it is NULL, fallback to the allocator of instance +void *loader_alloc_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *instance, + size_t size, VkSystemAllocationScope allocation_scope); +void *loader_calloc_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *instance, + size_t size, VkSystemAllocationScope allocation_scope); +void loader_free_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *instance, + void *pMemory); +void *loader_realloc_with_instance_fallback(const VkAllocationCallbacks *pAllocator, const struct loader_instance *instance, + void *pMemory, size_t orig_size, size_t size, VkSystemAllocationScope allocation_scope); + #if defined(__linux__) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__QNXNTO__) || defined(__FreeBSD__) #define loader_stack_alloc(size) alloca(size) #elif defined(_WIN32) #define loader_stack_alloc(size) _alloca(size) -#endif // defined(_WIN32)
\ No newline at end of file +#endif // defined(_WIN32) diff --git a/loader/debug_utils.c b/loader/debug_utils.c index 7944a5fe1..564f3d3ef 100644 --- a/loader/debug_utils.c +++ b/loader/debug_utils.c @@ -46,21 +46,12 @@ VkResult util_CreateDebugUtilsMessenger(struct loader_instance *inst, const VkDe const VkAllocationCallbacks *pAllocator, VkDebugUtilsMessengerEXT messenger) { VkLayerDbgFunctionNode *pNewDbgFuncNode = NULL; -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (pAllocator != NULL) { - pNewDbgFuncNode = (VkLayerDbgFunctionNode *)pAllocator->pfnAllocation(pAllocator->pUserData, sizeof(VkLayerDbgFunctionNode), - sizeof(int *), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); - } else { -#endif - pNewDbgFuncNode = (VkLayerDbgFunctionNode *)loader_instance_heap_alloc(inst, sizeof(VkLayerDbgFunctionNode), - VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); - } + pNewDbgFuncNode = (VkLayerDbgFunctionNode *)loader_calloc_with_instance_fallback( + pAllocator, inst, sizeof(VkLayerDbgFunctionNode), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); + if (!pNewDbgFuncNode) { return VK_ERROR_OUT_OF_HOST_MEMORY; } - memset(pNewDbgFuncNode, 0, sizeof(VkLayerDbgFunctionNode)); pNewDbgFuncNode->is_messenger = true; pNewDbgFuncNode->messenger.messenger = messenger; @@ -130,15 +121,7 @@ void util_DestroyDebugUtilsMessenger(struct loader_instance *inst, VkDebugUtilsM if (pTrav->is_messenger && pTrav->messenger.messenger == messenger) { pPrev->pNext = pTrav->pNext; if (inst->DbgFunctionHead == pTrav) inst->DbgFunctionHead = pTrav->pNext; -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (pAllocator != NULL) { - pAllocator->pfnFree(pAllocator->pUserData, pTrav); - } else { -#endif - loader_instance_heap_free(inst, pTrav); - } + loader_free_with_instance_fallback(pAllocator, inst, pTrav); break; } pPrev = pTrav; @@ -170,39 +153,19 @@ VkResult util_CopyDebugUtilsMessengerCreateInfos(const void *pChain, const VkAll return VK_SUCCESS; } -// 2nd, allocate memory for each VkDebugUtilsMessengerCreateInfoEXT: -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (pAllocator != NULL) { - pInfos = *infos = ((VkDebugUtilsMessengerCreateInfoEXT *)pAllocator->pfnAllocation( - pAllocator->pUserData, n * sizeof(VkDebugUtilsMessengerCreateInfoEXT), sizeof(void *), - VK_SYSTEM_ALLOCATION_SCOPE_OBJECT)); - } else { -#endif - pInfos = *infos = ((VkDebugUtilsMessengerCreateInfoEXT *)malloc(n * sizeof(VkDebugUtilsMessengerCreateInfoEXT))); - } + // 2nd, allocate memory for each VkDebugUtilsMessengerCreateInfoEXT: + pInfos = *infos = ((VkDebugUtilsMessengerCreateInfoEXT *)loader_alloc( + pAllocator, n * sizeof(VkDebugUtilsMessengerCreateInfoEXT), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT)); + if (!pInfos) { return VK_ERROR_OUT_OF_HOST_MEMORY; } -// 3rd, allocate memory for a unique handle for each callback: -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (pAllocator != NULL) { - pMessengers = *messengers = ((VkDebugUtilsMessengerEXT *)pAllocator->pfnAllocation( - pAllocator->pUserData, n * sizeof(VkDebugUtilsMessengerEXT), sizeof(void *), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT)); - if (NULL == pMessengers) { - pAllocator->pfnFree(pAllocator->pUserData, pInfos); - return VK_ERROR_OUT_OF_HOST_MEMORY; - } - } else { -#endif - pMessengers = *messengers = ((VkDebugUtilsMessengerEXT *)malloc(n * sizeof(VkDebugUtilsMessengerEXT))); - if (NULL == pMessengers) { - free(pInfos); - return VK_ERROR_OUT_OF_HOST_MEMORY; - } + // 3rd, allocate memory for a unique handle for each callback: + pMessengers = *messengers = ((VkDebugUtilsMessengerEXT *)loader_alloc(pAllocator, n * sizeof(VkDebugUtilsMessengerEXT), + VK_SYSTEM_ALLOCATION_SCOPE_OBJECT)); + if (NULL == pMessengers) { + loader_free(pAllocator, pInfos); + return VK_ERROR_OUT_OF_HOST_MEMORY; } // 4th, copy each VkDebugUtilsMessengerCreateInfoEXT for use by // vkDestroyInstance, and assign a unique handle to each messenger (just @@ -222,17 +185,8 @@ VkResult util_CopyDebugUtilsMessengerCreateInfos(const void *pChain, const VkAll void util_FreeDebugUtilsMessengerCreateInfos(const VkAllocationCallbacks *pAllocator, VkDebugUtilsMessengerCreateInfoEXT *infos, VkDebugUtilsMessengerEXT *messengers) { -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (pAllocator != NULL) { - pAllocator->pfnFree(pAllocator->pUserData, infos); - pAllocator->pfnFree(pAllocator->pUserData, messengers); - } else { -#endif - free(infos); - free(messengers); - } + loader_free(pAllocator, infos); + loader_free(pAllocator, messengers); } VkResult util_CreateDebugUtilsMessengers(struct loader_instance *inst, const VkAllocationCallbacks *pAllocator, @@ -288,20 +242,9 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDebugUtilsMessengerEXT(VkInstanc uint32_t storage_idx; VkLayerDbgFunctionNode *pNewDbgFuncNode = NULL; -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (pAllocator != NULL) { - icd_info = ((VkDebugUtilsMessengerEXT *)pAllocator->pfnAllocation(pAllocator->pUserData, - inst->total_icd_count * sizeof(VkDebugUtilsMessengerEXT), - sizeof(void *), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT)); - if (icd_info) { - memset(icd_info, 0, inst->total_icd_count * sizeof(VkDebugUtilsMessengerEXT)); - } - } else { -#endif - icd_info = calloc(sizeof(VkDebugUtilsMessengerEXT), inst->total_icd_count); - } + icd_info = (VkDebugUtilsMessengerEXT *)loader_calloc_with_instance_fallback( + pAllocator, inst, inst->total_icd_count * sizeof(VkDebugUtilsMessengerEXT), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); + if (!icd_info) { res = VK_ERROR_OUT_OF_HOST_MEMORY; goto out; @@ -321,25 +264,15 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDebugUtilsMessengerEXT(VkInstanc storage_idx++; } -// Setup the debug report callback in the terminator since a layer may want -// to grab the information itself (RenderDoc) and then return back to the -// user callback a sub-set of the messages. -#if (DEBUG_DISABLE_APP_ALLOCATORS == 0) - if (pAllocator != NULL) { - pNewDbgFuncNode = (VkLayerDbgFunctionNode *)pAllocator->pfnAllocation(pAllocator->pUserData, sizeof(VkLayerDbgFunctionNode), - sizeof(int *), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); - } else { -#else - { -#endif - pNewDbgFuncNode = (VkLayerDbgFunctionNode *)loader_instance_heap_alloc(inst, sizeof(VkLayerDbgFunctionNode), - VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); - } + // Setup the debug report callback in the terminator since a layer may want + // to grab the information itself (RenderDoc) and then return back to the + // user callback a sub-set of the messages. + pNewDbgFuncNode = (VkLayerDbgFunctionNode *)loader_calloc_with_instance_fallback( + pAllocator, inst, sizeof(VkLayerDbgFunctionNode), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); if (!pNewDbgFuncNode) { res = VK_ERROR_OUT_OF_HOST_MEMORY; goto out; } - memset(pNewDbgFuncNode, 0, sizeof(VkLayerDbgFunctionNode)); pNewDbgFuncNode->is_messenger = true; pNewDbgFuncNode->messenger.pfnUserCallback = pCreateInfo->pfnUserCallback; @@ -367,26 +300,8 @@ out: } storage_idx++; } - -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (pAllocator != NULL) { - if (NULL != pNewDbgFuncNode) { - pAllocator->pfnFree(pAllocator->pUserData, pNewDbgFuncNode); - } - if (NULL != icd_info) { - pAllocator->pfnFree(pAllocator->pUserData, icd_info); - } - } else { -#endif - if (NULL != pNewDbgFuncNode) { - free(pNewDbgFuncNode); - } - if (NULL != icd_info) { - free(icd_info); - } - } + loader_free(pAllocator, pNewDbgFuncNode); + loader_free(pAllocator, icd_info); } return res; @@ -415,15 +330,7 @@ VKAPI_ATTR void VKAPI_CALL terminator_DestroyDebugUtilsMessengerEXT(VkInstance i util_DestroyDebugUtilsMessenger(inst, messenger, pAllocator); -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (pAllocator != NULL) { - pAllocator->pfnFree(pAllocator->pUserData, icd_info); - } else { -#endif - free(icd_info); - } + loader_free(pAllocator, icd_info); } // This is the instance chain terminator function for SubmitDebugUtilsMessageEXT @@ -447,21 +354,11 @@ VkResult util_CreateDebugReportCallback(struct loader_instance *inst, VkDebugRep const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT callback) { VkLayerDbgFunctionNode *pNewDbgFuncNode = NULL; -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (pAllocator != NULL) { - pNewDbgFuncNode = (VkLayerDbgFunctionNode *)pAllocator->pfnAllocation(pAllocator->pUserData, sizeof(VkLayerDbgFunctionNode), - sizeof(int *), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); - } else { -#endif - pNewDbgFuncNode = (VkLayerDbgFunctionNode *)loader_instance_heap_alloc(inst, sizeof(VkLayerDbgFunctionNode), - VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); - } + pNewDbgFuncNode = (VkLayerDbgFunctionNode *)loader_calloc_with_instance_fallback( + pAllocator, inst, sizeof(VkLayerDbgFunctionNode), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); if (!pNewDbgFuncNode) { return VK_ERROR_OUT_OF_HOST_MEMORY; } - memset(pNewDbgFuncNode, 0, sizeof(VkLayerDbgFunctionNode)); pNewDbgFuncNode->is_messenger = false; pNewDbgFuncNode->report.msgCallback = callback; @@ -538,15 +435,7 @@ void util_DestroyDebugReportCallback(struct loader_instance *inst, VkDebugReport if (!pTrav->is_messenger && pTrav->report.msgCallback == callback) { pPrev->pNext = pTrav->pNext; if (inst->DbgFunctionHead == pTrav) inst->DbgFunctionHead = pTrav->pNext; -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (pAllocator != NULL) { - pAllocator->pfnFree(pAllocator->pUserData, pTrav); - } else { -#endif - loader_instance_heap_free(inst, pTrav); - } + loader_free_with_instance_fallback(pAllocator, inst, pTrav); break; } pPrev = pTrav; @@ -577,40 +466,21 @@ VkResult util_CopyDebugReportCreateInfos(const void *pChain, const VkAllocationC return VK_SUCCESS; } -// 2nd, allocate memory for each VkDebugReportCallbackCreateInfoEXT: -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (pAllocator != NULL) { - pInfos = *infos = ((VkDebugReportCallbackCreateInfoEXT *)pAllocator->pfnAllocation( - pAllocator->pUserData, n * sizeof(VkDebugReportCallbackCreateInfoEXT), sizeof(void *), - VK_SYSTEM_ALLOCATION_SCOPE_OBJECT)); - } else { -#endif - pInfos = *infos = ((VkDebugReportCallbackCreateInfoEXT *)malloc(n * sizeof(VkDebugReportCallbackCreateInfoEXT))); - } + // 2nd, allocate memory for each VkDebugReportCallbackCreateInfoEXT: + pInfos = *infos = ((VkDebugReportCallbackCreateInfoEXT *)loader_alloc( + pAllocator, n * sizeof(VkDebugReportCallbackCreateInfoEXT), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT)); if (!pInfos) { return VK_ERROR_OUT_OF_HOST_MEMORY; } -// 3rd, allocate memory for a unique handle for each callback: -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (pAllocator != NULL) { - pCallbacks = *callbacks = ((VkDebugReportCallbackEXT *)pAllocator->pfnAllocation( - pAllocator->pUserData, n * sizeof(VkDebugReportCallbackEXT), sizeof(void *), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT)); - if (!pCallbacks) { - pAllocator->pfnFree(pAllocator->pUserData, pInfos); - return VK_ERROR_OUT_OF_HOST_MEMORY; - } - } else { -#endif - pCallbacks = *callbacks = ((VkDebugReportCallbackEXT *)malloc(n * sizeof(VkDebugReportCallbackEXT))); - if (!pCallbacks) { - free(pInfos); - return VK_ERROR_OUT_OF_HOST_MEMORY; - } + // 3rd, allocate memory for a unique handle for each callback: + + pCallbacks = *callbacks = ((VkDebugReportCallbackEXT *)loader_alloc(pAllocator, n * sizeof(VkDebugReportCallbackEXT), + VK_SYSTEM_ALLOCATION_SCOPE_OBJECT)); + if (!pCallbacks) { + loader_free(pAllocator, pInfos); + return VK_ERROR_OUT_OF_HOST_MEMORY; } + // 4th, copy each VkDebugReportCallbackCreateInfoEXT for use by // vkDestroyInstance, and assign a unique handle to each callback (just // use the address of the copied VkDebugReportCallbackCreateInfoEXT): @@ -629,17 +499,8 @@ VkResult util_CopyDebugReportCreateInfos(const void *pChain, const VkAllocationC void util_FreeDebugReportCreateInfos(const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) { -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (pAllocator != NULL) { - pAllocator->pfnFree(pAllocator->pUserData, infos); - pAllocator->pfnFree(pAllocator->pUserData, callbacks); - } else { -#endif - free(infos); - free(callbacks); - } + loader_free(pAllocator, infos); + loader_free(pAllocator, callbacks); } VkResult util_CreateDebugReportCallbacks(struct loader_instance *inst, const VkAllocationCallbacks *pAllocator, @@ -681,7 +542,8 @@ static VKAPI_ATTR void VKAPI_CALL debug_utils_DebugReportMessageEXT(VkInstance i const char *pMsg) { struct loader_instance *inst = loader_get_instance(instance); - inst->disp->layer_inst_disp.DebugReportMessageEXT(inst->instance, flags, objType, object, location, msgCode, pLayerPrefix, pMsg); + inst->disp->layer_inst_disp.DebugReportMessageEXT(inst->instance, flags, objType, object, location, msgCode, pLayerPrefix, + pMsg); } // This is the instance chain terminator function @@ -697,20 +559,8 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDebugReportCallbackEXT(VkInstanc uint32_t storage_idx; VkLayerDbgFunctionNode *pNewDbgFuncNode = NULL; -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (pAllocator != NULL) { - icd_info = ((VkDebugReportCallbackEXT *)pAllocator->pfnAllocation(pAllocator->pUserData, - inst->total_icd_count * sizeof(VkDebugReportCallbackEXT), - sizeof(void *), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT)); - if (icd_info) { - memset(icd_info, 0, inst->total_icd_count * sizeof(VkDebugReportCallbackEXT)); - } - } else { -#endif - icd_info = calloc(sizeof(VkDebugReportCallbackEXT), inst->total_icd_count); - } + icd_info = ((VkDebugReportCallbackEXT *)loader_calloc(pAllocator, inst->total_icd_count * sizeof(VkDebugReportCallbackEXT), + VK_SYSTEM_ALLOCATION_SCOPE_OBJECT)); if (!icd_info) { res = VK_ERROR_OUT_OF_HOST_MEMORY; goto out; @@ -730,25 +580,16 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDebugReportCallbackEXT(VkInstanc storage_idx++; } -// Setup the debug report callback in the terminator since a layer may want -// to grab the information itself (RenderDoc) and then return back to the -// user callback a sub-set of the messages. -#if (DEBUG_DISABLE_APP_ALLOCATORS == 0) - if (pAllocator != NULL) { - pNewDbgFuncNode = (VkLayerDbgFunctionNode *)pAllocator->pfnAllocation(pAllocator->pUserData, sizeof(VkLayerDbgFunctionNode), - sizeof(int *), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); - } else { -#else - { -#endif - pNewDbgFuncNode = (VkLayerDbgFunctionNode *)loader_instance_heap_alloc(inst, sizeof(VkLayerDbgFunctionNode), - VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); - } + // Setup the debug report callback in the terminator since a layer may want + // to grab the information itself (RenderDoc) and then return back to the + // user callback a sub-set of the messages. + pNewDbgFuncNode = (VkLayerDbgFunctionNode *)loader_calloc_with_instance_fallback( + pAllocator, inst, sizeof(VkLayerDbgFunctionNode), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); + if (!pNewDbgFuncNode) { res = VK_ERROR_OUT_OF_HOST_MEMORY; goto out; } - memset(pNewDbgFuncNode, 0, sizeof(VkLayerDbgFunctionNode)); pNewDbgFuncNode->is_messenger = false; pNewDbgFuncNode->report.pfnMsgCallback = pCreateInfo->pfnCallback; @@ -775,26 +616,8 @@ out: } storage_idx++; } - -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (pAllocator != NULL) { - if (NULL != pNewDbgFuncNode) { - pAllocator->pfnFree(pAllocator->pUserData, pNewDbgFuncNode); - } - if (NULL != icd_info) { - pAllocator->pfnFree(pAllocator->pUserData, icd_info); - } - } else { -#endif - if (NULL != pNewDbgFuncNode) { - free(pNewDbgFuncNode); - } - if (NULL != icd_info) { - free(icd_info); - } - } + loader_free(pAllocator, pNewDbgFuncNode); + loader_free(pAllocator, icd_info); } return res; @@ -823,15 +646,7 @@ VKAPI_ATTR void VKAPI_CALL terminator_DestroyDebugReportCallbackEXT(VkInstance i util_DestroyDebugReportCallback(inst, callback, pAllocator); -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (pAllocator != NULL) { - pAllocator->pfnFree(pAllocator->pUserData, icd_info); - } else { -#endif - free(icd_info); - } + loader_free(pAllocator, icd_info); } // This is the instance chain terminator function for DebugReportMessage diff --git a/loader/get_environment.c b/loader/get_environment.c index 10a3ddf40..a389a9333 100644 --- a/loader/get_environment.c +++ b/loader/get_environment.c @@ -28,6 +28,7 @@ #include "get_environment.h" +#include "allocation.h" #include "log.h" // Environment variables @@ -114,13 +115,7 @@ char *loader_getenv(const char *name, const struct loader_instance *inst) { // will always be at least 1. If it's 0, the variable wasn't set. if (valSize == 0) return NULL; - // Allocate the space necessary for the registry entry - if (NULL != inst && NULL != inst->alloc_callbacks.pfnAllocation) { - retVal = (char *)inst->alloc_callbacks.pfnAllocation(inst->alloc_callbacks.pUserData, valSize, sizeof(char *), - VK_SYSTEM_ALLOCATION_SCOPE_COMMAND); - } else { - retVal = (char *)malloc(valSize); - } + retVal = loader_instance_heap_alloc(inst, valSize, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND); if (NULL != retVal) { GetEnvironmentVariableA(name, retVal, valSize); @@ -141,13 +136,7 @@ char *loader_secure_getenv(const char *name, const struct loader_instance *inst) return loader_getenv(name, inst); } -void loader_free_getenv(char *val, const struct loader_instance *inst) { - if (NULL != inst && NULL != inst->alloc_callbacks.pfnFree) { - inst->alloc_callbacks.pfnFree(inst->alloc_callbacks.pUserData, val); - } else { - free((void *)val); - } -} +void loader_free_getenv(char *val, const struct loader_instance *inst) { loader_instance_heap_free(inst, (void *)val); } #else @@ -163,4 +152,4 @@ void loader_free_getenv(char *val, const struct loader_instance *inst) { (void)inst; } -#endif
\ No newline at end of file +#endif diff --git a/loader/loader.c b/loader/loader.c index 4447fb52d..597689f28 100644 --- a/loader/loader.c +++ b/loader/loader.c @@ -1218,23 +1218,13 @@ void loader_destroy_logical_device(const struct loader_instance *inst, struct lo struct loader_device *loader_create_logical_device(const struct loader_instance *inst, const VkAllocationCallbacks *pAllocator) { struct loader_device *new_dev; -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (pAllocator) { - new_dev = (struct loader_device *)pAllocator->pfnAllocation(pAllocator->pUserData, sizeof(struct loader_device), - sizeof(int *), VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); - } else { -#endif - new_dev = (struct loader_device *)malloc(sizeof(struct loader_device)); - } + new_dev = loader_calloc(pAllocator, sizeof(struct loader_device), VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); if (!new_dev) { loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_create_logical_device: Failed to alloc struct loader_device"); return NULL; } - memset(new_dev, 0, sizeof(struct loader_device)); if (pAllocator) { new_dev->alloc_callbacks = *pAllocator; } @@ -1881,10 +1871,8 @@ static void remove_all_non_valid_override_layers(struct loader_instance *inst, s } } if (!found_active_override_layer) { - loader_log( - inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_LAYER_BIT, 0, - "--Override layer found but not used because app \'%s\' is not in \'app_keys\' list!", - cur_path); + loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_LAYER_BIT, 0, + "--Override layer found but not used because app \'%s\' is not in \'app_keys\' list!", cur_path); // Remove non-global override layers that don't have an app_key that matches cur_path loader_remove_layer_in_list(inst, instance_layers, i); diff --git a/loader/trampoline.c b/loader/trampoline.c index c1a9bd18e..4f34b84e4 100644 --- a/loader/trampoline.c +++ b/loader/trampoline.c @@ -447,16 +447,8 @@ LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateInstance(const VkInstanceCr goto out; } -#if (DEBUG_DISABLE_APP_ALLOCATORS == 1) - { -#else - if (pAllocator) { - ptr_instance = (struct loader_instance *)pAllocator->pfnAllocation(pAllocator->pUserData, sizeof(struct loader_instance), - sizeof(int *), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); - } else { -#endif - ptr_instance = (struct loader_instance *)malloc(sizeof(struct loader_instance)); - } + ptr_instance = + (struct loader_instance *)loader_alloc(pAllocator, sizeof(struct loader_instance), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); VkInstanceCreateInfo ici = *pCreateInfo; diff --git a/loader/vk_loader_platform.h b/loader/vk_loader_platform.h index 594a24986..62f74312e 100644 --- a/loader/vk_loader_platform.h +++ b/loader/vk_loader_platform.h @@ -85,9 +85,6 @@ #define LOADER_EXPORT #endif -// A debug option to disable allocators at compile time to investigate future issues. -#define DEBUG_DISABLE_APP_ALLOCATORS 0 - #define MAX_STRING_SIZE 1024 // This is defined in vk_layer.h, but if there's problems we need to create the define |