arena: wip tests and allocation
authorLukas Krickl <lukas@krickl.dev>
Mon, 23 Feb 2026 17:00:32 +0000 (18:00 +0100)
committerLukas Krickl <lukas@krickl.dev>
Mon, 23 Feb 2026 17:00:32 +0000 (18:00 +0100)
STYLE.md
src/tests/t_arena.c
src/tests/t_defs.h
src/u_arena.c
src/u_assert.h

index 22ea69b90a1dca46dfdb2c181437fe8b692a9d15..7f1a27a670293ae31ada2d3932eac1e4a422c096 100644 (file)
--- 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
 
index a6701b751543a452a0482a770f35822ed5fe08c0..ba243c3be27a99e35ca888f57851300cbadaa393 100644 (file)
@@ -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;
 }
index 401200eee80d69f45bba8e88dac04fbfd2bcc145..2b13d564dfa748de0b8fdf896e51321ffa973991 100644 (file)
@@ -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; \
        } \
 }
index 5702f2a95094813e403b1cbbf5f7c54481738a89..6992532f1f713f7a0c5068ad844a0451d06aad8d 100644 (file)
@@ -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;
+}
index 967209a17107c7dbdba3e2f9bdadb6fa39f8911a..578c0e7feef605c1aa1b986792faf8abadb81a10 100644 (file)
@@ -1,15 +1,18 @@
 #ifndef U_ASSERT_H__
 #define U_ASSERT_H__
 
+
 #ifdef DEBUG
 
-#define lrts_assert(e, message) assert(e)
+#include <assert.h>
+
+#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)