INITIAL COMMIT
This commit is contained in:
@@ -0,0 +1,192 @@
|
||||
// vim:fileencoding=utf-8:foldmethod=marker
|
||||
|
||||
#include "mem_arena.h"
|
||||
#include "../../mem/mem_os.h"
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../../common/assert/assert.h"
|
||||
#include "../../../common/misc/misc_utils.h"
|
||||
#include "../../../base/mem/utils/mem_utils.h"
|
||||
#include <string.h>
|
||||
|
||||
#ifndef DEFAULT_ALIGNMENT
|
||||
// Why 2 * sizeof(void *) instead of sizeof(void *)
|
||||
// https://handmade.network/forums/t/6860-alignment_arena_allocator
|
||||
#define DEFAULT_ALIGNMENT (2 * sizeof(void *))
|
||||
#endif /* ifndef DEFAULT_ALIGNMENT */
|
||||
|
||||
#define ARENA_MINIMUM_CAPACITY KiB(16) // Allocate minimum of 4 pages
|
||||
|
||||
typedef enum {
|
||||
ARENA_STORAGE_TYPE_ALLOCATED,
|
||||
ARENA_STORAGE_TYPE_BUFFER,
|
||||
} ArenaStorageType;
|
||||
|
||||
struct Arena {
|
||||
u8 *buf;
|
||||
u8 *offset;
|
||||
u8 *prev_offset;
|
||||
u64 capacity;
|
||||
ArenaStorageType type;
|
||||
b8 committed;
|
||||
|
||||
wapp_misc_utils_reserve_padding(sizeof(u8 *) * 3 + sizeof(u64) + sizeof(ArenaStorageType) + sizeof(b8));
|
||||
};
|
||||
|
||||
b8 wapp_mem_arena_init_buffer(Arena **arena, u8 *buffer, u64 buffer_size) {
|
||||
if (!arena || *arena || buffer_size < sizeof(Arena)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*arena = (Arena *)buffer;
|
||||
Arena *arena_ptr = *arena;
|
||||
|
||||
arena_ptr->buf = (u8 *)(arena_ptr + 1);
|
||||
arena_ptr->offset = arena_ptr->buf;
|
||||
arena_ptr->prev_offset = NULL;
|
||||
arena_ptr->capacity = buffer_size - sizeof(Arena);
|
||||
arena_ptr->type = ARENA_STORAGE_TYPE_BUFFER;
|
||||
arena_ptr->committed = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
b8 wapp_mem_arena_init_allocated_custom(Arena **arena, u64 base_capacity, MemAllocFlags flags, b8 zero_buffer) {
|
||||
if (!arena || *arena || base_capacity == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
u64 size = sizeof(Arena) + (base_capacity >= ARENA_MINIMUM_CAPACITY ? base_capacity : ARENA_MINIMUM_CAPACITY);
|
||||
u64 alloc_size = wapp_misc_utils_u64_round_up_pow2(size);
|
||||
u8 *allocated = (u8 *)wapp_os_mem_alloc(NULL, alloc_size, WAPP_MEM_ACCESS_READ_WRITE, flags,
|
||||
zero_buffer ? WAPP_MEM_INIT_INITIALISED : WAPP_MEM_INIT_UNINITIALISED);
|
||||
if (!allocated) {
|
||||
return false;
|
||||
}
|
||||
|
||||
b8 committed = (flags & WAPP_MEM_ALLOC_COMMIT) == WAPP_MEM_ALLOC_COMMIT;
|
||||
|
||||
#ifdef WAPP_PLATFORM_WINDOWS
|
||||
if (!committed) {
|
||||
wapp_os_mem_alloc(allocated, sizeof(Arena), WAPP_MEM_ACCESS_READ_WRITE, WAPP_MEM_ALLOC_COMMIT,
|
||||
WAPP_MEM_INIT_INITIALISED);
|
||||
}
|
||||
#endif // ifdef WAPP_PLATFORM_WINDOWS
|
||||
|
||||
if (!wapp_mem_arena_init_buffer(arena, allocated, alloc_size)) {
|
||||
wapp_mem_arena_destroy(arena);
|
||||
return false;
|
||||
}
|
||||
|
||||
Arena *arena_ptr = *arena;
|
||||
arena_ptr->type = ARENA_STORAGE_TYPE_ALLOCATED;
|
||||
arena_ptr->committed = committed;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void *wapp_mem_arena_alloc(Arena *arena, u64 size) {
|
||||
return wapp_mem_arena_alloc_aligned(arena, size, DEFAULT_ALIGNMENT);
|
||||
}
|
||||
|
||||
void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment) {
|
||||
wapp_debug_assert(arena != NULL, "`arena` should not be NULL");
|
||||
|
||||
u8 *alloc_start = arena->offset;
|
||||
|
||||
u8 *output = wapp_mem_util_align_forward((void *)alloc_start, alignment);
|
||||
if (output + size >= arena->buf + arena->capacity) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
arena->offset = output + size;
|
||||
|
||||
#ifdef WAPP_PLATFORM_WINDOWS
|
||||
if (arena->type == ARENA_STORAGE_TYPE_ALLOCATED && !(arena->committed)) {
|
||||
wapp_os_mem_alloc(alloc_start, (uptr)(arena->offset) - (uptr)(alloc_start),
|
||||
WAPP_MEM_ACCESS_READ_WRITE, WAPP_MEM_ALLOC_COMMIT,
|
||||
WAPP_MEM_INIT_UNINITIALISED);
|
||||
}
|
||||
#endif // ifdef WAPP_PLATFORM_WINDOWS
|
||||
|
||||
memset(output, 0, size);
|
||||
|
||||
return (void *)output;
|
||||
}
|
||||
|
||||
void *wapp_mem_arena_realloc(Arena *arena, void *ptr, u64 old_size, u64 new_size) {
|
||||
wapp_debug_assert(arena != NULL, "`arena` should not be NULL");
|
||||
|
||||
if ((u8*)ptr < arena->buf || (u8*)ptr > arena->offset ||
|
||||
arena->offset + new_size >= arena->buf + arena->capacity) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *new_ptr = wapp_mem_arena_alloc(arena, new_size);
|
||||
if (!new_ptr) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
u64 copy_size = new_size <= old_size ? new_size : old_size;
|
||||
memcpy(new_ptr, ptr, copy_size);
|
||||
|
||||
return new_ptr;
|
||||
}
|
||||
|
||||
void *wapp_mem_arena_realloc_aligned(Arena *arena, void *ptr, u64 old_size, u64 new_size, u64 alignment) {
|
||||
wapp_debug_assert(arena != NULL, "`arena` should not be NULL");
|
||||
|
||||
if ((u8*)ptr < arena->buf || (u8*)ptr > arena->offset ||
|
||||
arena->offset + new_size >= arena->buf + arena->capacity) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *new_ptr = wapp_mem_arena_alloc_aligned(arena, new_size, alignment);
|
||||
if (!new_ptr) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
u64 copy_size = new_size <= old_size ? new_size : old_size;
|
||||
memcpy(new_ptr, ptr, copy_size);
|
||||
|
||||
return new_ptr;
|
||||
}
|
||||
|
||||
void wapp_mem_arena_temp_begin(Arena *arena) {
|
||||
wapp_debug_assert(arena != NULL, "`arena` should not be NULL");
|
||||
|
||||
if (arena->prev_offset != NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
arena->prev_offset = arena->offset;
|
||||
}
|
||||
|
||||
void wapp_mem_arena_temp_end(Arena *arena) {
|
||||
wapp_debug_assert(arena != NULL, "`arena` should not be NULL");
|
||||
|
||||
if (arena->prev_offset == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
arena->offset = arena->prev_offset;
|
||||
arena->prev_offset = NULL;
|
||||
}
|
||||
|
||||
void wapp_mem_arena_clear(Arena *arena) {
|
||||
wapp_debug_assert(arena != NULL, "`arena` should not be NULL");
|
||||
|
||||
memset(arena->buf, 0, arena->offset - arena->buf);
|
||||
arena->offset = arena->buf;
|
||||
}
|
||||
|
||||
void wapp_mem_arena_destroy(Arena **arena) {
|
||||
wapp_debug_assert(arena != NULL && (*arena) != NULL, "`arena` double pointer is not valid");
|
||||
|
||||
Arena *arena_ptr = *arena;
|
||||
|
||||
if (arena_ptr->type == ARENA_STORAGE_TYPE_ALLOCATED) {
|
||||
wapp_os_mem_free(*arena, sizeof(Arena) + arena_ptr->capacity);
|
||||
}
|
||||
|
||||
*arena = NULL;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// vim:fileencoding=utf-8:foldmethod=marker
|
||||
|
||||
#ifndef MEM_ARENA_H
|
||||
#define MEM_ARENA_H
|
||||
|
||||
#include "../../mem/mem_os.h"
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../../common/platform/platform.h"
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
BEGIN_C_LINKAGE
|
||||
#endif // !WAPP_PLATFORM_CPP
|
||||
|
||||
typedef struct Arena Arena;
|
||||
|
||||
#define wapp_mem_arena_init_allocated(arena_dptr, base_capacity) \
|
||||
(wapp_mem_arena_init_allocated_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE, false))
|
||||
#define wapp_mem_arena_init_allocated_commit(arena_dptr, base_capacity) \
|
||||
(wapp_mem_arena_init_allocated_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, false))
|
||||
#define wapp_mem_arena_init_allocated_zero(arena_dptr, base_capacity) \
|
||||
(wapp_mem_arena_init_allocated_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE, true))
|
||||
#define wapp_mem_arena_init_allocated_commit_and_zero(arena_dptr, base_capacity) \
|
||||
(wapp_mem_arena_init_allocated_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, true))
|
||||
|
||||
/**
|
||||
* Arena initialisation function. `wapp_mem_arena_init_allocated_custom` provides the most
|
||||
* control over how the Arena is initialised. Wrapper macros are provided for
|
||||
* easier use.
|
||||
*/
|
||||
b8 wapp_mem_arena_init_allocated_custom(Arena **arena, u64 base_capacity, MemAllocFlags flags, b8 zero_buffer);
|
||||
b8 wapp_mem_arena_init_buffer(Arena **arena, u8 *buffer, u64 buffer_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_realloc(Arena *arena, void *ptr, u64 old_size, u64 new_size);
|
||||
void *wapp_mem_arena_realloc_aligned(Arena *arena, void *ptr, u64 old_size, u64 new_size, u64 alignment);
|
||||
void wapp_mem_arena_temp_begin(Arena *arena);
|
||||
void wapp_mem_arena_temp_end(Arena *arena);
|
||||
void wapp_mem_arena_clear(Arena *arena);
|
||||
void wapp_mem_arena_destroy(Arena **arena);
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
END_C_LINKAGE
|
||||
#endif // !WAPP_PLATFORM_CPP
|
||||
|
||||
#endif // !MEM_ARENA_H
|
||||
@@ -0,0 +1,87 @@
|
||||
// vim:fileencoding=utf-8:foldmethod=marker
|
||||
|
||||
#include "mem_arena_allocator.h"
|
||||
#include "mem_arena.h"
|
||||
#include "../../mem/mem_os.h"
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../../common/assert/assert.h"
|
||||
|
||||
wapp_intern void initialise_arena_allocator(Allocator *allocator);
|
||||
wapp_intern void *mem_arena_alloc(u64 size, void *alloc_obj);
|
||||
wapp_intern void *mem_arena_alloc_aligned(u64 size, u64 alignment, void *alloc_obj);
|
||||
wapp_intern void *mem_arena_realloc(void *ptr, u64 old_size, u64 new_size, void *alloc_obj);
|
||||
wapp_intern void *mem_arena_realloc_aligned(void *ptr, u64 old_size, u64 new_size, u64 alignment,
|
||||
void *alloc_obj);
|
||||
|
||||
Allocator wapp_mem_arena_allocator_init_with_buffer(u8 *buffer, u64 buffer_size) {
|
||||
Allocator allocator = {0};
|
||||
b8 initialised = wapp_mem_arena_init_buffer((Arena **)(&allocator.obj), buffer, buffer_size);
|
||||
if (!initialised) {
|
||||
return allocator;
|
||||
}
|
||||
|
||||
initialise_arena_allocator(&allocator);
|
||||
|
||||
return allocator;
|
||||
}
|
||||
|
||||
Allocator wapp_mem_arena_allocator_init_custom(u64 base_capacity, MemAllocFlags flags, b8 zero_buffer) {
|
||||
Allocator allocator = {0};
|
||||
b8 initialised = wapp_mem_arena_init_allocated_custom((Arena **)(&allocator.obj), base_capacity, flags, zero_buffer);
|
||||
if (!initialised) {
|
||||
return allocator;
|
||||
}
|
||||
|
||||
initialise_arena_allocator(&allocator);
|
||||
|
||||
return allocator;
|
||||
}
|
||||
|
||||
void wapp_mem_arena_allocator_temp_begin(const Allocator *allocator) {
|
||||
wapp_debug_assert(allocator != NULL, "`allocator` should not be NULL");
|
||||
wapp_mem_arena_temp_begin((Arena *)(allocator->obj));
|
||||
}
|
||||
|
||||
void wapp_mem_arena_allocator_temp_end(const Allocator *allocator) {
|
||||
wapp_debug_assert(allocator != NULL, "`allocator` should not be NULL");
|
||||
wapp_mem_arena_temp_end((Arena *)(allocator->obj));
|
||||
}
|
||||
|
||||
void wapp_mem_arena_allocator_clear(Allocator *allocator) {
|
||||
wapp_debug_assert(allocator != NULL, "`allocator` should not be NULL");
|
||||
wapp_mem_arena_clear((Arena *)(allocator->obj));
|
||||
}
|
||||
|
||||
void wapp_mem_arena_allocator_destroy(Allocator *allocator) {
|
||||
wapp_debug_assert(allocator != NULL, "`allocator` should not be NULL");
|
||||
wapp_mem_arena_destroy((Arena **)(&(allocator->obj)));
|
||||
*allocator = (Allocator){0};
|
||||
}
|
||||
|
||||
wapp_intern void initialise_arena_allocator(Allocator *allocator) {
|
||||
allocator->alloc = mem_arena_alloc;
|
||||
allocator->alloc_aligned = mem_arena_alloc_aligned;
|
||||
allocator->realloc = mem_arena_realloc;
|
||||
allocator->realloc_aligned = mem_arena_realloc_aligned;
|
||||
}
|
||||
|
||||
wapp_intern void *mem_arena_alloc(u64 size, void *alloc_obj) {
|
||||
Arena *arena = (Arena *)alloc_obj;
|
||||
return wapp_mem_arena_alloc(arena, size);
|
||||
}
|
||||
|
||||
wapp_intern 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);
|
||||
}
|
||||
|
||||
wapp_intern void *mem_arena_realloc(void *ptr, u64 old_size, u64 new_size, void *alloc_obj) {
|
||||
Arena *arena = (Arena *)alloc_obj;
|
||||
return wapp_mem_arena_realloc(arena, ptr, old_size, new_size);
|
||||
}
|
||||
|
||||
wapp_intern void *mem_arena_realloc_aligned(void *ptr, u64 old_size, u64 new_size, u64 alignment,
|
||||
void *alloc_obj) {
|
||||
Arena *arena = (Arena *)alloc_obj;
|
||||
return wapp_mem_arena_realloc_aligned(arena, ptr, old_size, new_size, alignment);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// vim:fileencoding=utf-8:foldmethod=marker
|
||||
|
||||
#ifndef MEM_ARENA_ALLOCATOR_H
|
||||
#define MEM_ARENA_ALLOCATOR_H
|
||||
|
||||
#include "../../mem/mem_os.h"
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../../common/platform/platform.h"
|
||||
#include "../../../base/mem/allocator/mem_allocator.h"
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
BEGIN_C_LINKAGE
|
||||
#endif // !WAPP_PLATFORM_CPP
|
||||
|
||||
#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))
|
||||
|
||||
/**
|
||||
* 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, b8 zero_buffer);
|
||||
Allocator wapp_mem_arena_allocator_init_with_buffer(u8 *buffer, u64 buffer_size);
|
||||
void wapp_mem_arena_allocator_temp_begin(const Allocator *allocator);
|
||||
void wapp_mem_arena_allocator_temp_end(const Allocator *allocator);
|
||||
void wapp_mem_arena_allocator_clear(Allocator *allocator);
|
||||
void wapp_mem_arena_allocator_destroy(Allocator *allocator);
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
END_C_LINKAGE
|
||||
#endif // !WAPP_PLATFORM_CPP
|
||||
|
||||
#endif // !MEM_ARENA_ALLOCATOR_H
|
||||
Reference in New Issue
Block a user