doc: add Christian to AUTHORS file
[netsniff-ng.git] / built_in.h
blob8dd1824016c608cad3c7909edb618961890d21a2
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 noinline
80 # define noinline __attribute__((noinline))
81 #endif
83 #ifndef __noreturn
84 # define __noreturn __attribute__((noreturn))
85 #endif
87 #ifndef __hidden
88 # define __hidden __attribute__((visibility("hidden")))
89 #endif
91 #ifndef __pure
92 # define __pure __attribute__ ((pure))
93 #endif
95 #ifndef __force
96 # define __force /* unimplemented */
97 #endif
99 #ifndef force_cast
100 # define force_cast(type, arg) ((type) (arg))
101 #endif
103 #ifndef max
104 # define max(a, b) \
105 ({ \
106 typeof (a) _a = (a); \
107 typeof (b) _b = (b); \
108 _a > _b ? _a : _b; \
110 #endif /* max */
112 #ifndef max_t
113 # define max_t(type, a, b) \
114 ({ \
115 type ___max1 = (a); \
116 type ___max2 = (b); \
117 ___max1 > ___max2 ? ___max1 : ___max2; \
119 #endif /* max_t */
121 #ifndef min
122 # define min(a, b) \
123 ({ \
124 typeof (a) _a = (a); \
125 typeof (b) _b = (b); \
126 _a < _b ? _a : _b; \
128 #endif /* min */
130 #ifndef min_t
131 # define min_t(type, a, b) \
132 ({ \
133 type ___min1 = (a); \
134 type ___min2 = (b); \
135 ___min1 < ___min2 ? ___min1 : ___min2; \
137 #endif /* min_t */
139 #ifndef ispow2
140 # define ispow2(x) ({ !!((x) && !((x) & ((x) - 1))); })
141 #endif
143 #ifndef offsetof
144 # define offsetof(type, member) ((size_t) &((type *) 0)->member)
145 #endif
147 #ifndef container_of
148 # define container_of(ptr, type, member) \
149 ({ \
150 const typeof(((type *) 0)->member) * __mptr = (ptr); \
151 (type *) ((char *) __mptr - offsetof(type, member)); \
153 #endif
155 #ifndef array_size
156 # define array_size(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
157 #endif
159 #ifndef __must_be_array
160 # define __must_be_array(x) \
161 build_bug_on_zero(__builtin_types_compatible_p(typeof(x), \
162 typeof(&x[0])))
163 #endif
165 #ifndef build_bug_on_zero
166 # define build_bug_on_zero(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
167 #endif
169 #ifndef build_bug_on
170 # define build_bug_on(e) ((void)sizeof(char[1 - 2*!!(e)]))
171 #endif
173 #ifndef bug_on
174 # define bug_on(cond) assert(!(cond))
175 #endif
177 #ifndef bug
178 # define bug() assert(0)
179 #endif
181 #ifndef PAGE_SIZE
182 # define PAGE_SIZE (getpagesize())
183 #endif
184 #define PAGE_MASK (~(PAGE_SIZE - 1))
185 #define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)
187 #if __BYTE_ORDER == __LITTLE_ENDIAN
188 static inline uint64_t htonll(uint64_t x)
190 return bswap_64(x);
193 static inline uint64_t ntohll(uint64_t x)
195 return bswap_64(x);
197 #elif __BYTE_ORDER == __BIG_ENDIAN
198 static inline uint64_t htonll(uint64_t x)
200 return x;
203 static inline uint64_t ntohll(uint64_t x)
205 return x;
207 #else
208 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
209 #endif
210 #ifndef ___constant_swab16
211 # define ___constant_swab16(x) ((__u16)( \
212 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
213 (((__u16)(x) & (__u16)0xff00U) >> 8)))
214 #endif
215 #ifndef ___constant_swab32
216 # define ___constant_swab32(x) ((__u32)( \
217 (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
218 (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
219 (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
220 (((__u32)(x) & (__u32)0xff000000UL) >> 24)))
221 #endif
222 #if __BYTE_ORDER == __LITTLE_ENDIAN
223 static inline u16 cpu_to_be16(u16 val)
225 return bswap_16(val);
228 static inline u32 cpu_to_be32(u32 val)
230 return bswap_32(val);
233 static inline u64 cpu_to_be64(u64 val)
235 return bswap_64(val);
238 static inline u16 cpu_to_le16(u16 val)
240 return val;
243 static inline u32 cpu_to_le32(u32 val)
245 return val;
248 static inline u64 cpu_to_le64(u64 val)
250 return val;
253 # ifndef __constant_htonl
254 # define __constant_htonl(x) ((__force __be32)___constant_swab32((x)))
255 # endif
256 # ifndef __constant_ntohl
257 # define __constant_ntohl(x) ___constant_swab32((__force __be32)(x))
258 # endif
259 # ifndef __constant_htons
260 # define __constant_htons(x) ((__force __be16)___constant_swab16((x)))
261 # endif
262 # ifndef __constant_ntohs
263 # define __constant_ntohs(x) ___constant_swab16((__force __be16)(x))
264 # endif
265 #elif __BYTE_ORDER == __BIG_ENDIAN
266 static inline u16 cpu_to_be16(u16 val)
268 return val;
271 static inline u32 cpu_to_be32(u32 val)
273 return val;
276 static inline u64 cpu_to_be64(u64 val)
278 return val;
281 static inline u16 cpu_to_le16(u16 val)
283 return bswap_16(val);
286 static inline u32 cpu_to_le32(u32 val)
288 return bswap_32(val);
291 static inline u64 cpu_to_le64(u64 val)
293 return bswap_64(val);
296 # ifndef __constant_htonl
297 # define __constant_htonl(x) ((__force __be32)(__u32)(x))
298 # endif
299 # ifndef __constant_ntohl
300 # define __constant_ntohl(x) ((__force __u32)(__be32)(x))
301 # endif
302 # ifndef __constant_htons
303 # define __constant_htons(x) ((__force __be16)(__u16)(x))
304 # endif
305 # ifndef __constant_ntohs
306 # define __constant_ntohs(x) ((__force __u16)(__be16)(x))
307 # endif
308 #else
309 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
310 #endif
312 #define le64_to_cpu cpu_to_le64
313 #define le32_to_cpu cpu_to_le32
314 #define le16_to_cpu cpu_to_le16
315 #define be64_to_cpu cpu_to_be64
316 #define be32_to_cpu cpu_to_be32
317 #define be16_to_cpu cpu_to_be16
319 #undef memset
320 #undef memcpy
322 #define memset fmemset
323 #define memcpy fmemcpy
325 #if defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || \
326 defined(_M_X64) || defined(__amd64)
327 # define CO_IN_CACHE_SHIFT 7
328 #elif defined(__i386__) || defined(__x86__) || defined(__X86__) || \
329 defined(_M_IX86) || defined(__i386)
330 # define CO_IN_CACHE_SHIFT 7
331 #elif defined(__ia64__) || defined(__IA64__) || defined(__M_IA64)
332 # define CO_IN_CACHE_SHIFT 6
333 #elif defined(__SPU__)
334 # define CO_IN_CACHE_SHIFT 7
335 #elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
336 defined(_ARCH_PPC64)
337 # define CO_IN_CACHE_SHIFT 8
338 #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || \
339 defined(_ARCH_PPC)
340 # define CO_IN_CACHE_SHIFT 7
341 #elif defined(__sparcv9__) || defined(__sparcv9)
342 # define CO_IN_CACHE_SHIFT 6
343 #elif defined(__sparc_v8__)
344 # define CO_IN_CACHE_SHIFT 5
345 #elif defined(__sparc__) || defined(__sparc)
346 # define CO_IN_CACHE_SHIFT 5
347 #elif defined(__ARM_EABI__)
348 # define CO_IN_CACHE_SHIFT 5
349 #elif defined(__arm__)
350 # define CO_IN_CACHE_SHIFT 5
351 #elif defined(__mips__) || defined(__mips) || defined(__MIPS__)
352 # if defined(_ABIO32)
353 # define CO_IN_CACHE_SHIFT 5
354 # elif defined(_ABIN32)
355 # define CO_IN_CACHE_SHIFT 5
356 # else
357 # define CO_IN_CACHE_SHIFT 6
358 # endif
359 #else
360 # define CO_IN_CACHE_SHIFT 5
361 #endif
363 #ifndef TP_STATUS_TS_SOFTWARE
364 # define TP_STATUS_TS_SOFTWARE (1 << 29)
365 #endif
367 #ifndef TP_STATUS_TS_SYS_HARDWARE
368 # define TP_STATUS_TS_SYS_HARDWARE (1 << 30)
369 #endif
371 #ifndef TP_STATUS_TS_RAW_HARDWARE
372 # define TP_STATUS_TS_RAW_HARDWARE (1 << 31)
373 #endif
375 #ifndef PACKET_QDISC_BYPASS
376 # define PACKET_QDISC_BYPASS 20
377 #endif
379 #ifndef POLLRDNORM
380 # define POLLRDNORM 0x0040
381 #endif
383 #ifndef POLLWRNORM
384 # define POLLWRNORM 0x0100
385 #endif
387 #ifndef POLLRDHUP
388 # define POLLRDHUP 0x2000
389 #endif
391 #ifndef ARPHRD_IEEE802154_MONITOR
392 # define ARPHRD_IEEE802154_MONITOR 805
393 #endif
395 #ifndef ARPHRD_IP6GRE
396 # define ARPHRD_IP6GRE 823
397 #endif
399 #ifndef ARPHRD_NETLINK
400 # define ARPHRD_NETLINK 824
401 #endif
403 #ifndef PACKET_USER
404 # define PACKET_USER 6
405 #endif
407 #ifndef PACKET_KERNEL
408 # define PACKET_KERNEL 7
409 #endif
411 #ifndef DEFFILEMODE
412 # define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) /* 0666 */
413 #endif
415 #endif /* BUILT_IN_H */