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 
43 #if __STDC_VERSION__ >= 201112L
44 # define container_of(PTR, TYPE, MEMBER) \
45  (_Generic((PTR), \
46  const __typeof__ (((TYPE *) 0)->MEMBER) *: \
47  ((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER))), \
48  __typeof__ (((TYPE *) 0)->MEMBER) *: \
49  ((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER))) \
50  ))
51 #elif defined __GNUC__
52 # define container_of(PTR, TYPE, MEMBER) \
53  (__extension__ ({ \
54  __extension__ const __typeof__ (((TYPE *) 0)->MEMBER) *__m____ = (PTR); \
55  ((TYPE *) ((char *) __m____ - offsetof(TYPE, MEMBER))); \
56  }))
57 #else
58 # define container_of(PTR, TYPE, MEMBER) \
59  ((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER)))
60 #endif
61 
67 #ifdef __GNUC__
68 #define NORETURN __attribute__((noreturn))
69 #else
70 #define NORETURN
71 #endif
72 
79 #ifdef __GNUC__
80 #define CONST __attribute__((const))
81 #else
82 #define CONST
83 #endif
84 
92 #ifdef __GNUC__
93 #define PURE __attribute__((pure))
94 #else
95 #define PURE
96 #endif
97 
105 #if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ >= 5)
106 #define UNREACHABLE() __builtin_unreachable()
107 #else
108 #define UNREACHABLE() do { /* nothing */ } while (1)
109 #endif
110 
117 #define ALIGN_OF(T) (offsetof(struct { char c; T t; }, t))
118 
129 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)]))
130 
131 #ifdef __cplusplus
132 }
133 #endif
134 
135 #endif /* KERNEL_DEFINES_H */
136