kernel_defines.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Freie Universit├Ąt Berlin
3  * 2017 HAW-Hamburg
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  */
9 
21 #ifndef KERNEL_DEFINES_H
22 #define KERNEL_DEFINES_H
23 
24 #include <stddef.h>
25 #include <stdint.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /* uncrustify gets mightily confused by these macros... */
32 /* begin{code-style-ignore} */
33 
47 #if __STDC_VERSION__ >= 201112L
48 # define container_of(PTR, TYPE, MEMBER) \
49  (_Generic((PTR), \
50  const __typeof__ (((TYPE *) 0)->MEMBER) *: \
51  ((TYPE *) ((uintptr_t) (PTR) - offsetof(TYPE, MEMBER))), \
52  __typeof__ (((TYPE *) 0)->MEMBER) *: \
53  ((TYPE *) ((uintptr_t) (PTR) - offsetof(TYPE, MEMBER))) \
54  ))
55 #elif defined __GNUC__
56 # define container_of(PTR, TYPE, MEMBER) \
57  (__extension__ ({ \
58  __extension__ const __typeof__ (((TYPE *) 0)->MEMBER) *__m____ = (PTR); \
59  ((TYPE *) ((uintptr_t) __m____ - offsetof(TYPE, MEMBER))); \
60  }))
61 #else
62 # define container_of(PTR, TYPE, MEMBER) \
63  ((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER)))
64 #endif
65 
74 #define index_of(ARRAY, ELEMENT) (((uintptr_t)(ELEMENT) - (uintptr_t)(ARRAY)) / sizeof((ARRAY)[0]))
75 
81 #ifdef __GNUC__
82 #define NORETURN __attribute__((noreturn))
83 #else
84 #define NORETURN
85 #endif
86 
94 #ifdef __GNUC__
95 #define PURE __attribute__((pure))
96 #else
97 #define PURE
98 #endif
99 
107 #if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ >= 5)
108 #define UNREACHABLE() __builtin_unreachable()
109 #else
110 #define UNREACHABLE() do { /* nothing */ } while (1)
111 #endif
112 
119 #ifndef ARRAY_SIZE
120 #define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
121 #endif
122 
155 #define IS_ACTIVE(macro) __is_active(macro)
156 
166 #define IS_USED(module) IS_ACTIVE(module)
167 
193 #define RIOT_VERSION_NUM(major, minor, patch, extra) \
194  (((0ULL + major) << 48) + ((0ULL + minor) << 32) + \
195  ((0ULL + patch) << 16) + (extra))
196 
208 #define WITHOUT_PEDANTIC(...) \
209  _Pragma("GCC diagnostic push") \
210  _Pragma("GCC diagnostic ignored \"-Wpedantic\"") \
211  __VA_ARGS__ \
212  _Pragma("GCC diagnostic pop")
213 
224 #define DECLARE_CONSTANT(identifier, const_expr) \
225  WITHOUT_PEDANTIC(enum { identifier = const_expr };)
226 
227 #if DOXYGEN
240 #define IS_CT_CONSTANT(expr) <IMPLEMENTATION>
241 #elif defined(__GNUC__)
242 /* both clang and gcc (which both define __GNUC__) support this */
243 #define IS_CT_CONSTANT(expr) __builtin_constant_p(expr)
244 #else
245 #define IS_CT_CONSTANT(expr) 0
246 #endif
247 
251 /* Here a prefix "__PREFIX_WHEN_" is added to the macro. So if it was a 1 we
252  * have "__PREFIX_WHEN_1", and if it was not defined we have "__PREFIX_WHEN_".
253  */
254 #define __is_active(val) ___is_active(__PREFIX_WHEN_##val)
255 
256 /* With this placeholder we turn the original value into two arguments when the
257  * original value was defined as 1 (note the comma).
258  */
259 #define __PREFIX_WHEN_1 0,
260 
261 /* Here we add two extra arguments, that way the next macro can accept varargs.
262  *
263  * If the original macro was defined as 1, this will have three arguments
264  * (__take_second_arg(0, 1, 0, 0)), otherwise it will have two
265  * (__take_second_arg(__PREFIX_WHEN_ 1, 0, 0)). The third zero is there just to
266  * be compliant with C99, which states that when a function-like macro ends
267  * with ellipsis (...) it should be called with at least one argument for the
268  * variable list.
269  */
270 #define ___is_active(arg1_or_junk) __take_second_arg(arg1_or_junk 1, 0, 0)
271 
272 /* Finally, we just always take the second argument, which will be either 1
273  * (when three arguments are passed, i.e. macro was defined as 1) or 0 (when
274  * only two arguments are passed).
275  */
276 #define __take_second_arg(__ignored, val, ...) val
281 /* end{code-style-ignore} */
282 
283 #ifdef __cplusplus
284 }
285 #endif
286 
287 #endif /* KERNEL_DEFINES_H */