From: Lukas Krickl Date: Mon, 23 Feb 2026 17:00:32 +0000 (+0100) Subject: arena: wip tests and allocation X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=fd127b8f642daa6f1d57ef892d330b7c0d200d95;p=lrts%2F.git arena: wip tests and allocation --- diff --git a/STYLE.md b/STYLE.md index 22ea69b..7f1a27a 100644 --- a/STYLE.md +++ b/STYLE.md @@ -51,6 +51,7 @@ Some C features might be hard to wrap. These are allowed even in core code. - stdarg.h: for va_list va_start and va_end +- assert.h: for debug build asserts ## Vendoring diff --git a/src/tests/t_arena.c b/src/tests/t_arena.c index a6701b7..ba243c3 100644 --- a/src/tests/t_arena.c +++ b/src/tests/t_arena.c @@ -2,12 +2,23 @@ #include "t_defs.h" int t_test_arena(void) { - struct u_arena a = u_arena_init(1, 2); - T_ASSERT(a.err == 0, "Areana failed to allocate"); - T_ASSERT(a.data, "Areana failed to allocate"); - T_ASSERT(a.o_max == 2, "Invalid max"); - T_ASSERT(a.o_size == 1, "Invalid size"); + struct u_arena a = u_arena_init(2, 4); + void *p1 = LRTS_NULL; + + /* create */ + T_ASSERT(a.err == 0, ("Areana failed to allocate\n")); + T_ASSERT(a.data, ("Areana failed to allocate\n")); + T_ASSERT(a.o_max == 4, ("Invalid max\n")); + T_ASSERT(a.o_size == 2, ("Invalid size\n")); + + /* alloc */ + p1 = u_arena_alloc(&a, 1); + T_ASSERT((char *)p1 - (char*)a.data == 0, ("Unable to alloc index is at %d\n", a.o_index)); + T_ASSERT(a.o_index, ("o_index bad\n")); + /* reset */ + + /* free */ return 0; } diff --git a/src/tests/t_defs.h b/src/tests/t_defs.h index 401200e..2b13d56 100644 --- a/src/tests/t_defs.h +++ b/src/tests/t_defs.h @@ -6,10 +6,15 @@ #define T_TESTEND(name) { u_fprintf(u_stderr, "=== %s ===\n", name); } -/* asserta a test if expr is false prints message and returns 1 */ +#define T_ASSERT_OUT_FILE() { u_fprintf(u_stderr, "\n%s:%d ", __FILE__, __LINE__); } + +/* asserta a test if expr is false prints message and returns 1 + * called like this T_ASSERT(expr, (message, arg1, arg2...)) + **/ #define T_ASSERT(expr, message) {\ if (!(expr)) { \ - u_log(U_LOG_CRIT, "\n%s:%d: %s\n", __FILE__, __LINE__, message); \ + T_ASSERT_OUT_FILE(); \ + printf message; \ return 1; \ } \ } diff --git a/src/u_arena.c b/src/u_arena.c index 5702f2a..6992532 100644 --- a/src/u_arena.c +++ b/src/u_arena.c @@ -16,3 +16,24 @@ struct u_arena u_arena_init(u32 o_size, u32 o_max) { return a; } + +void *u_arena_alloc(struct u_arena *a, u32 n) { + u32 new_o_index = a->o_index + n; + void *p = NULL; + + lrts_assert(a); + lrts_assert(a.err == U_ERRNO_OK); + lrts_assert(a.data); + + if (new_o_index >= a->o_max) { + u_log(U_LOG_ERR, "Unable to allocate %d objects from arena\n", n); + a->err = U_ERRNO_MALLOC_FAIL; + return LRTS_NULL; + } + + /* get the new start address */ + p = (char *)a->data + a->o_index + a->o_size * n; + a->o_index = new_o_index; + + return p; +} diff --git a/src/u_assert.h b/src/u_assert.h index 967209a..578c0e7 100644 --- a/src/u_assert.h +++ b/src/u_assert.h @@ -1,15 +1,18 @@ #ifndef U_ASSERT_H__ #define U_ASSERT_H__ + #ifdef DEBUG -#define lrts_assert(e, message) assert(e) +#include + +#define lrts_assert(e) assert(e) #define lrts_todo(message) assert(0) #else -#define lrts_assert(e, message) +#define lrts_assert(e) #define lrts_todo(message)