From 15cbe531afe9e7ca7ee886b04b146bbfe5851323 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Mon, 23 Feb 2026 08:22:53 +0100 Subject: [PATCH] engine: Added error handling setup and arena allocator stub --- STYLE.md | 5 +++++ makefile | 2 +- src/lrts.c | 2 ++ src/lrts.h | 1 + src/u_arena.c | 2 ++ src/u_arena.h | 43 +++++++++++++++++++++++++++++++++++++++++++ src/u_defs.h | 2 ++ src/u_errno.h | 26 ++++++++++++++++++++++++++ src/u_log.c | 1 + src/u_math.c | 15 +++++++++++++++ src/u_rand.c | 1 + 11 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 src/u_arena.c create mode 100644 src/u_arena.h create mode 100644 src/u_errno.h diff --git a/STYLE.md b/STYLE.md index cf02484..22ea69b 100644 --- a/STYLE.md +++ b/STYLE.md @@ -51,3 +51,8 @@ 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 + +## Vendoring + +All 3rd party libraries should be forked and included as a submodule. +Linking should be static. diff --git a/makefile b/makefile index b2aff04..bf1d2c9 100644 --- a/makefile +++ b/makefile @@ -2,7 +2,7 @@ NAME=lrts TEST_NAME=test$(NAME) DBGCFLAGS=-g DBGLDFLAGS= -CFLAGS=-Wall -pedantic $(DBGCFLAGS) -std=c89 +CFLAGS=-Wall -Wextra -Werror -pedantic $(DBGCFLAGS) -std=c89 LIBS=-lSDL3 LDFLAGS=$(DBGLDFLAGS) $(LIBS) diff --git a/src/lrts.c b/src/lrts.c index ff85d87..ab3ae2f 100644 --- a/src/lrts.c +++ b/src/lrts.c @@ -9,6 +9,8 @@ struct lrts_config* lrts_cfg() { } void lrts_help(int argc, char **argv) { + LRTS_UNUSED(argc); + u_fprintf(u_stderr, "%s\n", argv[0]); u_fprintf(u_stderr, "Usage: %s [-%s] [-c=command] [demo file]\n\n", argv[0], LRTS_OPTS); diff --git a/src/lrts.h b/src/lrts.h index 278276a..0f83a5f 100644 --- a/src/lrts.h +++ b/src/lrts.h @@ -49,6 +49,7 @@ #include "u_math.c" #include "t_tile.c" #include "u_log.c" +#include "u_arena.c" #endif diff --git a/src/u_arena.c b/src/u_arena.c new file mode 100644 index 0000000..46c89a5 --- /dev/null +++ b/src/u_arena.c @@ -0,0 +1,2 @@ +#include "u_arena.h" + diff --git a/src/u_arena.h b/src/u_arena.h new file mode 100644 index 0000000..460b5c2 --- /dev/null +++ b/src/u_arena.h @@ -0,0 +1,43 @@ +#ifndef U_ARENA_H__ +#define U_ARENA_H__ + +#include "u_defs.h" +#include "u_errno.h" + +/** + * This module contains a simple arena allocator + * This is the most common type of allocator that should + * be used in the program. + */ + +struct u_arena { + enum u_errno_code err; + u32 o_size; + u32 o_max; + + /* The current object index */ + u32 o_index; + + void *data; +}; + +/** + * Creates a new arena + * suitable for o_max objects of o_size size + * returns a new arena. sets err to 0 if no error occured + */ +struct u_arena u_arena_init(u32 o_size, u32 o_max); + +/** + * Allocates n objects from an arena + * sets err if allocation is not possible + */ +void *u_arena_alloc(struct u_arena *a, u32 n); + +/* Clears the arena resetting the current o_index */ +void u_arena_clear(struct u_arena *a); + +/* Releases an arena buffer */ +void u_arena_free(struct u_arena *a); + +#endif diff --git a/src/u_defs.h b/src/u_defs.h index bf2b82a..25b62df 100644 --- a/src/u_defs.h +++ b/src/u_defs.h @@ -52,4 +52,6 @@ int lrts_main(int argc, char **argv); #define U_RENDER_W 640 #define U_RENDER_H 640 +#define LRTS_UNUSED(x) (void)(x) + #endif diff --git a/src/u_errno.h b/src/u_errno.h new file mode 100644 index 0000000..462a633 --- /dev/null +++ b/src/u_errno.h @@ -0,0 +1,26 @@ +#ifndef U_ERRNO_H__ +#define U_ERRNO_H__ + +#define U_ERRNO_MESSAGE_MAX 512 + +/** + * This message may be written to whenever a function fails + */ +extern char u_errno_message[U_ERRNO_MESSAGE_MAX]; + +/** + * This error code usually gets returned by + * functions that may fail. + * usually the pattenr is as follows + * return_type my_fun(enum u_errno_code *err); + * or + * struct my_struct { + * enum u_errno_code err; + * } + */ +enum u_errno_code { + U_ERRNO_OK = 0, + U_ERRNO_FAIL +}; + +#endif diff --git a/src/u_log.c b/src/u_log.c index 0459f5c..93a94ea 100644 --- a/src/u_log.c +++ b/src/u_log.c @@ -2,6 +2,7 @@ #include lrts_bool u_log_should_log(enum u_log_levels level) { + LRTS_UNUSED(level); /* TODO: implement log levels */ return LRTS_TRUE; } diff --git a/src/u_math.c b/src/u_math.c index a1af48a..ff65375 100644 --- a/src/u_math.c +++ b/src/u_math.c @@ -2,30 +2,45 @@ struct u_fp u_fp_add(struct u_fp a, struct u_fp b) { struct u_fp c = U_FP(0, 0); + + LRTS_UNUSED(a); + LRTS_UNUSED(b); lrts_todo(""); return c; } struct u_fp u_fp_sub(struct u_fp a, struct u_fp b) { struct u_fp c = U_FP(0, 0); + + LRTS_UNUSED(a); + LRTS_UNUSED(b); lrts_todo(""); return c; } struct u_fp u_fp_mul(struct u_fp a, struct u_fp b) { struct u_fp c = U_FP(0, 0); + + LRTS_UNUSED(a); + LRTS_UNUSED(b); lrts_todo(""); return c; } struct u_fp u_fp_div(struct u_fp a, struct u_fp b) { struct u_fp c = U_FP(0, 0); + + LRTS_UNUSED(a); + LRTS_UNUSED(b); lrts_todo(""); return c; } struct u_fp u_fp_mod(struct u_fp a, struct u_fp b) { struct u_fp c = U_FP(0, 0); + + LRTS_UNUSED(a); + LRTS_UNUSED(b); lrts_todo(""); return c; } diff --git a/src/u_rand.c b/src/u_rand.c index 874c09b..703dda9 100644 --- a/src/u_rand.c +++ b/src/u_rand.c @@ -2,6 +2,7 @@ #include "u_assert.h" void u_srand(u32 s) { + LRTS_UNUSED(s); lrts_todo("Not yet implemented"); } -- 2.30.2