118 lines
3.1 KiB
C
118 lines
3.1 KiB
C
|
//----------------------------------------------------------------------------//
|
||
|
// GNU GPL OS/K //
|
||
|
// //
|
||
|
// Authors: spectral` //
|
||
|
// NeoX //
|
||
|
// //
|
||
|
// Desc: Kaleid assert() support //
|
||
|
//----------------------------------------------------------------------------//
|
||
|
|
||
|
#ifndef _KALBASE_ASSERT_H
|
||
|
#define _KALBASE_ASSERT_H
|
||
|
|
||
|
//------------------------------------------//
|
||
|
// Macros //
|
||
|
//------------------------------------------//
|
||
|
|
||
|
#ifndef noreturn
|
||
|
#define noreturn __attribute__((__noreturn__))
|
||
|
#endif
|
||
|
|
||
|
#ifndef unlikely
|
||
|
#define unlikely(x) (__builtin_expect((x), 0))
|
||
|
#endif
|
||
|
|
||
|
#ifndef static_assert
|
||
|
#define static_assert _Static_assert
|
||
|
#endif
|
||
|
|
||
|
//------------------------------------------//
|
||
|
// API compatibility checks //
|
||
|
//------------------------------------------//
|
||
|
|
||
|
#define _SA_MSG "Incompatible type sizes"
|
||
|
static_assert(sizeof(char) == 1, _SA_MSG);
|
||
|
static_assert(sizeof(short) == 2, _SA_MSG);
|
||
|
static_assert(sizeof(int) == 4, _SA_MSG);
|
||
|
static_assert(sizeof(long) == 8, _SA_MSG);
|
||
|
static_assert(sizeof(void *) == 8, _SA_MSG);
|
||
|
#undef _SA_MSG
|
||
|
|
||
|
//------------------------------------------//
|
||
|
// Assert core //
|
||
|
//------------------------------------------//
|
||
|
|
||
|
//
|
||
|
// Failed assert handler
|
||
|
//
|
||
|
noreturn void __assert_handler(const char *, const char *, int, const char *);
|
||
|
|
||
|
//
|
||
|
// Unconditional assert
|
||
|
//
|
||
|
#define KalAlwaysAssert(x) \
|
||
|
do { \
|
||
|
if unlikely (!(x)) \
|
||
|
__assert_handler(#x, __FILE__, __LINE__, __func__); \
|
||
|
} while (0)
|
||
|
|
||
|
//------------------------------------------//
|
||
|
// When debugging //
|
||
|
//------------------------------------------//
|
||
|
|
||
|
#if !defined(_NO_DEBUG) && !defined(NDEBUG) && !defined(KalAssert)
|
||
|
|
||
|
//
|
||
|
// Check whether (x) holds, if not call __assert_handler
|
||
|
//
|
||
|
#define KalAssert KalAlwaysAssert
|
||
|
|
||
|
#ifndef _OSK_SOURCE
|
||
|
|
||
|
//
|
||
|
// When not building for OS/K, use the system's assert
|
||
|
//
|
||
|
#include <assert.h>
|
||
|
|
||
|
#undef KalAwaysAssert
|
||
|
#define KalAlwaysAssert assert
|
||
|
|
||
|
#endif
|
||
|
|
||
|
//------------------------------------------//
|
||
|
// When not debugging //
|
||
|
//------------------------------------------//
|
||
|
|
||
|
#else
|
||
|
|
||
|
#ifndef NDEBUG
|
||
|
#define NDEBUG 1
|
||
|
#endif
|
||
|
|
||
|
#ifndef _NO_DEBUG
|
||
|
#define _NO_DEBUG 1
|
||
|
#endif
|
||
|
|
||
|
#ifndef KalAssert
|
||
|
#define KalAssert(x) ((void)0)
|
||
|
#endif
|
||
|
|
||
|
#endif
|
||
|
|
||
|
//------------------------------------------//
|
||
|
// Aliases and extensions //
|
||
|
//------------------------------------------//
|
||
|
|
||
|
#ifndef assert
|
||
|
#define assert KalAssert
|
||
|
#endif
|
||
|
|
||
|
#define KalAssertEx(x,m) KalAssert(x && m)
|
||
|
#define KalAlwaysAssertEx(x,m) KalAlwaysAssert(x && m)
|
||
|
|
||
|
//------------------------------------------//
|
||
|
// End of header //
|
||
|
//------------------------------------------//
|
||
|
|
||
|
#endif
|