2019-01-01 13:09:57 +01:00
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
// GNU GPL OS/K //
|
|
|
|
// //
|
|
|
|
// Authors: spectral` //
|
|
|
|
// NeoX //
|
|
|
|
// //
|
|
|
|
// Desc: Kaleid assert() support //
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
|
|
|
|
#ifndef _KALASSRT_H
|
|
|
|
#define _KALASSRT_H
|
|
|
|
|
|
|
|
//------------------------------------------//
|
2019-01-01 17:11:30 +01:00
|
|
|
// Macros //
|
2019-01-01 13:09:57 +01:00
|
|
|
//------------------------------------------//
|
|
|
|
|
|
|
|
#ifndef noreturn
|
2019-01-01 17:11:30 +01:00
|
|
|
#define noreturn __attribute__((__noreturn__))
|
2019-01-01 13:09:57 +01:00
|
|
|
#endif
|
|
|
|
|
2019-01-14 14:31:49 +01:00
|
|
|
#ifndef unlikely
|
2019-01-01 13:09:57 +01:00
|
|
|
#define unlikely(x) (__builtin_expect((x), 0))
|
|
|
|
#endif
|
|
|
|
|
2019-01-14 14:31:49 +01:00
|
|
|
#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
|
|
|
|
|
2019-01-01 13:09:57 +01:00
|
|
|
//------------------------------------------//
|
|
|
|
// When debugging //
|
|
|
|
//------------------------------------------//
|
|
|
|
|
|
|
|
#if !defined(_NO_DEBUG) && !defined(NDEBUG) && !defined(assert)
|
|
|
|
|
2019-01-14 14:31:49 +01:00
|
|
|
#ifdef _OSK_SOURCE
|
|
|
|
|
2019-01-01 13:09:57 +01:00
|
|
|
//
|
2019-01-14 14:31:49 +01:00
|
|
|
// Failed assert handler
|
2019-01-01 13:09:57 +01:00
|
|
|
//
|
|
|
|
noreturn void _assert_handler(const char *, const char *, int, const char *);
|
|
|
|
|
2019-01-14 14:31:49 +01:00
|
|
|
//
|
|
|
|
// Checks whether (x) holds, if not call _assert_handler
|
|
|
|
//
|
2019-01-01 13:09:57 +01:00
|
|
|
#define assert(x) \
|
|
|
|
do { \
|
2019-01-14 14:31:49 +01:00
|
|
|
if unlikely (!(x)) \
|
2019-01-01 13:09:57 +01:00
|
|
|
_assert_handler(#x, __FILE__, __LINE__, __func__); \
|
|
|
|
} while (0);
|
|
|
|
|
2019-01-14 14:31:49 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
//
|
|
|
|
// When not building for OS/K, use the system's assert
|
|
|
|
//
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-01-01 13:09:57 +01:00
|
|
|
//------------------------------------------//
|
|
|
|
// When not debugging //
|
|
|
|
//------------------------------------------//
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2019-01-01 17:37:58 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#define NDEBUG 1
|
2019-01-01 13:09:57 +01:00
|
|
|
#endif
|
|
|
|
|
2019-01-01 17:37:58 +01:00
|
|
|
#ifndef _NO_DEBUG
|
|
|
|
#define _NO_DEBUG 1
|
2019-01-01 13:09:57 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef assert
|
2019-01-14 14:31:49 +01:00
|
|
|
#define assert(x) ((void)0)
|
2019-01-01 13:09:57 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-01-01 17:11:30 +01:00
|
|
|
//------------------------------------------//
|
|
|
|
// Aliases for assert() //
|
|
|
|
//------------------------------------------//
|
|
|
|
|
|
|
|
#ifndef Assert
|
|
|
|
#define Assert assert
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef KalAssert
|
|
|
|
#define KalAssert assert
|
|
|
|
#endif
|
|
|
|
|
2019-01-01 13:09:57 +01:00
|
|
|
//------------------------------------------//
|
2019-01-14 14:31:49 +01:00
|
|
|
// End of header //
|
2019-01-01 13:09:57 +01:00
|
|
|
//------------------------------------------//
|
|
|
|
|
|
|
|
#endif
|