Remove complicated memory abstractions
This commit is contained in:
parent
6195b521f5
commit
92db9206cc
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -1,45 +0,0 @@
|
|||||||
#include "mem_allocator.h"
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
@ -1,106 +0,0 @@
|
|||||||
#include "mem_ctx.h"
|
|
||||||
#include "aliases.h"
|
|
||||||
#include "mem_arena.h"
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
@ -1,48 +0,0 @@
|
|||||||
#include "mem_libc.h"
|
|
||||||
#include "aliases.h"
|
|
||||||
#include "mem_allocator.h"
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user