write the allocators layer
This commit is contained in:
17
projects/core/gryphn_allocators.h
Normal file
17
projects/core/gryphn_allocators.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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)
|
10
projects/validation_layers/allocators/CMakeLists.txt
Normal file
10
projects/validation_layers/allocators/CMakeLists.txt
Normal file
@@ -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/)
|
39
projects/validation_layers/allocators/allocator_checker.c
Normal file
39
projects/validation_layers/allocators/allocator_checker.c
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#include "allocator_checker.h"
|
||||||
|
#include <core/src/gryphn_handles.h>
|
||||||
|
#include "core/src/instance/gryphn_debugger.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
@@ -0,0 +1,7 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
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);
|
Reference in New Issue
Block a user