loader: Make use of app provided allocation callbacks

Convert all heap allocs/frees to use loader_heap_alloc() or loader_heap_free().
Before CreateInstance this will use malloc/free. At (and after) CreateInstance
alloc callbacks are stored and used for any allocations.

Exceptions are thirdparty code (cJSON  and dirent_on_windows) still always use
malloc/free. Plan to address these in later patch if ti makes sense.
This commit is contained in:
Jon Ashburn 2015-08-28 13:38:21 -06:00
parent 4bde9c4b34
commit 8fd9081ab4
7 changed files with 362 additions and 206 deletions

View File

@ -46,9 +46,10 @@ static const VkExtensionProperties debug_report_extension_info = {
};
void debug_report_add_instance_extensions(
const struct loader_instance *inst,
struct loader_extension_list *ext_list)
{
loader_add_to_ext_list(ext_list, 1, &debug_report_extension_info);
loader_add_to_ext_list(inst, ext_list, 1, &debug_report_extension_info);
}
void debug_report_create_instance(
@ -72,7 +73,7 @@ static VkResult debug_report_DbgCreateMsgCallback(
void* pUserData,
VkDbgMsgCallback* pMsgCallback)
{
VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *) malloc(sizeof(VkLayerDbgFunctionNode));
VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *) loader_heap_alloc((struct loader_instance *)instance, sizeof(VkLayerDbgFunctionNode), VK_SYSTEM_ALLOC_TYPE_INTERNAL);
if (!pNewDbgFuncNode)
return VK_ERROR_OUT_OF_HOST_MEMORY;
@ -87,7 +88,7 @@ static VkResult debug_report_DbgCreateMsgCallback(
pNewDbgFuncNode->pNext = inst->DbgFunctionHead;
inst->DbgFunctionHead = pNewDbgFuncNode;
} else {
free(pNewDbgFuncNode);
loader_heap_free((struct loader_instance *) instance, pNewDbgFuncNode);
}
loader_platform_thread_unlock_mutex(&loader_lock);
return result;
@ -109,7 +110,7 @@ static VkResult debug_report_DbgDestroyMsgCallback(
pPrev->pNext = pTrav->pNext;
if (inst->DbgFunctionHead == pTrav)
inst->DbgFunctionHead = pTrav->pNext;
free(pTrav);
loader_heap_free((struct loader_instance *) instance, pTrav);
break;
}
pPrev = pTrav;

View File

@ -91,6 +91,7 @@
*/
void debug_report_add_instance_extensions(
const struct loader_instance *inst,
struct loader_extension_list *ext_list);
void debug_report_create_instance(

File diff suppressed because it is too large Load Diff

View File

@ -193,6 +193,7 @@ struct loader_struct {
struct loader_instance *instances;
unsigned int loaded_layer_lib_count;
unsigned int loaded_layer_lib_capacity;
struct loader_lib_info *loaded_layer_lib_list;
// TODO add ref counting of ICD libraries
char *layer_dirs;
@ -345,28 +346,44 @@ bool has_vk_extension_property(
const struct loader_extension_list *ext_list);
void loader_add_to_ext_list(
const struct loader_instance *inst,
struct loader_extension_list *ext_list,
uint32_t prop_list_count,
const VkExtensionProperties *props);
void loader_destroy_ext_list(struct loader_extension_list *ext_info);
void loader_delete_layer_properties(struct loader_layer_list *layer_list);
void loader_destroy_ext_list(
const struct loader_instance *inst,
struct loader_extension_list *ext_info);
void loader_delete_layer_properties(
const struct loader_instance *inst,
struct loader_layer_list *layer_list);
void loader_add_to_layer_list(
const struct loader_instance *inst,
struct loader_layer_list *list,
uint32_t prop_list_count,
const struct loader_layer_properties *props);
void loader_scanned_icd_clear(struct loader_icd_libs *icd_libs);
void loader_icd_scan(struct loader_icd_libs *icds);
void loader_scanned_icd_clear(
const struct loader_instance *inst,
struct loader_icd_libs *icd_libs);
void loader_icd_scan(
const struct loader_instance *inst,
struct loader_icd_libs *icds);
void loader_layer_scan(
const struct loader_instance *inst,
struct loader_layer_list *instance_layers,
struct loader_layer_list *device_layers);
void loader_get_icd_loader_instance_extensions(
const struct loader_instance *inst,
struct loader_icd_libs *icd_libs,
struct loader_extension_list *inst_exts);
struct loader_icd *loader_get_icd_and_device(
const VkDevice device,
struct loader_device **found_dev);
struct loader_icd * loader_get_icd(
const VkPhysicalDevice gpu,
uint32_t *gpu_index);
void loader_remove_logical_device(VkDevice device);
void loader_remove_logical_device(
const struct loader_instance *inst,
VkDevice device);
VkResult loader_enable_instance_layers(
struct loader_instance *inst,
const VkInstanceCreateInfo *pCreateInfo,
@ -376,17 +393,17 @@ uint32_t loader_activate_instance_layers(struct loader_instance *inst);
void loader_activate_instance_layer_extensions(struct loader_instance *inst);
void* loader_heap_alloc(
struct loader_instance *instance,
const struct loader_instance *instance,
size_t size,
VkSystemAllocType allocType);
void* loader_aligned_heap_alloc(
struct loader_instance *instance,
const struct loader_instance *instance,
size_t size,
size_t alignment,
VkSystemAllocType allocType);
void loader_heap_free(
struct loader_instance *instance,
void *pMem);
const struct loader_instance *instance,
void *pMem);
#endif /* LOADER_H */

View File

@ -44,6 +44,8 @@ LOADER_EXPORT VkResult VKAPI vkCreateInstance(
struct loader_instance *ptr_instance = NULL;
VkResult res = VK_ERROR_INITIALIZATION_FAILED;
if (pCreateInfo == NULL)
return VK_ERROR_INVALID_POINTER;
loader_platform_thread_once(&once_init, loader_initialize);
if (pCreateInfo->pAllocCb
@ -77,7 +79,11 @@ LOADER_EXPORT VkResult VKAPI vkCreateInstance(
* get layer list (both instance and device) via loader_layer_scan(). */
memset(&ptr_instance->instance_layer_list, 0, sizeof(ptr_instance->instance_layer_list));
memset(&ptr_instance->device_layer_list, 0, sizeof(ptr_instance->device_layer_list));
loader_layer_scan(&ptr_instance->instance_layer_list, &ptr_instance->device_layer_list);
loader_layer_scan(ptr_instance,
&ptr_instance->instance_layer_list,
&ptr_instance->device_layer_list);
/* validate the app requested layers to be enabled */
if (pCreateInfo->layerCount > 0) {
@ -91,16 +97,22 @@ LOADER_EXPORT VkResult VKAPI vkCreateInstance(
/* Scan/discover all ICD libraries */
memset(&ptr_instance->icd_libs, 0, sizeof(ptr_instance->icd_libs));
loader_icd_scan(&ptr_instance->icd_libs);
loader_icd_scan(ptr_instance, &ptr_instance->icd_libs);
/* get extensions from all ICD's, merge so no duplicates, then validate */
loader_get_icd_loader_instance_extensions(&ptr_instance->icd_libs, &ptr_instance->ext_list);
res = loader_validate_instance_extensions(&ptr_instance->ext_list, &ptr_instance->instance_layer_list, pCreateInfo);
loader_get_icd_loader_instance_extensions(ptr_instance,
&ptr_instance->icd_libs,
&ptr_instance->ext_list);
res = loader_validate_instance_extensions(&ptr_instance->ext_list,
&ptr_instance->instance_layer_list,
pCreateInfo);
if (res != VK_SUCCESS) {
loader_delete_layer_properties(&ptr_instance->device_layer_list);
loader_delete_layer_properties(&ptr_instance->instance_layer_list);
loader_scanned_icd_clear(&ptr_instance->icd_libs);
loader_destroy_ext_list(&ptr_instance->ext_list);
loader_delete_layer_properties(ptr_instance,
&ptr_instance->device_layer_list);
loader_delete_layer_properties(ptr_instance,
&ptr_instance->instance_layer_list);
loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_libs);
loader_destroy_ext_list(ptr_instance, &ptr_instance->ext_list);
loader_platform_thread_unlock_mutex(&loader_lock);
loader_heap_free(ptr_instance, ptr_instance);
return res;
@ -111,10 +123,14 @@ LOADER_EXPORT VkResult VKAPI vkCreateInstance(
sizeof(VkLayerInstanceDispatchTable),
VK_SYSTEM_ALLOC_TYPE_INTERNAL);
if (ptr_instance->disp == NULL) {
loader_delete_layer_properties(&ptr_instance->device_layer_list);
loader_delete_layer_properties(&ptr_instance->instance_layer_list);
loader_scanned_icd_clear(&ptr_instance->icd_libs);
loader_destroy_ext_list(&ptr_instance->ext_list);
loader_delete_layer_properties(ptr_instance,
&ptr_instance->device_layer_list);
loader_delete_layer_properties(ptr_instance,
&ptr_instance->instance_layer_list);
loader_scanned_icd_clear(ptr_instance,
&ptr_instance->icd_libs);
loader_destroy_ext_list(ptr_instance,
&ptr_instance->ext_list);
loader_platform_thread_unlock_mutex(&loader_lock);
loader_heap_free(ptr_instance, ptr_instance);
return VK_ERROR_OUT_OF_HOST_MEMORY;
@ -124,12 +140,18 @@ LOADER_EXPORT VkResult VKAPI vkCreateInstance(
loader.instances = ptr_instance;
/* activate any layers on instance chain */
res = loader_enable_instance_layers(ptr_instance, pCreateInfo, &ptr_instance->instance_layer_list);
res = loader_enable_instance_layers(ptr_instance,
pCreateInfo,
&ptr_instance->instance_layer_list);
if (res != VK_SUCCESS) {
loader_delete_layer_properties(&ptr_instance->device_layer_list);
loader_delete_layer_properties(&ptr_instance->instance_layer_list);
loader_scanned_icd_clear(&ptr_instance->icd_libs);
loader_destroy_ext_list(&ptr_instance->ext_list);
loader_delete_layer_properties(ptr_instance,
&ptr_instance->device_layer_list);
loader_delete_layer_properties(ptr_instance,
&ptr_instance->instance_layer_list);
loader_scanned_icd_clear(ptr_instance,
&ptr_instance->icd_libs);
loader_destroy_ext_list(ptr_instance,
&ptr_instance->ext_list);
loader.instances = ptr_instance->next;
loader_platform_thread_unlock_mutex(&loader_lock);
loader_heap_free(ptr_instance, ptr_instance->disp);
@ -303,12 +325,14 @@ LOADER_EXPORT VkResult VKAPI vkDestroyDevice(VkDevice device)
{
const VkLayerDispatchTable *disp;
VkResult res;
struct loader_device *dev;
struct loader_icd *icd = loader_get_icd_and_device(device, &dev);
const struct loader_instance *inst = icd->this_instance;
disp = loader_get_dispatch(device);
loader_platform_thread_lock_mutex(&loader_lock);
res = disp->DestroyDevice(device);
loader_remove_logical_device(device);
loader_remove_logical_device(inst, device);
loader_platform_thread_unlock_mutex(&loader_lock);
return res;
}

View File

@ -40,9 +40,10 @@ static const VkExtensionProperties wsi_swapchain_extension_info = {
};
void wsi_swapchain_add_instance_extensions(
const struct loader_instance *inst,
struct loader_extension_list *ext_list)
{
loader_add_to_ext_list(ext_list, 1, &wsi_swapchain_extension_info);
loader_add_to_ext_list(inst, ext_list, 1, &wsi_swapchain_extension_info);
}
void wsi_swapchain_create_instance(

View File

@ -32,6 +32,7 @@
#include "vk_ext_khr_swapchain.h"
void wsi_swapchain_add_instance_extensions(
const struct loader_instance *inst,
struct loader_extension_list *ext_list);
void wsi_swapchain_create_instance(