Files
Gryphn/projects/core/src/instance/gryphn_instance.c
2025-08-09 20:04:57 -04:00

93 lines
4.3 KiB
C

#include "gryphn_instance.h"
#include "instance/gryphn_instance.h"
#include <loader/src/gryphn_instance_functions.h>
#include "loader/src/gryphn_extension_loader.h"
#include "loader/src/gryphn_loader.h"
#include "loader/src/gryphn_loader.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 = 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();
loaderLayerArrayListAdd((*instance)->layers, loadLayer((loaderInfo){
.api = info->coreAPI,
.layerToLoad = api_layer
}));
gnBool unsupportedExtension = GN_FALSE;
for (int c = 0; c < GN_EXT_MAX; c++) (*instance)->enabledExtensions[c] = GN_FALSE;
for (int c = 0; c < GN_LAYER_MAX; c++) (*instance)->enabledLayerCounts[c] = 0;
for (uint32_t i = 0; i < info->extensionCount; i++) {
(*instance)->enabledExtensions[info->extensions[i]] = GN_TRUE;
if (!gnIsExtensionSuppoted(info->coreAPI, info->extensions[i])) unsupportedExtension = GN_TRUE;
}
if ((*instance)->enabledExtensions[GN_EXT_SYNCHRONIZATION]) loaderLayerArrayListRefAt((*instance)->layers, 0)->syncFunctions = loadAPISyncFunctions(info->coreAPI);
if ((*instance)->enabledExtensions[GN_EXT_QUEUES]) loaderLayerArrayListRefAt((*instance)->layers, 0)->queueFunctions = loadAPIQueueFunctions(info->coreAPI);
if (info->debuggerInfo.layerCount > 0) {
for (uint32_t i = 0; i < info->debuggerInfo.layerCount; i++) {
(*instance)->enabledLayerCounts[info->debuggerInfo.layers[i]]++;
if (info->debuggerInfo.layers[i] == GN_DEBUGGER_LAYER_FUNCTIONS) {
loaderLayerArrayListAdd((*instance)->layers, loadLayer((loaderInfo){
.api = info->coreAPI,
.layerToLoad = function_checker_layer
}));
}
}
(*instance)->debugger = info->debuggerInfo;
(*instance)->hasDebugger = GN_TRUE;
}
(*instance)->allLayers = malloc(sizeof(gryphnInstanceFunctionLayers) * (
(*instance)->enabledLayerCounts[GN_DEBUGGER_LAYER_FUNCTIONS] +
1 // for the core layer
));
int layerIDX = 0;
for (uint32_t i = 0; i < info->debuggerInfo.layerCount; i++) {
if (info->debuggerInfo.layers[i] == GN_DEBUGGER_LAYER_FUNCTIONS) (*instance)->allLayers[layerIDX++] = checkerLoadInstanceFunctions();
(*instance)->allLayers[layerIDX - 1].next = &(*instance)->allLayers[layerIDX];
}
(*instance)->allLayers[layerIDX] = gryphnLoadAPILayer(info->coreAPI);
(*instance)->functions = &(*instance)->allLayers[0];
resetLayer(*instance);
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;
(*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)->allocators);
(*instance)->allocators.free(*instance, (*instance)->allocators.userData);
*instance = GN_NULL_HANDLE;
}