diff options
| author | Syndamia <kamen@syndamia.com> | 2026-04-28 14:06:44 +0300 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2026-05-12 07:08:56 +0300 |
| commit | 0d249b1d47a7f84cfe5f09ad070d1affd26566da (patch) | |
| tree | f395f67eecb27daea3fc4a9ad8e59bf40f988984 /MemoryManager.c | |
| parent | 5c36b582ad60a569ed996fe188ed92ef6bd7fc5f (diff) | |
| download | foollib-0d249b1d47a7f84cfe5f09ad070d1affd26566da.tar foollib-0d249b1d47a7f84cfe5f09ad070d1affd26566da.tar.gz foollib-0d249b1d47a7f84cfe5f09ad070d1affd26566da.zip | |
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.
Diffstat (limited to 'MemoryManager.c')
| -rw-r--r-- | MemoryManager.c | 31 |
1 files changed, 31 insertions, 0 deletions
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 <stdlib.h> + +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, + }; +} |
