From: Lukas Krickl Date: Mon, 23 Feb 2026 14:54:00 +0000 (+0100) Subject: arena: Added arena alloc and tests X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=0f56954a5ba1c5938384fe9024e45ad6c122faaa;p=lrts%2F.git arena: Added arena alloc and tests --- diff --git a/src/test.c b/src/test.c index bc6906d..9e9f97b 100644 --- a/src/test.c +++ b/src/test.c @@ -10,6 +10,7 @@ #ifdef LRTS_IMPL #include "tests/t_args.c" +#include "tests/t_arena.c" #endif int main(int argc, char **argv) { @@ -19,6 +20,7 @@ int main(int argc, char **argv) { lrts_getopt(argc, argv, lrts_cfg()); T_TESTCASE("test-argv", t_test_argv); + T_TESTCASE("test-arena", t_test_arena); T_TESTEND("lrts test"); diff --git a/src/tests/t_arena.c b/src/tests/t_arena.c new file mode 100644 index 0000000..a6701b7 --- /dev/null +++ b/src/tests/t_arena.c @@ -0,0 +1,13 @@ +#include "../u_arena.h" +#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"); + + + return 0; +} diff --git a/src/tests/t_defs.h b/src/tests/t_defs.h index 7290a49..401200e 100644 --- a/src/tests/t_defs.h +++ b/src/tests/t_defs.h @@ -6,6 +6,14 @@ #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(expr, message) {\ + if (!(expr)) { \ + u_log(U_LOG_CRIT, "\n%s:%d: %s\n", __FILE__, __LINE__, message); \ + return 1; \ + } \ +} + /** * Runs a test case * Test functions should return 0 when they succeed otherwise 1 diff --git a/src/u_arena.c b/src/u_arena.c index 46c89a5..5702f2a 100644 --- a/src/u_arena.c +++ b/src/u_arena.c @@ -1,2 +1,18 @@ #include "u_arena.h" +#include "u_mem.h" +#include "u_log.h" +struct u_arena u_arena_init(u32 o_size, u32 o_max) { + struct u_arena a; + memset(&a, 0, sizeof(a)); + a.o_size = o_size; + a.o_max = o_max; + + a.data = u_malloc(o_size * o_max); + if (a.data == LRTS_NULL) { + u_log(U_LOG_ERR, "Unable to malloc arena of size %d\n", o_size * o_max); + a.err = U_ERRNO_MALLOC_FAIL; + } + + return a; +} diff --git a/src/u_errno.h b/src/u_errno.h index 462a633..a130185 100644 --- a/src/u_errno.h +++ b/src/u_errno.h @@ -20,7 +20,8 @@ extern char u_errno_message[U_ERRNO_MESSAGE_MAX]; */ enum u_errno_code { U_ERRNO_OK = 0, - U_ERRNO_FAIL + U_ERRNO_FAIL, + U_ERRNO_MALLOC_FAIL }; #endif diff --git a/src/u_log.c b/src/u_log.c index 93a94ea..54d24c8 100644 --- a/src/u_log.c +++ b/src/u_log.c @@ -15,7 +15,7 @@ int u_log(enum u_log_levels level, const char *fmt, ...) { return 0; } va_start(args, fmt); - res = u_fprintf(u_stderr, fmt, args); + res = u_vfprintf(u_stderr, fmt, args); va_end(args); return res; }