diff --git a/CMakeLists.txt b/CMakeLists.txt index 95a8c9c..e3d1c0b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,8 +33,11 @@ add_subdirectory(projects/loader) # build gryphn loader add_subdirectory(projects/core) # build gryphn core add_subdirectory(projects/extensions) add_subdirectory(projects/platform) # build gryphn platform + add_subdirectory(projects/validation_layers/function_loader/) -target_link_libraries(Gryphn INTERFACE GryphnUtils GryphnCore GryphnLoader GryphnPlatform GryphnFunctionValidator GryphnExtensions) +add_subdirectory(projects/validation_layers/allocators/) + +target_link_libraries(Gryphn INTERFACE GryphnUtils GryphnCore GryphnLoader GryphnPlatform GryphnFunctionValidator GryphnAllocatorChecker GryphnExtensions) if (VULKAN_BUILT) target_link_libraries(Gryphn INTERFACE GryphnVulkanImpl) diff --git a/projects/apis/metal/src/instance/metal_instance.h b/projects/apis/metal/src/instance/metal_instance.h index 351cee3..fa3a263 100644 --- a/projects/apis/metal/src/instance/metal_instance.h +++ b/projects/apis/metal/src/instance/metal_instance.h @@ -7,5 +7,5 @@ typedef struct gnPlatformInstance_t { NSView* metalContentView; } gnPlatformInstance; -gnReturnCode metalCreateInstance(gnInstanceHandle instance, gnInstanceCreateInfo* instanceInfo, gryphnInstanceFunctionLayers* next); -void metalDestroyInstance(gnInstance instance, gryphnInstanceFunctionLayers* next); +gnReturnCode metalCreateInstance(gnInstanceHandle instance, gnInstanceCreateInfo* instanceInfo, gryphnInstanceFunctionLayers* next, gnAllocators* allocators); +void metalDestroyInstance(gnInstance instance, gryphnInstanceFunctionLayers* next, gnAllocators* allocators); diff --git a/projects/apis/metal/src/instance/metal_instance.m b/projects/apis/metal/src/instance/metal_instance.m index 3c93422..382831f 100644 --- a/projects/apis/metal/src/instance/metal_instance.m +++ b/projects/apis/metal/src/instance/metal_instance.m @@ -1,14 +1,14 @@ #include "metal_instance.h" // metal instances are kinda useless -gnReturnCode metalCreateInstance(gnInstanceHandle instance, gnInstanceCreateInfo* instanceInfo, gryphnInstanceFunctionLayers* next) { +gnReturnCode metalCreateInstance(gnInstanceHandle instance, gnInstanceCreateInfo* instanceInfo, gryphnInstanceFunctionLayers* next, gnAllocators* allocators) { if (next != NULL) return GN_SUCCESS; if (instanceInfo == NULL) return GN_INCOMPLETE; - instance->instance = malloc(sizeof(gnPlatformInstance)); + instance->instance = allocators->malloc(sizeof(gnPlatformInstance), allocators->userData); return GN_SUCCESS; } -void metalDestroyInstance(gnInstanceHandle instance, gryphnInstanceFunctionLayers* next) { +void metalDestroyInstance(gnInstanceHandle instance, gryphnInstanceFunctionLayers* next, gnAllocators* allocators) { if (next != NULL) return; - free(instance->instance); + allocators->free(instance->instance, allocators->userData); } diff --git a/projects/apis/vulkan/src/instance/vulkan_instance.c b/projects/apis/vulkan/src/instance/vulkan_instance.c index 00f3338..6b0d3d1 100644 --- a/projects/apis/vulkan/src/instance/vulkan_instance.c +++ b/projects/apis/vulkan/src/instance/vulkan_instance.c @@ -43,9 +43,9 @@ static VKAPI_ATTR VkBool32 VKAPI_CALL vk_debuggerDebugCallback( return VK_TRUE; } -gnReturnCode vulkanCreateInstance(gnInstanceHandle instance, gnInstanceCreateInfo* instanceInfo, gryphnInstanceFunctionLayers* next) { +gnReturnCode vulkanCreateInstance(gnInstanceHandle instance, gnInstanceCreateInfo* instanceInfo, gryphnInstanceFunctionLayers* next, gnAllocators* alloctors) { if (next != NULL) { return GN_SUCCESS; } - instance->instance = malloc(sizeof(gnPlatformInstance)); + instance->instance = alloctors->malloc(sizeof(gnPlatformInstance), alloctors->userData); vkStringArrayList extensions = vkStringArrayListCreate(); vkStringArrayListAdd(extensions, "VK_KHR_surface"); @@ -108,7 +108,8 @@ gnReturnCode vulkanCreateInstance(gnInstanceHandle instance, gnInstanceCreateInf return VkResultToGnReturnCode(vkCreateInstance(&createInfo, NULL, &instance->instance->vk_instance)); } -void vulkanDestroyInstance(gnInstanceHandle instance, gryphnInstanceFunctionLayers* next) { +void vulkanDestroyInstance(gnInstanceHandle instance, gryphnInstanceFunctionLayers* next, gnAllocators* alloctors) { if (next != NULL) { return; } vkDestroyInstance(instance->instance->vk_instance, NULL); + alloctors->free(instance->instance, alloctors->userData); } diff --git a/projects/apis/vulkan/src/instance/vulkan_instance.h b/projects/apis/vulkan/src/instance/vulkan_instance.h index 833ae1f..32c667d 100644 --- a/projects/apis/vulkan/src/instance/vulkan_instance.h +++ b/projects/apis/vulkan/src/instance/vulkan_instance.h @@ -14,8 +14,8 @@ typedef struct gnPlatformInstance_t { vkUserData userData; } gnPlatformInstance; -gnReturnCode vulkanCreateInstance(gnInstanceHandle instance, gnInstanceCreateInfo* instanceInfo, gryphnInstanceFunctionLayers* next); -void vulkanDestroyInstance(gnInstanceHandle instance, gryphnInstanceFunctionLayers* next); +gnReturnCode vulkanCreateInstance(gnInstanceHandle instance, gnInstanceCreateInfo* instanceInfo, gryphnInstanceFunctionLayers* next, gnAllocators* alloctors); +void vulkanDestroyInstance(gnInstanceHandle instance, gryphnInstanceFunctionLayers* next, gnAllocators* alloctors); typedef const char* vkString; GN_ARRAY_LIST_HEADER(vkString); diff --git a/projects/core/src/instance/gryphn_instance.c b/projects/core/src/instance/gryphn_instance.c index 18e6e80..464ff8a 100644 --- a/projects/core/src/instance/gryphn_instance.c +++ b/projects/core/src/instance/gryphn_instance.c @@ -4,12 +4,30 @@ #include "loader/src/gryphn_extension_loader.h" #include "loader/src/gryphn_loader.h" #include "loader/src/gryphn_loader.h" -#include "stdio.h" #include "validation_layers/function_loader/loader/function_loader.h" +#include "validation_layers/allocators/allocator_checker.h" // this implementation of gnCreateInstance cannot return GN_UNLOADED_LAYER gnReturnCode gnCreateInstance(gnInstanceHandle* instance, gnInstanceCreateInfo* info) { - *instance = malloc(sizeof(struct gnInstance_t)); + *instance = GN_NULL_HANDLE; + gnAllocators tmpAllocators = { + .userData = &info->debuggerInfo, + .malloc = (PFN_gnMalloc)malloc, + .calloc = (PFN_gnCalloc)calloc, + .realloc = (PFN_gnRealloc)realloc, + .free = (PFN_gnFree)free + }; + + for (int i = 0; i < info->debuggerInfo.layerCount; i++) { + if (info->debuggerInfo.layers[i] == GN_DEBUGGER_LAYER_ALLOCATORS) { + tmpAllocators.malloc = gnMallocFunc; + tmpAllocators.calloc = gnCallocFunc; + tmpAllocators.realloc = gnReallocFunc; + tmpAllocators.free = gnFreeFunc; + } + } + + *instance = tmpAllocators.malloc(sizeof(struct gnInstance_t), tmpAllocators.userData); (*instance)->hasDebugger = GN_FALSE; (*instance)->layers = loaderLayerArrayListCreate(); @@ -61,11 +79,14 @@ gnReturnCode gnCreateInstance(gnInstanceHandle* instance, gnInstanceCreateInfo* for (uint32_t i = 0; i < loaderLayerArrayListCount((*instance)->layers); i++) loaderLayerArrayListRefAt((*instance)->layers, i)->layerIndex = i; (*instance)->callingLayer = loaderLayerArrayListRefAt((*instance)->layers, (*instance)->currentLayer); if (unsupportedExtension) return GN_UNLOADED_EXTENSION; - return (*instance)->functions->createInstance(*instance, info, (*instance)->functions->next); + (*instance)->allocators = tmpAllocators; + (*instance)->allocators.userData = &(*instance)->debugger; + return (*instance)->functions->createInstance(*instance, info, (*instance)->functions->next, &(*instance)->allocators); } void gnDestroyInstance(gnInstanceHandle* instance) { if (instance == GN_NULL_HANDLE) return; - (*instance)->functions->destroyInstance(*instance, (*instance)->functions->next); + (*instance)->functions->destroyInstance(*instance, (*instance)->functions->next, &(*instance)->allocators); + (*instance)->allocators.free(*instance, (*instance)->allocators.userData); *instance = GN_NULL_HANDLE; } diff --git a/projects/core/src/instance/gryphn_instance.h b/projects/core/src/instance/gryphn_instance.h index c7bc971..5f04990 100644 --- a/projects/core/src/instance/gryphn_instance.h +++ b/projects/core/src/instance/gryphn_instance.h @@ -4,8 +4,10 @@ #include "utils/gryphn_version.h" #include "core/gryphn_return_code.h" #include "core/src/instance/gryphn_debugger.h" +#include "gryphn_allocators.h" #include + typedef struct gnApplicationInfo { gnString applicationName; gnVersion applicationVersion; @@ -32,12 +34,14 @@ struct gnInstance_t { int enabledLayerCounts[GN_LAYER_MAX]; gryphnInstanceFunctionLayers* allLayers; gryphnInstanceFunctionLayers* functions; + gnAllocators allocators; loaderLayerArrayList layers; loaderLayer* callingLayer; uint32_t currentLayer; gnBool hasDebugger; }; + #endif gnReturnCode gnCreateInstance(gnInstanceHandle* instance, gnInstanceCreateInfo* info); diff --git a/projects/loader/src/gryphn_instance_functions.h b/projects/loader/src/gryphn_instance_functions.h index 607e3e5..096be97 100644 --- a/projects/loader/src/gryphn_instance_functions.h +++ b/projects/loader/src/gryphn_instance_functions.h @@ -3,6 +3,7 @@ #include "core/gryphn_return_code.h" #include "utils/gryphn_bool.h" #include "gryphn_handles.h" +#include "gryphn_allocators.h" typedef struct gnInstanceCreateInfo gnInstanceCreateInfo; typedef struct gnSurfaceDetails gnSurfaceDetails; @@ -19,8 +20,8 @@ typedef struct gnOutputDeviceInfo gnOutputDeviceInfo; #endif typedef struct gryphnInstanceFunctionLayers gryphnInstanceFunctionLayers; -typedef gnReturnCode (*PFN_gnCreateInstance)(gnInstanceHandle instance, gnInstanceCreateInfo* info, gryphnInstanceFunctionLayers* next); -typedef void (*PFN_gnDestroyInstance)(gnInstanceHandle instance, gryphnInstanceFunctionLayers* next); +typedef gnReturnCode (*PFN_gnCreateInstance)(gnInstanceHandle instance, gnInstanceCreateInfo* info, gryphnInstanceFunctionLayers* next, gnAllocators* alloctors); +typedef void (*PFN_gnDestroyInstance)(gnInstanceHandle instance, gryphnInstanceFunctionLayers* next, gnAllocators* alloctors); typedef struct gnInstanceFunctions { gnPhysicalDevice* (*_gnGetPhysicalDevices)(gnInstanceHandle instance, uint32_t* count); diff --git a/projects/validation_layers/function_loader/src/instance_functions.c b/projects/validation_layers/function_loader/src/instance_functions.c index f4862e7..4a80407 100644 --- a/projects/validation_layers/function_loader/src/instance_functions.c +++ b/projects/validation_layers/function_loader/src/instance_functions.c @@ -4,24 +4,24 @@ #include "core/src/output_device/gryphn_output_device.h" #include "core/src/window_surface/gryphn_surface.h" -gnReturnCode checkCreateInstance(gnInstanceHandle instance, gnInstanceCreateInfo* info, gryphnInstanceFunctionLayers* next) { +gnReturnCode checkCreateInstance(gnInstanceHandle instance, gnInstanceCreateInfo* info, gryphnInstanceFunctionLayers* next, gnAllocators* alloctors) { if (next == NULL || next->createInstance == NULL) { gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){ .message = gnCreateString("Failed to load gnCreateInstance this indicates a bug within gryphn") }); return GN_FAILED_TO_LOAD_FUNCTION; } - return next->createInstance(instance, info, next->next); + return next->createInstance(instance, info, next->next, alloctors); } -void checkDestroyInstance(gnInstanceHandle instance, gryphnInstanceFunctionLayers* next) { +void checkDestroyInstance(gnInstanceHandle instance, gryphnInstanceFunctionLayers* next, gnAllocators* alloctors) { if (next == NULL || next->destroyInstance == NULL) { gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){ .message = gnCreateString("Failed to load gnDestroyInstance this indicates a bug within gryphn") }); return; } - next->destroyInstance(instance, next->next); + next->destroyInstance(instance, next->next, alloctors); } gnPhysicalDevice* checkGetPhysicalDevices(gnInstanceHandle instance, uint32_t* count) { diff --git a/projects/validation_layers/function_loader/src/instance_functions.h b/projects/validation_layers/function_loader/src/instance_functions.h index 366843f..ef60bf8 100644 --- a/projects/validation_layers/function_loader/src/instance_functions.h +++ b/projects/validation_layers/function_loader/src/instance_functions.h @@ -2,8 +2,8 @@ #include "core/src/instance/gryphn_instance.h" #include -gnReturnCode checkCreateInstance(gnInstanceHandle instance, gnInstanceCreateInfo* info, gryphnInstanceFunctionLayers* next); -void checkDestroyInstance(gnInstanceHandle instance, gryphnInstanceFunctionLayers* next); +gnReturnCode checkCreateInstance(gnInstanceHandle instance, gnInstanceCreateInfo* info, gryphnInstanceFunctionLayers* next, gnAllocators* alloctors); +void checkDestroyInstance(gnInstanceHandle instance, gryphnInstanceFunctionLayers* next, gnAllocators* alloctors); gnPhysicalDevice* checkGetPhysicalDevices(gnInstanceHandle instance, uint32_t* count); gnBool checkCanDevicePresent(gnPhysicalDevice device, gnWindowSurfaceHandle windowSurface);