Add support for initialising arena with backing buffer
This commit is contained in:
@@ -15,3 +15,19 @@ TestFuncResult test_arena_allocator(void) {
|
||||
|
||||
return wapp_tester_result(result);
|
||||
}
|
||||
|
||||
TestFuncResult test_arena_allocator_with_buffer(void) {
|
||||
u8 buffer[KiB(4)] = {0};
|
||||
|
||||
Allocator allocator = wapp_mem_arena_allocator_init_with_buffer(buffer, KiB(4));
|
||||
b8 result = allocator.obj != NULL && allocator.alloc != NULL &&
|
||||
allocator.alloc_aligned != NULL &&
|
||||
allocator.realloc != NULL && allocator.realloc_aligned != NULL &&
|
||||
allocator.free == NULL;
|
||||
void *ptr = wapp_mem_allocator_alloc(&allocator, 20);
|
||||
result = result && (ptr != NULL);
|
||||
|
||||
wapp_mem_arena_allocator_destroy(&allocator);
|
||||
|
||||
return wapp_tester_result(result);
|
||||
}
|
||||
|
||||
@@ -15,3 +15,19 @@ TestFuncResult test_arena_allocator(void) {
|
||||
|
||||
return wapp_tester_result(result);
|
||||
}
|
||||
|
||||
TestFuncResult test_arena_allocator_with_buffer(void) {
|
||||
u8 buffer[KiB(4)] = {0};
|
||||
|
||||
Allocator allocator = wapp_mem_arena_allocator_init_with_buffer(buffer, KiB(4));
|
||||
b8 result = allocator.obj != NULL && allocator.alloc != NULL &&
|
||||
allocator.alloc_aligned != NULL &&
|
||||
allocator.realloc != NULL && allocator.realloc_aligned != NULL &&
|
||||
allocator.free == NULL;
|
||||
void *ptr = wapp_mem_allocator_alloc(&allocator, 20);
|
||||
result = result && (ptr != NULL);
|
||||
|
||||
wapp_mem_arena_allocator_destroy(&allocator);
|
||||
|
||||
return wapp_tester_result(result);
|
||||
}
|
||||
|
||||
@@ -4,5 +4,6 @@
|
||||
#include "wapp.h"
|
||||
|
||||
TestFuncResult test_arena_allocator(void);
|
||||
TestFuncResult test_arena_allocator_with_buffer(void);
|
||||
|
||||
#endif // !TEST_ALLOCATOR_H
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -13,14 +13,18 @@
|
||||
int main(void) {
|
||||
wapp_tester_run_tests(
|
||||
test_arena_allocator,
|
||||
test_arena_init,
|
||||
test_arena_allocator_with_buffer,
|
||||
test_arena_init_buffer,
|
||||
test_arena_init_allocated,
|
||||
test_arena_init_succeeds_when_reserving_very_large_size,
|
||||
test_arena_alloc_with_buffer,
|
||||
test_arena_alloc_succeeds_when_within_capacity,
|
||||
test_arena_alloc_fails_when_over_capacity,
|
||||
test_arena_realloc_bigger_size,
|
||||
test_arena_realloc_smaller_size,
|
||||
test_arena_clear,
|
||||
test_arena_destroy,
|
||||
test_arena_destroy_buffer,
|
||||
test_arena_destroy_allocated,
|
||||
test_str8_array,
|
||||
test_i32_array,
|
||||
test_i32_array_with_capacity,
|
||||
|
||||
@@ -13,14 +13,18 @@
|
||||
int main(void) {
|
||||
wapp_tester_run_tests(
|
||||
test_arena_allocator,
|
||||
test_arena_init,
|
||||
test_arena_allocator_with_buffer,
|
||||
test_arena_init_buffer,
|
||||
test_arena_init_allocated,
|
||||
test_arena_init_succeeds_when_reserving_very_large_size,
|
||||
test_arena_alloc_with_buffer,
|
||||
test_arena_alloc_succeeds_when_within_capacity,
|
||||
test_arena_alloc_fails_when_over_capacity,
|
||||
test_arena_realloc_bigger_size,
|
||||
test_arena_realloc_smaller_size,
|
||||
test_arena_clear,
|
||||
test_arena_destroy,
|
||||
test_arena_destroy_buffer,
|
||||
test_arena_destroy_allocated,
|
||||
test_str8_array,
|
||||
test_i32_array,
|
||||
test_i32_array_with_capacity,
|
||||
|
||||
Reference in New Issue
Block a user