os-k/include/base/assert.h

113 lines
3.9 KiB
C

//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Desc: assert() support //
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// This file is part of OS/K. //
// //
// OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// any later version. //
// //
// OS/K is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#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 //
//------------------------------------------//
#ifdef __cplusplus
extern "C" {
#endif
// Failed assert handler
noreturn void __assert_handler(const char *, const char *, int, const char *);
#ifdef __cplusplus
}
#endif
// 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
//------------------------------------------//
// When not debugging //
//------------------------------------------//
#else
#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