From 0d249b1d47a7f84cfe5f09ad070d1affd26566da Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 28 Apr 2026 14:06:44 +0300 Subject: feat!: Refactor using MemoryManager The idea is to provide a universal memory management "API", through which you allocate and deallocate. This allows our implementations (for String and Vector) to be more generic. It does also allow usage of different allocators depending on situation. --- MemoryManager.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 MemoryManager.c (limited to 'MemoryManager.c') diff --git a/MemoryManager.c b/MemoryManager.c new file mode 100644 index 0000000..68dab6e --- /dev/null +++ b/MemoryManager.c @@ -0,0 +1,31 @@ +#include "MemoryManager.h" +#include + +Result std_alloc(MemoryManager mm, void** ptr, usize size) { + *ptr = malloc(size); + return (*ptr == NULL) ? R_Err : R_Ok; +} + +Result std_realloc(MemoryManager mm, void** ptr, usize size) { + *ptr = realloc(*ptr, size); + return (*ptr == NULL) ? R_Err : R_Ok; +} + +Result std_free(MemoryManager mm, void** ptr) { + free(*ptr); + *ptr = NULL; + return R_Ok; +} + +Result std_destroy(MemoryManager *mm) { + return R_Unavailable; +} + +MemoryManager M_std() { + return (MemoryManager){ + .alloc = &std_alloc, + .realloc = &std_realloc, + .free = &std_free, + .destroy = &std_destroy, + }; +} -- cgit v1.2.3