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 
71 #ifdef __GNUC__
72 #define NORETURN __attribute__((noreturn))
73 #else
74 #define NORETURN
75 #endif
76 
83 #ifdef __GNUC__
84 #define CONST __attribute__((const))
85 #else
86 #define CONST
87 #endif
88 
96 #ifdef __GNUC__
97 #define PURE __attribute__((pure))
98 #else
99 #define PURE
100 #endif
101 
109 #if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ >= 5)
110 #define UNREACHABLE() __builtin_unreachable()
111 #else
112 #define UNREACHABLE() do { /* nothing */ } while (1)
113 #endif
114 
121 #ifndef ARRAY_SIZE
122 #define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
123 #endif
124 
131 #define ALIGN_OF(T) (offsetof(struct { char c; T t; }, t))
132 
143 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)]))
144 
177 #define IS_ACTIVE(macro) __is_active(macro)
178 
188 #define IS_USED(module) IS_ACTIVE(module)
189 
193 /* Here a prefix "__PREFIX_WHEN_" is added to the macro. So if it was a 1 we
194  * have "__PREFIX_WHEN_1", and if it was not defined we have "__PREFIX_WHEN_".
195  */
196 #define __is_active(val) ___is_active(__PREFIX_WHEN_##val)
197 
198 /* With this placeholder we turn the original value into two arguments when the
199  * original value was defined as 1 (note the comma).
200  */
201 #define __PREFIX_WHEN_1 0,
202 
203 /* Here we add two extra arguments, that way the next macro can accept varargs.
204  *
205  * If the original macro was defined as 1, this will have three arguments
206  * (__take_second_arg(0, 1, 0, 0)), otherwise it will have two
207  * (__take_second_arg(__PREFIX_WHEN_ 1, 0, 0)). The third zero is there just to
208  * be compliant with C99, which states that when a function-like macro ends
209  * with ellipsis (...) it should be called with at least one argument for the
210  * variable list.
211  */
212 #define ___is_active(arg1_or_junk) __take_second_arg(arg1_or_junk 1, 0, 0)
213 
214 /* Finally, we just always take the second argument, which will be either 1
215  * (when three arguments are passed, i.e. macro was defined as 1) or 0 (when
216  * only two arguments are passed).
217  */
218 #define __take_second_arg(__ignored, val, ...) val
219 
223 /* end{code-style-ignore} */
224 
225 #ifdef __cplusplus
226 }
227 #endif
228 
229 #endif /* KERNEL_DEFINES_H */
230