mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
Basically, all Toy varaibles, functions, etc. are prepended with "Toy_", and macros are prepended with "TOY_". This is to reduce namespace pollution, which was an issue pointed out to be - blame @GyroVorbis. I've also bumped the minor version number - theoretically I should bump the major number, but I'm not quite ready for 1.0 yet.
59 lines
1.4 KiB
C
59 lines
1.4 KiB
C
#include "toy_memory.h"
|
|
#include "toy_refstring.h"
|
|
|
|
#include "toy_console_colors.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
//default allocator
|
|
static void* defaultMemoryAllocator(void* pointer, size_t oldSize, size_t newSize) {
|
|
if (newSize == 0 && oldSize == 0) {
|
|
//causes issues, so just skip out with a NO-OP
|
|
return NULL;
|
|
}
|
|
|
|
if (newSize == 0) {
|
|
free(pointer);
|
|
|
|
return NULL;
|
|
}
|
|
|
|
void* mem = realloc(pointer, newSize);
|
|
|
|
if (mem == NULL) {
|
|
fprintf(stderr, TOY_CC_ERROR "[internal] Memory allocation error (requested %d for %ld, replacing %d)\n" TOY_CC_RESET, (int)newSize, (long int)pointer, (int)oldSize);
|
|
exit(-1);
|
|
}
|
|
|
|
return mem;
|
|
}
|
|
|
|
//static variables
|
|
static Toy_MemoryAllocatorFn allocator;
|
|
|
|
//preload
|
|
static void __attribute__((constructor)) preloadMemoryAllocator() {
|
|
Toy_setMemoryAllocator(defaultMemoryAllocator);
|
|
}
|
|
|
|
//exposed API
|
|
void* Toy_reallocate(void* pointer, size_t oldSize, size_t newSize) {
|
|
return allocator(pointer, oldSize, newSize);
|
|
}
|
|
|
|
void Toy_setMemoryAllocator(Toy_MemoryAllocatorFn fn) {
|
|
if (fn == NULL) {
|
|
fprintf(stderr, TOY_CC_ERROR "[internal] Memory allocator error (can't be null)\n" TOY_CC_RESET);
|
|
exit(-1);
|
|
}
|
|
|
|
if (fn == Toy_reallocate) {
|
|
fprintf(stderr, TOY_CC_ERROR "[internal] Memory allocator error (can't loop the Toy_reallocate function)\n" TOY_CC_RESET);
|
|
exit(-1);
|
|
}
|
|
|
|
allocator = fn;
|
|
Toy_setRefStringAllocatorFn(fn);
|
|
}
|