Debian Codegraph @

include/linux/compiler.h

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, write to the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 */
16
17 #ifndef __SHARED_COMPILER_H
18 #define __SHARED_COMPILER_H
19
20 #define __noreturn __attribute__((noreturn))
21 #define __inline __attribute__((always_inline))
22 #ifndef __always_inline
23 #define __always_inline inline __attribute__((always_inline))
24 #endif
25 #define __hot __attribute__((hot))
26 #define __cold __attribute__((cold))
27 #define __force
28 #define __in_section(s) __attribute__((section("." # s)))
29 #define __visible __attribute__((externally_visible))
30 #ifndef __attribute_const__
31 #define __attribute_const__ __attribute__((__const__))
32 #endif
33
34 #include "swab.h"
35 #include "little_endian.h"
36
37 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
38
39 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
40 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
41
42 #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1)
43 #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
44
45 #define __roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
46
47 #define __must_be_array(a) \
48 BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0])))
49 #define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0]) + __must_be_array(arr))
50
51 #define BIT(b) (1 << (b))
52 #define MASK(w) (BIT(w) - 1)
53
54 /**
55 * __struct_group() - Create a mirrored named and anonyomous struct
56 *
57 * @TAG: The tag name for the named sub-struct (usually empty)
58 * @NAME: The identifier name of the mirrored sub-struct
59 * @ATTRS: Any struct attributes (usually empty)
60 * @MEMBERS: The member declarations for the mirrored structs
61 *
62 * Used to create an anonymous union of two structs with identical layout
63 * and size: one anonymous and one named. The former's members can be used
64 * normally without sub-struct naming, and the latter can be used to
65 * reason about the start, end, and size of the group of struct members.
66 * The named struct can also be explicitly tagged for layer reuse, as well
67 * as both having struct attributes appended.
68 */
69 #define __struct_group(TAG, NAME, ATTRS, MEMBERS...) \
70 union { \
71 struct { MEMBERS } ATTRS; \
72 struct TAG { MEMBERS } ATTRS NAME; \
73 }
74
75 /**
76 * struct_group() - Wrap a set of declarations in a mirrored struct
77 *
78 * @NAME: The identifier name of the mirrored sub-struct
79 * @MEMBERS: The member declarations for the mirrored structs
80 *
81 * Used to create an anonymous union of two structs with identical
82 * layout and size: one anonymous and one named. The former can be
83 * used normally without sub-struct naming, and the latter can be
84 * used to reason about the start, end, and size of the group of
85 * struct members.
86 */
87 #define struct_group(NAME, MEMBERS...) \
88 __struct_group(/* no tag */, NAME, /* no attrs */, MEMBERS)
89
90 #undef offsetof
91 #ifdef __compiler_offsetof
92 # define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE, MEMBER)
93 #else
94 # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
95 #endif
96
97 /**
98 * sizeof_field() - Report the size of a struct field in bytes
99 *
100 * @TYPE: The structure containing the field of interest
101 * @MEMBER: The field to return the size of
102 */
103 #define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
104
105 /**
106 * offsetofend() - Report the offset of a struct field within the struct
107 *
108 * @TYPE: The type of the structure
109 * @MEMBER: The member within the structure to get the end offset of
110 */
111 #define offsetofend(TYPE, MEMBER) \
112 (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
113
114 #define likely(x) __builtin_expect(!!(x), 1)
115 #define unlikely(x) __builtin_expect(!!(x), 0)
116
117 #define min(x, y) ({ \
118 typeof(x) _min1 = (x); \
119 typeof(y) _min2 = (y); \
120 (void) (&_min1 == &_min2); \
121 _min1 < _min2 ? _min1 : _min2; })
122
123 #define max(x, y) ({ \
124 typeof(x) _max1 = (x); \
125 typeof(y) _max2 = (y); \
126 (void) (&_max1 == &_max2); \
127 _max1 > _max2 ? _max1 : _max2; })
128
129 #define min_t(type, x, y) ({ \
130 type __min1 = (x); \
131 type __min2 = (y); \
132 __min1 < __min2 ? __min1 : __min2; })
133
134 #define max_t(type, x, y) ({ \
135 type __max1 = (x); \
136 type __max2 = (y); \
137 __max1 > __max2 ? __max1 : __max2; })
138
139
140 #define container_of(ptr, type, member) ({ \
141 const typeof(((type *)0)->member) * __mptr = (ptr); \
142 (type *)(((unsigned long)__mptr - offsetof(type, member))); })
143
144 #define MAX_ERRNO 4095
145
146 #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
147
148 static inline void *ERR_PTR(long errornr)
149 {
150 return (void *) errornr;
151 }
152
153 static inline long PTR_ERR(const void *ptr)
154 {
155 return (long) ptr;
156 }
157
158 static inline long IS_ERR(const void *ptr)
159 {
160 return IS_ERR_VALUE((unsigned long)ptr);
161 }
162
163 static inline long IS_ERR_OR_NULL(const void *ptr)
164 {
165 return !ptr || IS_ERR_VALUE((unsigned long)ptr);
166 }
167
168 static inline unsigned int hweight8(unsigned int w)
169 {
170 unsigned int res = w - ((w >> 1) & 0x55);
171 res = (res & 0x33) + ((res >> 2) & 0x33);
172 return (res + (res >> 4)) & 0x0F;
173 }
174
175 static inline unsigned int hweight16(unsigned int w)
176 {
177 unsigned int res = w - ((w >> 1) & 0x5555);
178 res = (res & 0x3333) + ((res >> 2) & 0x3333);
179 res = (res + (res >> 4)) & 0x0F0F;
180 return (res + (res >> 8)) & 0x00FF;
181 }
182
183 /**
184 * DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
185 *
186 * @TYPE: The type of each flexible array element
187 * @NAME: The name of the flexible array member
188 *
189 * In order to have a flexible array member in a union or alone in a
190 * struct, it needs to be wrapped in an anonymous struct with at least 1
191 * named member, but that member can be empty.
192 */
193 #define DECLARE_FLEX_ARRAY(TYPE, NAME) \
194 struct { \
195 struct { } __empty_ ## NAME; \
196 TYPE NAME[]; \
197 }
198
199 #define __bf_shf(x) (__builtin_ffsll(x) - 1)
200
201 #define FIELD_MAX(_mask) \
202 ({ \
203 (typeof(_mask))((_mask) >> __bf_shf(_mask)); \
204 })
205
206 #if __has_attribute(__error__)
207 # define __compiletime_error(msg) __attribute__((__error__(msg)))
208 #else
209 # define __compiletime_error(msg)
210 #endif
211
212 extern void __compiletime_error("value doesn't fit into mask")
213 __field_overflow(void);
214 extern void __compiletime_error("bad bitfield mask")
215 __bad_mask(void);
216 static inline u64 field_multiplier(u32 field)
217 {
218 if ((field | (field - 1)) & ((field | (field - 1)) + 1))
219 __bad_mask();
220 return field & -field;
221 }
222 static inline u64 field_mask(const u32 field)
223 {
224 return field / field_multiplier(field);
225 }
226 #define field_max(field) ((typeof(field))field_mask(field))
227 #define ____MAKE_OP(type,base,to,from) \
228 static inline __##type type##_encode_bits(base v, base field) \
229 { \
230 if (__builtin_constant_p(v) && (v & ~field_mask(field))) \
231 __field_overflow(); \
232 return to((v & field_mask(field)) * field_multiplier(field)); \
233 } \
234 static inline __##type type##_replace_bits(__##type old, \
235 base val, base field) \
236 { \
237 return (old & ~to(field)) | type##_encode_bits(val, field); \
238 } \
239 static inline void type##p_replace_bits(__##type *p, \
240 base val, base field) \
241 { \
242 *p = (*p & ~to(field)) | type##_encode_bits(val, field); \
243 } \
244 static inline base type##_get_bits(__##type v, base field) \
245 { \
246 return (from(v) & field)/field_multiplier(field); \
247 }
248 #define __MAKE_OP(size) \
249 ____MAKE_OP(le##size,u##size,cpu_to_le##size,le##size##_to_cpu) \
250 ____MAKE_OP(be##size,u##size,cpu_to_be##size,be##size##_to_cpu) \
251 ____MAKE_OP(u##size,u##size,,)
252 ____MAKE_OP(u8,u8,,)
253 __MAKE_OP(16)
254 __MAKE_OP(32)
255 #undef __MAKE_OP
256 #undef ____MAKE_OP
257
258 #endif /* __SHARED_COMPILER_H */