Add support for initialising arena with backing buffer

This commit is contained in:
2026-01-03 18:36:30 +00:00
parent 83b879a180
commit 326265722e
12 changed files with 195 additions and 61 deletions

View File

@@ -3,13 +3,21 @@
#include <stdlib.h>
#define ARENA_CAPACITY KiB(16)
#define ARENA_BUF_SIZE KiB(4)
wapp_intern Arena *arena = NULL;
wapp_intern i32 count = 20;
wapp_intern i32 *array = NULL;
wapp_intern Arena *arena = NULL;
wapp_intern i32 count = 20;
wapp_intern i32 *array = NULL;
wapp_intern Arena *buf_arena = NULL;
wapp_intern u8 buf[ARENA_BUF_SIZE] = {0};
TestFuncResult test_arena_init(void) {
b8 result = wapp_mem_arena_init(&arena, ARENA_CAPACITY);
TestFuncResult test_arena_init_buffer(void) {
b8 result = wapp_mem_arena_init_buffer(&buf_arena, buf, ARENA_BUF_SIZE);
return wapp_tester_result(result);
}
TestFuncResult test_arena_init_allocated(void) {
b8 result = wapp_mem_arena_init_allocated(&arena, ARENA_CAPACITY);
return wapp_tester_result(result);
}
@@ -17,7 +25,7 @@ TestFuncResult test_arena_init(void) {
TestFuncResult test_arena_init_succeeds_when_reserving_very_large_size(void) {
Arena *large_arena = NULL;
u64 capacity = GiB(512);
b8 result = wapp_mem_arena_init(&large_arena, capacity);
b8 result = wapp_mem_arena_init_allocated(&large_arena, capacity);
if (result) {
wapp_mem_arena_destroy(&large_arena);
}
@@ -25,6 +33,17 @@ TestFuncResult test_arena_init_succeeds_when_reserving_very_large_size(void) {
return wapp_tester_result(result);
}
TestFuncResult test_arena_alloc_with_buffer(void) {
i32 *array = (i32 *)wapp_mem_arena_alloc(arena, count * sizeof(i32));
b8 result = array != NULL;
for (i32 i = 0; i < count; ++i) {
array[i] = i * 10;
}
return wapp_tester_result(result);
}
TestFuncResult test_arena_alloc_succeeds_when_within_capacity(void) {
array = wapp_mem_arena_alloc(arena, count * sizeof(i32));
b8 result = array != NULL;
@@ -103,7 +122,14 @@ TestFuncResult test_arena_clear(void) {
return wapp_tester_result(result);
}
TestFuncResult test_arena_destroy(void) {
TestFuncResult test_arena_destroy_buffer(void) {
wapp_mem_arena_destroy(&buf_arena);
b8 result = buf_arena == NULL;
return wapp_tester_result(result);
}
TestFuncResult test_arena_destroy_allocated(void) {
wapp_mem_arena_destroy(&arena);
b8 result = arena == NULL;

View File

@@ -3,13 +3,21 @@
#include <stdlib.h>
#define ARENA_CAPACITY KiB(16)
#define ARENA_BUF_SIZE KiB(4)
wapp_intern Arena *arena = nullptr;
wapp_intern i32 count = 20;
wapp_intern i32 *array = nullptr;
wapp_intern Arena *arena = NULL;
wapp_intern i32 count = 20;
wapp_intern i32 *array = NULL;
wapp_intern Arena *buf_arena = NULL;
wapp_intern u8 buf[ARENA_BUF_SIZE] = {0};
TestFuncResult test_arena_init(void) {
b8 result = wapp_mem_arena_init(&arena, ARENA_CAPACITY);
TestFuncResult test_arena_init_buffer(void) {
b8 result = wapp_mem_arena_init_buffer(&buf_arena, buf, ARENA_BUF_SIZE);
return wapp_tester_result(result);
}
TestFuncResult test_arena_init_allocated(void) {
b8 result = wapp_mem_arena_init_allocated(&arena, ARENA_CAPACITY);
return wapp_tester_result(result);
}
@@ -17,7 +25,7 @@ TestFuncResult test_arena_init(void) {
TestFuncResult test_arena_init_succeeds_when_reserving_very_large_size(void) {
Arena *large_arena = nullptr;
u64 capacity = GiB(512);
b8 result = wapp_mem_arena_init(&large_arena, capacity);
b8 result = wapp_mem_arena_init_allocated(&large_arena, capacity);
if (result) {
wapp_mem_arena_destroy(&large_arena);
}
@@ -25,6 +33,17 @@ TestFuncResult test_arena_init_succeeds_when_reserving_very_large_size(void) {
return wapp_tester_result(result);
}
TestFuncResult test_arena_alloc_with_buffer(void) {
i32 *array = (i32 *)wapp_mem_arena_alloc(arena, count * sizeof(i32));
b8 result = array != NULL;
for (i32 i = 0; i < count; ++i) {
array[i] = i * 10;
}
return wapp_tester_result(result);
}
TestFuncResult test_arena_alloc_succeeds_when_within_capacity(void) {
array = (i32 *)wapp_mem_arena_alloc(arena, count * sizeof(i32));
b8 result = array != nullptr;
@@ -103,7 +122,14 @@ TestFuncResult test_arena_clear(void) {
return wapp_tester_result(result);
}
TestFuncResult test_arena_destroy(void) {
TestFuncResult test_arena_destroy_buffer(void) {
wapp_mem_arena_destroy(&buf_arena);
b8 result = buf_arena == NULL;
return wapp_tester_result(result);
}
TestFuncResult test_arena_destroy_allocated(void) {
wapp_mem_arena_destroy(&arena);
b8 result = arena == nullptr;

View File

@@ -3,13 +3,16 @@
#include "wapp.h"
TestFuncResult test_arena_init(void);
TestFuncResult test_arena_init_buffer(void);
TestFuncResult test_arena_init_allocated(void);
TestFuncResult test_arena_init_succeeds_when_reserving_very_large_size(void);
TestFuncResult test_arena_alloc_with_buffer(void);
TestFuncResult test_arena_alloc_succeeds_when_within_capacity(void);
TestFuncResult test_arena_alloc_fails_when_over_capacity(void);
TestFuncResult test_arena_realloc_bigger_size(void);
TestFuncResult test_arena_realloc_smaller_size(void);
TestFuncResult test_arena_clear(void);
TestFuncResult test_arena_destroy(void);
TestFuncResult test_arena_destroy_buffer(void);
TestFuncResult test_arena_destroy_allocated(void);
#endif // !TEST_ARENA_H