Remove alloc_obj and pass double pointer to free function

This commit is contained in:
Abdelrahman Said 2024-03-24 21:05:32 +00:00
parent 8a58a1cc48
commit 6f799c4330
4 changed files with 41 additions and 23 deletions

View File

@ -12,7 +12,7 @@ typedef void *(MemAllocAlignedFunc)(u64 size, u64 alignment, void *alloc_obj);
typedef void *(MemReallocFunc)(void *ptr, u64 size, void *alloc_obj); typedef void *(MemReallocFunc)(void *ptr, u64 size, void *alloc_obj);
typedef void *(MemReallocAlignedFunc)(void *ptr, u64 size, u64 alignment, typedef void *(MemReallocAlignedFunc)(void *ptr, u64 size, u64 alignment,
void *alloc_obj); void *alloc_obj);
typedef void(MemFreeFunc)(void *ptr, void *alloc_obj); typedef void(MemFreeFunc)(void **ptr, void *alloc_obj);
typedef struct allocator Allocator; typedef struct allocator Allocator;
struct allocator { struct allocator {
@ -24,6 +24,15 @@ struct allocator {
MemFreeFunc *free; 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 #ifdef __cplusplus
} }
#endif // __cplusplus #endif // __cplusplus

View File

@ -51,7 +51,8 @@ internal void base_arena_free(BaseArena *arena);
internal ArenaAllocHDR *find_alloc_header(BaseArena *arena, void *alloc_ptr); internal ArenaAllocHDR *find_alloc_header(BaseArena *arena, void *alloc_ptr);
internal void *mem_arena_alloc(u64 size, void *alloc_obj); internal void *mem_arena_alloc(u64 size, void *alloc_obj);
internal void *mem_arena_alloc_aligned(u64 size, u64 alignment, void *alloc_obj); internal void *mem_arena_alloc_aligned(u64 size, u64 alignment,
void *alloc_obj);
internal void *mem_arena_realloc(void *ptr, u64 size, void *alloc_obj); internal void *mem_arena_realloc(void *ptr, u64 size, void *alloc_obj);
internal void *mem_arena_realloc_aligned(void *ptr, u64 size, u64 alignment, internal void *mem_arena_realloc_aligned(void *ptr, u64 size, u64 alignment,
void *alloc_obj); void *alloc_obj);
@ -379,7 +380,8 @@ internal void *mem_arena_alloc(u64 size, void *alloc_obj) {
return wapp_mem_arena_alloc(arena, size); return wapp_mem_arena_alloc(arena, size);
} }
internal void *mem_arena_alloc_aligned(u64 size, u64 alignment, void *alloc_obj) { internal void *mem_arena_alloc_aligned(u64 size, u64 alignment,
void *alloc_obj) {
Arena *arena = (Arena *)alloc_obj; Arena *arena = (Arena *)alloc_obj;
return wapp_mem_arena_alloc_aligned(arena, size, alignment); return wapp_mem_arena_alloc_aligned(arena, size, alignment);

View File

@ -7,7 +7,7 @@
internal void *mem_libc_alloc(u64 size, void *alloc_obj); 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_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_realloc(void *ptr, u64 size, void *alloc_obj);
internal void mem_libc_free(void *ptr, void *alloc_obj); internal void mem_libc_free(void **ptr, void *alloc_obj);
Allocator wapp_mem_libc_allocator(void) { Allocator wapp_mem_libc_allocator(void) {
return (Allocator){ return (Allocator){
@ -43,4 +43,11 @@ internal void *mem_libc_realloc(void *ptr, u64 size, void *alloc_obj) {
return output; return output;
} }
internal void mem_libc_free(void *ptr, void *alloc_obj) { free(ptr); } internal void mem_libc_free(void **ptr, void *alloc_obj) {
if (!ptr || !(*ptr)) {
return;
}
free(*ptr);
*ptr = NULL;
}