curvetun: Fix issues detected by the Coverity scanner
[netsniff-ng.git] / built_in.h
blob6ebf27ec847388b4b42ccfc7bb76c955711dd172
1 #ifndef BUILT_IN_H
2 #define BUILT_IN_H
4 /* Parts taken from the Linux kernel, GPL, version 2. */
6 #include <linux/if_packet.h>
7 #include <assert.h>
8 #include <endian.h>
9 #include <byteswap.h>
10 #include <asm/byteorder.h>
11 #include <stdint.h>
13 typedef uint64_t u64;
14 typedef uint32_t u32;
15 typedef uint16_t u16;
16 typedef uint8_t u8;
18 #ifndef CO_CACHE_LINE_SIZE
19 # define CO_CACHE_LINE_SIZE (1 << CO_IN_CACHE_SHIFT)
20 #endif
22 #ifndef __aligned_16
23 # define __aligned_16 __attribute__((aligned(16)))
24 #endif
26 #ifndef __cacheline_aligned
27 # define __cacheline_aligned __attribute__((aligned(CO_CACHE_LINE_SIZE)))
28 #endif
30 #ifndef __aligned_tpacket
31 # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
32 #endif
34 #ifndef __align_tpacket
35 # define __align_tpacket(x) __attribute__((aligned(TPACKET_ALIGN(x))))
36 #endif
38 #ifndef __check_format_printf
39 # define __check_format_printf(pos_fmtstr, pos_fmtargs) \
40 __attribute__ ((format (printf, (pos_fmtstr), (pos_fmtargs))))
41 #endif
43 #ifndef __packed
44 # define __packed __attribute__((packed))
45 #endif
47 #ifndef round_up
48 # define round_up(x, alignment) (((x) + (alignment) - 1) & ~((alignment) - 1))
49 #endif
51 #ifndef round_up_cacheline
52 # define round_up_cacheline(x) round_up((x), CO_CACHE_LINE_SIZE)
53 #endif
55 #ifndef likely
56 # define likely(x) __builtin_expect(!!(x), 1)
57 #endif
59 #ifndef unlikely
60 # define unlikely(x) __builtin_expect(!!(x), 0)
61 #endif
63 #ifndef constant
64 # define constant(x) __builtin_constant_p(x)
65 #endif
67 #ifndef fmemset
68 # define fmemset __builtin_memset
69 #endif
71 #ifndef fmemcpy
72 # define fmemcpy __builtin_memcpy
73 #endif
75 #ifndef __maybe_unused
76 # define __maybe_unused __attribute__((__unused__))
77 #endif
79 #ifndef __warn_unused_result
80 # define __warn_unused_result __attribute__((warn_unused_result))
81 #endif
83 #ifndef noinline
84 # define noinline __attribute__((noinline))
85 #endif
87 #ifndef __noreturn
88 # define __noreturn __attribute__((noreturn))
89 #endif
91 #ifndef __hidden
92 # define __hidden __attribute__((visibility("hidden")))
93 #endif
95 #ifndef __pure
96 # define __pure __attribute__ ((pure))
97 #endif
99 #ifndef __force
100 # define __force /* unimplemented */
101 #endif
103 /* see config_enabled et al. in linux/kconfig.h for details. */
104 #define __ARG_PLACEHOLDER_1 0,
105 #define is_defined(cfg) _is_defined(cfg)
106 #define _is_defined(value) __is_defined(__ARG_PLACEHOLDER_##value)
107 #define __is_defined(arg1_or_junk) ___is_defined(arg1_or_junk 1, 0)
108 #define ___is_defined(__ignored, val, ...) val
110 #ifndef max
111 # define max(a, b) \
112 ({ \
113 typeof (a) _a = (a); \
114 typeof (b) _b = (b); \
115 _a > _b ? _a : _b; \
117 #endif /* max */
119 #ifndef max_t
120 # define max_t(type, a, b) \
121 ({ \
122 type ___max1 = (a); \
123 type ___max2 = (b); \
124 ___max1 > ___max2 ? ___max1 : ___max2; \
126 #endif /* max_t */
128 #ifndef min
129 # define min(a, b) \
130 ({ \
131 typeof (a) _a = (a); \
132 typeof (b) _b = (b); \
133 _a < _b ? _a : _b; \
135 #endif /* min */
137 #ifndef min_t
138 # define min_t(type, a, b) \
139 ({ \
140 type ___min1 = (a); \
141 type ___min2 = (b); \
142 ___min1 < ___min2 ? ___min1 : ___min2; \
144 #endif /* min_t */
146 #ifndef ispow2
147 # define ispow2(x) ({ !!((x) && !((x) & ((x) - 1))); })
148 #endif
150 #ifndef offsetof
151 # define offsetof(type, member) ((size_t) &((type *) 0)->member)
152 #endif
154 #ifndef container_of
155 # define container_of(ptr, type, member) \
156 ({ \
157 const typeof(((type *) 0)->member) * __mptr = (ptr); \
158 (type *) ((char *) __mptr - offsetof(type, member)); \
160 #endif
162 #ifndef array_size
163 # define array_size(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
164 #endif
166 #ifndef __must_be_array
167 # define __must_be_array(x) \
168 build_bug_on_zero(__builtin_types_compatible_p(typeof(x), \
169 typeof(&x[0])))
170 #endif
172 #ifndef build_bug_on_zero
173 # define build_bug_on_zero(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
174 #endif
176 #ifndef build_bug_on
177 # define build_bug_on(e) ((void)sizeof(char[1 - 2*!!(e)]))
178 #endif
180 #ifndef bug_on
181 # define bug_on(cond) assert(!(cond))
182 #endif
184 #ifndef bug
185 # define bug() assert(0)
186 #endif
188 #define RUNTIME_PAGE_SIZE (sysconf(_SC_PAGE_SIZE))
189 #define PAGE_MASK (~(RUNTIME_PAGE_SIZE - 1))
190 #define PAGE_ALIGN(addr) (((addr) + RUNTIME_PAGE_SIZE - 1) & PAGE_MASK)
192 #if __BYTE_ORDER == __LITTLE_ENDIAN
193 static inline uint64_t htonll(uint64_t x)
195 return bswap_64(x);
198 static inline uint64_t ntohll(uint64_t x)
200 return bswap_64(x);
202 #elif __BYTE_ORDER == __BIG_ENDIAN
203 static inline uint64_t htonll(uint64_t x)
205 return x;
208 static inline uint64_t ntohll(uint64_t x)
210 return x;
212 #else
213 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
214 #endif
215 #ifndef ___constant_swab16
216 # define ___constant_swab16(x) ((__u16)( \
217 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
218 (((__u16)(x) & (__u16)0xff00U) >> 8)))
219 #endif
220 #ifndef ___constant_swab32
221 # define ___constant_swab32(x) ((__u32)( \
222 (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
223 (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
224 (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
225 (((__u32)(x) & (__u32)0xff000000UL) >> 24)))
226 #endif
227 #if __BYTE_ORDER == __LITTLE_ENDIAN
228 static inline u16 cpu_to_be16(u16 val)
230 return bswap_16(val);
233 static inline u32 cpu_to_be32(u32 val)
235 return bswap_32(val);
238 static inline u64 cpu_to_be64(u64 val)
240 return bswap_64(val);
243 static inline u16 cpu_to_le16(u16 val)
245 return val;
248 static inline u32 cpu_to_le32(u32 val)
250 return val;
253 static inline u64 cpu_to_le64(u64 val)
255 return val;
258 # ifndef __constant_htonl
259 # define __constant_htonl(x) ((__force __be32)___constant_swab32((x)))
260 # endif
261 # ifndef __constant_ntohl
262 # define __constant_ntohl(x) ___constant_swab32((__force __be32)(x))
263 # endif
264 # ifndef __constant_htons
265 # define __constant_htons(x) ((__force __be16)___constant_swab16((x)))
266 # endif
267 # ifndef __constant_ntohs
268 # define __constant_ntohs(x) ___constant_swab16((__force __be16)(x))
269 # endif
270 #elif __BYTE_ORDER == __BIG_ENDIAN
271 static inline u16 cpu_to_be16(u16 val)
273 return val;
276 static inline u32 cpu_to_be32(u32 val)
278 return val;
281 static inline u64 cpu_to_be64(u64 val)
283 return val;
286 static inline u16 cpu_to_le16(u16 val)
288 return bswap_16(val);
291 static inline u32 cpu_to_le32(u32 val)
293 return bswap_32(val);
296 static inline u64 cpu_to_le64(u64 val)
298 return bswap_64(val);
301 # ifndef __constant_htonl
302 # define __constant_htonl(x) ((__force __be32)(__u32)(x))
303 # endif
304 # ifndef __constant_ntohl
305 # define __constant_ntohl(x) ((__force __u32)(__be32)(x))
306 # endif
307 # ifndef __constant_htons
308 # define __constant_htons(x) ((__force __be16)(__u16)(x))
309 # endif
310 # ifndef __constant_ntohs
311 # define __constant_ntohs(x) ((__force __u16)(__be16)(x))
312 # endif
313 #else
314 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
315 #endif
317 #define le64_to_cpu cpu_to_le64
318 #define le32_to_cpu cpu_to_le32
319 #define le16_to_cpu cpu_to_le16
320 #define be64_to_cpu cpu_to_be64
321 #define be32_to_cpu cpu_to_be32
322 #define be16_to_cpu cpu_to_be16
324 #undef memset
325 #undef memcpy
327 #define memset fmemset
328 #define memcpy fmemcpy
330 #if defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || \
331 defined(_M_X64) || defined(__amd64)
332 # define CO_IN_CACHE_SHIFT 7
333 #elif defined(__i386__) || defined(__x86__) || defined(__X86__) || \
334 defined(_M_IX86) || defined(__i386)
335 # define CO_IN_CACHE_SHIFT 7
336 #elif defined(__ia64__) || defined(__IA64__) || defined(__M_IA64)
337 # define CO_IN_CACHE_SHIFT 6
338 #elif defined(__SPU__)
339 # define CO_IN_CACHE_SHIFT 7
340 #elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
341 defined(_ARCH_PPC64)
342 # define CO_IN_CACHE_SHIFT 8
343 #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || \
344 defined(_ARCH_PPC)
345 # define CO_IN_CACHE_SHIFT 7
346 #elif defined(__sparcv9__) || defined(__sparcv9)
347 # define CO_IN_CACHE_SHIFT 6
348 #elif defined(__sparc_v8__)
349 # define CO_IN_CACHE_SHIFT 5
350 #elif defined(__sparc__) || defined(__sparc)
351 # define CO_IN_CACHE_SHIFT 5
352 #elif defined(__ARM_EABI__)
353 # define CO_IN_CACHE_SHIFT 5
354 #elif defined(__arm__)
355 # define CO_IN_CACHE_SHIFT 5
356 #elif defined(__mips__) || defined(__mips) || defined(__MIPS__)
357 # if defined(_ABIO32)
358 # define CO_IN_CACHE_SHIFT 5
359 # elif defined(_ABIN32)
360 # define CO_IN_CACHE_SHIFT 5
361 # else
362 # define CO_IN_CACHE_SHIFT 6
363 # endif
364 #else
365 # define CO_IN_CACHE_SHIFT 5
366 #endif
368 #ifndef TP_STATUS_TS_SOFTWARE
369 # define TP_STATUS_TS_SOFTWARE (1 << 29)
370 #endif
372 #ifndef TP_STATUS_TS_SYS_HARDWARE
373 # define TP_STATUS_TS_SYS_HARDWARE (1 << 30)
374 #endif
376 #ifndef TP_STATUS_TS_RAW_HARDWARE
377 # define TP_STATUS_TS_RAW_HARDWARE (1 << 31)
378 #endif
380 #ifndef PACKET_QDISC_BYPASS
381 # define PACKET_QDISC_BYPASS 20
382 #endif
384 #ifndef ARPHRD_IEEE802154_MONITOR
385 # define ARPHRD_IEEE802154_MONITOR 805
386 #endif
388 #ifndef ARPHRD_IP6GRE
389 # define ARPHRD_IP6GRE 823
390 #endif
392 #ifndef ARPHRD_NETLINK
393 # define ARPHRD_NETLINK 824
394 #endif
396 #ifndef PACKET_USER
397 # define PACKET_USER 6
398 #endif
400 #ifndef PACKET_KERNEL
401 # define PACKET_KERNEL 7
402 #endif
404 #ifndef DEFFILEMODE
405 # define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) /* 0666 */
406 #endif
408 #endif /* BUILT_IN_H */