Developer World
Spresense SDK Library v3.2.0-ebc0364
common_assert.h
1/****************************************************************************
2 * modules/include/memutils/common_utils/common_assert.h
3 *
4 * Copyright 2018 Sony Semiconductor Solutions Corporation
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 * 3. Neither the name of Sony Semiconductor Solutions Corporation nor
17 * the names of its contributors may be used to endorse or promote
18 * products derived from this software without specific prior written
19 * permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 ****************************************************************************/
35
36#ifndef COMMON_ASSERT_H_INCLUDED
37#define COMMON_ASSERT_H_INCLUDED
38
39#include "memutils/common_utils/common_macro.h"
40#include "assert.h"
41
42/* static assert.
43 * When exp evaluates to false, a compile error occurs.
44 * Exp can describe only constant expressions
45 * that can be evaluated at compile time.
46 */
47
48#define S_ASSERT(exp) \
49 enum { JOIN_MACRO(AssertionFail_, __LINE__) = 1/(!!(exp)) }
50
51/* debug assert.
52 * NDEBUG Empty sentence when defining a macro,
53 * so expressions with side effects on exp or func can not be described.
54 */
55
56#ifdef NDEBUG
57#define D_ASSERT(exp) ((void)0)
58#define D_ASSERT2(exp, func) ((void)0)
59#else /* NDEBUG */
60#define D_ASSERT(exp) F_ASSERT(exp)
61#define D_ASSERT2(exp, func) F_ASSERT2((exp), (func))
62#endif /* NDEBUG */
63
64/* fatal assert.
65 * Unlike D_ASSERT, it does not become invalid when NDEBUG.
66 * Expressions with side effects can be described in exp or func.
67 */
68
69#ifdef ASSERT_USE_RETURN_ADDR
70
71/* (void)((exp) || (_AssertionFail(#exp, __FILE__, __LINE__,
72 * GET_RETURN_ADDR()), 0))
73 */
74
75#define F_ASSERT(exp) ASSERT(exp)
76
77/* (void)((exp) || ((func), _AssertionFail(#exp, __FILE__,
78 * __LINE__, GET_RETURN_ADDR()), 0))
79 */
80
81#define F_ASSERT2(exp, func) ASSERT(exp)
82#else /* ASSERT_USE_RETURN_ADDR */
83
84/* (void)((exp) || (_AssertionFail(#exp, __FILE__, __LINE__), 0)) */
85
86#define F_ASSERT(exp) ASSERT(exp)
87
88/* (void)((exp) || ((func), _AssertionFail(#exp, __FILE__, __LINE__), 0)) */
89
90#define F_ASSERT2(exp, func) ASSERT(exp)
91#endif /* ASSERT_USE_RETURN_ADDR */
92
93#ifdef __cplusplus
94extern "C" {
95#endif
96
97#if defined(_MSC_VER)
98extern void * _ReturnAddress(void);
99#pragma intrinsic(_ReturnAddress)
100#define GET_RETURN_ADDR() _ReturnAddress()
101#elif defined(__CC_ARM)
102#define GET_RETURN_ADDR() (void*)__return_address()
103#else
104
105/* For most gcc other than for x86, you can only specify 0 as an argument.
106 * (0 for gcc 3.3.6 + allegro-2.2.3)
107 */
108
109#define GET_RETURN_ADDR() __builtin_return_address(0)
110#endif
111
112#ifdef __cplusplus
113} /* end of extern "C" */
114#endif
115
116#endif /* COMMON_ASSERT_H_INCLUDED */