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/include/base/assert.h

113 lines
3.9 KiB
C
Raw Normal View History

2019-01-21 15:00:04 +01:00
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
2019-02-16 23:36:33 +01:00
// Desc: assert() support //
2019-01-21 15:00:04 +01:00
// //
2019-02-16 23:36:33 +01:00
// //
// 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/>. //
2019-01-21 15:00:04 +01:00
//----------------------------------------------------------------------------//
#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 //
//------------------------------------------//
2019-02-16 23:36:33 +01:00
#ifdef __cplusplus
extern "C" {
#endif
2019-01-21 15:00:04 +01:00
// Failed assert handler
noreturn void __assert_handler(const char *, const char *, int, const char *);
2019-02-16 23:36:33 +01:00
#ifdef __cplusplus
}
#endif
2019-01-21 15:00:04 +01:00
// Unconditional assert
#define KalAlwaysAssert(x) \
do { \
if unlikely (!(x)) \
__assert_handler(#x, __FILE__, __LINE__, __func__); \
} while (0)
//------------------------------------------//
// When debugging //
//------------------------------------------//
2019-03-19 13:37:23 +01:00
#if /*!defined(_NO_DEBUG) && !defined(NDEBUG) &&*/ !defined(KalAssert)
2019-01-21 15:00:04 +01:00
//
// 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