mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Squashed commit of the following: commit c48929d25a84331ca8bd1b27be2c6aa4f3b4db12 Author: Kayne Ruse <kayneruse@gmail.com> Date: Fri Aug 9 23:12:49 2024 +1000 Update c-cpp.yml I'm only going a little bit nuts. commit 3f65882bdc75f1712c9a3c9d2ddf0e53a27ce4b9 Author: Kayne Ruse <kayneruse@gmail.com> Date: Fri Aug 9 22:49:18 2024 +1000 Update c-cpp.yml It would be great if this was documented better. commit d3abeda7c2776bb2e82ca635cd659967afa6ad75 Author: Kayne Ruse <kayneruse@gmail.com> Date: Fri Aug 9 21:40:39 2024 +1000 Bumped license date commit 17bbce9d7ca212064bc95e467933c5602a89fb4c Author: Kayne Ruse <kayneruse@gmail.com> Date: Fri Aug 9 21:33:57 2024 +1000 Fixed the failing build on mingw There seems to be persistent issues with different compilers displaying the values of size_t, so I simply cast it to an integer. commit 843a76d0ac44328776f8ecf83a66caa7ea7fdef6 Author: Kayne Ruse <kayneruse@gmail.com> Date: Fri Aug 9 21:17:17 2024 +1000 Updated CI commit 08cd89c58d8d028438b9f83a60f5dd9265cc3465 Author: Kayne Ruse <kayneruse@gmail.com> Date: Fri Aug 9 21:09:03 2024 +1000 Why did that fail last time?
55 lines
1.4 KiB
C
55 lines
1.4 KiB
C
#include "toy_memory.h"
|
|
#include "toy_refstring.h"
|
|
#include "toy_reffunction.h"
|
|
|
|
#include "toy_console_colors.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
//default allocator
|
|
void* Toy_private_defaultMemoryAllocator(void* pointer, size_t oldSize, size_t newSize) {
|
|
//causes issues, so just skip out with a NO-OP (DISABLED for performance reasons)
|
|
// if (newSize == 0 && oldSize == 0) {
|
|
// 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, replacing %d)\n" TOY_CC_RESET, (int)newSize, (int)oldSize);
|
|
return NULL;
|
|
}
|
|
|
|
return mem;
|
|
}
|
|
|
|
//static variables
|
|
static Toy_MemoryAllocatorFn allocator = Toy_private_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);
|
|
Toy_setRefFunctionAllocatorFn(fn);
|
|
}
|