Reorganise allocators
This commit is contained in:
parent
d30eee0cf8
commit
8468bb8e28
@ -1,7 +1,10 @@
|
|||||||
#include "mem_allocator.h"
|
#include "mem_allocator.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#pragma region Allocator API definitions
|
/******************************************************************************
|
||||||
|
**** Allocator API definitions
|
||||||
|
***************************************************************************///
|
||||||
|
|
||||||
void *wapp_mem_allocator_alloc(const Allocator *allocator, u64 size) {
|
void *wapp_mem_allocator_alloc(const Allocator *allocator, u64 size) {
|
||||||
if (!allocator || !(allocator->alloc)) {
|
if (!allocator || !(allocator->alloc)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -44,4 +47,5 @@ void wapp_mem_allocator_free(const Allocator *allocator, void **ptr) {
|
|||||||
|
|
||||||
allocator->free(ptr, allocator->obj);
|
allocator->free(ptr, allocator->obj);
|
||||||
}
|
}
|
||||||
#pragma endregion Allocator API definitions
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -7,16 +7,23 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif // __cplusplus
|
#endif // __cplusplus
|
||||||
|
|
||||||
#pragma region Allocator function pointer types
|
/******************************************************************************
|
||||||
|
**** Allocator function pointer types
|
||||||
|
***************************************************************************///
|
||||||
|
|
||||||
typedef void *(MemAllocFunc)(u64 size, void *alloc_obj);
|
typedef void *(MemAllocFunc)(u64 size, void *alloc_obj);
|
||||||
typedef void *(MemAllocAlignedFunc)(u64 size, u64 alignment, 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 *(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);
|
||||||
#pragma endregion Allocator function pointer types
|
|
||||||
|
|
||||||
#pragma region Allocator type
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
**** Allocator type
|
||||||
|
***************************************************************************///
|
||||||
|
|
||||||
typedef struct allocator Allocator;
|
typedef struct allocator Allocator;
|
||||||
struct allocator {
|
struct allocator {
|
||||||
void *obj;
|
void *obj;
|
||||||
@ -26,9 +33,13 @@ struct allocator {
|
|||||||
MemReallocAlignedFunc *realloc_aligned;
|
MemReallocAlignedFunc *realloc_aligned;
|
||||||
MemFreeFunc *free;
|
MemFreeFunc *free;
|
||||||
};
|
};
|
||||||
#pragma endregion Allocator type
|
|
||||||
|
|
||||||
#pragma region Allocator API declarations
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
**** Allocator API declarations
|
||||||
|
***************************************************************************///
|
||||||
|
|
||||||
void *wapp_mem_allocator_alloc(const Allocator *allocator, u64 size);
|
void *wapp_mem_allocator_alloc(const Allocator *allocator, u64 size);
|
||||||
void *wapp_mem_allocator_alloc_aligned(const Allocator *allocator, u64 size,
|
void *wapp_mem_allocator_alloc_aligned(const Allocator *allocator, u64 size,
|
||||||
u64 alignment);
|
u64 alignment);
|
||||||
@ -37,7 +48,8 @@ void *wapp_mem_allocator_realloc(const Allocator *allocator, void *ptr,
|
|||||||
void *wapp_mem_allocator_realloc_aligned(const Allocator *allocator, void *ptr,
|
void *wapp_mem_allocator_realloc_aligned(const Allocator *allocator, void *ptr,
|
||||||
u64 size, u64 alignment);
|
u64 size, u64 alignment);
|
||||||
void wapp_mem_allocator_free(const Allocator *allocator, void **ptr);
|
void wapp_mem_allocator_free(const Allocator *allocator, void **ptr);
|
||||||
#pragma endregion Allocator API declarations
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
#include "mem_arena.h"
|
#include "mem_arena.h"
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
#include "mem_allocator.h"
|
|
||||||
#include "mem_utils.h"
|
#include "mem_utils.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -25,40 +24,10 @@ struct arena {
|
|||||||
#endif // ifdef WAPP_PLATFORM_WINDOWS
|
#endif // ifdef WAPP_PLATFORM_WINDOWS
|
||||||
};
|
};
|
||||||
|
|
||||||
#pragma region Arena Allocator wrappers declarations
|
/******************************************************************************
|
||||||
internal inline void *mem_arena_alloc(u64 size, void *alloc_obj);
|
**** Arena API definitions
|
||||||
internal inline void *mem_arena_alloc_aligned(u64 size, u64 alignment,
|
***************************************************************************///
|
||||||
void *alloc_obj);
|
|
||||||
#pragma endregion Arena Allocator wrappers declarations
|
|
||||||
|
|
||||||
#pragma region Arena Allocator API definitions
|
|
||||||
Allocator wapp_mem_arena_allocator_init_custom(u64 base_capacity,
|
|
||||||
MemAllocFlags flags,
|
|
||||||
bool zero_buffer) {
|
|
||||||
Allocator allocator = {0};
|
|
||||||
bool initialised = wapp_mem_arena_init_custom(
|
|
||||||
(Arena **)(&allocator.obj), base_capacity, flags, zero_buffer);
|
|
||||||
if (!initialised) {
|
|
||||||
return allocator;
|
|
||||||
}
|
|
||||||
|
|
||||||
allocator.alloc = mem_arena_alloc;
|
|
||||||
allocator.alloc_aligned = mem_arena_alloc_aligned;
|
|
||||||
|
|
||||||
return allocator;
|
|
||||||
}
|
|
||||||
|
|
||||||
void wapp_mem_arena_allocator_clear(Allocator *allocator) {
|
|
||||||
wapp_mem_arena_clear((Arena *)(allocator->obj));
|
|
||||||
}
|
|
||||||
|
|
||||||
void wapp_mem_arena_allocator_destroy(Allocator *allocator) {
|
|
||||||
wapp_mem_arena_destroy((Arena **)(&(allocator->obj)));
|
|
||||||
*allocator = (Allocator){0};
|
|
||||||
}
|
|
||||||
#pragma endregion Arena Allocator API definitions
|
|
||||||
|
|
||||||
#pragma region API definitions
|
|
||||||
bool wapp_mem_arena_init_custom(Arena **arena, u64 base_capacity,
|
bool wapp_mem_arena_init_custom(Arena **arena, u64 base_capacity,
|
||||||
MemAllocFlags flags, bool zero_buffer) {
|
MemAllocFlags flags, bool zero_buffer) {
|
||||||
if (!arena || *arena || base_capacity == 0) {
|
if (!arena || *arena || base_capacity == 0) {
|
||||||
@ -149,17 +118,5 @@ void wapp_mem_arena_destroy(Arena **arena) {
|
|||||||
free(*arena);
|
free(*arena);
|
||||||
*arena = NULL;
|
*arena = NULL;
|
||||||
}
|
}
|
||||||
#pragma endregion API definitions
|
|
||||||
|
|
||||||
#pragma region Arena Allocator wrappers definitions
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
internal inline void *mem_arena_alloc(u64 size, void *alloc_obj) {
|
|
||||||
Arena *arena = (Arena *)alloc_obj;
|
|
||||||
return wapp_mem_arena_alloc(arena, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal inline void *mem_arena_alloc_aligned(u64 size, u64 alignment,
|
|
||||||
void *alloc_obj) {
|
|
||||||
Arena *arena = (Arena *)alloc_obj;
|
|
||||||
return wapp_mem_arena_alloc_aligned(arena, size, alignment);
|
|
||||||
}
|
|
||||||
#pragma endregion Arena Allocator wrappers definitions
|
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
#define MEM_ARENA_H
|
#define MEM_ARENA_H
|
||||||
|
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
#include "mem_allocator.h"
|
|
||||||
#include "mem_utils.h"
|
#include "mem_utils.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
@ -12,41 +11,10 @@ extern "C" {
|
|||||||
|
|
||||||
typedef struct arena Arena;
|
typedef struct arena Arena;
|
||||||
|
|
||||||
#pragma region Allocator wrapper macros
|
/******************************************************************************
|
||||||
#define wapp_mem_arena_allocator_init(base_capacity) \
|
**** Arena wrapper macros
|
||||||
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE, \
|
***************************************************************************///
|
||||||
false))
|
|
||||||
#define wapp_mem_arena_allocator_init_commit(base_capacity) \
|
|
||||||
(wapp_mem_arena_allocator_init_custom( \
|
|
||||||
base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, false))
|
|
||||||
#define wapp_mem_arena_allocator_init_zero(base_capacity) \
|
|
||||||
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE, \
|
|
||||||
true))
|
|
||||||
#define wapp_mem_arena_allocator_init_commit_and_zero(base_capacity) \
|
|
||||||
(wapp_mem_arena_allocator_init_custom( \
|
|
||||||
base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, true))
|
|
||||||
#pragma endregion Arena Allocator wrapper macros
|
|
||||||
|
|
||||||
#pragma region Allocator API declarations
|
|
||||||
/**
|
|
||||||
* Wraps an Arena in an Allocator object. It attempts to initialise the Arena
|
|
||||||
* and, if successful, defines the operations supported by it to be used by the
|
|
||||||
* Allocator.
|
|
||||||
*
|
|
||||||
* An Arena allocator only supports normal allocation and aligned allocation.
|
|
||||||
* Reallocation, aligned reallocation and freeing aren't implemented.
|
|
||||||
*
|
|
||||||
* The `wapp_mem_arena_allocator_init_custom` provides the most control over how
|
|
||||||
* the Arena is initialised. Wrapper macros are provided for easier use.
|
|
||||||
*/
|
|
||||||
Allocator wapp_mem_arena_allocator_init_custom(u64 base_capacity,
|
|
||||||
MemAllocFlags flags,
|
|
||||||
bool zero_buffer);
|
|
||||||
void wapp_mem_arena_allocator_clear(Allocator *allocator);
|
|
||||||
void wapp_mem_arena_allocator_destroy(Allocator *allocator);
|
|
||||||
#pragma endregion Arena Allocator API declarations
|
|
||||||
|
|
||||||
#pragma region wrapper macros
|
|
||||||
#define wapp_mem_arena_init(arena_dptr, base_capacity) \
|
#define wapp_mem_arena_init(arena_dptr, base_capacity) \
|
||||||
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, \
|
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, \
|
||||||
WAPP_MEM_ALLOC_RESERVE, false))
|
WAPP_MEM_ALLOC_RESERVE, false))
|
||||||
@ -61,9 +29,13 @@ void wapp_mem_arena_allocator_destroy(Allocator *allocator);
|
|||||||
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, \
|
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, \
|
||||||
WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, \
|
WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, \
|
||||||
true))
|
true))
|
||||||
#pragma endregion Arena wrapper macros
|
|
||||||
|
|
||||||
#pragma region Arena API declarations
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
**** Arena API declarations
|
||||||
|
***************************************************************************///
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Arena initialisation function. `wapp_mem_arena_init_custom` provides the most
|
* Arena initialisation function. `wapp_mem_arena_init_custom` provides the most
|
||||||
* control over how the Arena is initialised. Wrapper macros are provided for
|
* control over how the Arena is initialised. Wrapper macros are provided for
|
||||||
@ -75,7 +47,8 @@ void *wapp_mem_arena_alloc(Arena *arena, u64 size);
|
|||||||
void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment);
|
void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment);
|
||||||
void wapp_mem_arena_clear(Arena *arena);
|
void wapp_mem_arena_clear(Arena *arena);
|
||||||
void wapp_mem_arena_destroy(Arena **arena);
|
void wapp_mem_arena_destroy(Arena **arena);
|
||||||
#pragma endregion Arena API declarations
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
60
src/core/mem/arena/mem_arena_allocator.c
Normal file
60
src/core/mem/arena/mem_arena_allocator.c
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#include "mem_arena_allocator.h"
|
||||||
|
#include "mem_arena.h"
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
**** Arena Allocator wrappers declarations
|
||||||
|
***************************************************************************///
|
||||||
|
|
||||||
|
internal inline void *mem_arena_alloc(u64 size, void *alloc_obj);
|
||||||
|
internal inline void *mem_arena_alloc_aligned(u64 size, u64 alignment,
|
||||||
|
void *alloc_obj);
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
**** Arena Allocator API definitions
|
||||||
|
***************************************************************************///
|
||||||
|
|
||||||
|
Allocator wapp_mem_arena_allocator_init_custom(u64 base_capacity,
|
||||||
|
MemAllocFlags flags,
|
||||||
|
bool zero_buffer) {
|
||||||
|
Allocator allocator = {0};
|
||||||
|
bool initialised = wapp_mem_arena_init_custom(
|
||||||
|
(Arena **)(&allocator.obj), base_capacity, flags, zero_buffer);
|
||||||
|
if (!initialised) {
|
||||||
|
return allocator;
|
||||||
|
}
|
||||||
|
|
||||||
|
allocator.alloc = mem_arena_alloc;
|
||||||
|
allocator.alloc_aligned = mem_arena_alloc_aligned;
|
||||||
|
|
||||||
|
return allocator;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wapp_mem_arena_allocator_clear(Allocator *allocator) {
|
||||||
|
wapp_mem_arena_clear((Arena *)(allocator->obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
void wapp_mem_arena_allocator_destroy(Allocator *allocator) {
|
||||||
|
wapp_mem_arena_destroy((Arena **)(&(allocator->obj)));
|
||||||
|
*allocator = (Allocator){0};
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
**** Arena Allocator wrappers definitions
|
||||||
|
***************************************************************************///
|
||||||
|
|
||||||
|
internal inline void *mem_arena_alloc(u64 size, void *alloc_obj) {
|
||||||
|
Arena *arena = (Arena *)alloc_obj;
|
||||||
|
return wapp_mem_arena_alloc(arena, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal inline void *mem_arena_alloc_aligned(u64 size, u64 alignment,
|
||||||
|
void *alloc_obj) {
|
||||||
|
Arena *arena = (Arena *)alloc_obj;
|
||||||
|
return wapp_mem_arena_alloc_aligned(arena, size, alignment);
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
59
src/core/mem/arena/mem_arena_allocator.h
Normal file
59
src/core/mem/arena/mem_arena_allocator.h
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
#ifndef MEM_ARENA_ALLOCATOR_H
|
||||||
|
#define MEM_ARENA_ALLOCATOR_H
|
||||||
|
|
||||||
|
#include "aliases.h"
|
||||||
|
#include "mem_allocator.h"
|
||||||
|
#include "mem_utils.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
**** Arena Allocator wrapper macros
|
||||||
|
***************************************************************************///
|
||||||
|
|
||||||
|
#define wapp_mem_arena_allocator_init(base_capacity) \
|
||||||
|
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE, \
|
||||||
|
false))
|
||||||
|
#define wapp_mem_arena_allocator_init_commit(base_capacity) \
|
||||||
|
(wapp_mem_arena_allocator_init_custom( \
|
||||||
|
base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, false))
|
||||||
|
#define wapp_mem_arena_allocator_init_zero(base_capacity) \
|
||||||
|
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE, \
|
||||||
|
true))
|
||||||
|
#define wapp_mem_arena_allocator_init_commit_and_zero(base_capacity) \
|
||||||
|
(wapp_mem_arena_allocator_init_custom( \
|
||||||
|
base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, true))
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
**** Arena Allocator API declarations
|
||||||
|
***************************************************************************///
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wraps an Arena in an Allocator object. It attempts to initialise the Arena
|
||||||
|
* and, if successful, defines the operations supported by it to be used by the
|
||||||
|
* Allocator.
|
||||||
|
*
|
||||||
|
* An Arena allocator only supports normal allocation and aligned allocation.
|
||||||
|
* Reallocation, aligned reallocation and freeing aren't implemented.
|
||||||
|
*
|
||||||
|
* The `wapp_mem_arena_allocator_init_custom` provides the most control over how
|
||||||
|
* the Arena is initialised. Wrapper macros are provided for easier use.
|
||||||
|
*/
|
||||||
|
Allocator wapp_mem_arena_allocator_init_custom(u64 base_capacity,
|
||||||
|
MemAllocFlags flags,
|
||||||
|
bool zero_buffer);
|
||||||
|
void wapp_mem_arena_allocator_clear(Allocator *allocator);
|
||||||
|
void wapp_mem_arena_allocator_destroy(Allocator *allocator);
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // !MEM_ARENA_ALLOCATOR_H
|
@ -1,4 +1,4 @@
|
|||||||
#include "mem_libc.h"
|
#include "mem_libc_allocator.h"
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
#include "mem_allocator.h"
|
#include "mem_allocator.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
@ -1,7 +1,7 @@
|
|||||||
#include "test_allocator.h"
|
#include "test_allocator.h"
|
||||||
#include "libc/mem_libc.h"
|
#include "libc/mem_libc_allocator.h"
|
||||||
#include "mem_allocator.h"
|
#include "mem_allocator.h"
|
||||||
#include "mem_arena.h"
|
#include "mem_arena_allocator.h"
|
||||||
#include "tester.h"
|
#include "tester.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
Loading…
Reference in New Issue
Block a user