From 023af72f141005860219b3c63c3a63d2c179b2c1 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Fri, 26 Sep 2025 12:38:43 -0400 Subject: lib: Directly use _assert() functions Rather than the defined assert() macro that can change based on include order. As of now the build just checks for known implementation specific function names and assumes their signature. Obviously that is very fragile and should be improved in the future. Also add an AL_STATIC_ASSERT() macro. Signed-off-by: Andrew Opalach --- include/al/lib.h | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'include/al') diff --git a/include/al/lib.h b/include/al/lib.h index 5e65149..f5379e5 100644 --- a/include/al/lib.h +++ b/include/al/lib.h @@ -15,7 +15,10 @@ // https://forum.vcfed.org/index.php?threads/c-item-size-check-at-compile-time.1244920/ #define AL_ASSERT_TYPE_SIZE(type, size) \ - typedef char type##__size_test[(!!(sizeof(type) == size)) * 2 - 1] + typedef char type##__size_assert[(!!(sizeof(type) == size)) * 2 - 1] + +#define AL_STATIC_ASSERT(what, a, cmp, b) \ + typedef char what##__assert[(!!(a cmp b)) * 2 - 1] #define AL_USE_STDLIB //#define AL_PARANOID_REALLOC @@ -43,13 +46,6 @@ #include #include #include -#if !defined AL_DEBUG && defined AL_RELEASE_ASSERTS -#undef NDEBUG -#include -#define NDEBUG -#else -#include -#endif #include #ifdef AL_MEMORY_TRACKING @@ -100,12 +96,23 @@ static inline void *al_realloc(void *ptr, size_t n) { return ptr ? realloc(ptr, #define al_snprintf snprintf #define al_vsnprintf vsnprintf #define al_fflush fflush -#if defined AL_DEBUG || defined AL_RELEASE_ASSERTS -#define al_assert assert + +#if (defined AL_DEBUG || defined AL_RELEASE_ASSERTS) && defined AL_ASSERT_FUNCTION +#include +// Directly define al_assert() as would if NDEBUG was not set. +#if defined AL_ASSERT_INCLUDE_FUNC +#define al_assert(expr) ((expr) ? (void)0 : AL_ASSERT_FUNCTION(#expr, __FILE__, __LINE__, __func__)) +#elif defined AL_ASSERT_EXPR_LAST +#define al_assert(expr) ((expr) ? (void)0 : AL_ASSERT_FUNCTION(__FILE__, __LINE__, #expr)) #else -#define al_assert(expr) do { (void)sizeof(expr); } while (0) // NOLINT(bugprone-sizeof-expression) +#define al_assert(expr) ((expr) ? (void)0 : AL_ASSERT_FUNCTION(#expr, __FILE__, __LINE__)) #endif - +// Loosely assert that behaves as we expect and redefines itself on every include. +#undef assert +#else +#define al_assert(expr) ((void)sizeof(expr)) // NOLINT(bugprone-sizeof-expression) +#endif +// Use __VA_ARGS__ to allow for an empty return. #define al_assert_and_return(...) do { \ al_assert(false); \ return __VA_ARGS__; \ @@ -181,7 +188,6 @@ static inline char *al_memdup0(const char *s, size_t n) buf[n] = '\0'; return buf; } - #endif static inline u32 al_next_power_of_two(u32 n) -- cgit v1.2.3-101-g0448