aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCourtney Goeltzenleuchter <courtney@LunarG.com>2015-07-05 11:28:29 -0600
committerCourtney Goeltzenleuchter <courtney@LunarG.com>2015-07-07 17:57:48 -0600
commit5865a5ae7bcca7767ea7c8ed4d5e618627b6fe35 (patch)
tree4ea85083b16c6855e5bb9e9fd6b6cbcc87334a53
parent061c3793c7dbb414e9866c64bed7498239303380 (diff)
downloadusermoji-5865a5ae7bcca7767ea7c8ed4d5e618627b6fe35.tar.xz
loader: Save app allocation callbacks
Add host memory allocation functions that the loader can use. Add comments of where these allocation calls should go. Need to plumb the instance pointer to some functions to support this.
-rw-r--r--loader/debug_report.c1
-rw-r--r--loader/loader.c73
-rw-r--r--loader/loader.h17
-rw-r--r--loader/trampoline.c9
-rw-r--r--loader/vk_loader_platform.h4
-rw-r--r--loader/wsi_lunarg.c3
6 files changed, 84 insertions, 23 deletions
diff --git a/loader/debug_report.c b/loader/debug_report.c
index 2ad0782e..89340e6b 100644
--- a/loader/debug_report.c
+++ b/loader/debug_report.c
@@ -26,6 +26,7 @@
* Courtney Goeltzenleuchter <courtney@lunarg.com>
*/
+#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
diff --git a/loader/loader.c b/loader/loader.c
index f3e8559f..06c7f47d 100644
--- a/loader/loader.c
+++ b/loader/loader.c
@@ -106,6 +106,40 @@ LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_icd);
LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_layer);
LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_exts);
+void* loader_heap_alloc(
+ struct loader_instance *instance,
+ size_t size,
+ VkSystemAllocType alloc_type)
+{
+ if (instance && instance->alloc_callbacks.pfnAlloc) {
+ /* TODO: What should default alignment be? 1, 4, 8, other? */
+ return instance->alloc_callbacks.pfnAlloc(instance->alloc_callbacks.pUserData, size, 4, alloc_type);
+ }
+ return malloc(size);
+}
+
+void* loader_aligned_heap_alloc(
+ struct loader_instance *instance,
+ size_t size,
+ size_t alignment,
+ VkSystemAllocType alloc_type)
+{
+ if (!instance && instance->alloc_callbacks.pfnAlloc) {
+ return instance->alloc_callbacks.pfnAlloc(instance->alloc_callbacks.pUserData, size, alignment, alloc_type);
+ }
+ return aligned_alloc(size, alignment);
+}
+
+void loader_heap_free(
+ struct loader_instance *instance,
+ void *pMem)
+{
+ if (!instance && instance->alloc_callbacks.pfnFree) {
+ return instance->alloc_callbacks.pfnFree(instance->alloc_callbacks.pUserData, pMem);
+ }
+ return free(pMem);
+}
+
static void loader_log(VkFlags msg_type, int32_t msg_code,
const char *format, ...)
{
@@ -365,11 +399,7 @@ static void loader_add_global_extensions(
return;
}
-#ifdef WIN32
- extension_properties = _alloca(count * sizeof(VkExtensionProperties));
-#else
- extension_properties = alloca(count * sizeof(VkExtensionProperties));
-#endif
+ extension_properties = loader_stack_alloc(count * sizeof(VkExtensionProperties));
res = fp_get_props(NULL, &count, extension_properties);
if (res != VK_SUCCESS) {
@@ -404,6 +434,7 @@ static void loader_add_global_extensions(
return;
}
+/* TODO: Need to use loader_heap_alloc or loader_stack_alloc */
static void loader_add_global_layer_properties(
const char *lib_name,
const loader_platform_dl_handle lib_handle,
@@ -441,11 +472,7 @@ static void loader_add_global_layer_properties(
return;
}
-#ifdef WIN32
- layer_properties = _alloca(count * sizeof(VkLayerProperties));
-#else
- layer_properties = alloca(count * sizeof(VkLayerProperties));
-#endif
+ layer_properties = loader_stack_alloc(count * sizeof(VkLayerProperties));
res = fp_get_layer_props(&count, layer_properties);
if (res != VK_SUCCESS) {
@@ -501,11 +528,9 @@ static void loader_add_physical_device_extensions(
if (get_phys_dev_ext_props) {
res = get_phys_dev_ext_props(physical_device, NULL, &count, NULL);
if (res == VK_SUCCESS && count > 0) {
-#ifdef WIN32
- extension_properties = _alloca(count * sizeof(VkExtensionProperties));
-#else
- extension_properties = alloca(count * sizeof(VkExtensionProperties));
-#endif
+
+ extension_properties = loader_stack_alloc(count * sizeof(VkExtensionProperties));
+
res = get_phys_dev_ext_props(physical_device, NULL, &count, extension_properties);
for (i = 0; i < count; i++) {
char spec_version[64], version[64];
@@ -576,11 +601,7 @@ static void loader_add_physical_device_layer_properties(
return;
}
-#ifdef WIN32
- layer_properties = _alloca(count * sizeof(VkLayerProperties));
-#else
- layer_properties = alloca(count * sizeof(VkLayerProperties));
-#endif
+ layer_properties = loader_stack_alloc(count * sizeof(VkLayerProperties));
res = fp_get_layer_props(gpu, &count, layer_properties);
if (res != VK_SUCCESS) {
@@ -612,6 +633,7 @@ static void loader_add_physical_device_layer_properties(
static bool loader_init_ext_list(struct loader_extension_list *ext_info)
{
ext_info->capacity = 32 * sizeof(struct loader_extension_property);
+ /* TODO: Need to use loader_stack_alloc or loader_heap_alloc */
ext_info->list = malloc(ext_info->capacity);
if (ext_info->list == NULL) {
return false;
@@ -685,6 +707,7 @@ void loader_add_to_ext_list(
>= ext_list->capacity) {
// double capacity
ext_list->capacity *= 2;
+ /* TODO: Need to use loader_stack_alloc or loader_heap_alloc */
ext_list->list = realloc(ext_list->list, ext_list->capacity);
}
@@ -693,9 +716,13 @@ void loader_add_to_ext_list(
}
}
+/*
+ * Manage lists of VkLayerProperties
+ */
static bool loader_init_layer_list(struct loader_layer_list *list)
{
list->capacity = 32 * sizeof(struct loader_layer_properties);
+ /* TODO: Need to use loader_stack_alloc or loader_heap_alloc */
list->list = malloc(list->capacity);
if (list->list == NULL) {
return false;
@@ -1242,7 +1269,7 @@ static cJSON *loader_get_json(const char *filename)
fseek(file, 0, SEEK_END);
len = ftell(file);
fseek(file, 0, SEEK_SET);
- json_buf = (char*) alloca(len+1);
+ json_buf = (char*) loader_stack_alloc(len+1);
if (json_buf == NULL) {
loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "Out of memory can't get JSON file");
fclose(file);
@@ -1342,7 +1369,7 @@ static void loader_get_manifest_files(const char *env_override,
#endif
}
else {
- loc = alloca(strlen(override) + 1);
+ loc = loader_stack_alloc(strlen(override) + 1);
if (loc == NULL) {
loader_log(VK_DBG_REPORT_ERROR_BIT, 0, "Out of memory can't get manifest files");
return;
@@ -1803,7 +1830,7 @@ static void loader_add_layer_env(
if (layerEnv == NULL) {
return;
}
- name = alloca(strlen(layerEnv) + 1);
+ name = loader_stack_alloc(strlen(layerEnv) + 1);
if (name == NULL) {
return;
}
diff --git a/loader/loader.h b/loader/loader.h
index d4e8b354..013e40df 100644
--- a/loader/loader.h
+++ b/loader/loader.h
@@ -240,6 +240,8 @@ struct loader_instance {
bool debug_report_enabled;
VkLayerDbgFunctionNode *DbgFunctionHead;
+
+ VkAllocCallbacks alloc_callbacks;
};
struct loader_struct {
@@ -418,4 +420,19 @@ void loader_enable_instance_layers(struct loader_instance *inst, const VkInstanc
void loader_deactivate_instance_layers(struct loader_instance *instance);
uint32_t loader_activate_instance_layers(struct loader_instance *inst);
void loader_activate_instance_layer_extensions(struct loader_instance *inst);
+
+void* loader_heap_alloc(
+ struct loader_instance *instance,
+ size_t size,
+ VkSystemAllocType allocType);
+
+void* loader_aligned_heap_alloc(
+ struct loader_instance *instance,
+ size_t size,
+ size_t alignment,
+ VkSystemAllocType allocType);
+
+void loader_heap_free(
+ struct loader_instance *instance,
+ void *pMem);
#endif /* LOADER_H */
diff --git a/loader/trampoline.c b/loader/trampoline.c
index 6df6f68c..e707082a 100644
--- a/loader/trampoline.c
+++ b/loader/trampoline.c
@@ -21,6 +21,7 @@
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
+#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
@@ -68,6 +69,14 @@ LOADER_EXPORT VkResult VKAPI vkCreateInstance(
ptr_instance->next = loader.instances;
loader.instances = ptr_instance;
+ if (pCreateInfo->pAllocCb
+ && pCreateInfo->pAllocCb->pfnAlloc
+ && pCreateInfo->pAllocCb->pfnFree) {
+ ptr_instance->alloc_callbacks.pUserData = pCreateInfo->pAllocCb->pUserData;
+ ptr_instance->alloc_callbacks.pfnAlloc = pCreateInfo->pAllocCb->pfnAlloc;
+ ptr_instance->alloc_callbacks.pfnFree = pCreateInfo->pAllocCb->pfnFree;
+ }
+
loader_enable_instance_layers(ptr_instance, pCreateInfo);
debug_report_create_instance(ptr_instance, pCreateInfo);
diff --git a/loader/vk_loader_platform.h b/loader/vk_loader_platform.h
index b60f421f..c763765d 100644
--- a/loader/vk_loader_platform.h
+++ b/loader/vk_loader_platform.h
@@ -151,6 +151,8 @@ static inline void loader_platform_thread_cond_broadcast(loader_platform_thread_
pthread_cond_broadcast(pCond);
}
+#define loader_stack_alloc(size) alloca(size)
+static inline void *loader_aligned_alloc(size_t alignment, size_t size) { return aligned_alloc(alignment, size); }
#elif defined(_WIN32) // defined(__linux__)
/* Windows-specific common code: */
@@ -387,6 +389,8 @@ char *loader_get_registry_string(const HKEY hive,
#define LeaveCriticalSection PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION
#define DeleteCriticalSection PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION
+#define loader_stack_alloc(size) _alloca(size)
+static inline void *loader_aligned_alloc(size_t alignment, size_t size) { return _aligned_alloc(alignment, size); }
#endif // defined(_WIN32)
#endif /* LOADER_PLATFORM_H_TEMP */
diff --git a/loader/wsi_lunarg.c b/loader/wsi_lunarg.c
index ad7aa22c..c80c4c28 100644
--- a/loader/wsi_lunarg.c
+++ b/loader/wsi_lunarg.c
@@ -26,6 +26,9 @@
* Courtney Goeltzenleuchter <courtney@lunarg.com>
*/
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include "vk_loader_platform.h"
#include "loader.h"