diff options
Diffstat (limited to 'include/al')
| -rw-r--r-- | include/al/lib.h | 32 |
1 files changed, 19 insertions, 13 deletions
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 <wchar.h> #include <stdio.h> #include <stdarg.h> -#if !defined AL_DEBUG && defined AL_RELEASE_ASSERTS -#undef NDEBUG -#include <assert.h> -#define NDEBUG -#else -#include <assert.h> -#endif #include <ctype.h> #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 <assert.h> +// Directly define al_assert() as <assert.h> 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 <assert.h> 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) |