From af7d75b728d655680b327dbd4a9f87304949e5de Mon Sep 17 00:00:00 2001 From: Gregory Wells Date: Sat, 9 Aug 2025 20:04:34 -0400 Subject: [PATCH] write the allocators layer --- projects/core/gryphn_allocators.h | 17 ++++++++ .../allocators/CMakeLists.txt | 10 +++++ .../allocators/allocator_checker.c | 39 +++++++++++++++++++ .../allocators/allocator_checker.h | 7 ++++ 4 files changed, 73 insertions(+) create mode 100644 projects/core/gryphn_allocators.h create mode 100644 projects/validation_layers/allocators/CMakeLists.txt create mode 100644 projects/validation_layers/allocators/allocator_checker.c create mode 100644 projects/validation_layers/allocators/allocator_checker.h diff --git a/projects/core/gryphn_allocators.h b/projects/core/gryphn_allocators.h new file mode 100644 index 0000000..965236f --- /dev/null +++ b/projects/core/gryphn_allocators.h @@ -0,0 +1,17 @@ +#pragma once +#include + +typedef void* (*PFN_gnMalloc) (size_t size, void* userData); +typedef void* (*PFN_gnCalloc) (int cnt, size_t size, void* userData); +typedef void* (*PFN_gnRealloc) (void* ptr, size_t size, void* userData); +typedef void (*PFN_gnFree) (void* ptr, void* userData); + +typedef struct gnAllocators { + void* userData; + PFN_gnMalloc malloc; + PFN_gnCalloc calloc; + PFN_gnRealloc realloc; + PFN_gnFree free; +} gnAllocators; + +#define gnMalloc(allocators, size) allocators->malloc(size, allocators->userData) diff --git a/projects/validation_layers/allocators/CMakeLists.txt b/projects/validation_layers/allocators/CMakeLists.txt new file mode 100644 index 0000000..58bd50c --- /dev/null +++ b/projects/validation_layers/allocators/CMakeLists.txt @@ -0,0 +1,10 @@ +set(CMAKE_EXPORT_COMPILE_COMMANDS on) +project(GryphnAllocatorChecker) +add_library(GryphnAllocatorChecker STATIC allocator_checker.c) +target_include_directories(GryphnAllocatorChecker PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/) +target_include_directories(GryphnAllocatorChecker PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../) +target_include_directories(GryphnAllocatorChecker PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../core/src/) +target_include_directories(GryphnAllocatorChecker PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../platform/) +target_include_directories(GryphnAllocatorChecker PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../utils) +target_include_directories(GryphnAllocatorChecker PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../core/) +target_include_directories(GryphnAllocatorChecker PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../extensions/) diff --git a/projects/validation_layers/allocators/allocator_checker.c b/projects/validation_layers/allocators/allocator_checker.c new file mode 100644 index 0000000..2996856 --- /dev/null +++ b/projects/validation_layers/allocators/allocator_checker.c @@ -0,0 +1,39 @@ +#include "allocator_checker.h" +#include +#include "core/src/instance/gryphn_debugger.h" +#include +#include + +void* gnMallocFunc (size_t size, void* userData) { + char outBuffer[500]; + snprintf(outBuffer, sizeof(outBuffer), "Allocating %lu bytes with malloc", size); + + gnDebuggerSetVerboseMessage(userData, (gnMessageData) { + .message = gnCreateString(outBuffer) + }); + return malloc(size); +} +void* gnCallocFunc (int cnt, size_t size, void* userData) { + char outBuffer[500]; + snprintf(outBuffer, sizeof(outBuffer), "Allocating %i items of %lu bytes with calloc", cnt, size); + gnDebuggerSetVerboseMessage(userData, (gnMessageData){ + .message = gnCreateString(outBuffer) + }); + return calloc(cnt, size); +} +void* gnReallocFunc (void* ptr, size_t size, void* userData) { + char outBuffer[500]; + snprintf(outBuffer, sizeof(outBuffer), "Reallocating %p with new size %lu", ptr, size); + gnDebuggerSetVerboseMessage(userData, (gnMessageData){ + .message = gnCreateString(outBuffer) + }); + return realloc(ptr, size); +} +void gnFreeFunc (void* ptr, void* userData) { + char outBuffer[500]; + snprintf(outBuffer, sizeof(outBuffer), "Freeing %p", ptr); + gnDebuggerSetVerboseMessage(userData, (gnMessageData){ + .message = gnCreateString(outBuffer) + }); + free(ptr); +} diff --git a/projects/validation_layers/allocators/allocator_checker.h b/projects/validation_layers/allocators/allocator_checker.h new file mode 100644 index 0000000..397af88 --- /dev/null +++ b/projects/validation_layers/allocators/allocator_checker.h @@ -0,0 +1,7 @@ +#pragma once +#include + +void* gnMallocFunc (size_t size, void* userData); +void* gnCallocFunc (int cnt, size_t size, void* userData); +void* gnReallocFunc (void* ptr, size_t size, void* userData); +void gnFreeFunc (void* ptr, void* userData);