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 "mem_arena.h"
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
|
#include "misc_utils.h"
|
||||||
#include "mem_utils.h"
|
#include "mem_utils.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -11,7 +12,7 @@
|
|||||||
#define DEFAULT_ALIGNMENT (2 * sizeof(void *))
|
#define DEFAULT_ALIGNMENT (2 * sizeof(void *))
|
||||||
#endif /* ifndef DEFAULT_ALIGNMENT */
|
#endif /* ifndef DEFAULT_ALIGNMENT */
|
||||||
|
|
||||||
#define ARENA_MINIMUM_CAPACITY 1024
|
#define ARENA_MINIMUM_CAPACITY KB(16) // Allocate minimum of 4 pages
|
||||||
|
|
||||||
struct arena {
|
struct arena {
|
||||||
u8 *buf;
|
u8 *buf;
|
||||||
@ -20,19 +21,11 @@ struct arena {
|
|||||||
bool committed;
|
bool committed;
|
||||||
|
|
||||||
#ifdef WAPP_PLATFORM_WINDOWS
|
#ifdef WAPP_PLATFORM_WINDOWS
|
||||||
#include "misc_utils.h"
|
|
||||||
wapp_misc_utils_padding_size(sizeof(u8 *) * 2 + sizeof(u64) + sizeof(bool));
|
wapp_misc_utils_padding_size(sizeof(u8 *) * 2 + sizeof(u64) + sizeof(bool));
|
||||||
#endif // ifdef WAPP_PLATFORM_WINDOWS
|
#endif // ifdef WAPP_PLATFORM_WINDOWS
|
||||||
};
|
};
|
||||||
|
|
||||||
/***************************************************************************/ //
|
bool wapp_mem_arena_init_custom(Arena **arena, u64 base_capacity, MemAllocFlags flags, bool zero_buffer) {
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
////// Arena API definitions
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/***************************************************************************/ //
|
|
||||||
|
|
||||||
bool wapp_mem_arena_init_custom(Arena **arena, u64 base_capacity,
|
|
||||||
MemAllocFlags flags, bool zero_buffer) {
|
|
||||||
if (!arena || *arena || base_capacity == 0) {
|
if (!arena || *arena || base_capacity == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -43,9 +36,7 @@ bool wapp_mem_arena_init_custom(Arena **arena, u64 base_capacity,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 arena_capacity = base_capacity >= ARENA_MINIMUM_CAPACITY
|
u64 arena_capacity = base_capacity >= ARENA_MINIMUM_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,
|
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);
|
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
|
#ifdef WAPP_PLATFORM_WINDOWS
|
||||||
if (!(arena->committed)) {
|
if (!(arena->committed)) {
|
||||||
output = (u8 *)wapp_mem_util_alloc(
|
output = (u8 *)wapp_mem_util_alloc(alloc_start, (uptr)(arena->offset) - (uptr)(alloc_start),
|
||||||
alloc_start, (uptr)(arena->offset) - (uptr)(alloc_start),
|
WAPP_MEM_ACCESS_READ_WRITE, WAPP_MEM_ALLOC_COMMIT,
|
||||||
WAPP_MEM_ACCESS_READ_WRITE, WAPP_MEM_ALLOC_COMMIT,
|
WAPP_MEM_INIT_INITIALISED);
|
||||||
WAPP_MEM_INIT_INITIALISED);
|
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
memset(output, 0, size);
|
memset(output, 0, size);
|
||||||
|
@ -11,40 +11,21 @@ BEGIN_C_LINKAGE
|
|||||||
|
|
||||||
typedef struct arena Arena;
|
typedef struct arena Arena;
|
||||||
|
|
||||||
/***************************************************************************/ //
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
////// Arena 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))
|
|
||||||
#define wapp_mem_arena_init_commit(arena_dptr, base_capacity) \
|
#define wapp_mem_arena_init_commit(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 | WAPP_MEM_ALLOC_COMMIT, false))
|
||||||
WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, \
|
|
||||||
false))
|
|
||||||
#define wapp_mem_arena_init_zero(arena_dptr, base_capacity) \
|
#define wapp_mem_arena_init_zero(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, true))
|
||||||
WAPP_MEM_ALLOC_RESERVE, true))
|
|
||||||
#define wapp_mem_arena_init_commit_and_zero(arena_dptr, base_capacity) \
|
#define wapp_mem_arena_init_commit_and_zero(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 | WAPP_MEM_ALLOC_COMMIT, true))
|
||||||
WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, \
|
|
||||||
true))
|
|
||||||
|
|
||||||
/***************************************************************************/ //
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
////// 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
|
||||||
* easier use.
|
* easier use.
|
||||||
*/
|
*/
|
||||||
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);
|
|
||||||
void *wapp_mem_arena_alloc(Arena *arena, u64 size);
|
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_realloc(Arena *arena, void *ptr, u64 old_size, u64 new_size);
|
void *wapp_mem_arena_realloc(Arena *arena, void *ptr, u64 old_size, u64 new_size);
|
||||||
|
@ -2,38 +2,22 @@
|
|||||||
#define MEM_ARENA_ALLOCATOR_H
|
#define MEM_ARENA_ALLOCATOR_H
|
||||||
|
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
#include "mem_allocator.h"
|
|
||||||
#include "mem_utils.h"
|
#include "mem_utils.h"
|
||||||
|
#include "mem_allocator.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
BEGIN_C_LINKAGE
|
BEGIN_C_LINKAGE
|
||||||
#endif // __cplusplus
|
#endif // __cplusplus
|
||||||
|
|
||||||
/***************************************************************************/ //
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
////// Arena Allocator wrapper macros
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/***************************************************************************/ //
|
|
||||||
|
|
||||||
#define wapp_mem_arena_allocator_init(base_capacity) \
|
#define wapp_mem_arena_allocator_init(base_capacity) \
|
||||||
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE, \
|
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE, false))
|
||||||
false))
|
|
||||||
#define wapp_mem_arena_allocator_init_commit(base_capacity) \
|
#define wapp_mem_arena_allocator_init_commit(base_capacity) \
|
||||||
(wapp_mem_arena_allocator_init_custom( \
|
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, false))
|
||||||
base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, false))
|
|
||||||
#define wapp_mem_arena_allocator_init_zero(base_capacity) \
|
#define wapp_mem_arena_allocator_init_zero(base_capacity) \
|
||||||
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE, \
|
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE, true))
|
||||||
true))
|
|
||||||
#define wapp_mem_arena_allocator_init_commit_and_zero(base_capacity) \
|
#define wapp_mem_arena_allocator_init_commit_and_zero(base_capacity) \
|
||||||
(wapp_mem_arena_allocator_init_custom( \
|
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, true))
|
||||||
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
|
* Wraps an Arena in an Allocator object. It attempts to initialise the Arena
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#define MEM_LIBC_H
|
#define MEM_LIBC_H
|
||||||
|
|
||||||
#include "mem_allocator.h"
|
#include "mem_allocator.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
BEGIN_C_LINKAGE
|
BEGIN_C_LINKAGE
|
||||||
#endif // __cplusplus
|
#endif // __cplusplus
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define ARENA_CAPACITY 1024
|
#define ARENA_CAPACITY KB(16)
|
||||||
|
|
||||||
internal Arena *arena = NULL;
|
internal Arena *arena = NULL;
|
||||||
internal i32 count = 20;
|
internal i32 count = 20;
|
||||||
|
Loading…
Reference in New Issue
Block a user