Reformat and increase minimum arena capacity to 4 pages
This commit is contained in:
parent
1de3608b1f
commit
e9451f10f8
@ -1,5 +1,6 @@
|
||||
#include "mem_arena.h"
|
||||
#include "aliases.h"
|
||||
#include "misc_utils.h"
|
||||
#include "mem_utils.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
@ -11,7 +12,7 @@
|
||||
#define DEFAULT_ALIGNMENT (2 * sizeof(void *))
|
||||
#endif /* ifndef DEFAULT_ALIGNMENT */
|
||||
|
||||
#define ARENA_MINIMUM_CAPACITY 1024
|
||||
#define ARENA_MINIMUM_CAPACITY KB(16) // Allocate minimum of 4 pages
|
||||
|
||||
struct arena {
|
||||
u8 *buf;
|
||||
@ -20,19 +21,11 @@ struct arena {
|
||||
bool committed;
|
||||
|
||||
#ifdef WAPP_PLATFORM_WINDOWS
|
||||
#include "misc_utils.h"
|
||||
wapp_misc_utils_padding_size(sizeof(u8 *) * 2 + sizeof(u64) + sizeof(bool));
|
||||
#endif // ifdef WAPP_PLATFORM_WINDOWS
|
||||
};
|
||||
|
||||
/***************************************************************************/ //
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////// Arena API definitions
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/***************************************************************************/ //
|
||||
|
||||
bool wapp_mem_arena_init_custom(Arena **arena, u64 base_capacity,
|
||||
MemAllocFlags flags, bool zero_buffer) {
|
||||
bool wapp_mem_arena_init_custom(Arena **arena, u64 base_capacity, MemAllocFlags flags, bool zero_buffer) {
|
||||
if (!arena || *arena || base_capacity == 0) {
|
||||
return false;
|
||||
}
|
||||
@ -43,9 +36,7 @@ bool wapp_mem_arena_init_custom(Arena **arena, u64 base_capacity,
|
||||
return false;
|
||||
}
|
||||
|
||||
u64 arena_capacity = base_capacity >= ARENA_MINIMUM_CAPACITY
|
||||
? base_capacity
|
||||
: ARENA_MINIMUM_CAPACITY;
|
||||
u64 arena_capacity = base_capacity >= ARENA_MINIMUM_CAPACITY ? base_capacity : ARENA_MINIMUM_CAPACITY;
|
||||
|
||||
arena_ptr->buf = (u8 *)wapp_mem_util_alloc(NULL, arena_capacity, WAPP_MEM_ACCESS_READ_WRITE, flags,
|
||||
zero_buffer ? WAPP_MEM_INIT_INITIALISED : WAPP_MEM_INIT_UNINITIALISED);
|
||||
@ -82,10 +73,9 @@ void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment) {
|
||||
|
||||
#ifdef WAPP_PLATFORM_WINDOWS
|
||||
if (!(arena->committed)) {
|
||||
output = (u8 *)wapp_mem_util_alloc(
|
||||
alloc_start, (uptr)(arena->offset) - (uptr)(alloc_start),
|
||||
WAPP_MEM_ACCESS_READ_WRITE, WAPP_MEM_ALLOC_COMMIT,
|
||||
WAPP_MEM_INIT_INITIALISED);
|
||||
output = (u8 *)wapp_mem_util_alloc(alloc_start, (uptr)(arena->offset) - (uptr)(alloc_start),
|
||||
WAPP_MEM_ACCESS_READ_WRITE, WAPP_MEM_ALLOC_COMMIT,
|
||||
WAPP_MEM_INIT_INITIALISED);
|
||||
}
|
||||
#else
|
||||
memset(output, 0, size);
|
||||
|
@ -11,40 +11,21 @@ BEGIN_C_LINKAGE
|
||||
|
||||
typedef struct arena Arena;
|
||||
|
||||
/***************************************************************************/ //
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////// Arena wrapper macros
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/***************************************************************************/ //
|
||||
|
||||
#define wapp_mem_arena_init(arena_dptr, base_capacity) \
|
||||
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, \
|
||||
WAPP_MEM_ALLOC_RESERVE, false))
|
||||
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE, false))
|
||||
#define wapp_mem_arena_init_commit(arena_dptr, base_capacity) \
|
||||
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, \
|
||||
WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, \
|
||||
false))
|
||||
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, false))
|
||||
#define wapp_mem_arena_init_zero(arena_dptr, base_capacity) \
|
||||
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, \
|
||||
WAPP_MEM_ALLOC_RESERVE, true))
|
||||
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE, true))
|
||||
#define wapp_mem_arena_init_commit_and_zero(arena_dptr, base_capacity) \
|
||||
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, \
|
||||
WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, \
|
||||
true))
|
||||
|
||||
/***************************************************************************/ //
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////// Arena API declarations
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/***************************************************************************/ //
|
||||
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, true))
|
||||
|
||||
/**
|
||||
* Arena initialisation function. `wapp_mem_arena_init_custom` provides the most
|
||||
* control over how the Arena is initialised. Wrapper macros are provided for
|
||||
* easier use.
|
||||
*/
|
||||
bool wapp_mem_arena_init_custom(Arena **arena, u64 base_capacity,
|
||||
MemAllocFlags flags, bool zero_buffer);
|
||||
bool wapp_mem_arena_init_custom(Arena **arena, u64 base_capacity, MemAllocFlags flags, bool zero_buffer);
|
||||
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_realloc(Arena *arena, void *ptr, u64 old_size, u64 new_size);
|
||||
|
@ -2,38 +2,22 @@
|
||||
#define MEM_ARENA_ALLOCATOR_H
|
||||
|
||||
#include "aliases.h"
|
||||
#include "mem_allocator.h"
|
||||
#include "mem_utils.h"
|
||||
#include "mem_allocator.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
BEGIN_C_LINKAGE
|
||||
#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))
|
||||
(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))
|
||||
(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))
|
||||
(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
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/***************************************************************************/ //
|
||||
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, true))
|
||||
|
||||
/**
|
||||
* Wraps an Arena in an Allocator object. It attempts to initialise the Arena
|
||||
|
@ -2,6 +2,7 @@
|
||||
#define MEM_LIBC_H
|
||||
|
||||
#include "mem_allocator.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
BEGIN_C_LINKAGE
|
||||
#endif // __cplusplus
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define ARENA_CAPACITY 1024
|
||||
#define ARENA_CAPACITY KB(16)
|
||||
|
||||
internal Arena *arena = NULL;
|
||||
internal i32 count = 20;
|
||||
|
Loading…
Reference in New Issue
Block a user