arena: Added arena alloc and tests
authorLukas Krickl <lukas@krickl.dev>
Mon, 23 Feb 2026 14:54:00 +0000 (15:54 +0100)
committerLukas Krickl <lukas@krickl.dev>
Mon, 23 Feb 2026 14:54:00 +0000 (15:54 +0100)
src/test.c
src/tests/t_arena.c [new file with mode: 0644]
src/tests/t_defs.h
src/u_arena.c
src/u_errno.h
src/u_log.c

index bc6906de622e47d9b17deaf2d4b700f3d1c1da01..9e9f97b196ee600a9b6d648158d287b45efd9ba9 100644 (file)
@@ -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 (file)
index 0000000..a6701b7
--- /dev/null
@@ -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;
+}
index 7290a492888d447d86039a81f46e4dafeb6120f5..401200eee80d69f45bba8e88dac04fbfd2bcc145 100644 (file)
@@ -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
index 46c89a5f567a284d6d944629fe99b30b814f7c36..5702f2a95094813e403b1cbbf5f7c54481738a89 100644 (file)
@@ -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;
+}
index 462a6331b6dae3ca081fb0b71461c70c89153e72..a13018532651bf45460b7a22a92955a1ec54790f 100644 (file)
@@ -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
index 93a94ea7fd7fea2149d1d8ae4b45668eeb7fe88a..54d24c8a23594d6c0f6d4a2551fa70cc816b76a7 100644 (file)
@@ -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;
 }