1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00
os-k/kaleid/include/base/assert.h
2019-02-06 14:07:38 +01:00

108 lines
2.9 KiB
C

//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Kaleid assert() support //
//----------------------------------------------------------------------------//
#ifndef _KALBASE_ASSERT_H
#define _KALBASE_ASSERT_H
//------------------------------------------//
#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 KalAlwaysAssert
#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)
//------------------------------------------//
#endif