diff options
author | bonmas14 <bonmas14@gmail.com> | 2025-08-23 01:28:41 +0300 |
---|---|---|
committer | bonmas14 <bonmas14@gmail.com> | 2025-08-23 01:28:41 +0300 |
commit | 8ebdc95621bc61fdf3c98cd7ae4ddca67398df23 (patch) | |
tree | 205413ed09ac001e37267889d429a363c37008f2 /src/un_memory.c | |
parent | c96c355b360f18f982459e1a866dcbf5864efdb8 (diff) | |
download | ungrateful-8ebdc95621bc61fdf3c98cd7ae4ddca67398df23.tar.gz ungrateful-8ebdc95621bc61fdf3c98cd7ae4ddca67398df23.zip |
Diffstat (limited to 'src/un_memory.c')
-rw-r--r-- | src/un_memory.c | 90 |
1 files changed, 66 insertions, 24 deletions
diff --git a/src/un_memory.c b/src/un_memory.c index b092196..178fcbc 100644 --- a/src/un_memory.c +++ b/src/un_memory.c @@ -56,14 +56,9 @@ void *un_memory_alloc(u64 size, Allocator alloc) { return alloc.proc(NULL, size, UN_ALLOC_MSG_ALLOCATE, alloc.data); } -void *un_memory_realloc(void *ptr, u64 size, Allocator alloc) { +void un_memory_free(void *ptr, Allocator alloc) { assert(alloc.proc != NULL); - return alloc.proc(ptr, size, UN_ALLOC_MSG_REALLOCATE, alloc.data); -} - -void *un_memory_free(void *ptr, Allocator alloc) { - assert(alloc.proc != NULL); - return alloc.proc(ptr, 0, UN_ALLOC_MSG_FREE, alloc.data); + alloc.proc(ptr, 0, UN_ALLOC_MSG_FREE, alloc.data); } void un_memory_destroy(Allocator *alloc) { @@ -77,22 +72,16 @@ void un_memory_destroy(Allocator *alloc) { /// -------- Stdlib allocator -static ALLOCATOR_PROC_SIGNATURE(un_std_alloc_proc) { - UNUSED(data); +#if defined(OS_WINDOWS) +#include "un_mem_win_x64.c" - switch (message) { - case UN_ALLOC_MSG_ALLOCATE: - return calloc(1, size); - case UN_ALLOC_MSG_REALLOCATE: - return realloc(p, size); - case UN_ALLOC_MSG_FREE: - free(p); - break; - case UN_ALLOC_MSG_SELF_DELETE: break; - } +#elif defined(OS_LINUX) +#include "un_mem_linux_x64.c" - return NULL; -} +#else +#include "un_mem_std.c" + +#endif Allocator un_allocator_get_standard(void) { return CLITERAL(Allocator) { .proc = un_std_alloc_proc }; @@ -134,7 +123,6 @@ static ALLOCATOR_PROC_SIGNATURE(un_temp_alloc_proc) { switch (message) { case UN_ALLOC_MSG_ALLOCATE: return temp_alloc(size); - case UN_ALLOC_MSG_REALLOCATE: case UN_ALLOC_MSG_FREE: break; case UN_ALLOC_MSG_SELF_DELETE: break; @@ -204,8 +192,6 @@ static ALLOCATOR_PROC_SIGNATURE(un_arena_alloc_proc) { switch (message) { case UN_ALLOC_MSG_ALLOCATE: return arena_allocate(size, (Arena*)data); - case UN_ALLOC_MSG_REALLOCATE: - break; case UN_ALLOC_MSG_FREE: break; case UN_ALLOC_MSG_SELF_DELETE: @@ -224,3 +210,59 @@ Allocator un_allocator_create_arena(u64 size) { return alloc; } + +/// Wrapping allocator + + +typedef struct Allocation_Info { + void *p; + u64 size; +} Allocation_Info; + +typedef struct Wrapper { + // we need to create hashmap and add all allocations in here + // List allocations; + Allocator target; +} Wrapper; + +static Wrapper *wrapper_create(Allocator target) { + Wrapper *wrapper = (Wrapper *)un_memory_alloc(sizeof(Wrapper), un_allocator_get_standard()); + + un_memory_set((void*)wrapper, 0, sizeof(Wrapper)); + wrapper->target = target; + + return wrapper; +} + +static void wrapper_destroy(Wrapper *wrapper) { + assert(wrapper != NULL); + + un_memory_free(wrapper, un_allocator_get_standard()); +} + +static ALLOCATOR_PROC_SIGNATURE(un_wrapper_alloc_proc) { + assert(data != NULL); + + switch (message) { + case UN_ALLOC_MSG_ALLOCATE: + return un_memory_alloc(size, ((Wrapper*)data)->target); + case UN_ALLOC_MSG_FREE: + un_memory_free(p, ((Wrapper*)data)->target); + break; + case UN_ALLOC_MSG_SELF_DELETE: + un_memory_destroy(&((Wrapper*)data)->target); + wrapper_destroy((Wrapper*)data); + break; + } + + return NULL; +} + +Allocator un_allocator_create_wrapper(Allocator target) { + Allocator alloc = { 0 }; + + alloc.proc = un_wrapper_alloc_proc; + alloc.data = wrapper_create(target); + + return alloc; +} |