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 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /* uncrustify gets mightily confused by these macros... */
31 /* begin{code-style-ignore} */
32 
46 #if __STDC_VERSION__ >= 201112L
47 # define container_of(PTR, TYPE, MEMBER) \
48  (_Generic((PTR), \
49  const __typeof__ (((TYPE *) 0)->MEMBER) *: \
50  ((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER))), \
51  __typeof__ (((TYPE *) 0)->MEMBER) *: \
52  ((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER))) \
53  ))
54 #elif defined __GNUC__
55 # define container_of(PTR, TYPE, MEMBER) \
56  (__extension__ ({ \
57  __extension__ const __typeof__ (((TYPE *) 0)->MEMBER) *__m____ = (PTR); \
58  ((TYPE *) ((char *) __m____ - offsetof(TYPE, MEMBER))); \
59  }))
60 #else
61 # define container_of(PTR, TYPE, MEMBER) \
62  ((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER)))
63 #endif
64 
70 #ifdef __GNUC__
71 #define NORETURN __attribute__((noreturn))
72 #else
73 #define NORETURN
74 #endif
75 
82 #ifdef __GNUC__
83 #define CONST __attribute__((const))
84 #else
85 #define CONST
86 #endif
87 
95 #ifdef __GNUC__
96 #define PURE __attribute__((pure))
97 #else
98 #define PURE
99 #endif
100 
108 #if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ >= 5)
109 #define UNREACHABLE() __builtin_unreachable()
110 #else
111 #define UNREACHABLE() do { /* nothing */ } while (1)
112 #endif
113 
120 #ifndef ARRAY_SIZE
121 #define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
122 #endif
123 
130 #define ALIGN_OF(T) (offsetof(struct { char c; T t; }, t))
131 
142 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)]))
143 
176 #define IS_ACTIVE(macro) __is_active(macro)
177 
187 #define IS_USED(module) IS_ACTIVE(module)
188 
192 /* Here a prefix "__PREFIX_WHEN_" is added to the macro. So if it was a 1 we
193  * have "__PREFIX_WHEN_1", and if it was not defined we have "__PREFIX_WHEN_".
194  */
195 #define __is_active(val) ___is_active(__PREFIX_WHEN_##val)
196 
197 /* With this placeholder we turn the original value into two arguments when the
198  * original value was defined as 1 (note the comma).
199  */
200 #define __PREFIX_WHEN_1 0,
201 
202 /* Here we add two extra arguments, that way the next macro can accept varargs.
203  *
204  * If the original macro was defined as 1, this will have three arguments
205  * (__take_second_arg(0, 1, 0, 0)), otherwise it will have two
206  * (__take_second_arg(__PREFIX_WHEN_ 1, 0, 0)). The third zero is there just to
207  * be compliant with C99, which states that when a function-like macro ends
208  * with ellipsis (...) it should be called with at least one argument for the
209  * variable list.
210  */
211 #define ___is_active(arg1_or_junk) __take_second_arg(arg1_or_junk 1, 0, 0)
212 
213 /* Finally, we just always take the second argument, which will be either 1
214  * (when three arguments are passed, i.e. macro was defined as 1) or 0 (when
215  * only two arguments are passed).
216  */
217 #define __take_second_arg(__ignored, val, ...) val
218 
222 /* end{code-style-ignore} */
223 
224 #ifdef __cplusplus
225 }
226 #endif
227 
228 #endif /* KERNEL_DEFINES_H */
229