From 92db9206cc4619ec0f8f6d629fb45e45a5ec8501 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Wed, 24 Apr 2024 22:51:42 +0100 Subject: [PATCH] Remove complicated memory abstractions --- mem/include/allocator/mem_allocator.h | 40 ---------- mem/include/ctx/mem_ctx.h | 32 -------- mem/include/libc/mem_libc.h | 15 ---- mem/src/allocator/mem_allocator.c | 45 ----------- mem/src/ctx/mem_ctx.c | 106 -------------------------- mem/src/libc/mem_libc.c | 48 ------------ 6 files changed, 286 deletions(-) delete mode 100644 mem/include/allocator/mem_allocator.h delete mode 100644 mem/include/ctx/mem_ctx.h delete mode 100644 mem/include/libc/mem_libc.h delete mode 100644 mem/src/allocator/mem_allocator.c delete mode 100644 mem/src/ctx/mem_ctx.c delete mode 100644 mem/src/libc/mem_libc.c diff --git a/mem/include/allocator/mem_allocator.h b/mem/include/allocator/mem_allocator.h deleted file mode 100644 index 44f4751..0000000 --- a/mem/include/allocator/mem_allocator.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef MEM_ALLOCATOR_H -#define MEM_ALLOCATOR_H - -#include "aliases.h" - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus - -typedef void *(MemAllocFunc)(u64 size, void *alloc_obj); -typedef void *(MemAllocAlignedFunc)(u64 size, u64 alignment, void *alloc_obj); -typedef void *(MemReallocFunc)(void *ptr, u64 size, void *alloc_obj); -typedef void *(MemReallocAlignedFunc)(void *ptr, u64 size, u64 alignment, - void *alloc_obj); -typedef void(MemFreeFunc)(void **ptr, void *alloc_obj); - -typedef struct allocator Allocator; -struct allocator { - void *obj; - MemAllocFunc *alloc; - MemAllocAlignedFunc *alloc_aligned; - MemReallocFunc *realloc; - MemReallocAlignedFunc *realloc_aligned; - MemFreeFunc *free; -}; - -void *wapp_mem_allocator_alloc(const Allocator *allocator, u64 size); -void *wapp_mem_allocator_alloc_aligned(const Allocator *allocator, u64 size, - u64 alignment); -void *wapp_mem_allocator_realloc(const Allocator *allocator, void *ptr, - u64 size); -void *wapp_mem_allocator_realloc_aligned(const Allocator *allocator, void *ptr, - u64 size, u64 alignment); -void wapp_mem_allocator_free(const Allocator *allocator, void **ptr); - -#ifdef __cplusplus -} -#endif // __cplusplus - -#endif // !MEM_ALLOCATOR_H diff --git a/mem/include/ctx/mem_ctx.h b/mem/include/ctx/mem_ctx.h deleted file mode 100644 index aea0fc4..0000000 --- a/mem/include/ctx/mem_ctx.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef MEM_CTX_H -#define MEM_CTX_H - -#include "mem_allocator.h" -#include "mem_arena.h" - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus - -typedef enum { - CTX_DEST_BUFFER_MAIN, - CTX_DEST_BUFFER_TEMP, - - COUNT_CTX_DEST_BUFFER, -} CTXDestBuffer; - -Allocator wapp_mem_ctx_allocator(CTXDestBuffer buffer); -void wapp_mem_ctx_init(u64 main_buf_capacity, u64 temp_buf_capacity); -void *wapp_mem_ctx_alloc(CTXDestBuffer buffer, u64 size); -void *wapp_mem_ctx_alloc_aligned(CTXDestBuffer buffer, u64 size, u64 alignment); -void *wapp_mem_ctx_realloc(CTXDestBuffer buffer, void *ptr, u64 size); -void *wapp_mem_ctx_realloc_aligned(CTXDestBuffer buffer, void *ptr, u64 size, - u64 alignment); -void wapp_mem_ctx_clear(CTXDestBuffer buffer); -void wapp_mem_ctx_free(void); - -#ifdef __cplusplus -} -#endif // __cplusplus - -#endif // !MEM_CTX_H diff --git a/mem/include/libc/mem_libc.h b/mem/include/libc/mem_libc.h deleted file mode 100644 index 608821c..0000000 --- a/mem/include/libc/mem_libc.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef MEM_LIBC_H -#define MEM_LIBC_H - -#include "mem_allocator.h" -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus - -Allocator wapp_mem_libc_allocator(void); - -#ifdef __cplusplus -} -#endif // __cplusplus - -#endif // !MEM_LIBC_H diff --git a/mem/src/allocator/mem_allocator.c b/mem/src/allocator/mem_allocator.c deleted file mode 100644 index 3c9fdde..0000000 --- a/mem/src/allocator/mem_allocator.c +++ /dev/null @@ -1,45 +0,0 @@ -#include "mem_allocator.h" -#include - -void *wapp_mem_allocator_alloc(const Allocator *allocator, u64 size) { - if (!allocator || !(allocator->alloc)) { - return NULL; - } - - return allocator->alloc(size, allocator->obj); -} - -void *wapp_mem_allocator_alloc_aligned(const Allocator *allocator, u64 size, - u64 alignment) { - if (!allocator || !(allocator->alloc_aligned)) { - return NULL; - } - - return allocator->alloc_aligned(size, alignment, allocator->obj); -} - -void *wapp_mem_allocator_realloc(const Allocator *allocator, void *ptr, - u64 size) { - if (!allocator || !(allocator->realloc)) { - return NULL; - } - - return allocator->realloc(ptr, size, allocator->obj); -} - -void *wapp_mem_allocator_realloc_aligned(const Allocator *allocator, void *ptr, - u64 size, u64 alignment) { - if (!allocator || !(allocator->realloc_aligned)) { - return NULL; - } - - return allocator->realloc_aligned(ptr, size, alignment, allocator->obj); -} - -void wapp_mem_allocator_free(const Allocator *allocator, void **ptr) { - if (!allocator || !(allocator->free)) { - return; - } - - allocator->free(ptr, allocator->obj); -} diff --git a/mem/src/ctx/mem_ctx.c b/mem/src/ctx/mem_ctx.c deleted file mode 100644 index e95c48c..0000000 --- a/mem/src/ctx/mem_ctx.c +++ /dev/null @@ -1,106 +0,0 @@ -#include "mem_ctx.h" -#include "aliases.h" -#include "mem_arena.h" -#include -#include - -typedef struct mem_ctx MemCTX; -struct mem_ctx { - Arena *main; - Arena *temp; - bool main_initialised; - bool temp_initialised; -}; - -internal MemCTX g_context = {0}; - -internal Arena *get_arena(CTXDestBuffer buffer); - -Allocator wapp_mem_ctx_allocator(CTXDestBuffer buffer) { - Arena *arena = get_arena(buffer); - return wapp_mem_arena_allocator(arena); -} - -void wapp_mem_ctx_init(u64 main_buf_capacity, u64 temp_buf_capacity) { - g_context.main_initialised = - wapp_mem_arena_init(&g_context.main, main_buf_capacity); - g_context.temp_initialised = - wapp_mem_arena_init(&g_context.temp, temp_buf_capacity); -} - -void *wapp_mem_ctx_alloc(CTXDestBuffer buffer, u64 size) { - Arena *arena = get_arena(buffer); - if (!arena) { - return NULL; - } - - return wapp_mem_arena_alloc(arena, size); -} - -void *wapp_mem_ctx_alloc_aligned(CTXDestBuffer buffer, u64 size, - u64 alignment) { - Arena *arena = get_arena(buffer); - if (!arena) { - return NULL; - } - - return wapp_mem_arena_alloc_aligned(arena, size, alignment); -} - -void *wapp_mem_ctx_realloc(CTXDestBuffer buffer, void *ptr, u64 size) { - Arena *arena = get_arena(buffer); - if (!arena) { - return NULL; - } - - return wapp_mem_arena_realloc(arena, ptr, size); -} - -void *wapp_mem_ctx_realloc_aligned(CTXDestBuffer buffer, void *ptr, u64 size, - u64 alignment) { - Arena *arena = get_arena(buffer); - if (!arena) { - return NULL; - } - - return wapp_mem_arena_realloc_aligned(arena, ptr, size, alignment); -} - -void wapp_mem_ctx_clear(CTXDestBuffer buffer) { - Arena *arena = get_arena(buffer); - if (!arena) { - return; - } - - wapp_mem_arena_clear(arena); -} - -void wapp_mem_ctx_free(void) { - wapp_mem_arena_free(&(g_context.main)); - g_context.main_initialised = false; - - wapp_mem_arena_free(&(g_context.temp)); - g_context.temp_initialised = false; -} - -internal Arena *get_arena(CTXDestBuffer buffer) { - Arena *output = NULL; - - switch (buffer) { - case CTX_DEST_BUFFER_MAIN: - if (g_context.main_initialised) { - output = g_context.main; - } - break; - case CTX_DEST_BUFFER_TEMP: - if (g_context.temp_initialised) { - output = g_context.temp; - } - break; - default: - assert(false && "Not all context destination buffers are handled"); - break; - } - - return output; -} diff --git a/mem/src/libc/mem_libc.c b/mem/src/libc/mem_libc.c deleted file mode 100644 index 8ab787e..0000000 --- a/mem/src/libc/mem_libc.c +++ /dev/null @@ -1,48 +0,0 @@ -#include "mem_libc.h" -#include "aliases.h" -#include "mem_allocator.h" -#include -#include - -internal void *mem_libc_alloc(u64 size, void *alloc_obj); -internal void *mem_libc_alloc_aligned(u64 size, u64 alignment, void *alloc_obj); -internal void *mem_libc_realloc(void *ptr, u64 size, void *alloc_obj); -internal void mem_libc_free(void **ptr, void *alloc_obj); - -Allocator wapp_mem_libc_allocator(void) { - return (Allocator){ - .obj = NULL, - .alloc = mem_libc_alloc, - .alloc_aligned = mem_libc_alloc_aligned, - .realloc = mem_libc_realloc, - .realloc_aligned = NULL, - .free = mem_libc_free, - }; -} - -internal void *mem_libc_alloc(u64 size, void *alloc_obj) { - return calloc(1, size); -} - -internal void *mem_libc_alloc_aligned(u64 size, u64 alignment, - void *alloc_obj) { - void *output = aligned_alloc(alignment, size); - if (output) { - memset(output, 0, size); - } - - return output; -} - -internal void *mem_libc_realloc(void *ptr, u64 size, void *alloc_obj) { - return realloc(ptr, size); -} - -internal void mem_libc_free(void **ptr, void *alloc_obj) { - if (!ptr || !(*ptr)) { - return; - } - - free(*ptr); - *ptr = NULL; -}