loader: GH370 - re-enable allocator usage
authorMark Young <marky@lunarg.com>
Thu, 30 Jun 2016 00:33:53 +0000 (18:33 -0600)
committerMark Young <marky@lunarg.com>
Thu, 30 Jun 2016 00:45:22 +0000 (18:45 -0600)
Re-enable reverted changes done in commit
d6f491b88eaf11f6953c02638d079f6a76806658.  But also include
fixes for the Linux and Windows release runs.

Change-Id: I7644bb305faab068b3229eb9c1d8a67b052af165

loader/cJSON.c
loader/cJSON.h
loader/debug_report.c
loader/dirent_on_windows.c
loader/loader.c
loader/loader.h
loader/trampoline.c
loader/vk_loader_platform.h
loader/wsi.c

index fc3ba04..e7266c3 100644 (file)
@@ -88,6 +88,10 @@ void cJSON_Delete(cJSON *c) {
     }
 }
 
+void cJSON_Free(void *p) {
+    cJSON_free(p);
+}
+
 /* Parse the input text to generate a number, and populate the result into item.
  */
 static const char *parse_number(cJSON *item, const char *num) {
index c6a57be..cab3051 100644 (file)
@@ -84,6 +84,8 @@ extern char *cJSON_PrintUnformatted(cJSON *item);
 extern char *cJSON_PrintBuffered(cJSON *item, int prebuffer, int fmt);
 /* Delete a cJSON entity and all subentities. */
 extern void cJSON_Delete(cJSON *c);
+/* Delete an item allocated inside the JSON parser*/
+extern void cJSON_Free(void *p);
 
 /* Returns the number of items in an array (or object). */
 extern int cJSON_GetArraySize(cJSON *array);
index c339c70..4da1413 100644 (file)
@@ -66,12 +66,16 @@ util_CreateDebugReportCallback(struct loader_instance *inst,
                                const VkAllocationCallbacks *pAllocator,
                                VkDebugReportCallbackEXT callback) {
     VkLayerDbgFunctionNode *pNewDbgFuncNode;
+#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
+    {
+#else
     if (pAllocator != NULL) {
         pNewDbgFuncNode = (VkLayerDbgFunctionNode *)pAllocator->pfnAllocation(
             pAllocator->pUserData, sizeof(VkLayerDbgFunctionNode),
-            sizeof(int *), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+            sizeof(int *), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
     } else {
-        pNewDbgFuncNode = (VkLayerDbgFunctionNode *)loader_heap_alloc(
+#endif
+        pNewDbgFuncNode = (VkLayerDbgFunctionNode *)loader_instance_heap_alloc(
             inst, sizeof(VkLayerDbgFunctionNode),
             VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
     }
@@ -137,10 +141,14 @@ void util_DestroyDebugReportCallback(struct loader_instance *inst,
             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 {
-                loader_heap_free(inst, pTrav);
+#endif
+                loader_instance_heap_free(inst, pTrav);
             }
             break;
         }
@@ -159,6 +167,8 @@ VkResult util_CopyDebugReportCreateInfos(
     uint32_t *num_callbacks, VkDebugReportCallbackCreateInfoEXT **infos,
     VkDebugReportCallbackEXT **callbacks) {
     uint32_t n = *num_callbacks = 0;
+    VkDebugReportCallbackCreateInfoEXT *pInfos = NULL;
+    VkDebugReportCallbackEXT *pCallbacks = NULL;
 
     // NOTE: The loader is not using pAllocator, and so this function doesn't
     // either.
@@ -176,17 +186,38 @@ VkResult util_CopyDebugReportCreateInfos(
         return VK_SUCCESS;
     }
 
-    // 2nd, allocate memory for each VkDebugReportCallbackCreateInfoEXT:
-    VkDebugReportCallbackCreateInfoEXT *pInfos = *infos =
-        ((VkDebugReportCallbackCreateInfoEXT *)malloc(
+// 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)));
+    }
     if (!pInfos) {
         return VK_ERROR_OUT_OF_HOST_MEMORY;
     }
-    // 3rd, allocate memory for a unique handle for each callback:
-    VkDebugReportCallbackEXT *pCallbacks = *callbacks =
-        ((VkDebugReportCallbackEXT *)malloc(n *
-                                            sizeof(VkDebugReportCallbackEXT)));
+// 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));
+    } else {
+#endif
+        pCallbacks = *callbacks = ((VkDebugReportCallbackEXT *)malloc(
+            n * sizeof(VkDebugReportCallbackEXT)));
+    }
     if (!pCallbacks) {
         free(pInfos);
         return VK_ERROR_OUT_OF_HOST_MEMORY;
@@ -281,7 +312,21 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDebugReportCallback(
     VkResult res = VK_SUCCESS;
     uint32_t storage_idx;
 
-    icd_info = calloc(sizeof(VkDebugReportCallbackEXT), inst->total_icd_count);
+#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));
+        memset(icd_info, 0,
+               inst->total_icd_count * sizeof(VkDebugReportCallbackEXT));
+    } else {
+#endif
+        icd_info =
+            calloc(sizeof(VkDebugReportCallbackEXT), inst->total_icd_count);
+    }
     if (!icd_info) {
         return VK_ERROR_OUT_OF_HOST_MEMORY;
     }
@@ -328,10 +373,9 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDebugReportCallback(
  * This is the instance chain terminator function
  * for DestroyDebugReportCallback
  */
-VKAPI_ATTR void VKAPI_CALL
-terminator_DestroyDebugReportCallback(VkInstance instance,
-                                      VkDebugReportCallbackEXT callback,
-                                      const VkAllocationCallbacks *pAllocator) {
+VKAPI_ATTR void VKAPI_CALL terminator_DestroyDebugReportCallback(
+    VkInstance instance, VkDebugReportCallbackEXT callback,
+    const VkAllocationCallbacks *pAllocator) {
     uint32_t storage_idx;
     VkDebugReportCallbackEXT *icd_info;
     const struct loader_icd *icd;
@@ -356,11 +400,10 @@ terminator_DestroyDebugReportCallback(VkInstance instance,
  * This is the instance chain terminator function
  * for DebugReportMessage
  */
-VKAPI_ATTR void VKAPI_CALL
-terminator_DebugReportMessage(VkInstance instance, VkDebugReportFlagsEXT flags,
-                              VkDebugReportObjectTypeEXT objType,
-                              uint64_t object, size_t location, int32_t msgCode,
-                              const char *pLayerPrefix, const char *pMsg) {
+VKAPI_ATTR void VKAPI_CALL terminator_DebugReportMessage(
+    VkInstance instance, VkDebugReportFlagsEXT flags,
+    VkDebugReportObjectTypeEXT objType, uint64_t object, size_t location,
+    int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {
     const struct loader_icd *icd;
 
     struct loader_instance *inst = (struct loader_instance *)instance;
index e408224..6f78cf9 100644 (file)
@@ -36,9 +36,9 @@ DIR *opendir(const char *name) {
         const char *all = /* search pattern must end with suitable wildcard */
             strchr("/\\", name[base_length - 1]) ? "*" : "/*";
 
-        if ((dir = (DIR *)loader_tls_heap_alloc(sizeof *dir)) != 0 &&
-            (dir->name = (char *)loader_tls_heap_alloc(base_length +
-                                                       strlen(all) + 1)) != 0) {
+        if ((dir = (DIR *)loader_instance_tls_heap_alloc(sizeof *dir)) != 0 &&
+            (dir->name = (char *)loader_instance_tls_heap_alloc(
+                 base_length + strlen(all) + 1)) != 0) {
             strcat(strcpy(dir->name, name), all);
 
             if ((dir->handle =
@@ -46,13 +46,13 @@ DIR *opendir(const char *name) {
                 dir->result.d_name = 0;
             } else /* rollback */
             {
-                loader_tls_heap_free(dir->name);
-                loader_tls_heap_free(dir);
+                loader_instance_tls_heap_free(dir->name);
+                loader_instance_tls_heap_free(dir);
                 dir = 0;
             }
         } else /* rollback */
         {
-            loader_tls_heap_free(dir);
+            loader_instance_tls_heap_free(dir);
             dir = 0;
             errno = ENOMEM;
         }
@@ -71,8 +71,8 @@ int closedir(DIR *dir) {
             result = _findclose(dir->handle);
         }
 
-        loader_tls_heap_free(dir->name);
-        loader_tls_heap_free(dir);
+        loader_instance_tls_heap_free(dir->name);
+        loader_instance_tls_heap_free(dir);
     }
 
     if (result == -1) /* map all errors to EBADF */
index 5b12306..eca0246 100644 (file)
@@ -1,4 +1,4 @@
-/*
+/*
  *
  * Copyright (c) 2014-2016 The Khronos Group Inc.
  * Copyright (c) 2014-2016 Valve Corporation
@@ -159,65 +159,210 @@ const VkLayerInstanceDispatchTable instance_disp = {
 
 LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_init);
 
-void *loader_heap_alloc(const struct loader_instance *instance, size_t size,
-                        VkSystemAllocationScope alloc_scope) {
+void *loader_instance_heap_alloc(const struct loader_instance *instance,
+                                 size_t size,
+                                 VkSystemAllocationScope alloc_scope) {
+    void *pMemory = NULL;
+#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
+    {
+#else
     if (instance && instance->alloc_callbacks.pfnAllocation) {
-        /* TODO: What should default alignment be? 1, 4, 8, other? */
-        return instance->alloc_callbacks.pfnAllocation(
-            instance->alloc_callbacks.pUserData, size, sizeof(int),
+        /* 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);
+    } else {
+#endif
+        pMemory = malloc(size);
     }
-    return malloc(size);
+    return pMemory;
 }
 
-void loader_heap_free(const struct loader_instance *instance, void *pMemory) {
-    if (pMemory == NULL)
-        return;
-    if (instance && instance->alloc_callbacks.pfnFree) {
-        instance->alloc_callbacks.pfnFree(instance->alloc_callbacks.pUserData,
-                                          pMemory);
-        return;
+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);
+        }
     }
-    free(pMemory);
 }
 
-void *loader_heap_realloc(const struct loader_instance *instance, void *pMemory,
-                          size_t orig_size, size_t size,
-                          VkSystemAllocationScope alloc_scope) {
-    if (pMemory == NULL || orig_size == 0)
-        return loader_heap_alloc(instance, size, alloc_scope);
-    if (size == 0) {
-        loader_heap_free(instance, pMemory);
-        return NULL;
+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);
     }
-    // TODO use the callback realloc function
-    if (instance && instance->alloc_callbacks.pfnAllocation) {
-        if (size <= orig_size) {
-            memset(((uint8_t *)pMemory) + size, 0, orig_size - size);
-            return pMemory;
+    return pNewMem;
+}
+
+void *loader_instance_tls_heap_alloc(size_t size) {
+    return loader_instance_heap_alloc(tls_instance, size,
+                                      VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
+}
+
+void loader_instance_tls_heap_free(void *pMemory) {
+    loader_instance_heap_free(tls_instance, pMemory);
+}
+
+void *loader_device_heap_alloc(const struct loader_device *device, size_t size,
+                               VkSystemAllocationScope alloc_scope) {
+    void *pMemory = NULL;
+#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
+    {
+#else
+    if (device && device->alloc_callbacks.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);
+    } else {
+#endif
+        pMemory = malloc(size);
+    }
+    return pMemory;
+}
+
+void loader_device_heap_free(const struct loader_device *device,
+                             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);
+        } else {
+#endif
+            free(pMemory);
         }
-        /* TODO: What should default alignment be? 1, 4, 8, other? */
-        void *new_ptr = instance->alloc_callbacks.pfnAllocation(
-            instance->alloc_callbacks.pUserData, size, sizeof(int),
+    }
+}
+
+void *loader_device_heap_realloc(const struct loader_device *device,
+                                 void *pMemory, size_t orig_size, size_t size,
+                                 VkSystemAllocationScope alloc_scope) {
+    void *pNewMem = NULL;
+    if (pMemory == NULL || orig_size == 0) {
+        pNewMem = loader_device_heap_alloc(device, size, alloc_scope);
+    } else if (size == 0) {
+        loader_device_heap_free(device, pMemory);
+#if (DEBUG_DISABLE_APP_ALLOCATORS == 1)
+#else
+    } else if (device && device->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 = device->alloc_callbacks.pfnReallocation(
+            device->alloc_callbacks.pUserData, pMemory, size, sizeof(uint64_t),
             alloc_scope);
-        if (!new_ptr)
-            return NULL;
-        memcpy(new_ptr, pMemory, orig_size);
-        instance->alloc_callbacks.pfnFree(instance->alloc_callbacks.pUserData,
-                                          pMemory);
-        return new_ptr;
+#endif
+    } else {
+        pNewMem = realloc(pMemory, size);
+    }
+    return pNewMem;
+}
+
+// Environment variables
+#if defined(__linux__)
+
+static inline char *loader_getenv(const char *name,
+                                  const struct loader_instance *inst) {
+    // No allocation of memory necessary for Linux, but we should at least touch
+    // the inst pointer to get rid of compiler warnings.
+    (void)inst;
+    return getenv(name);
+}
+static inline void loader_free_getenv(const char *val,
+                                      const struct loader_instance *inst) {
+    // No freeing of memory necessary for Linux, but we should at least touch
+    // the val and inst pointers to get rid of compiler warnings.
+    (void)val;
+    (void)inst;
+}
+
+#elif defined(WIN32)
+
+static inline char *loader_getenv(const char *name,
+                                  const struct loader_instance *inst) {
+    char *retVal;
+    DWORD valSize;
+
+    valSize = GetEnvironmentVariableA(name, NULL, 0);
+
+    // valSize DOES include the null terminator, so for any set variable
+    // 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);
     }
-    return realloc(pMemory, size);
+
+    if (NULL != retVal) {
+        GetEnvironmentVariableA(name, retVal, valSize);
+    }
+
+    return retVal;
 }
 
-void *loader_tls_heap_alloc(size_t size) {
-    return loader_heap_alloc(tls_instance, size,
-                             VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
+static inline 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_tls_heap_free(void *pMemory) {
-    loader_heap_free(tls_instance, pMemory);
+#else
+
+static inline char *loader_getenv(const char *name,
+    const struct loader_instance *inst) {
+    // stub func
+    (void)inst;
+    (void)name;
+    return NULL;
 }
+static inline void loader_free_getenv(const char *val,
+    const struct loader_instance *inst) {
+    // stub func
+    (void)val;
+    (void)inst;
+}
+
+#endif
 
 void loader_log(const struct loader_instance *inst, VkFlags msg_type,
                 int32_t msg_code, const char *format, ...) {
@@ -322,21 +467,25 @@ static char *loader_get_registry_files(const struct loader_instance *inst,
                ERROR_SUCCESS) {
             if (value_size == sizeof(value) && value == 0) {
                 if (out == NULL) {
-                    out = loader_heap_alloc(
+                    out = loader_instance_heap_alloc(
                         inst, total_size, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+                    if (NULL == out) {
+                        loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
+                                   "Out of memory can't alloc space for registry data");
+                        return NULL;
+                    }
                     out[0] = '\0';
                 } else if (strlen(out) + name_size + 1 > total_size) {
-                    out = loader_heap_realloc(
+                    out = loader_instance_heap_realloc(
                         inst, out, total_size, total_size * 2,
                         VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+                    if (NULL == out) {
+                        loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
+                                   "Out of memory can't realloc space for registry data");
+                        return NULL;
+                    }
                     total_size *= 2;
                 }
-                if (out == NULL) {
-                    loader_log(
-                        inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
-                        "Out of memory, failed loader_get_registry_files");
-                    return NULL;
-                }
                 if (strlen(out) == 0)
                     snprintf(out, name_size + 1, "%s", name);
                 else
@@ -502,8 +651,9 @@ loader_get_next_layer_property(const struct loader_instance *inst,
                                struct loader_layer_list *layer_list) {
     if (layer_list->capacity == 0) {
         layer_list->list =
-            loader_heap_alloc(inst, sizeof(struct loader_layer_properties) * 64,
-                              VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+            loader_instance_heap_alloc(
+                inst, sizeof(struct loader_layer_properties) * 64,
+                VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
         if (layer_list->list == NULL) {
             loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
                        "Out of memory can't add any layer properties to list");
@@ -517,12 +667,13 @@ loader_get_next_layer_property(const struct loader_instance *inst,
     // ensure enough room to add an entry
     if ((layer_list->count + 1) * sizeof(struct loader_layer_properties) >
         layer_list->capacity) {
-        layer_list->list = loader_heap_realloc(
+        layer_list->list = loader_instance_heap_realloc(
             inst, layer_list->list, layer_list->capacity,
             layer_list->capacity * 2, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
         if (layer_list->list == NULL) {
             loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
                        "realloc failed for layer list");
+            return NULL;
         }
         layer_list->capacity *= 2;
     }
@@ -547,11 +698,12 @@ void loader_delete_layer_properties(const struct loader_instance *inst,
                       .instance_extension_list);
         dev_ext_list = &layer_list->list[i].device_extension_list;
         if (dev_ext_list->capacity > 0 &&
+            NULL != dev_ext_list->list &&
             dev_ext_list->list->entrypoint_count > 0) {
             for (j = 0; j < dev_ext_list->list->entrypoint_count; j++) {
-                loader_heap_free(inst, dev_ext_list->list->entrypoints[j]);
+                loader_instance_heap_free(inst, dev_ext_list->list->entrypoints[j]);
             }
-            loader_heap_free(inst, dev_ext_list->list->entrypoints);
+            loader_instance_heap_free(inst, dev_ext_list->list->entrypoints);
         }
         loader_destroy_generic_list(inst,
                                     (struct loader_generic_list *)dev_ext_list);
@@ -560,7 +712,7 @@ void loader_delete_layer_properties(const struct loader_instance *inst,
 
     if (layer_list->capacity > 0) {
         layer_list->capacity = 0;
-        loader_heap_free(inst, layer_list->list);
+        loader_instance_heap_free(inst, layer_list->list);
     }
 }
 
@@ -703,8 +855,8 @@ bool loader_init_generic_list(const struct loader_instance *inst,
                               struct loader_generic_list *list_info,
                               size_t element_size) {
     list_info->capacity = 32 * element_size;
-    list_info->list = loader_heap_alloc(inst, list_info->capacity,
-                                        VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+    list_info->list = loader_instance_heap_alloc(
+        inst, list_info->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
     if (list_info->list == NULL) {
         return false;
     }
@@ -715,7 +867,7 @@ bool loader_init_generic_list(const struct loader_instance *inst,
 
 void loader_destroy_generic_list(const struct loader_instance *inst,
                                  struct loader_generic_list *list) {
-    loader_heap_free(inst, list->list);
+    loader_instance_heap_free(inst, list->list);
     list->count = 0;
     list->capacity = 0;
 }
@@ -754,7 +906,7 @@ VkResult loader_add_to_ext_list(const struct loader_instance *inst,
         if (ext_list->count * sizeof(VkExtensionProperties) >=
             ext_list->capacity) {
 
-            ext_list->list = loader_heap_realloc(
+            ext_list->list = loader_instance_heap_realloc(
                 inst, ext_list->list, ext_list->capacity,
                 ext_list->capacity * 2, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
 
@@ -802,7 +954,7 @@ loader_add_to_dev_ext_list(const struct loader_instance *inst,
     // check for enough capacity
     if (idx * sizeof(struct loader_dev_ext_props) >= ext_list->capacity) {
 
-        ext_list->list = loader_heap_realloc(
+        ext_list->list = loader_instance_heap_realloc(
             inst, ext_list->list, ext_list->capacity, ext_list->capacity * 2,
             VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
 
@@ -817,15 +969,25 @@ loader_add_to_dev_ext_list(const struct loader_instance *inst,
            sizeof(struct loader_dev_ext_props));
     ext_list->list[idx].entrypoint_count = entry_count;
     ext_list->list[idx].entrypoints =
-        loader_heap_alloc(inst, sizeof(char *) * entry_count,
-                          VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
-    if (ext_list->list[idx].entrypoints == NULL)
+        loader_instance_heap_alloc(inst, sizeof(char *) * entry_count,
+                                   VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+    if (ext_list->list[idx].entrypoints == NULL) {
+        ext_list->list[idx].entrypoint_count = 0;
         return VK_ERROR_OUT_OF_HOST_MEMORY;
+    }
     for (uint32_t i = 0; i < entry_count; i++) {
-        ext_list->list[idx].entrypoints[i] = loader_heap_alloc(
+        ext_list->list[idx].entrypoints[i] = loader_instance_heap_alloc(
             inst, strlen(entrys[i]) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
-        if (ext_list->list[idx].entrypoints[i] == NULL)
+        if (ext_list->list[idx].entrypoints[i] == NULL) {
+            for (uint32_t j = 0; j < i; j++) {
+                loader_instance_heap_free(inst,
+                                          ext_list->list[idx].entrypoints[j]);
+            }
+            loader_instance_heap_free(inst, ext_list->list[idx].entrypoints);
+            ext_list->list[idx].entrypoint_count = 0;
+            ext_list->list[idx].entrypoints = NULL;
             return VK_ERROR_OUT_OF_HOST_MEMORY;
+        }
         strcpy(ext_list->list[idx].entrypoints[i], entrys[i]);
     }
     ext_list->count++;
@@ -856,7 +1018,7 @@ loader_add_layer_names_to_list(const struct loader_instance *inst,
             continue;
         }
 
-        loader_add_to_layer_list(inst, output_list, 1, layer_prop);
+        err = loader_add_to_layer_list(inst, output_list, 1, layer_prop);
     }
 
     return err;
@@ -868,8 +1030,8 @@ loader_add_layer_names_to_list(const struct loader_instance *inst,
 static bool loader_init_layer_list(const struct loader_instance *inst,
                                    struct loader_layer_list *list) {
     list->capacity = 32 * sizeof(struct loader_layer_properties);
-    list->list = loader_heap_alloc(inst, list->capacity,
-                                   VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+    list->list = loader_instance_heap_alloc(
+        inst, list->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
     if (list->list == NULL) {
         return false;
     }
@@ -879,8 +1041,13 @@ static bool loader_init_layer_list(const struct loader_instance *inst,
 }
 
 void loader_destroy_layer_list(const struct loader_instance *inst,
+                               struct loader_device *device,
                                struct loader_layer_list *layer_list) {
-    loader_heap_free(inst, layer_list->list);
+    if (device) {
+        loader_device_heap_free(device, layer_list->list);
+    } else {
+        loader_instance_heap_free(inst, layer_list->list);
+    }
     layer_list->count = 0;
     layer_list->capacity = 0;
 }
@@ -914,10 +1081,10 @@ bool has_layer_name(const char *name, const struct loader_layer_list *list) {
  * Append non-duplicate layer properties defined in prop_list
  * to the given layer_info 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) {
+VkResult 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) {
     uint32_t i;
     struct loader_layer_properties *layer;
 
@@ -926,7 +1093,7 @@ void loader_add_to_layer_list(const struct loader_instance *inst,
     }
 
     if (list->list == NULL)
-        return;
+        return VK_SUCCESS;
 
     for (i = 0; i < prop_list_count; i++) {
         layer = (struct loader_layer_properties *)&props[i];
@@ -941,9 +1108,15 @@ void loader_add_to_layer_list(const struct loader_instance *inst,
         if (list->count * sizeof(struct loader_layer_properties) >=
             list->capacity) {
 
-            list->list = loader_heap_realloc(
+            list->list = loader_instance_heap_realloc(
                 inst, list->list, list->capacity, list->capacity * 2,
                 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+            if (NULL == list->list) {
+                loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
+                           "realloc failed for layer list when attempting to "
+                           "add new layer");
+                return VK_ERROR_OUT_OF_HOST_MEMORY;
+            }
             // double capacity
             list->capacity *= 2;
         }
@@ -952,6 +1125,8 @@ void loader_add_to_layer_list(const struct loader_instance *inst,
                sizeof(struct loader_layer_properties));
         list->count++;
     }
+
+    return VK_SUCCESS;
 }
 
 /**
@@ -971,8 +1146,9 @@ void loader_find_layer_name_add_list(
         if (0 == strcmp(layer_prop->info.layerName, name) &&
             (layer_prop->type & type)) {
             /* Found a layer with the same name, add to found_list */
-            loader_add_to_layer_list(inst, found_list, 1, layer_prop);
-            found = true;
+            if (VK_SUCCESS == loader_add_to_layer_list(inst, found_list, 1, layer_prop)) {
+                found = true;
+            }
         }
     }
     if (!found) {
@@ -1064,19 +1240,37 @@ struct loader_icd *loader_get_icd_and_device(const VkDevice device,
     return NULL;
 }
 
-static void loader_destroy_logical_device(const struct loader_instance *inst,
-                                          struct loader_device *dev) {
-    loader_heap_free(inst, dev->app_extension_props);
-    loader_deactivate_layers(inst, &dev->activated_layer_list);
-    loader_heap_free(inst, dev);
+void loader_destroy_logical_device(const struct loader_instance *inst,
+                                   struct loader_device *dev,
+                                   const VkAllocationCallbacks *pAllocator) {
+    if (pAllocator) {
+        dev->alloc_callbacks = *pAllocator;
+    }
+    if (NULL != dev->app_extension_props) {
+        loader_device_heap_free(dev, dev->app_extension_props);
+    }
+    if (NULL != dev->activated_layer_list.list) {
+        loader_deactivate_layers(inst, dev, &dev->activated_layer_list);
+    }
+    loader_device_heap_free(dev, dev);
 }
 
 struct loader_device *
-loader_create_logical_device(const struct loader_instance *inst) {
+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_heap_alloc(inst, sizeof(struct loader_device),
-                                VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
     if (!new_dev) {
         loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
                    "Failed to alloc struct loader-device");
@@ -1084,6 +1278,9 @@ loader_create_logical_device(const struct loader_instance *inst) {
     }
 
     memset(new_dev, 0, sizeof(struct loader_device));
+    if (pAllocator) {
+        new_dev->alloc_callbacks = *pAllocator;
+    }
 
     return new_dev;
 }
@@ -1097,7 +1294,8 @@ void loader_add_logical_device(const struct loader_instance *inst,
 
 void loader_remove_logical_device(const struct loader_instance *inst,
                                   struct loader_icd *icd,
-                                  struct loader_device *found_dev) {
+                                  struct loader_device *found_dev,
+                                  const VkAllocationCallbacks *pAllocator) {
     struct loader_device *dev, *prev_dev;
 
     if (!icd || !found_dev)
@@ -1114,27 +1312,28 @@ void loader_remove_logical_device(const struct loader_instance *inst,
         prev_dev->next = found_dev->next;
     else
         icd->logical_device_list = found_dev->next;
-    loader_destroy_logical_device(inst, found_dev);
+    loader_destroy_logical_device(inst, found_dev, pAllocator);
 }
 
 static void loader_icd_destroy(struct loader_instance *ptr_inst,
-                               struct loader_icd *icd) {
+                               struct loader_icd *icd,
+                               const VkAllocationCallbacks *pAllocator) {
     ptr_inst->total_icd_count--;
     for (struct loader_device *dev = icd->logical_device_list; dev;) {
         struct loader_device *next_dev = dev->next;
-        loader_destroy_logical_device(ptr_inst, dev);
+        loader_destroy_logical_device(ptr_inst, dev, pAllocator);
         dev = next_dev;
     }
 
-    loader_heap_free(ptr_inst, icd);
+    loader_instance_heap_free(ptr_inst, icd);
 }
 
 static struct loader_icd *
 loader_icd_create(const struct loader_instance *inst) {
     struct loader_icd *icd;
 
-    icd = loader_heap_alloc(inst, sizeof(*icd),
-                            VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+    icd = loader_instance_heap_alloc(inst, sizeof(*icd),
+                                     VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
     if (!icd)
         return NULL;
 
@@ -1211,20 +1410,27 @@ void loader_scanned_icd_clear(const struct loader_instance *inst,
         return;
     for (uint32_t i = 0; i < icd_libs->count; i++) {
         loader_platform_close_library(icd_libs->list[i].handle);
-        loader_heap_free(inst, icd_libs->list[i].lib_name);
+        loader_instance_heap_free(inst, icd_libs->list[i].lib_name);
     }
-    loader_heap_free(inst, icd_libs->list);
+    loader_instance_heap_free(inst, icd_libs->list);
     icd_libs->capacity = 0;
     icd_libs->count = 0;
     icd_libs->list = NULL;
 }
 
-static void loader_scanned_icd_init(const struct loader_instance *inst,
-                                    struct loader_icd_libs *icd_libs) {
+static VkResult loader_scanned_icd_init(const struct loader_instance *inst,
+                                        struct loader_icd_libs *icd_libs) {
+    VkResult err = VK_SUCCESS;
     loader_scanned_icd_clear(inst, icd_libs);
     icd_libs->capacity = 8 * sizeof(struct loader_scanned_icds);
-    icd_libs->list = loader_heap_alloc(inst, icd_libs->capacity,
-                                       VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+    icd_libs->list = loader_instance_heap_alloc(
+        inst, icd_libs->capacity, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+    if (NULL == icd_libs->list) {
+        loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
+            "realloc failed for layer list when attempting to add new layer");
+        err = VK_ERROR_OUT_OF_HOST_MEMORY;
+    }
+    return err;
 }
 
 static void loader_scanned_icd_add(const struct loader_instance *inst,
@@ -1248,15 +1454,16 @@ static void loader_scanned_icd_add(const struct loader_instance *inst,
     }
 
     // Get and settle on an ICD interface version
-    fp_negotiate_icd_version =
-        loader_platform_get_proc_address(handle, "vk_icdNegotiateLoaderICDInterfaceVersion");
+    fp_negotiate_icd_version = loader_platform_get_proc_address(
+        handle, "vk_icdNegotiateLoaderICDInterfaceVersion");
 
     if (!loader_get_icd_interface_version(fp_negotiate_icd_version,
-            &interface_vers)) {
-                    loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
-                       "ICD (%s) doesn't support interface version compatible"
-                       "with loader, skip this ICD %s", filename);
-            return;
+                                          &interface_vers)) {
+        loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
+                   "ICD (%s) doesn't support interface version compatible"
+                   "with loader, skip this ICD %s",
+                   filename);
+        return;
     }
 
     fp_get_proc_addr =
@@ -1275,15 +1482,16 @@ static void loader_scanned_icd_add(const struct loader_instance *inst,
             loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
                        "Using deprecated ICD interface of "
                        "vkGetInstanceProcAddr instead of "
-                       "vk_icdGetInstanceProcAddr for ICD %s", filename);
+                       "vk_icdGetInstanceProcAddr for ICD %s",
+                       filename);
         }
         fp_create_inst =
             loader_platform_get_proc_address(handle, "vkCreateInstance");
         if (!fp_create_inst) {
-            loader_log(
-                inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
-                "Couldn't get vkCreateInstance via dlsym/loadlibrary for ICD %s",
-                    filename);
+            loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
+                       "Couldn't get vkCreateInstance via dlsym/loadlibrary "
+                       "for ICD %s",
+                       filename);
             return;
         }
         fp_get_inst_ext_props = loader_platform_get_proc_address(
@@ -1291,7 +1499,8 @@ static void loader_scanned_icd_add(const struct loader_instance *inst,
         if (!fp_get_inst_ext_props) {
             loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
                        "Couldn't get vkEnumerateInstanceExtensionProperties "
-                       "via dlsym/loadlibrary for ICD %s", filename);
+                       "via dlsym/loadlibrary for ICD %s",
+                       filename);
             return;
         }
     } else {
@@ -1304,7 +1513,8 @@ static void loader_scanned_icd_add(const struct loader_instance *inst,
         if (!fp_create_inst) {
             loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
                        "Couldn't get vkCreateInstance via "
-                       "vk_icdGetInstanceProcAddr for ICD %s", filename);
+                       "vk_icdGetInstanceProcAddr for ICD %s",
+                       filename);
             return;
         }
         fp_get_inst_ext_props =
@@ -1313,7 +1523,8 @@ static void loader_scanned_icd_add(const struct loader_instance *inst,
         if (!fp_get_inst_ext_props) {
             loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
                        "Couldn't get vkEnumerateInstanceExtensionProperties "
-                       "via vk_icdGetInstanceProcAddr for ICD %s", filename);
+                       "via vk_icdGetInstanceProcAddr for ICD %s",
+                       filename);
             return;
         }
     }
@@ -1322,9 +1533,14 @@ static void loader_scanned_icd_add(const struct loader_instance *inst,
     if ((icd_libs->count * sizeof(struct loader_scanned_icds)) >=
         icd_libs->capacity) {
 
-        icd_libs->list = loader_heap_realloc(
+        icd_libs->list = loader_instance_heap_realloc(
             inst, icd_libs->list, icd_libs->capacity, icd_libs->capacity * 2,
             VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+        if (NULL == icd_libs->list) {
+            loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
+                       "realloc failed on icd library list");
+            return;
+        }
         // double capacity
         icd_libs->capacity *= 2;
     }
@@ -1337,9 +1553,9 @@ static void loader_scanned_icd_add(const struct loader_instance *inst,
     new_node->CreateInstance = fp_create_inst;
     new_node->interface_version = interface_vers;
 
-    new_node->lib_name = (char *)loader_heap_alloc(
+    new_node->lib_name = (char *)loader_instance_heap_alloc(
         inst, strlen(filename) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
-    if (!new_node->lib_name) {
+    if (NULL == new_node->lib_name) {
         loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
                    "Out of memory can't add icd");
         return;
@@ -1408,7 +1624,7 @@ static bool loader_icd_init_entrys(struct loader_icd *icd, VkInstance inst,
 }
 
 static void loader_debug_init(void) {
-    const char *env, *orig;
+    char *env, *orig;
 
     if (g_loader_debug > 0)
         return;
@@ -1416,9 +1632,9 @@ static void loader_debug_init(void) {
     g_loader_debug = 0;
 
     /* parse comma-separated debug options */
-    orig = env = loader_getenv("VK_LOADER_DEBUG");
+    orig = env = loader_getenv("VK_LOADER_DEBUG", NULL);
     while (env) {
-        const char *p = strchr(env, ',');
+        char *p = strchr(env, ',');
         size_t len;
 
         if (p)
@@ -1455,7 +1671,7 @@ static void loader_debug_init(void) {
         env = p + 1;
     }
 
-    loader_free_getenv(orig);
+    loader_free_getenv(orig, NULL);
 }
 
 void loader_initialize(void) {
@@ -1468,7 +1684,8 @@ void loader_initialize(void) {
 
     // initial cJSON to use alloc callbacks
     cJSON_Hooks alloc_fns = {
-        .malloc_fn = loader_tls_heap_alloc, .free_fn = loader_tls_heap_free,
+        .malloc_fn = loader_instance_tls_heap_alloc,
+        .free_fn = loader_instance_tls_heap_free,
     };
     cJSON_InitHooks(&alloc_fns);
 }
@@ -1601,23 +1818,35 @@ static cJSON *loader_get_json(const struct loader_instance *inst,
 /**
  * Do a deep copy of the loader_layer_properties structure.
  */
-void loader_copy_layer_properties(const struct loader_instance *inst,
-                                         struct loader_layer_properties *dst,
-                                         struct loader_layer_properties *src) {
+VkResult loader_copy_layer_properties(const struct loader_instance *inst,
+                                      struct loader_layer_properties *dst,
+                                      struct loader_layer_properties *src) {
     uint32_t cnt, i;
     memcpy(dst, src, sizeof(*src));
     dst->instance_extension_list.list =
-        loader_heap_alloc(inst, sizeof(VkExtensionProperties) *
-                                    src->instance_extension_list.count,
-                          VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+        loader_instance_heap_alloc(inst, sizeof(VkExtensionProperties) *
+                                             src->instance_extension_list.count,
+                                   VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+    if (NULL == dst->instance_extension_list.list) {
+        loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
+                   "alloc failed for instance extension list");
+        return VK_ERROR_OUT_OF_HOST_MEMORY;
+    }
     dst->instance_extension_list.capacity =
         sizeof(VkExtensionProperties) * src->instance_extension_list.count;
     memcpy(dst->instance_extension_list.list, src->instance_extension_list.list,
            dst->instance_extension_list.capacity);
     dst->device_extension_list.list =
-        loader_heap_alloc(inst, sizeof(struct loader_dev_ext_props) *
-                                    src->device_extension_list.count,
-                          VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+        loader_instance_heap_alloc(inst, sizeof(struct loader_dev_ext_props) *
+                                             src->device_extension_list.count,
+                                   VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+    if (NULL == dst->device_extension_list.list) {
+        loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
+                   "alloc failed for device extension list");
+        return VK_ERROR_OUT_OF_HOST_MEMORY;
+    }
+    memset(dst->device_extension_list.list, 0, sizeof(struct loader_dev_ext_props) *
+        src->device_extension_list.count);
 
     dst->device_extension_list.capacity =
         sizeof(struct loader_dev_ext_props) * src->device_extension_list.count;
@@ -1626,17 +1855,35 @@ void loader_copy_layer_properties(const struct loader_instance *inst,
     if (src->device_extension_list.count > 0 &&
         src->device_extension_list.list->entrypoint_count > 0) {
         cnt = src->device_extension_list.list->entrypoint_count;
-        dst->device_extension_list.list->entrypoints = loader_heap_alloc(
-            inst, sizeof(char *) * cnt, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+        dst->device_extension_list.list->entrypoints =
+            loader_instance_heap_alloc(inst, sizeof(char *) * cnt,
+                                       VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+        if (NULL == dst->device_extension_list.list->entrypoints) {
+            loader_log(
+                inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
+                "alloc failed for device extension list entrypoint array");
+            return VK_ERROR_OUT_OF_HOST_MEMORY;
+        }
+        memset(dst->device_extension_list.list->entrypoints, 0, sizeof(char *) * cnt);
+
         for (i = 0; i < cnt; i++) {
-            dst->device_extension_list.list->entrypoints[i] = loader_heap_alloc(
-                inst,
-                strlen(src->device_extension_list.list->entrypoints[i]) + 1,
-                VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+            dst->device_extension_list.list->entrypoints[i] =
+                loader_instance_heap_alloc(
+                    inst,
+                    strlen(src->device_extension_list.list->entrypoints[i]) + 1,
+                    VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+            if (NULL == dst->device_extension_list.list->entrypoints[i]) {
+                loader_log(
+                    inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
+                    "alloc failed for device extension list entrypoint %d", i);
+                return VK_ERROR_OUT_OF_HOST_MEMORY;
+            }
             strcpy(dst->device_extension_list.list->entrypoints[i],
                    src->device_extension_list.list->entrypoints[i]);
         }
     }
+
+    return VK_SUCCESS;
 }
 
 static bool
@@ -1683,9 +1930,8 @@ bool loader_find_layer_name_array(
  * @param layer_count
  * @param ppp_layer_names
  */
-void loader_expand_layer_names(
-    struct loader_instance *inst, const char *key_name,
-    uint32_t expand_count,
+VkResult loader_expand_layer_names(
+    struct loader_instance *inst, const char *key_name, uint32_t expand_count,
     const char expand_names[][VK_MAX_EXTENSION_NAME_SIZE],
     uint32_t *layer_count, char const *const **ppp_layer_names) {
 
@@ -1694,7 +1940,7 @@ void loader_expand_layer_names(
     if (!loader_find_layer_name(key_name, *layer_count,
                                 (char const **)pp_src_layers)) {
         inst->activated_layers_are_std_val = false;
-        return; // didn't find the key_name in the list.
+        return VK_SUCCESS; // didn't find the key_name in the list.
     }
 
     loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
@@ -1702,9 +1948,14 @@ void loader_expand_layer_names(
                key_name);
 
     inst->activated_layers_are_std_val = true;
-    char const **pp_dst_layers = loader_heap_alloc(
+    char const **pp_dst_layers = loader_instance_heap_alloc(
         inst, (expand_count + *layer_count - 1) * sizeof(char const *),
         VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
+    if (NULL == pp_dst_layers) {
+        loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
+                   "alloc failed for dst layer array");
+        return VK_ERROR_OUT_OF_HOST_MEMORY;
+    }
 
     // copy layers from src to dst, stripping key_name and anything in
     // expand_names.
@@ -1730,6 +1981,8 @@ void loader_expand_layer_names(
 
     *ppp_layer_names = pp_dst_layers;
     *layer_count = dst_index;
+
+    return VK_SUCCESS;
 }
 
 void loader_delete_shadow_inst_layer_names(const struct loader_instance *inst,
@@ -1737,7 +1990,7 @@ void loader_delete_shadow_inst_layer_names(const struct loader_instance *inst,
                                            VkInstanceCreateInfo *ours) {
     /* Free the layer names array iff we had to reallocate it */
     if (orig->ppEnabledLayerNames != ours->ppEnabledLayerNames) {
-        loader_heap_free(inst, (void *)ours->ppEnabledLayerNames);
+        loader_instance_heap_free(inst, (void *)ours->ppEnabledLayerNames);
     }
 }
 
@@ -1792,6 +2045,10 @@ static void loader_add_layer_property_meta(
     struct loader_layer_properties *props;
     if (found) {
         props = loader_get_next_layer_property(inst, layer_list);
+        if (NULL == props) {
+            // Error already triggered in loader_get_next_layer_property.
+            return;
+        }
         loader_init_std_validation_props(props);
 
     }
@@ -1808,16 +2065,16 @@ static void loader_read_json_layer(
     cJSON *ext_item;
     VkExtensionProperties ext_prop;
 
-    /*
    * The following are required in the "layer" object:
    * (required) "name"
    * (required) "type"
    * (required) “library_path”
    * (required) “api_version”
    * (required) “implementation_version”
    * (required) “description”
    * (required for implicit layers) “disable_environment”
    */
+/*
+ * The following are required in the "layer" object:
+ * (required) "name"
+ * (required) "type"
+ * (required) “library_path”
+ * (required) “api_version”
+ * (required) “implementation_version”
+ * (required) “description”
+ * (required for implicit layers) “disable_environment”
+ */
 
 #define GET_JSON_OBJECT(node, var)                                             \
     {                                                                          \
@@ -1843,10 +2100,18 @@ static void loader_read_json_layer(
             return;                                                            \
         }                                                                      \
         temp = cJSON_Print(item);                                              \
+        if (temp == NULL) {                                                    \
+            layer_node = layer_node->next;                                     \
+            loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,               \
+                       "Problem accessing layer value %s in manifest JSON "    \
+                       "file, skipping this layer",                            \
+                       #var);                                                  \
+            return;                                                            \
+        }                                                                      \
         temp[strlen(temp) - 1] = '\0';                                         \
         var = loader_stack_alloc(strlen(temp) + 1);                            \
         strcpy(var, &temp[1]);                                                 \
-        loader_tls_heap_free(temp);                                            \
+        cJSON_Free(temp);                                                      \
     }
     GET_JSON_ITEM(layer_node, name)
     GET_JSON_ITEM(layer_node, type)
@@ -1876,6 +2141,10 @@ static void loader_read_json_layer(
             return;
         }
         props = loader_get_next_layer_property(inst, layer_instance_list);
+        if (NULL == props) {
+            // Error already triggered in loader_get_next_layer_property.
+            return;
+        }
         props->type = (is_implicit) ? VK_LAYER_TYPE_INSTANCE_IMPLICIT
                                     : VK_LAYER_TYPE_INSTANCE_EXPLICIT;
     }
@@ -1940,10 +2209,12 @@ static void loader_read_json_layer(
         item = cJSON_GetObjectItem(node, #var);                                \
         if (item != NULL) {                                                    \
             temp = cJSON_Print(item);                                          \
-            temp[strlen(temp) - 1] = '\0';                                     \
-            var = loader_stack_alloc(strlen(temp) + 1);                        \
-            strcpy(var, &temp[1]);                                             \
-            loader_tls_heap_free(temp);                                        \
+            if (temp != NULL) {                                                \
+                temp[strlen(temp) - 1] = '\0';                                 \
+                var = loader_stack_alloc(strlen(temp) + 1);                    \
+                strcpy(var, &temp[1]);                                         \
+                cJSON_Free(temp);                                              \
+            }                                                                  \
         }                                                                      \
     }
 
@@ -1989,14 +2260,18 @@ static void loader_read_json_layer(
         for (i = 0; i < count; i++) {
             ext_item = cJSON_GetArrayItem(instance_extensions, i);
             GET_JSON_ITEM(ext_item, name)
-            GET_JSON_ITEM(ext_item, spec_version)
             if (name != NULL) {
                 strncpy(ext_prop.extensionName, name,
                         sizeof(ext_prop.extensionName));
                 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] =
                     '\0';
             }
-            ext_prop.specVersion = atoi(spec_version);
+            GET_JSON_ITEM(ext_item, spec_version)
+            if (NULL != spec_version) {
+                ext_prop.specVersion = atoi(spec_version);
+            } else {
+                ext_prop.specVersion = 0;
+            }
             bool ext_unsupported =
                 wsi_unsupported_instance_extension(&ext_prop);
             if (!ext_unsupported) {
@@ -2025,7 +2300,11 @@ static void loader_read_json_layer(
                 ext_prop.extensionName[sizeof(ext_prop.extensionName) - 1] =
                     '\0';
             }
-            ext_prop.specVersion = atoi(spec_version);
+            if (NULL != spec_version) {
+                ext_prop.specVersion = atoi(spec_version);
+            } else {
+                ext_prop.specVersion = 0;
+            }
             // entrypoints = cJSON_GetObjectItem(ext_item, "entrypoints");
             GET_JSON_OBJECT(ext_item, entrypoints)
             int entry_count;
@@ -2035,17 +2314,22 @@ static void loader_read_json_layer(
                 continue;
             }
             entry_count = cJSON_GetArraySize(entrypoints);
-            if (entry_count)
+            if (entry_count) {
                 entry_array =
                     (char **)loader_stack_alloc(sizeof(char *) * entry_count);
+            }
             for (j = 0; j < entry_count; j++) {
                 ext_item = cJSON_GetArrayItem(entrypoints, j);
                 if (ext_item != NULL) {
                     temp = cJSON_Print(ext_item);
+                    if (NULL == temp) {
+                        entry_array[j] = NULL;
+                        continue;
+                    }
                     temp[strlen(temp) - 1] = '\0';
                     entry_array[j] = loader_stack_alloc(strlen(temp) + 1);
                     strcpy(entry_array[j], &temp[1]);
-                    loader_tls_heap_free(temp);
+                    cJSON_Free(temp);
                 }
             }
             loader_add_to_dev_ext_list(inst, &props->device_extension_list,
@@ -2091,7 +2375,8 @@ loader_add_layer_properties(const struct loader_instance *inst,
     /* Fields in layer manifest file that are required:
      * (required) “file_format_version”
      *
-     * If more than one "layer" object are to be used, use the "layers" array instead.
+     * If more than one "layer" object are to be used, use the "layers" array
+     * instead.
      *
      * First get all required items and if any missing abort
      */
@@ -2107,6 +2392,9 @@ loader_add_layer_properties(const struct loader_instance *inst,
         return;
     }
     char *file_vers = cJSON_PrintUnformatted(item);
+    if (NULL == file_vers) {
+        return;
+    }
     loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
                "Found manifest file %s, version %s", filename, file_vers);
     // Get the major/minor/and patch as integers for easier comparison
@@ -2128,7 +2416,7 @@ loader_add_layer_properties(const struct loader_instance *inst,
                    "1.0.1), may cause errors",
                    filename);
     }
-    loader_tls_heap_free(file_vers);
+    cJSON_Free(file_vers);
     // If "layers" is present, read in the array of layer objects
     layers_node = cJSON_GetObjectItem(json, "layers");
     if (layers_node != NULL) {
@@ -2208,6 +2496,7 @@ loader_add_layer_properties(const struct loader_instance *inst,
  * This "home_location" is only used on Linux.
  *
  * \returns
+ * VKResult
  * A string list of manifest files to be opened in out_files param.
  * List has a pointer to string for each manifest filename.
  * When done using the list in out_files, pointers should be freed.
@@ -2220,31 +2509,32 @@ loader_add_layer_properties(const struct loader_instance *inst,
  * Linux ICD  | dirs     | files
  * Linux Layer| dirs     | dirs
  */
-static void loader_get_manifest_files(const struct loader_instance *inst,
-                                      const char *env_override,
-                                      const char *source_override, bool is_layer,
-                                      const char *location,
-                                      const char *home_location,
-                                      struct loader_manifest_files *out_files) {
-    const char *override = NULL;
-    char *loc;
+static VkResult loader_get_manifest_files(
+    const struct loader_instance *inst, const char *env_override,
+    char *source_override, bool is_layer, const char *location,
+    const char *home_location, struct loader_manifest_files *out_files) {
+    char * override = NULL;
+    char *loc, *orig_loc = NULL;
+    char *reg = NULL;
     char *file, *next_file, *name;
     size_t alloced_count = 64;
     char full_path[2048];
     DIR *sysdir = NULL;
     bool list_is_dirs = false;
     struct dirent *dent;
+    VkResult res = VK_SUCCESS;
 
     out_files->count = 0;
     out_files->filename_list = NULL;
 
     if (source_override != NULL) {
         override = source_override;
-    } else if (env_override != NULL && (override = loader_getenv(env_override))) {
+    } else if (env_override != NULL &&
+               (override = loader_getenv(env_override, inst))) {
 #if !defined(_WIN32)
         if (geteuid() != getuid() || getegid() != getgid()) {
             /* Don't allow setuid apps to use the env var: */
-            loader_free_getenv(override);
+            loader_free_getenv(override, inst);
             override = NULL;
         }
 #endif
@@ -2260,7 +2550,8 @@ static void loader_get_manifest_files(const struct loader_instance *inst,
             inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
             "Can't get manifest files with NULL location, env_override=%s",
             env_override);
-        return;
+        res = VK_ERROR_INITIALIZATION_FAILED;
+        goto out;
     }
 
 #if defined(_WIN32)
@@ -2275,35 +2566,45 @@ static void loader_get_manifest_files(const struct loader_instance *inst,
         if (loc == NULL) {
             loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
                        "Out of memory can't get manifest files");
-            return;
+            res = VK_ERROR_OUT_OF_HOST_MEMORY;
+            goto out;
         }
         strcpy(loc, location);
 #if defined(_WIN32)
-        loc = loader_get_registry_files(inst, loc);
-        if (loc == NULL) {
+        reg = loader_get_registry_files(inst, loc);
+        if (reg == NULL) {
             if (!is_layer) {
                 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
                            "Registry lookup failed can't get ICD manifest "
                            "files, do you have a Vulkan driver installed");
+                // This typically only fails when out of memory, which is
+                // critical
+                // if this is for the loader.
+                res = VK_ERROR_OUT_OF_HOST_MEMORY;
             } else {
                 // warning only for layers
                 loader_log(
                     inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
                     "Registry lookup failed can't get layer manifest files");
+                // Return success for now since it's not critical for layers
+                res = VK_SUCCESS;
             }
-            return;
+            goto out;
         }
+        orig_loc = loc;
+        loc = reg;
 #endif
     } else {
         loc = loader_stack_alloc(strlen(override) + 1);
         if (loc == NULL) {
             loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
                        "Out of memory can't get manifest files");
-            return;
+            res = VK_ERROR_OUT_OF_HOST_MEMORY;
+            goto out;
         }
         strcpy(loc, override);
         if (source_override == NULL) {
-            loader_free_getenv(override);
+            loader_free_getenv(override, inst);
         }
     }
 
@@ -2336,7 +2637,7 @@ static void loader_get_manifest_files(const struct loader_instance *inst,
             if (dir == NULL) {
                 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
                            "Out of memory can't get manifest files");
-                return;
+                goto out;
             }
             strcpy(dir, loc);
 
@@ -2351,28 +2652,32 @@ static void loader_get_manifest_files(const struct loader_instance *inst,
             const char *suf = name + nlen - 5;
             if ((nlen > 5) && !strncmp(suf, ".json", 5)) {
                 if (out_files->count == 0) {
-                    out_files->filename_list =
-                        loader_heap_alloc(inst, alloced_count * sizeof(char *),
-                                          VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
+                    out_files->filename_list = loader_instance_heap_alloc(
+                        inst, alloced_count * sizeof(char *),
+                        VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
                 } else if (out_files->count == alloced_count) {
-                    out_files->filename_list =
-                        loader_heap_realloc(inst, out_files->filename_list,
-                                            alloced_count * sizeof(char *),
-                                            alloced_count * sizeof(char *) * 2,
-                                            VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
+                    out_files->filename_list = loader_instance_heap_realloc(
+                        inst, out_files->filename_list,
+                        alloced_count * sizeof(char *),
+                        alloced_count * sizeof(char *) * 2,
+                        VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
                     alloced_count *= 2;
                 }
                 if (out_files->filename_list == NULL) {
                     loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
                                "Out of memory can't alloc manifest file list");
-                    return;
+                    res = VK_ERROR_OUT_OF_HOST_MEMORY;
+                    goto out;
                 }
-                out_files->filename_list[out_files->count] = loader_heap_alloc(
-                    inst, strlen(name) + 1, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
+                out_files->filename_list[out_files->count] =
+                    loader_instance_heap_alloc(
+                        inst, strlen(name) + 1,
+                        VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
                 if (out_files->filename_list[out_files->count] == NULL) {
                     loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
                                "Out of memory can't get manifest files");
-                    return;
+                    res = VK_ERROR_OUT_OF_HOST_MEMORY;
+                    goto out;
                 }
                 strcpy(out_files->filename_list[out_files->count], name);
                 out_files->count++;
@@ -2384,8 +2689,9 @@ static void loader_get_manifest_files(const struct loader_instance *inst,
             }
             if (list_is_dirs) {
                 dent = readdir(sysdir);
-                if (dent == NULL)
+                if (dent == NULL) {
                     break;
+                }
                 name = &(dent->d_name[0]);
                 loader_get_fullpath(name, file, sizeof(full_path), full_path);
                 name = full_path;
@@ -2393,8 +2699,10 @@ static void loader_get_manifest_files(const struct loader_instance *inst,
                 break;
             }
         }
-        if (sysdir)
+        if (sysdir) {
             closedir(sysdir);
+            sysdir = NULL;
+        }
         file = next_file;
 #if !defined(_WIN32)
         if (home_location != NULL &&
@@ -2407,7 +2715,8 @@ static void loader_get_manifest_files(const struct loader_instance *inst,
                 if (home_loc == NULL) {
                     loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
                                "Out of memory can't get manifest files");
-                    return;
+                    res = VK_ERROR_OUT_OF_HOST_MEMORY;
+                    goto out;
                 }
                 strcpy(home_loc, home);
                 // Add directory separator if needed
@@ -2430,7 +2739,25 @@ static void loader_get_manifest_files(const struct loader_instance *inst,
         }
 #endif
     }
-    return;
+
+out:
+    if (VK_SUCCESS != res && NULL != out_files->filename_list) {
+        for (uint32_t remove = 0; remove < out_files->count; remove++) {
+            loader_instance_heap_free(inst, out_files->filename_list[remove]);
+        }
+        loader_instance_heap_free(inst, out_files->filename_list);
+        out_files->count = 0;
+        out_files->filename_list = NULL;
+    }
+
+    if (NULL != sysdir) {
+        closedir(sysdir);
+    }
+
+    if (NULL != reg && reg != orig_loc) {
+        loader_instance_heap_free(inst, reg);
+    }
+    return res;
 }
 
 void loader_init_icd_lib_list() {}
@@ -2445,41 +2772,60 @@ void loader_destroy_icd_lib_list() {}
  * manifest files it finds the ICD libraries.
  *
  * \returns
- * a list of icds that were discovered
+ * Vulkan result
+ * (on result == VK_SUCCESS) a list of icds that were discovered
  */
-void loader_icd_scan(const struct loader_instance *inst,
-                     struct loader_icd_libs *icds) {
+VkResult loader_icd_scan(const struct loader_instance *inst,
+                         struct loader_icd_libs *icds) {
     char *file_str;
     uint16_t file_major_vers = 0;
     uint16_t file_minor_vers = 0;
     uint16_t file_patch_vers = 0;
     char *vers_tok;
     struct loader_manifest_files manifest_files;
+    VkResult res = VK_SUCCESS;
+    bool lockedMutex = false;
+    cJSON *json = NULL;
+
+    memset(&manifest_files, 0, sizeof(struct loader_manifest_files));
+
+    res = loader_scanned_icd_init(inst, icds);
+    if (VK_SUCCESS != res) {
+        goto out;
+    }
 
-    loader_scanned_icd_init(inst, icds);
     // Get a list of manifest files for ICDs
-    loader_get_manifest_files(inst, "VK_ICD_FILENAMES", NULL, false,
-                              DEFAULT_VK_DRIVERS_INFO, HOME_VK_DRIVERS_INFO,
-                              &manifest_files);
-    if (manifest_files.count == 0)
-        return;
+    res = loader_get_manifest_files(inst, "VK_ICD_FILENAMES", NULL, false,
+                                    DEFAULT_VK_DRIVERS_INFO,
+                                    HOME_VK_DRIVERS_INFO, &manifest_files);
+    if (VK_SUCCESS != res || manifest_files.count == 0) {
+        goto out;
+    }
     loader_platform_thread_lock_mutex(&loader_json_lock);
+    lockedMutex = true;
     for (uint32_t i = 0; i < manifest_files.count; i++) {
         file_str = manifest_files.filename_list[i];
-        if (file_str == NULL)
+        if (file_str == NULL) {
             continue;
+        }
 
-        cJSON *json;
         json = loader_get_json(inst, file_str);
-        if (!json)
+        if (!json) {
             continue;
+        }
         cJSON *item, *itemICD;
         item = cJSON_GetObjectItem(json, "file_format_version");
         if (item == NULL) {
-            loader_platform_thread_unlock_mutex(&loader_json_lock);
-            return;
+            res = VK_ERROR_INITIALIZATION_FAILED;
+            goto out;
         }
         char *file_vers = cJSON_Print(item);
+        if (NULL == file_vers) {
+            // Only reason the print can fail is if there was an allocation
+            // issue
+            res = VK_ERROR_OUT_OF_HOST_MEMORY;
+            goto out;
+        }
         loader_log(inst, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, 0,
                    "Found manifest file %s, version %s", file_str, file_vers);
         // Get the major/minor/and patch as integers for easier comparison
@@ -2497,9 +2843,10 @@ void loader_icd_scan(const struct loader_instance *inst,
         }
         if (file_major_vers != 1 || file_minor_vers != 0 || file_patch_vers > 1)
             loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
-                       "Unexpected manifest file version (expected 1.0.0 or 1.0.1), may "
+                       "Unexpected manifest file version (expected 1.0.0 or "
+                       "1.0.1), may "
                        "cause errors");
-        loader_tls_heap_free(file_vers);
+        cJSON_Free(file_vers);
         itemICD = cJSON_GetObjectItem(json, "ICD");
         if (itemICD != NULL) {
             item = cJSON_GetObjectItem(itemICD, "library_path");
@@ -2510,23 +2857,23 @@ void loader_icd_scan(const struct loader_instance *inst,
                                "Can't find \"library_path\" in ICD JSON file "
                                "%s, skipping",
                                file_str);
-                    loader_tls_heap_free(temp);
-                    loader_heap_free(inst, file_str);
+                    cJSON_Free(temp);
                     cJSON_Delete(json);
+                    json = NULL;
                     continue;
                 }
                 // strip out extra quotes
                 temp[strlen(temp) - 1] = '\0';
                 char *library_path = loader_stack_alloc(strlen(temp) + 1);
                 strcpy(library_path, &temp[1]);
-                loader_tls_heap_free(temp);
+                cJSON_Free(temp);
                 if (!library_path || strlen(library_path) == 0) {
                     loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
                                "Can't find \"library_path\" in ICD JSON file "
                                "%s, skipping",
                                file_str);
-                    loader_heap_free(inst, file_str);
                     cJSON_Delete(json);
+                    json = NULL;
                     continue;
                 }
                 char fullpath[MAX_STRING_SIZE];
@@ -2553,26 +2900,50 @@ void loader_icd_scan(const struct loader_instance *inst,
                 item = cJSON_GetObjectItem(itemICD, "api_version");
                 if (item != NULL) {
                     temp = cJSON_Print(item);
+                    if (NULL == temp) {
+                        // Only reason the print can fail is if there was an
+                        // allocation issue
+                        res = VK_ERROR_OUT_OF_HOST_MEMORY;
+                        goto out;
+                    }
                     vers = loader_make_version(temp);
-                    loader_tls_heap_free(temp);
+                    cJSON_Free(temp);
                 }
                 loader_scanned_icd_add(inst, icds, fullpath, vers);
-            } else
+            } else {
                 loader_log(inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
                            "Can't find \"library_path\" object in ICD JSON "
                            "file %s, skipping",
                            file_str);
-        } else
+            }
+        } else {
             loader_log(
                 inst, VK_DEBUG_REPORT_WARNING_BIT_EXT, 0,
                 "Can't find \"ICD\" object in ICD JSON file %s, skipping",
                 file_str);
+        }
 
-        loader_heap_free(inst, file_str);
         cJSON_Delete(json);
+        json = NULL;
     }
-    loader_heap_free(inst, manifest_files.filename_list);
-    loader_platform_thread_unlock_mutex(&loader_json_lock);
+
+out:
+    if (NULL != json) {
+        cJSON_Delete(json);
+    }
+    if (NULL != manifest_files.filename_list) {
+        for (uint32_t i = 0; i < manifest_files.count; i++) {
+            if (NULL != manifest_files.filename_list[i]) {
+                loader_instance_heap_free(inst,
+                                          manifest_files.filename_list[i]);
+            }
+        }
+        loader_instance_heap_free(inst, manifest_files.filename_list);
+    }
+    if (lockedMutex) {
+        loader_platform_thread_unlock_mutex(&loader_json_lock);
+    }
+    return res;
 }
 
 void loader_layer_scan(const struct loader_instance *inst,
@@ -2583,22 +2954,31 @@ void loader_layer_scan(const struct loader_instance *inst,
     cJSON *json;
     uint32_t i;
     uint32_t implicit;
+    bool lockedMutex = false;
+
+    memset(manifest_files, 0, sizeof(struct loader_manifest_files) * 2);
 
     // Get a list of manifest files for  explicit layers
-    loader_get_manifest_files(inst, LAYERS_PATH_ENV, LAYERS_SOURCE_PATH, true,
-                              DEFAULT_VK_ELAYERS_INFO, HOME_VK_ELAYERS_INFO,
-                              &manifest_files[0]);
+    VkResult res = loader_get_manifest_files(
+        inst, LAYERS_PATH_ENV, LAYERS_SOURCE_PATH, true,
+        DEFAULT_VK_ELAYERS_INFO, HOME_VK_ELAYERS_INFO, &manifest_files[0]);
+    if (VK_SUCCESS != res || manifest_files[0].count == 0) {
+        goto out;
+    }
     // Pass NULL for environment variable override - implicit layers are not
     // overridden by LAYERS_PATH_ENV
-    loader_get_manifest_files(inst, NULL, NULL, true, DEFAULT_VK_ILAYERS_INFO,
-                              HOME_VK_ILAYERS_INFO, &manifest_files[1]);
-    if (manifest_files[0].count == 0 && manifest_files[1].count == 0)
-        return;
+    res = loader_get_manifest_files(inst, NULL, NULL, true,
+                                    DEFAULT_VK_ILAYERS_INFO,
+                                    HOME_VK_ILAYERS_INFO, &manifest_files[1]);
+    if (VK_SUCCESS != res || manifest_files[1].count == 0) {
+        goto out;
+    }
 
     /* cleanup any previously scanned libraries */
     loader_delete_layer_properties(inst, instance_layers);
 
     loader_platform_thread_lock_mutex(&loader_json_lock);
+    lockedMutex = true;
     for (implicit = 0; implicit < 2; implicit++) {
         for (i = 0; i < manifest_files[implicit].count; i++) {
             file_str = manifest_files[implicit].filename_list[i];
@@ -2613,23 +2993,31 @@ void loader_layer_scan(const struct loader_instance *inst,
 
             loader_add_layer_properties(inst, instance_layers, json,
                                         (implicit == 1), file_str);
-
-            loader_heap_free(inst, file_str);
             cJSON_Delete(json);
         }
     }
-    if (manifest_files[0].count != 0)
-        loader_heap_free(inst, manifest_files[0].filename_list);
-
-    if (manifest_files[1].count != 0)
-        loader_heap_free(inst, manifest_files[1].filename_list);
 
     // add a meta layer for validation if the validation layers are all present
-    loader_add_layer_property_meta(
-        inst, sizeof(std_validation_names) / sizeof(std_validation_names[0]),
-        std_validation_names, instance_layers);
-
-    loader_platform_thread_unlock_mutex(&loader_json_lock);
+    loader_add_layer_property_meta(inst, sizeof(std_validation_names) /
+                                             sizeof(std_validation_names[0]),
+                                   std_validation_names, instance_layers);
+
+out:
+    for (uint32_t manFile = 0; manFile < 2; manFile++) {
+        if (NULL != manifest_files[manFile].filename_list) {
+            for (uint32_t i = 0; i < manifest_files[manFile].count; i++) {
+                if (NULL != manifest_files[manFile].filename_list[i]) {
+                    loader_instance_heap_free(
+                        inst, manifest_files[manFile].filename_list[i]);
+                }
+            }
+            loader_instance_heap_free(inst,
+                                      manifest_files[manFile].filename_list);
+        }
+    }
+    if (lockedMutex) {
+        loader_platform_thread_unlock_mutex(&loader_json_lock);
+    }
 }
 
 void loader_implicit_layer_scan(const struct loader_instance *inst,
@@ -2641,9 +3029,10 @@ void loader_implicit_layer_scan(const struct loader_instance *inst,
 
     // Pass NULL for environment variable override - implicit layers are not
     // overridden by LAYERS_PATH_ENV
-    loader_get_manifest_files(inst, NULL, NULL, true, DEFAULT_VK_ILAYERS_INFO,
-                              HOME_VK_ILAYERS_INFO, &manifest_files);
-    if (manifest_files.count == 0) {
+    VkResult res = loader_get_manifest_files(
+        inst, NULL, NULL, true, DEFAULT_VK_ILAYERS_INFO, HOME_VK_ILAYERS_INFO,
+        &manifest_files);
+    if (VK_SUCCESS != res || manifest_files.count == 0) {
         return;
     }
 
@@ -2664,21 +3053,18 @@ void loader_implicit_layer_scan(const struct loader_instance *inst,
             continue;
         }
 
-        loader_add_layer_properties(inst, instance_layers, json,
-                                    true, file_str);
+        loader_add_layer_properties(inst, instance_layers, json, true,
+                                    file_str);
 
-        loader_heap_free(inst, file_str);
+        loader_instance_heap_free(inst, file_str);
         cJSON_Delete(json);
     }
-
-    if (manifest_files.count != 0) {
-        loader_heap_free(inst, manifest_files.filename_list);
-    }
+    loader_instance_heap_free(inst, manifest_files.filename_list);
 
     // add a meta layer for validation if the validation layers are all present
-    loader_add_layer_property_meta(
-        inst, sizeof(std_validation_names) / sizeof(std_validation_names[0]),
-        std_validation_names, instance_layers);
+    loader_add_layer_property_meta(inst, sizeof(std_validation_names) /
+                                             sizeof(std_validation_names[0]),
+                                   std_validation_names, instance_layers);
 
     loader_platform_thread_unlock_mutex(&loader_json_lock);
 }
@@ -2827,8 +3213,8 @@ loader_check_layers_for_address(const struct loader_instance *const inst,
 
 static void loader_free_dev_ext_table(struct loader_instance *inst) {
     for (uint32_t i = 0; i < MAX_NUM_DEV_EXTS; i++) {
-        loader_heap_free(inst, inst->disp_hash[i].func_name);
-        loader_heap_free(inst, inst->disp_hash[i].list.index);
+        loader_instance_heap_free(inst, inst->disp_hash[i].func_name);
+        loader_instance_heap_free(inst, inst->disp_hash[i].list.index);
     }
     memset(inst->disp_hash, 0, sizeof(inst->disp_hash));
 }
@@ -2842,7 +3228,7 @@ static bool loader_add_dev_ext_table(struct loader_instance *inst,
     if (!inst->disp_hash[idx].func_name) {
         // no entry here at this idx, so use it
         assert(list->capacity == 0);
-        inst->disp_hash[idx].func_name = (char *)loader_heap_alloc(
+        inst->disp_hash[idx].func_name = (char *)loader_instance_heap_alloc(
             inst, strlen(funcName) + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
         if (inst->disp_hash[idx].func_name == NULL) {
             loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
@@ -2856,7 +3242,7 @@ static bool loader_add_dev_ext_table(struct loader_instance *inst,
 
     // check for enough capacity
     if (list->capacity == 0) {
-        list->index = loader_heap_alloc(inst, 8 * sizeof(*(list->index)),
+        list->index = loader_instance_heap_alloc(inst, 8 * sizeof(*(list->index)),
                                         VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
         if (list->index == NULL) {
             loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
@@ -2865,7 +3251,7 @@ static bool loader_add_dev_ext_table(struct loader_instance *inst,
         }
         list->capacity = 8 * sizeof(*(list->index));
     } else if (list->capacity < (list->count + 1) * sizeof(*(list->index))) {
-        list->index = loader_heap_realloc(inst, list->index, list->capacity,
+        list->index = loader_instance_heap_realloc(inst, list->index, list->capacity,
                                           list->capacity * 2,
                                           VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
         if (list->index == NULL) {
@@ -2883,7 +3269,7 @@ static bool loader_add_dev_ext_table(struct loader_instance *inst,
         if (!inst->disp_hash[i].func_name) {
             assert(inst->disp_hash[i].list.capacity == 0);
             inst->disp_hash[i].func_name =
-                (char *)loader_heap_alloc(inst, strlen(funcName) + 1,
+                (char *)loader_instance_heap_alloc(inst, strlen(funcName) + 1,
                                           VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
             if (inst->disp_hash[i].func_name == NULL) {
                 loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
@@ -3020,6 +3406,7 @@ loader_close_layer_lib(const struct loader_instance *inst,
 }
 
 void loader_deactivate_layers(const struct loader_instance *instance,
+                              struct loader_device *device,
                               struct loader_layer_list *list) {
     /* delete instance list of enabled layers and close any layer libraries */
     for (uint32_t i = 0; i < list->count; i++) {
@@ -3027,7 +3414,7 @@ void loader_deactivate_layers(const struct loader_instance *instance,
 
         loader_close_layer_lib(instance, layer_prop);
     }
-    loader_destroy_layer_list(instance, list);
+    loader_destroy_layer_list(instance, device, list);
 }
 
 /**
@@ -3054,23 +3441,25 @@ loader_add_layer_implicit(const struct loader_instance *inst,
             if (prop->enable_env_var.name[0] == 0) {
                 enable = true;
             } else {
-                env_value = loader_getenv(prop->enable_env_var.name);
+                env_value = loader_getenv(prop->enable_env_var.name, inst);
                 if (env_value && !strcmp(prop->enable_env_var.value, env_value))
                     enable = true;
-                loader_free_getenv(env_value);
+                loader_free_getenv(env_value, inst);
             }
 
             // disable_environment has priority, i.e. if both enable and disable
             // environment variables are set, the layer is disabled. Implicit
             // layers
             // are required to have a disable_environment variables
-            env_value = loader_getenv(prop->disable_env_var.name);
-            if (env_value)
+            env_value = loader_getenv(prop->disable_env_var.name, inst);
+            if (env_value) {
                 enable = false;
-            loader_free_getenv(env_value);
+            }
+            loader_free_getenv(env_value, inst);
 
-            if (enable)
+            if (enable) {
                 loader_add_to_layer_list(inst, list, 1, prop);
+            }
         }
     }
 }
@@ -3088,7 +3477,7 @@ static void loader_add_layer_env(struct loader_instance *inst,
     char *layerEnv;
     char *next, *name;
 
-    layerEnv = loader_getenv(env_name);
+    layerEnv = loader_getenv(env_name, inst);
     if (layerEnv == NULL) {
         return;
     }
@@ -3098,7 +3487,7 @@ static void loader_add_layer_env(struct loader_instance *inst,
     }
     strcpy(name, layerEnv);
 
-    loader_free_getenv(layerEnv);
+    loader_free_getenv(layerEnv, inst);
 
     while (name && *name) {
         next = loader_get_next_path(name);
@@ -3554,10 +3943,9 @@ VkResult loader_validate_device_extensions(
  * Terminator functions for the Instance chain
  * All named terminator_<Vulakn API name>
  */
-VKAPI_ATTR VkResult VKAPI_CALL
-terminator_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
-                          const VkAllocationCallbacks *pAllocator,
-                          VkInstance *pInstance) {
+VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateInstance(
+    const VkInstanceCreateInfo *pCreateInfo,
+    const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) {
     struct loader_icd *icd;
     VkExtensionProperties *prop;
     char **filtered_extension_names = NULL;
@@ -3588,51 +3976,58 @@ terminator_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
 
     for (uint32_t i = 0; i < ptr_instance->icd_libs.count; i++) {
         icd = loader_icd_add(ptr_instance, &ptr_instance->icd_libs.list[i]);
-        if (icd) {
-            icd_create_info.enabledExtensionCount = 0;
-            struct loader_extension_list icd_exts;
-
-            loader_log(ptr_instance, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
-                       "Build ICD instance extension list");
-            // traverse scanned icd list adding non-duplicate extensions to the
-            // list
-            loader_init_generic_list(ptr_instance,
-                                     (struct loader_generic_list *)&icd_exts,
-                                     sizeof(VkExtensionProperties));
-            loader_add_instance_extensions(
-                ptr_instance,
-                icd->this_icd_lib->EnumerateInstanceExtensionProperties,
-                icd->this_icd_lib->lib_name, &icd_exts);
-
-            for (uint32_t j = 0; j < pCreateInfo->enabledExtensionCount; j++) {
-                prop = get_extension_property(
-                    pCreateInfo->ppEnabledExtensionNames[j], &icd_exts);
-                if (prop) {
-                    filtered_extension_names[icd_create_info
-                                                 .enabledExtensionCount] =
-                        (char *)pCreateInfo->ppEnabledExtensionNames[j];
-                    icd_create_info.enabledExtensionCount++;
-                }
+        if (NULL == icd) {
+            while (NULL != ptr_instance->icds) {
+                icd = ptr_instance->icds;
+                ptr_instance->icds = icd->next;
+                icd->DestroyInstance(icd->instance, pAllocator);
+                loader_icd_destroy(ptr_instance, icd, pAllocator);
             }
+            return VK_ERROR_OUT_OF_HOST_MEMORY;
+        }
+        icd_create_info.enabledExtensionCount = 0;
+        struct loader_extension_list icd_exts;
 
-            loader_destroy_generic_list(
-                ptr_instance, (struct loader_generic_list *)&icd_exts);
-
-            res = ptr_instance->icd_libs.list[i].CreateInstance(
-                &icd_create_info, pAllocator, &(icd->instance));
-            if (res == VK_SUCCESS)
-                success = loader_icd_init_entrys(
-                    icd, icd->instance,
-                    ptr_instance->icd_libs.list[i].GetInstanceProcAddr);
-
-            if (res != VK_SUCCESS || !success) {
-                ptr_instance->icds = ptr_instance->icds->next;
-                loader_icd_destroy(ptr_instance, icd);
-                loader_log(ptr_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
-                           "ICD ignored: failed to CreateInstance and find "
-                           "entrypoints with ICD");
+        loader_log(ptr_instance, VK_DEBUG_REPORT_DEBUG_BIT_EXT, 0,
+                   "Build ICD instance extension list");
+        // traverse scanned icd list adding non-duplicate extensions to the
+        // list
+        loader_init_generic_list(ptr_instance,
+                                 (struct loader_generic_list *)&icd_exts,
+                                 sizeof(VkExtensionProperties));
+        loader_add_instance_extensions(
+            ptr_instance,
+            icd->this_icd_lib->EnumerateInstanceExtensionProperties,
+            icd->this_icd_lib->lib_name, &icd_exts);
+
+        for (uint32_t j = 0; j < pCreateInfo->enabledExtensionCount; j++) {
+            prop = get_extension_property(
+                pCreateInfo->ppEnabledExtensionNames[j], &icd_exts);
+            if (prop) {
+                filtered_extension_names[icd_create_info
+                                             .enabledExtensionCount] =
+                    (char *)pCreateInfo->ppEnabledExtensionNames[j];
+                icd_create_info.enabledExtensionCount++;
             }
         }
+
+        loader_destroy_generic_list(ptr_instance,
+                                    (struct loader_generic_list *)&icd_exts);
+
+        res = ptr_instance->icd_libs.list[i].CreateInstance(
+            &icd_create_info, pAllocator, &(icd->instance));
+        if (res == VK_SUCCESS)
+            success = loader_icd_init_entrys(
+                icd, icd->instance,
+                ptr_instance->icd_libs.list[i].GetInstanceProcAddr);
+
+        if (res != VK_SUCCESS || !success) {
+            ptr_instance->icds = ptr_instance->icds->next;
+            loader_icd_destroy(ptr_instance, icd, pAllocator);
+            loader_log(ptr_instance, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
+                       "ICD ignored: failed to CreateInstance and find "
+                       "entrypoints with ICD");
+        }
     }
 
     /*
@@ -3651,9 +4046,8 @@ terminator_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
     return VK_SUCCESS;
 }
 
-VKAPI_ATTR void VKAPI_CALL
-terminator_DestroyInstance(VkInstance instance,
-                           const VkAllocationCallbacks *pAllocator) {
+VKAPI_ATTR void VKAPI_CALL terminator_DestroyInstance(
+    VkInstance instance, const VkAllocationCallbacks *pAllocator) {
     struct loader_instance *ptr_instance = loader_instance(instance);
     struct loader_icd *icds = ptr_instance->icds;
     struct loader_icd *next_icd;
@@ -3680,7 +4074,7 @@ terminator_DestroyInstance(VkInstance instance,
         }
         next_icd = icds->next;
         icds->instance = VK_NULL_HANDLE;
-        loader_icd_destroy(ptr_instance, icds);
+        loader_icd_destroy(ptr_instance, icds, pAllocator);
 
         icds = next_icd;
     }
@@ -3691,23 +4085,26 @@ terminator_DestroyInstance(VkInstance instance,
     loader_destroy_generic_list(
         ptr_instance, (struct loader_generic_list *)&ptr_instance->ext_list);
     if (ptr_instance->phys_devs_term)
-        loader_heap_free(ptr_instance, ptr_instance->phys_devs_term);
+        loader_instance_heap_free(ptr_instance, ptr_instance->phys_devs_term);
     loader_free_dev_ext_table(ptr_instance);
 }
 
-VKAPI_ATTR VkResult VKAPI_CALL
-terminator_CreateDevice(VkPhysicalDevice physicalDevice,
-                        const VkDeviceCreateInfo *pCreateInfo,
-                        const VkAllocationCallbacks *pAllocator,
-                        VkDevice *pDevice) {
+VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDevice(
+    VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
+    const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
+    VkResult res = VK_SUCCESS;
     struct loader_physical_device *phys_dev;
     phys_dev = (struct loader_physical_device *)physicalDevice;
 
     struct loader_device *dev = (struct loader_device *)*pDevice;
     PFN_vkCreateDevice fpCreateDevice = phys_dev->this_icd->CreateDevice;
+    struct loader_extension_list icd_exts;
+
+    icd_exts.list = NULL;
 
     if (fpCreateDevice == NULL) {
-        return VK_ERROR_INITIALIZATION_FAILED;
+        res = VK_ERROR_INITIALIZATION_FAILED;
+        goto out;
     }
 
     VkDeviceCreateInfo localCreateInfo;
@@ -3735,12 +4132,11 @@ terminator_CreateDevice(VkPhysicalDevice physicalDevice,
         (const char *const *)filtered_extension_names;
 
     /* Get the physical device (ICD) extensions  */
-    struct loader_extension_list icd_exts;
-    VkResult res;
     if (!loader_init_generic_list(phys_dev->this_icd->this_instance,
                                   (struct loader_generic_list *)&icd_exts,
                                   sizeof(VkExtensionProperties))) {
-        return VK_ERROR_OUT_OF_HOST_MEMORY;
+        res = VK_ERROR_OUT_OF_HOST_MEMORY;
+        goto out;
     }
 
     res = loader_add_device_extensions(
@@ -3749,7 +4145,7 @@ terminator_CreateDevice(VkPhysicalDevice physicalDevice,
         phys_dev->phys_dev, phys_dev->this_icd->this_icd_lib->lib_name,
         &icd_exts);
     if (res != VK_SUCCESS) {
-        return res;
+        goto out;
     }
 
     for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
@@ -3771,7 +4167,7 @@ terminator_CreateDevice(VkPhysicalDevice physicalDevice,
                                            pAllocator, &dev->device);
 
     if (res != VK_SUCCESS) {
-        return res;
+        goto out;
     }
 
     *pDevice = dev->device;
@@ -3781,6 +4177,12 @@ terminator_CreateDevice(VkPhysicalDevice physicalDevice,
     /* Init dispatch pointer in new device object */
     loader_init_dispatch(*pDevice, &dev->loader_dispatch);
 
+out:
+    if (NULL != icd_exts.list) {
+        loader_destroy_generic_list(phys_dev->this_icd->this_instance,
+                                    (struct loader_generic_list *)&icd_exts);
+    }
+
     return res;
 }
 
@@ -3846,8 +4248,8 @@ terminator_EnumeratePhysicalDevices(VkInstance instance,
                      : *pPhysicalDeviceCount;
 
     if (inst->phys_devs_term)
-        loader_heap_free(inst, inst->phys_devs_term);
-    inst->phys_devs_term = loader_heap_alloc(
+        loader_instance_heap_free(inst, inst->phys_devs_term);
+    inst->phys_devs_term = loader_instance_heap_alloc(
         inst, sizeof(struct loader_physical_device) * copy_count,
         VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
     if (!inst->phys_devs_term)
@@ -4017,8 +4419,6 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceExtensionProperties(
         loader_add_to_ext_list(icd->this_instance, &all_exts, icd_exts.count,
                                icd_exts.list);
 
-        loader_init_layer_list(icd->this_instance, &implicit_layer_list);
-
         loader_add_layer_implicit(
             icd->this_instance, VK_LAYER_TYPE_INSTANCE_IMPLICIT,
             &implicit_layer_list, &icd->this_instance->instance_layer_list);
@@ -4048,6 +4448,8 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_EnumerateDeviceExtensionProperties(
         }
         loader_destroy_generic_list(icd->this_instance,
                                     (struct loader_generic_list *)&all_exts);
+        loader_destroy_generic_list(icd->this_instance,
+                                    (struct loader_generic_list *)&icd_exts);
     } else {
         /* just return the count; need to add in the count of implicit layer
          * extensions
index ecc945e..0a30092 100644 (file)
@@ -44,6 +44,9 @@
 #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
 #define VK_MAJOR(version) (version >> 22)
 #define VK_MINOR(version) ((version >> 12) & 0x3ff)
@@ -170,6 +173,8 @@ struct loader_device {
 
     struct loader_layer_list activated_layer_list;
 
+    VkAllocationCallbacks alloc_callbacks;
+
     struct loader_device *next;
 };
 
@@ -393,14 +398,14 @@ struct loader_msg_callback_map_entry {
 };
 
 /* helper function definitions */
-void *loader_heap_alloc(const struct loader_instance *instance, size_t size,
-                        VkSystemAllocationScope allocationScope);
-
-void loader_heap_free(const struct loader_instance *instance, void *pMemory);
-
-void *loader_tls_heap_alloc(size_t size);
-
-void loader_tls_heap_free(void *pMemory);
+void *loader_instance_heap_alloc(const struct loader_instance *instance, size_t size, VkSystemAllocationScope allocationScope);
+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);
+void *loader_instance_tls_heap_alloc(size_t size);
+void loader_instance_tls_heap_free(void *pMemory);
+void *loader_device_heap_alloc(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);
 
 void loader_log(const struct loader_instance *inst, VkFlags msg_type,
                 int32_t msg_code, const char *format, ...);
@@ -420,9 +425,9 @@ VkResult loader_validate_instance_extensions(
     const VkInstanceCreateInfo *pCreateInfo);
 
 void loader_initialize(void);
-void loader_copy_layer_properties(const struct loader_instance *inst,
-                                         struct loader_layer_properties *dst,
-                                         struct loader_layer_properties *src);
+VkResult loader_copy_layer_properties(const struct loader_instance *inst,
+                                      struct loader_layer_properties *dst,
+                                      struct loader_layer_properties *src);
 bool has_vk_extension_property_array(const VkExtensionProperties *vk_ext_prop,
                                      const uint32_t count,
                                      const VkExtensionProperties *ext_array);
@@ -450,12 +455,13 @@ bool loader_init_generic_list(const struct loader_instance *inst,
 void loader_destroy_generic_list(const struct loader_instance *inst,
                                  struct loader_generic_list *list);
 void loader_destroy_layer_list(const struct loader_instance *inst,
+                               struct loader_device *device,
                                struct loader_layer_list *layer_list);
 void loader_delete_layer_properties(const struct loader_instance *inst,
                                     struct loader_layer_list *layer_list);
 bool loader_find_layer_name_array(const char *name, uint32_t layer_count,
                         const char layer_list[][VK_MAX_EXTENSION_NAME_SIZE]);
-void loader_expand_layer_names(
+VkResult loader_expand_layer_names(
     struct loader_instance *inst, const char *key_name,
     uint32_t expand_count,
     const char expand_names[][VK_MAX_EXTENSION_NAME_SIZE],
@@ -467,17 +473,17 @@ void loader_delete_shadow_dev_layer_names(const struct loader_instance *inst,
 void loader_delete_shadow_inst_layer_names(const struct loader_instance *inst,
                                            const VkInstanceCreateInfo *orig,
                                            VkInstanceCreateInfo *ours);
-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);
+VkResult 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_find_layer_name_add_list(
     const struct loader_instance *inst, const char *name,
     const enum layer_type type, const struct loader_layer_list *search_list,
     struct loader_layer_list *found_list);
 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,
+VkResult 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);
@@ -494,15 +500,22 @@ void *loader_dev_ext_gpa(struct loader_instance *inst, const char *funcName);
 void *loader_get_dev_ext_trampoline(uint32_t index);
 struct loader_instance *loader_get_instance(const VkInstance instance);
 void loader_deactivate_layers(const struct loader_instance *instance,
+                              struct loader_device *device,
                               struct loader_layer_list *list);
 struct loader_device *
-loader_create_logical_device(const struct loader_instance *inst);
+loader_create_logical_device(const struct loader_instance *inst, const VkAllocationCallbacks *pAllocator);
 void loader_add_logical_device(const struct loader_instance *inst,
                                struct loader_icd *icd,
                                struct loader_device *found_dev);
 void loader_remove_logical_device(const struct loader_instance *inst,
                                   struct loader_icd *icd,
-                                  struct loader_device *found_dev);
+                                  struct loader_device *found_dev,
+                                  const VkAllocationCallbacks *pAllocator);
+// NOTE: Outside of loader, this entry-point is only proivided for error cleanup.
+void loader_destroy_logical_device(const struct loader_instance *inst,
+                                   struct loader_device *dev,
+                                   const VkAllocationCallbacks *pAllocator);
+
 VkResult
 loader_enable_instance_layers(struct loader_instance *inst,
                               const VkInstanceCreateInfo *pCreateInfo,
index 8b1117d..4e4aaf7 100644 (file)
@@ -122,6 +122,7 @@ vkEnumerateInstanceExtensionProperties(const char *pLayerName,
     struct loader_extension_list local_ext_list;
     struct loader_icd_libs icd_libs;
     uint32_t copy_size;
+    VkResult res = VK_SUCCESS;
 
     tls_instance = NULL;
     memset(&local_ext_list, 0, sizeof(local_ext_list));
@@ -134,7 +135,8 @@ vkEnumerateInstanceExtensionProperties(const char *pLayerName,
             VK_STRING_ERROR_NONE) {
             assert(VK_FALSE && "vkEnumerateInstanceExtensionProperties:  "
                                "pLayerName is too long or is badly formed");
-            return VK_ERROR_EXTENSION_NOT_PRESENT;
+            res = VK_ERROR_EXTENSION_NOT_PRESENT;
+            goto out;
         }
 
         loader_layer_scan(NULL, &instance_layers);
@@ -154,7 +156,7 @@ vkEnumerateInstanceExtensionProperties(const char *pLayerName,
                 loader_add_to_ext_list(NULL, &local_ext_list, ext_list->count,
                                        ext_list->list);
             }
-            loader_destroy_layer_list(NULL, &local_list);
+            loader_destroy_layer_list(NULL, NULL, &local_list);
             global_ext_list = &local_ext_list;
 
         } else {
@@ -170,7 +172,10 @@ vkEnumerateInstanceExtensionProperties(const char *pLayerName,
     } else {
         /* Scan/discover all ICD libraries */
         memset(&icd_libs, 0, sizeof(struct loader_icd_libs));
-        loader_icd_scan(NULL, &icd_libs);
+        res = loader_icd_scan(NULL, &icd_libs);
+        if (VK_SUCCESS != res) {
+            goto out;
+        }
         /* get extensions from all ICD's, merge so no duplicates */
         loader_get_icd_loader_instance_extensions(NULL, &icd_libs,
                                                   &local_ext_list);
@@ -189,16 +194,13 @@ vkEnumerateInstanceExtensionProperties(const char *pLayerName,
     }
 
     if (global_ext_list == NULL) {
-        loader_destroy_layer_list(NULL, &instance_layers);
-        return VK_ERROR_LAYER_NOT_PRESENT;
+        res = VK_ERROR_LAYER_NOT_PRESENT;
+        goto out;
     }
 
     if (pProperties == NULL) {
         *pPropertyCount = global_ext_list->count;
-        loader_destroy_layer_list(NULL, &instance_layers);
-        loader_destroy_generic_list(
-            NULL, (struct loader_generic_list *)&local_ext_list);
-        return VK_SUCCESS;
+        goto out;
     }
 
     copy_size = *pPropertyCount < global_ext_list->count
@@ -209,16 +211,16 @@ vkEnumerateInstanceExtensionProperties(const char *pLayerName,
                sizeof(VkExtensionProperties));
     }
     *pPropertyCount = copy_size;
-    loader_destroy_generic_list(NULL,
-                                (struct loader_generic_list *)&local_ext_list);
 
     if (copy_size < global_ext_list->count) {
-        loader_destroy_layer_list(NULL, &instance_layers);
-        return VK_INCOMPLETE;
+        res = VK_INCOMPLETE;
+        goto out;
     }
 
-    loader_destroy_layer_list(NULL, &instance_layers);
-    return VK_SUCCESS;
+out:
+    loader_destroy_generic_list(NULL, (struct loader_generic_list *)&local_ext_list);
+    loader_destroy_layer_list(NULL, NULL, &instance_layers);
+    return res;
 }
 
 LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
@@ -238,7 +240,7 @@ vkEnumerateInstanceLayerProperties(uint32_t *pPropertyCount,
 
     if (pProperties == NULL) {
         *pPropertyCount = instance_layer_list.count;
-        loader_destroy_layer_list(NULL, &instance_layer_list);
+        loader_destroy_layer_list(NULL, NULL, &instance_layer_list);
         return VK_SUCCESS;
     }
 
@@ -251,7 +253,7 @@ vkEnumerateInstanceLayerProperties(uint32_t *pPropertyCount,
     }
 
     *pPropertyCount = copy_size;
-    loader_destroy_layer_list(NULL, &instance_layer_list);
+    loader_destroy_layer_list(NULL, NULL, &instance_layer_list);
 
     if (copy_size < instance_layer_list.count) {
         return VK_INCOMPLETE;
@@ -260,41 +262,43 @@ vkEnumerateInstanceLayerProperties(uint32_t *pPropertyCount,
     return VK_SUCCESS;
 }
 
-LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
-vkCreateInstance(const VkInstanceCreateInfo *pCreateInfo,
-                 const VkAllocationCallbacks *pAllocator,
-                 VkInstance *pInstance) {
+LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateInstance(
+    const VkInstanceCreateInfo *pCreateInfo,
+    const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) {
     struct loader_instance *ptr_instance = NULL;
     VkInstance created_instance = VK_NULL_HANDLE;
+    bool loaderLocked = false;
     VkResult res = VK_ERROR_INITIALIZATION_FAILED;
 
     loader_platform_thread_once(&once_init, loader_initialize);
 
-    //TODO start handling the pAllocators again
-#if 0
-       if (pAllocator) {
-        ptr_instance = (struct loader_instance *) pAllocator->pfnAllocation(
-                           pAllocator->pUserData,
-                           sizeof(struct loader_instance),
-                           sizeof(int *),
-                           VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+#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 *)malloc(sizeof(struct loader_instance));
+    }
+
+    VkInstanceCreateInfo ici = *pCreateInfo;
+
     if (ptr_instance == NULL) {
-        return VK_ERROR_OUT_OF_HOST_MEMORY;
+        res = VK_ERROR_OUT_OF_HOST_MEMORY;
+        goto out;
     }
 
     tls_instance = ptr_instance;
     loader_platform_thread_lock_mutex(&loader_lock);
+    loaderLocked = true;
     memset(ptr_instance, 0, sizeof(struct loader_instance));
-#if 0
     if (pAllocator) {
         ptr_instance->alloc_callbacks = *pAllocator;
     }
-#endif
 
     /*
      * Look for one or more debug report create info structures
@@ -309,9 +313,8 @@ vkCreateInstance(const VkInstanceCreateInfo *pCreateInfo,
                                         &ptr_instance->tmp_callbacks)) {
         // One or more were found, but allocation failed.  Therefore, clean up
         // and fail this function:
-        loader_heap_free(ptr_instance, ptr_instance);
-        loader_platform_thread_unlock_mutex(&loader_lock);
-        return VK_ERROR_OUT_OF_HOST_MEMORY;
+        res = VK_ERROR_OUT_OF_HOST_MEMORY;
+        goto out;
     } else if (ptr_instance->num_tmp_callbacks > 0) {
         // Setup the temporary callback(s) here to catch early issues:
         if (util_CreateDebugReportCallbacks(ptr_instance, pAllocator,
@@ -320,12 +323,8 @@ vkCreateInstance(const VkInstanceCreateInfo *pCreateInfo,
                                             ptr_instance->tmp_callbacks)) {
             // Failure of setting up one or more of the callback.  Therefore,
             // clean up and fail this function:
-            util_FreeDebugReportCreateInfos(pAllocator,
-                                            ptr_instance->tmp_dbg_create_infos,
-                                            ptr_instance->tmp_callbacks);
-            loader_heap_free(ptr_instance, ptr_instance);
-            loader_platform_thread_unlock_mutex(&loader_lock);
-            return VK_ERROR_OUT_OF_HOST_MEMORY;
+            res = VK_ERROR_OUT_OF_HOST_MEMORY;
+            goto out;
         }
     }
 
@@ -343,28 +342,26 @@ vkCreateInstance(const VkInstanceCreateInfo *pCreateInfo,
                                    pCreateInfo->ppEnabledLayerNames,
                                    &ptr_instance->instance_layer_list);
         if (res != VK_SUCCESS) {
-            util_DestroyDebugReportCallbacks(ptr_instance, pAllocator,
-                                             ptr_instance->num_tmp_callbacks,
-                                             ptr_instance->tmp_callbacks);
-            util_FreeDebugReportCreateInfos(pAllocator,
-                                            ptr_instance->tmp_dbg_create_infos,
-                                            ptr_instance->tmp_callbacks);
-            loader_heap_free(ptr_instance, ptr_instance);
-            loader_platform_thread_unlock_mutex(&loader_lock);
-            return res;
+            goto out;
         }
     }
 
     /* convert any meta layers to the actual layers makes a copy of layer name*/
-    VkInstanceCreateInfo ici = *pCreateInfo;
-    loader_expand_layer_names(
+    VkResult layerErr = loader_expand_layer_names(
         ptr_instance, std_validation_str,
         sizeof(std_validation_names) / sizeof(std_validation_names[0]),
         std_validation_names, &ici.enabledLayerCount, &ici.ppEnabledLayerNames);
+    if (VK_SUCCESS != layerErr) {
+        res = layerErr;
+        goto out;
+    }
 
     /* Scan/discover all ICD libraries */
     memset(&ptr_instance->icd_libs, 0, sizeof(ptr_instance->icd_libs));
-    loader_icd_scan(ptr_instance, &ptr_instance->icd_libs);
+    res = loader_icd_scan(ptr_instance, &ptr_instance->icd_libs);
+    if (res != VK_SUCCESS) {
+        goto out;
+    }
 
     /* get extensions from all ICD's, merge so no duplicates, then validate */
     loader_get_icd_loader_instance_extensions(
@@ -373,45 +370,15 @@ vkCreateInstance(const VkInstanceCreateInfo *pCreateInfo,
         ptr_instance, &ptr_instance->ext_list,
         &ptr_instance->instance_layer_list, &ici);
     if (res != VK_SUCCESS) {
-        loader_delete_shadow_inst_layer_names(ptr_instance, pCreateInfo, &ici);
-        loader_delete_layer_properties(ptr_instance,
-                                       &ptr_instance->instance_layer_list);
-        loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_libs);
-        loader_destroy_generic_list(
-            ptr_instance,
-            (struct loader_generic_list *)&ptr_instance->ext_list);
-        util_DestroyDebugReportCallbacks(ptr_instance, pAllocator,
-                                         ptr_instance->num_tmp_callbacks,
-                                         ptr_instance->tmp_callbacks);
-        util_FreeDebugReportCreateInfos(pAllocator,
-                                        ptr_instance->tmp_dbg_create_infos,
-                                        ptr_instance->tmp_callbacks);
-        loader_platform_thread_unlock_mutex(&loader_lock);
-        loader_heap_free(ptr_instance, ptr_instance);
-        return res;
+        goto out;
     }
 
-    ptr_instance->disp =
-        loader_heap_alloc(ptr_instance, sizeof(VkLayerInstanceDispatchTable),
-                          VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+    ptr_instance->disp = loader_instance_heap_alloc(
+        ptr_instance, sizeof(VkLayerInstanceDispatchTable),
+        VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
     if (ptr_instance->disp == NULL) {
-        loader_delete_shadow_inst_layer_names(ptr_instance, pCreateInfo, &ici);
-
-        loader_delete_layer_properties(ptr_instance,
-                                       &ptr_instance->instance_layer_list);
-        loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_libs);
-        loader_destroy_generic_list(
-            ptr_instance,
-            (struct loader_generic_list *)&ptr_instance->ext_list);
-        util_DestroyDebugReportCallbacks(ptr_instance, pAllocator,
-                                         ptr_instance->num_tmp_callbacks,
-                                         ptr_instance->tmp_callbacks);
-        util_FreeDebugReportCreateInfos(pAllocator,
-                                        ptr_instance->tmp_dbg_create_infos,
-                                        ptr_instance->tmp_callbacks);
-        loader_platform_thread_unlock_mutex(&loader_lock);
-        loader_heap_free(ptr_instance, ptr_instance);
-        return VK_ERROR_OUT_OF_HOST_MEMORY;
+        res = VK_ERROR_OUT_OF_HOST_MEMORY;
+        goto out;
     }
     memcpy(ptr_instance->disp, &instance_disp, sizeof(instance_disp));
     ptr_instance->next = loader.instances;
@@ -421,24 +388,7 @@ vkCreateInstance(const VkInstanceCreateInfo *pCreateInfo,
     res = loader_enable_instance_layers(ptr_instance, &ici,
                                         &ptr_instance->instance_layer_list);
     if (res != VK_SUCCESS) {
-        loader_delete_shadow_inst_layer_names(ptr_instance, pCreateInfo, &ici);
-        loader_delete_layer_properties(ptr_instance,
-                                       &ptr_instance->instance_layer_list);
-        loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_libs);
-        loader_destroy_generic_list(
-            ptr_instance,
-            (struct loader_generic_list *)&ptr_instance->ext_list);
-        loader.instances = ptr_instance->next;
-        util_DestroyDebugReportCallbacks(ptr_instance, pAllocator,
-                                         ptr_instance->num_tmp_callbacks,
-                                         ptr_instance->tmp_callbacks);
-        util_FreeDebugReportCreateInfos(pAllocator,
-                                        ptr_instance->tmp_dbg_create_infos,
-                                        ptr_instance->tmp_callbacks);
-        loader_platform_thread_unlock_mutex(&loader_lock);
-        loader_heap_free(ptr_instance, ptr_instance->disp);
-        loader_heap_free(ptr_instance, ptr_instance);
-        return res;
+        goto out;
     }
 
     created_instance = (VkInstance)ptr_instance;
@@ -462,18 +412,57 @@ vkCreateInstance(const VkInstanceCreateInfo *pCreateInfo,
         // TODO: cleanup here.
     }
 
-    /* Remove temporary debug_report callback */
-    util_DestroyDebugReportCallbacks(ptr_instance, pAllocator,
-                                     ptr_instance->num_tmp_callbacks,
-                                     ptr_instance->tmp_callbacks);
-    loader_delete_shadow_inst_layer_names(ptr_instance, pCreateInfo, &ici);
-    loader_platform_thread_unlock_mutex(&loader_lock);
+out:
+
+    if (NULL != ptr_instance) {
+        if (res != VK_SUCCESS) {
+            if (NULL != ptr_instance->next) {
+                loader.instances = ptr_instance->next;
+            }
+            if (NULL != ptr_instance->disp) {
+                loader_instance_heap_free(ptr_instance, ptr_instance->disp);
+            }
+            if (ptr_instance->num_tmp_callbacks > 0) {
+                util_DestroyDebugReportCallbacks(
+                    ptr_instance, pAllocator, ptr_instance->num_tmp_callbacks,
+                    ptr_instance->tmp_callbacks);
+                util_FreeDebugReportCreateInfos(
+                    pAllocator, ptr_instance->tmp_dbg_create_infos,
+                    ptr_instance->tmp_callbacks);
+            }
+
+            loader_deactivate_layers(ptr_instance, NULL,
+                                     &ptr_instance->activated_layer_list);
+
+            loader_delete_shadow_inst_layer_names(ptr_instance, pCreateInfo,
+                                                  &ici);
+            loader_delete_layer_properties(ptr_instance,
+                                           &ptr_instance->instance_layer_list);
+            loader_scanned_icd_clear(ptr_instance, &ptr_instance->icd_libs);
+            loader_destroy_generic_list(
+                ptr_instance,
+                (struct loader_generic_list *)&ptr_instance->ext_list);
+
+            loader_instance_heap_free(ptr_instance, ptr_instance);
+        } else {
+            /* Remove temporary debug_report callback */
+            util_DestroyDebugReportCallbacks(ptr_instance, pAllocator,
+                                             ptr_instance->num_tmp_callbacks,
+                                             ptr_instance->tmp_callbacks);
+            loader_delete_shadow_inst_layer_names(ptr_instance, pCreateInfo,
+                                                  &ici);
+        }
+
+        if (loaderLocked) {
+            loader_platform_thread_unlock_mutex(&loader_lock);
+        }
+    }
+
     return res;
 }
 
-LOADER_EXPORT VKAPI_ATTR void VKAPI_CALL
-vkDestroyInstance(VkInstance instance,
-                  const VkAllocationCallbacks *pAllocator) {
+LOADER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyInstance(
+    VkInstance instance, const VkAllocationCallbacks *pAllocator) {
     const VkLayerInstanceDispatchTable *disp;
     struct loader_instance *ptr_instance = NULL;
     bool callback_setup = false;
@@ -488,6 +477,10 @@ vkDestroyInstance(VkInstance instance,
 
     ptr_instance = loader_get_instance(instance);
 
+    if (pAllocator) {
+        ptr_instance->alloc_callbacks = *pAllocator;
+    }
+
     if (ptr_instance->num_tmp_callbacks > 0) {
         // Setup the temporary callback(s) here to catch cleanup issues:
         if (!util_CreateDebugReportCallbacks(ptr_instance, pAllocator,
@@ -500,9 +493,11 @@ vkDestroyInstance(VkInstance instance,
 
     disp->DestroyInstance(instance, pAllocator);
 
-    loader_deactivate_layers(ptr_instance, &ptr_instance->activated_layer_list);
-    if (ptr_instance->phys_devs)
-        loader_heap_free(ptr_instance, ptr_instance->phys_devs);
+    loader_deactivate_layers(ptr_instance, NULL,
+                             &ptr_instance->activated_layer_list);
+    if (ptr_instance->phys_devs) {
+        loader_instance_heap_free(ptr_instance, ptr_instance->phys_devs);
+    }
     if (callback_setup) {
         util_DestroyDebugReportCallbacks(ptr_instance, pAllocator,
                                          ptr_instance->num_tmp_callbacks,
@@ -511,8 +506,8 @@ vkDestroyInstance(VkInstance instance,
                                         ptr_instance->tmp_dbg_create_infos,
                                         ptr_instance->tmp_callbacks);
     }
-    loader_heap_free(ptr_instance, ptr_instance->disp);
-    loader_heap_free(ptr_instance, ptr_instance);
+    loader_instance_heap_free(ptr_instance, ptr_instance->disp);
+    loader_instance_heap_free(ptr_instance, ptr_instance);
     loader_platform_thread_unlock_mutex(&loader_lock);
 }
 
@@ -551,7 +546,7 @@ vkEnumeratePhysicalDevices(VkInstance instance, uint32_t *pPhysicalDeviceCount,
     *pPhysicalDeviceCount = count;
     if (!inst->phys_devs) {
         inst->phys_devs =
-            (struct loader_physical_device_tramp *)loader_heap_alloc(
+            (struct loader_physical_device_tramp *)loader_instance_heap_alloc(
                 inst, inst->total_gpu_count *
                           sizeof(struct loader_physical_device_tramp),
                 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
@@ -643,14 +638,13 @@ LOADER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceMemoryProperties(
                                             pMemoryProperties);
 }
 
-LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
-vkCreateDevice(VkPhysicalDevice physicalDevice,
-               const VkDeviceCreateInfo *pCreateInfo,
-               const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
+LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDevice(
+    VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
+    const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
     VkResult res;
-    struct loader_physical_device_tramp *phys_dev;
-    struct loader_device *dev;
-    struct loader_instance *inst;
+    struct loader_physical_device_tramp *phys_dev = NULL;
+    struct loader_device *dev = NULL;
+    struct loader_instance *inst = NULL;
 
     assert(pCreateInfo->queueCreateInfoCount >= 1);
 
@@ -661,52 +655,51 @@ vkCreateDevice(VkPhysicalDevice physicalDevice,
 
     /* Get the physical device (ICD) extensions  */
     struct loader_extension_list icd_exts;
+    icd_exts.list = NULL;
     if (!loader_init_generic_list(inst, (struct loader_generic_list *)&icd_exts,
                                   sizeof(VkExtensionProperties))) {
-        loader_platform_thread_unlock_mutex(&loader_lock);
-        return VK_ERROR_OUT_OF_HOST_MEMORY;
+        res = VK_ERROR_OUT_OF_HOST_MEMORY;
+        goto out;
     }
 
     res = loader_add_device_extensions(
         inst, inst->disp->EnumerateDeviceExtensionProperties,
         phys_dev->phys_dev, "Unknown", &icd_exts);
     if (res != VK_SUCCESS) {
-        loader_platform_thread_unlock_mutex(&loader_lock);
-        return res;
+        goto out;
     }
 
     /* make sure requested extensions to be enabled are supported */
-    res = loader_validate_device_extensions(phys_dev, &inst->activated_layer_list,
-                                            &icd_exts, pCreateInfo);
+    res = loader_validate_device_extensions(
+        phys_dev, &inst->activated_layer_list, &icd_exts, pCreateInfo);
     if (res != VK_SUCCESS) {
-        loader_platform_thread_unlock_mutex(&loader_lock);
-        return res;
+        goto out;
     }
 
-    dev = loader_create_logical_device(inst);
+    dev = loader_create_logical_device(inst, pAllocator);
     if (dev == NULL) {
-        loader_platform_thread_unlock_mutex(&loader_lock);
-        return VK_ERROR_OUT_OF_HOST_MEMORY;
+        res = VK_ERROR_OUT_OF_HOST_MEMORY;
+        goto out;
     }
 
     /* copy the instance layer list into the device */
     dev->activated_layer_list.capacity = inst->activated_layer_list.capacity;
     dev->activated_layer_list.count = inst->activated_layer_list.count;
-    dev->activated_layer_list.list = loader_heap_alloc(inst,
-                            inst->activated_layer_list.capacity,
-                            VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+    dev->activated_layer_list.list =
+        loader_device_heap_alloc(dev, inst->activated_layer_list.capacity,
+                                 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
     if (dev->activated_layer_list.list == NULL) {
-        loader_platform_thread_unlock_mutex(&loader_lock);
-        return VK_ERROR_OUT_OF_HOST_MEMORY;
+        res = VK_ERROR_OUT_OF_HOST_MEMORY;
+        goto out;
     }
     memcpy(dev->activated_layer_list.list, inst->activated_layer_list.list,
-            sizeof(*dev->activated_layer_list.list) * dev->activated_layer_list.count);
+           sizeof(*dev->activated_layer_list.list) *
+               dev->activated_layer_list.count);
 
-
-    res = loader_create_device_chain(phys_dev, pCreateInfo, pAllocator, inst, dev);
+    res = loader_create_device_chain(phys_dev, pCreateInfo, pAllocator, inst,
+                                     dev);
     if (res != VK_SUCCESS) {
-        loader_platform_thread_unlock_mutex(&loader_lock);
-        return res;
+        goto out;
     }
 
     *pDevice = dev->device;
@@ -721,6 +714,19 @@ vkCreateDevice(VkPhysicalDevice physicalDevice,
         &dev->loader_dispatch,
         dev->loader_dispatch.core_dispatch.GetDeviceProcAddr, *pDevice);
 
+out:
+
+    // Failure cleanup
+    if (VK_SUCCESS != res) {
+        if (NULL != dev) {
+            loader_destroy_logical_device(inst, dev, pAllocator);
+        }
+    }
+
+    if (NULL != icd_exts.list) {
+        loader_destroy_generic_list(inst,
+                                    (struct loader_generic_list *)&icd_exts);
+    }
     loader_platform_thread_unlock_mutex(&loader_lock);
     return res;
 }
@@ -742,7 +748,7 @@ vkDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) {
 
     disp->DestroyDevice(device, pAllocator);
     dev->device = NULL;
-    loader_remove_logical_device(inst, icd, dev);
+    loader_remove_logical_device(inst, icd, dev, pAllocator);
 
     loader_platform_thread_unlock_mutex(&loader_lock);
 }
@@ -880,7 +886,7 @@ vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
         enabled_layers->count = count;
         enabled_layers->capacity = enabled_layers->count *
                                  sizeof(struct loader_layer_properties);
-        enabled_layers->list = loader_heap_alloc(inst, enabled_layers->capacity,
+        enabled_layers->list = loader_instance_heap_alloc(inst, enabled_layers->capacity,
                                 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
         if (!enabled_layers->list)
             return VK_ERROR_OUT_OF_HOST_MEMORY;
@@ -893,14 +899,21 @@ vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
                     std_val_count, std_validation_names)) {
                 struct loader_layer_properties props;
                 loader_init_std_validation_props(&props);
-                loader_copy_layer_properties(inst,
-                                             &enabled_layers->list[j], &props);
+                VkResult err = loader_copy_layer_properties(inst,
+                                                            &enabled_layers->list[j],
+                                                            &props);
+                if (err != VK_SUCCESS) {
+                    return err;
+                }
                 i += std_val_count;
             }
             else {
-                loader_copy_layer_properties(inst,
-                                         &enabled_layers->list[j],
-                                         &inst->activated_layer_list.list[i++]);
+                VkResult err = loader_copy_layer_properties(inst,
+                                                            &enabled_layers->list[j],
+                                                            &inst->activated_layer_list.list[i++]);
+                if (err != VK_SUCCESS) {
+                    return err;
+                }
             }
         }
     }
@@ -916,8 +929,9 @@ vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
     }
     *pPropertyCount = copy_size;
 
-    if (inst->activated_layers_are_std_val)
+    if (inst->activated_layers_are_std_val) {
         loader_delete_layer_properties(inst, enabled_layers);
+    }
     if (copy_size < count) {
         loader_platform_thread_unlock_mutex(&loader_lock);
         return VK_INCOMPLETE;
index a93bd6f..d131853 100644 (file)
@@ -136,12 +136,6 @@ static inline char *loader_platform_dirname(char *path) {
     return dirname(path);
 }
 
-// Environment variables
-
-static inline char *loader_getenv(const char *name) { return getenv(name); }
-
-static inline void loader_free_getenv(const char *val) {}
-
 // Dynamic Loading of libraries:
 typedef void *loader_platform_dl_handle;
 static inline loader_platform_dl_handle
@@ -322,29 +316,6 @@ static char *loader_platform_basename(char *pathname) {
     return current;
 }
 
-// Environment variables
-
-static inline char *loader_getenv(const char *name) {
-    char *retVal;
-    DWORD valSize;
-
-    valSize = GetEnvironmentVariableA(name, NULL, 0);
-
-    // valSize DOES include the null terminator, so for any set variable
-    // will always be at least 1. If it's 0, the variable wasn't set.
-    if (valSize == 0)
-        return NULL;
-
-    // TODO; FIXME This should be using any app defined memory allocation
-    retVal = (char *)malloc(valSize);
-
-    GetEnvironmentVariableA(name, retVal, valSize);
-
-    return retVal;
-}
-
-static inline void loader_free_getenv(const char *val) { free((void *)val); }
-
 // Dynamic Loading:
 typedef HMODULE loader_platform_dl_handle;
 static loader_platform_dl_handle
index c5f527f..9bac94b 100644 (file)
@@ -214,7 +214,7 @@ terminator_DestroySurfaceKHR(VkInstance instance, VkSurfaceKHR surface,
                              const VkAllocationCallbacks *pAllocator) {
     struct loader_instance *ptr_instance = loader_get_instance(instance);
 
-    loader_heap_free(ptr_instance, (void *)surface);
+    loader_instance_heap_free(ptr_instance, (void *)surface);
 }
 
 /*
@@ -431,11 +431,9 @@ terminator_GetPhysicalDeviceSurfacePresentModesKHR(
  * This is the trampoline entrypoint
  * for CreateSwapchainKHR
  */
-LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
-vkCreateSwapchainKHR(VkDevice device,
-                     const VkSwapchainCreateInfoKHR *pCreateInfo,
-                     const VkAllocationCallbacks *pAllocator,
-                     VkSwapchainKHR *pSwapchain) {
+LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateSwapchainKHR(
+    VkDevice device, const VkSwapchainCreateInfoKHR *pCreateInfo,
+    const VkAllocationCallbacks *pAllocator, VkSwapchainKHR *pSwapchain) {
     const VkLayerDispatchTable *disp;
     disp = loader_get_dispatch(device);
     VkResult res =
@@ -507,11 +505,9 @@ vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *pPresentInfo) {
  * This is the trampoline entrypoint
  * for CreateWin32SurfaceKHR
  */
-LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
-vkCreateWin32SurfaceKHR(VkInstance instance,
-                        const VkWin32SurfaceCreateInfoKHR *pCreateInfo,
-                        const VkAllocationCallbacks *pAllocator,
-                        VkSurfaceKHR *pSurface) {
+LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateWin32SurfaceKHR(
+    VkInstance instance, const VkWin32SurfaceCreateInfoKHR *pCreateInfo,
+    const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
     const VkLayerInstanceDispatchTable *disp;
     disp = loader_get_instance_dispatch(instance);
     VkResult res;
@@ -525,11 +521,9 @@ vkCreateWin32SurfaceKHR(VkInstance instance,
  * This is the instance chain terminator function
  * for CreateWin32SurfaceKHR
  */
-VKAPI_ATTR VkResult VKAPI_CALL
-terminator_CreateWin32SurfaceKHR(VkInstance instance,
-                                 const VkWin32SurfaceCreateInfoKHR *pCreateInfo,
-                                 const VkAllocationCallbacks *pAllocator,
-                                 VkSurfaceKHR *pSurface) {
+VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateWin32SurfaceKHR(
+    VkInstance instance, const VkWin32SurfaceCreateInfoKHR *pCreateInfo,
+    const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
     // First, check to ensure the appropriate extension was enabled:
     struct loader_instance *ptr_instance = loader_get_instance(instance);
     if (!ptr_instance->wsi_win32_surface_enabled) {
@@ -542,8 +536,9 @@ terminator_CreateWin32SurfaceKHR(VkInstance instance,
     // Next, if so, proceed with the implementation of this function:
     VkIcdSurfaceWin32 *pIcdSurface = NULL;
 
-    pIcdSurface = loader_heap_alloc(ptr_instance, sizeof(VkIcdSurfaceWin32),
-                                    VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+    pIcdSurface =
+        loader_instance_heap_alloc(ptr_instance, sizeof(VkIcdSurfaceWin32),
+                                   VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
     if (pIcdSurface == NULL) {
         return VK_ERROR_OUT_OF_HOST_MEMORY;
     }
@@ -615,11 +610,9 @@ terminator_GetPhysicalDeviceWin32PresentationSupportKHR(
  * This is the trampoline entrypoint
  * for CreateMirSurfaceKHR
  */
-LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
-vkCreateMirSurfaceKHR(VkInstance instance,
-                      const VkMirSurfaceCreateInfoKHR *pCreateInfo,
-                      const VkAllocationCallbacks *pAllocator,
-                      VkSurfaceKHR *pSurface) {
+LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateMirSurfaceKHR(
+    VkInstance instance, const VkMirSurfaceCreateInfoKHR *pCreateInfo,
+    const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
     const VkLayerInstanceDispatchTable *disp;
     disp = loader_get_instance_dispatch(instance);
     VkResult res;
@@ -633,11 +626,9 @@ vkCreateMirSurfaceKHR(VkInstance instance,
  * This is the instance chain terminator function
  * for CreateMirSurfaceKHR
  */
-VKAPI_ATTR VkResult VKAPI_CALL
-terminator_CreateMirSurfaceKHR(VkInstance instance,
-                               const VkMirSurfaceCreateInfoKHR *pCreateInfo,
-                               const VkAllocationCallbacks *pAllocator,
-                               VkSurfaceKHR *pSurface) {
+VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateMirSurfaceKHR(
+    VkInstance instance, const VkMirSurfaceCreateInfoKHR *pCreateInfo,
+    const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
     // First, check to ensure the appropriate extension was enabled:
     struct loader_instance *ptr_instance = loader_get_instance(instance);
     if (!ptr_instance->wsi_mir_surface_enabled) {
@@ -650,8 +641,9 @@ terminator_CreateMirSurfaceKHR(VkInstance instance,
     // Next, if so, proceed with the implementation of this function:
     VkIcdSurfaceMir *pIcdSurface = NULL;
 
-    pIcdSurface = loader_heap_alloc(ptr_instance, sizeof(VkIcdSurfaceMir),
-                                    VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+    pIcdSurface =
+        loader_instance_heap_alloc(ptr_instance, sizeof(VkIcdSurfaceMir),
+                                   VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
     if (pIcdSurface == NULL) {
         return VK_ERROR_OUT_OF_HOST_MEMORY;
     }
@@ -743,8 +735,7 @@ vkCreateWaylandSurfaceKHR(VkInstance instance,
  * This is the instance chain terminator function
  * for CreateWaylandSurfaceKHR
  */
-VKAPI_ATTR VkResult VKAPI_CALL
-terminator_CreateWaylandSurfaceKHR(
+VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateWaylandSurfaceKHR(
     VkInstance instance, const VkWaylandSurfaceCreateInfoKHR *pCreateInfo,
     const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
     // First, check to ensure the appropriate extension was enabled:
@@ -759,8 +750,9 @@ terminator_CreateWaylandSurfaceKHR(
     // Next, if so, proceed with the implementation of this function:
     VkIcdSurfaceWayland *pIcdSurface = NULL;
 
-    pIcdSurface = loader_heap_alloc(ptr_instance, sizeof(VkIcdSurfaceWayland),
-                                    VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+    pIcdSurface =
+        loader_instance_heap_alloc(ptr_instance, sizeof(VkIcdSurfaceWayland),
+                                   VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
     if (pIcdSurface == NULL) {
         return VK_ERROR_OUT_OF_HOST_MEMORY;
     }
@@ -834,11 +826,9 @@ terminator_GetPhysicalDeviceWaylandPresentationSupportKHR(
  * This is the trampoline entrypoint
  * for CreateXcbSurfaceKHR
  */
-LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
-vkCreateXcbSurfaceKHR(VkInstance instance,
-                      const VkXcbSurfaceCreateInfoKHR *pCreateInfo,
-                      const VkAllocationCallbacks *pAllocator,
-                      VkSurfaceKHR *pSurface) {
+LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateXcbSurfaceKHR(
+    VkInstance instance, const VkXcbSurfaceCreateInfoKHR *pCreateInfo,
+    const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
     const VkLayerInstanceDispatchTable *disp;
     disp = loader_get_instance_dispatch(instance);
     VkResult res;
@@ -852,11 +842,9 @@ vkCreateXcbSurfaceKHR(VkInstance instance,
  * This is the instance chain terminator function
  * for CreateXcbSurfaceKHR
  */
-VKAPI_ATTR VkResult VKAPI_CALL
-terminator_CreateXcbSurfaceKHR(VkInstance instance,
-                               const VkXcbSurfaceCreateInfoKHR *pCreateInfo,
-                               const VkAllocationCallbacks *pAllocator,
-                               VkSurfaceKHR *pSurface) {
+VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateXcbSurfaceKHR(
+    VkInstance instance, const VkXcbSurfaceCreateInfoKHR *pCreateInfo,
+    const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
     // First, check to ensure the appropriate extension was enabled:
     struct loader_instance *ptr_instance = loader_get_instance(instance);
     if (!ptr_instance->wsi_xcb_surface_enabled) {
@@ -869,8 +857,9 @@ terminator_CreateXcbSurfaceKHR(VkInstance instance,
     // Next, if so, proceed with the implementation of this function:
     VkIcdSurfaceXcb *pIcdSurface = NULL;
 
-    pIcdSurface = loader_heap_alloc(ptr_instance, sizeof(VkIcdSurfaceXcb),
-                                    VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+    pIcdSurface =
+        loader_instance_heap_alloc(ptr_instance, sizeof(VkIcdSurfaceXcb),
+                                   VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
     if (pIcdSurface == NULL) {
         return VK_ERROR_OUT_OF_HOST_MEMORY;
     }
@@ -945,11 +934,9 @@ terminator_GetPhysicalDeviceXcbPresentationSupportKHR(
  * This is the trampoline entrypoint
  * for CreateXlibSurfaceKHR
  */
-LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
-vkCreateXlibSurfaceKHR(VkInstance instance,
-                       const VkXlibSurfaceCreateInfoKHR *pCreateInfo,
-                       const VkAllocationCallbacks *pAllocator,
-                       VkSurfaceKHR *pSurface) {
+LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateXlibSurfaceKHR(
+    VkInstance instance, const VkXlibSurfaceCreateInfoKHR *pCreateInfo,
+    const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
     const VkLayerInstanceDispatchTable *disp;
     disp = loader_get_instance_dispatch(instance);
     VkResult res;
@@ -963,11 +950,9 @@ vkCreateXlibSurfaceKHR(VkInstance instance,
  * This is the instance chain terminator function
  * for CreateXlibSurfaceKHR
  */
-VKAPI_ATTR VkResult VKAPI_CALL
-terminator_CreateXlibSurfaceKHR(VkInstance instance,
-                                const VkXlibSurfaceCreateInfoKHR *pCreateInfo,
-                                const VkAllocationCallbacks *pAllocator,
-                                VkSurfaceKHR *pSurface) {
+VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateXlibSurfaceKHR(
+    VkInstance instance, const VkXlibSurfaceCreateInfoKHR *pCreateInfo,
+    const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
     // First, check to ensure the appropriate extension was enabled:
     struct loader_instance *ptr_instance = loader_get_instance(instance);
     if (!ptr_instance->wsi_xlib_surface_enabled) {
@@ -980,8 +965,9 @@ terminator_CreateXlibSurfaceKHR(VkInstance instance,
     // Next, if so, proceed with the implementation of this function:
     VkIcdSurfaceXlib *pIcdSurface = NULL;
 
-    pIcdSurface = loader_heap_alloc(ptr_instance, sizeof(VkIcdSurfaceXlib),
-                                    VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+    pIcdSurface =
+        loader_instance_heap_alloc(ptr_instance, sizeof(VkIcdSurfaceXlib),
+                                   VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
     if (pIcdSurface == NULL) {
         return VK_ERROR_OUT_OF_HOST_MEMORY;
     }
@@ -1055,10 +1041,9 @@ terminator_GetPhysicalDeviceXlibPresentationSupportKHR(
  * This is the trampoline entrypoint
  * for CreateAndroidSurfaceKHR
  */
-LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
-vkCreateAndroidSurfaceKHR(VkInstance instance, ANativeWindow *window,
-                          const VkAllocationCallbacks *pAllocator,
-                          VkSurfaceKHR *pSurface) {
+LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateAndroidSurfaceKHR(
+    VkInstance instance, ANativeWindow *window,
+    const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
     const VkLayerInstanceDispatchTable *disp;
     disp = loader_get_instance_dispatch(instance);
     VkResult res;
@@ -1071,10 +1056,9 @@ vkCreateAndroidSurfaceKHR(VkInstance instance, ANativeWindow *window,
  * This is the instance chain terminator function
  * for CreateAndroidSurfaceKHR
  */
-VKAPI_ATTR VkResult VKAPI_CALL
-terminator_CreateAndroidSurfaceKHR(VkInstance instance, Window window,
-                                   const VkAllocationCallbacks *pAllocator,
-                                   VkSurfaceKHR *pSurface) {
+VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateAndroidSurfaceKHR(
+    VkInstance instance, Window window, const VkAllocationCallbacks *pAllocator,
+    VkSurfaceKHR *pSurface) {
     // First, check to ensure the appropriate extension was enabled:
     struct loader_instance *ptr_instance = loader_get_instance(instance);
     if (!ptr_instance->wsi_display_enabled) {
@@ -1087,8 +1071,9 @@ terminator_CreateAndroidSurfaceKHR(VkInstance instance, Window window,
     // Next, if so, proceed with the implementation of this function:
     VkIcdSurfaceAndroid *pIcdSurface = NULL;
 
-    pIcdSurface = loader_heap_alloc(ptr_instance, sizeof(VkIcdSurfaceAndroid),
-                                    VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+    pIcdSurface =
+        loader_instance_heap_alloc(ptr_instance, sizeof(VkIcdSurfaceAndroid),
+                                   VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
     if (pIcdSurface == NULL) {
         return VK_ERROR_OUT_OF_HOST_MEMORY;
     }
@@ -1265,11 +1250,10 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_GetDisplayModePropertiesKHR(
                                             pPropertyCount, pProperties);
 }
 
-LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
-vkCreateDisplayModeKHR(VkPhysicalDevice physicalDevice, VkDisplayKHR display,
-                       const VkDisplayModeCreateInfoKHR *pCreateInfo,
-                       const VkAllocationCallbacks *pAllocator,
-                       VkDisplayModeKHR *pMode) {
+LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDisplayModeKHR(
+    VkPhysicalDevice physicalDevice, VkDisplayKHR display,
+    const VkDisplayModeCreateInfoKHR *pCreateInfo,
+    const VkAllocationCallbacks *pAllocator, VkDisplayModeKHR *pMode) {
     VkPhysicalDevice unwrapped_phys_dev =
         loader_unwrap_physical_device(physicalDevice);
     const VkLayerInstanceDispatchTable *disp;
@@ -1279,12 +1263,10 @@ vkCreateDisplayModeKHR(VkPhysicalDevice physicalDevice, VkDisplayKHR display,
     return res;
 }
 
-VKAPI_ATTR VkResult VKAPI_CALL
-terminator_CreateDisplayModeKHR(VkPhysicalDevice physicalDevice,
-                                VkDisplayKHR display,
-                                const VkDisplayModeCreateInfoKHR *pCreateInfo,
-                                const VkAllocationCallbacks *pAllocator,
-                                VkDisplayModeKHR *pMode) {
+VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDisplayModeKHR(
+    VkPhysicalDevice physicalDevice, VkDisplayKHR display,
+    const VkDisplayModeCreateInfoKHR *pCreateInfo,
+    const VkAllocationCallbacks *pAllocator, VkDisplayModeKHR *pMode) {
     // First, check to ensure the appropriate extension was enabled:
     struct loader_physical_device *phys_dev =
         (struct loader_physical_device *)physicalDevice;
@@ -1345,11 +1327,9 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_GetDisplayPlaneCapabilitiesKHR(
                                                planeIndex, pCapabilities);
 }
 
-LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
-vkCreateDisplayPlaneSurfaceKHR(VkInstance instance,
-                               const VkDisplaySurfaceCreateInfoKHR *pCreateInfo,
-                               const VkAllocationCallbacks *pAllocator,
-                               VkSurfaceKHR *pSurface) {
+LOADER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDisplayPlaneSurfaceKHR(
+    VkInstance instance, const VkDisplaySurfaceCreateInfoKHR *pCreateInfo,
+    const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
     const VkLayerInstanceDispatchTable *disp;
     disp = loader_get_instance_dispatch(instance);
     VkResult res;
@@ -1372,8 +1352,8 @@ VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDisplayPlaneSurfaceKHR(
         return VK_ERROR_EXTENSION_NOT_PRESENT;
     }
 
-    pIcdSurface = loader_heap_alloc(inst, sizeof(VkIcdSurfaceDisplay),
-                                    VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+    pIcdSurface = loader_instance_heap_alloc(
+        inst, sizeof(VkIcdSurfaceDisplay), VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
     if (pIcdSurface == NULL) {
         return VK_ERROR_OUT_OF_HOST_MEMORY;
     }