ifpps: Rename variable containing top number of CPUs
[netsniff-ng.git] / built_in.h
blobea81257892ce6def8f5bfc31685485bd8bbdcc85
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009-2012 Daniel Borkmann.
4 * Parts taken from the Linux kernel, GPL, version 2.
5 * Subject to the GPL, version 2.
6 */
8 #ifndef BUILT_IN_H
9 #define BUILT_IN_H
11 #include <linux/if_packet.h>
12 #include <assert.h>
13 #include <endian.h>
14 #include <byteswap.h>
15 #include <asm/byteorder.h>
16 #include <stdint.h>
18 typedef uint64_t u64;
19 typedef uint32_t u32;
20 typedef uint16_t u16;
21 typedef uint8_t u8;
23 #ifndef CO_CACHE_LINE_SIZE
24 # define CO_CACHE_LINE_SIZE (1 << CO_IN_CACHE_SHIFT)
25 #endif
27 #ifndef __aligned_16
28 # define __aligned_16 __attribute__((aligned(16)))
29 #endif
31 #ifndef __cacheline_aligned
32 # define __cacheline_aligned __attribute__((aligned(CO_CACHE_LINE_SIZE)))
33 #endif
35 #ifndef __aligned_tpacket
36 # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
37 #endif
39 #ifndef __align_tpacket
40 # define __align_tpacket(x) __attribute__((aligned(TPACKET_ALIGN(x))))
41 #endif
43 #ifndef __check_format_printf
44 # define __check_format_printf(pos_fmtstr, pos_fmtargs) \
45 __attribute__ ((format (printf, (pos_fmtstr), (pos_fmtargs))))
46 #endif
48 #ifndef __packed
49 # define __packed __attribute__((packed))
50 #endif
52 #ifndef round_up
53 # define round_up(x, alignment) (((x) + (alignment) - 1) & ~((alignment) - 1))
54 #endif
56 #ifndef round_up_cacheline
57 # define round_up_cacheline(x) round_up((x), CO_CACHE_LINE_SIZE)
58 #endif
60 #ifndef likely
61 # define likely(x) __builtin_expect(!!(x), 1)
62 #endif
64 #ifndef unlikely
65 # define unlikely(x) __builtin_expect(!!(x), 0)
66 #endif
68 #ifndef constant
69 # define constant(x) __builtin_constant_p(x)
70 #endif
72 #ifndef fmemset
73 # define fmemset __builtin_memset
74 #endif
76 #ifndef fmemcpy
77 # define fmemcpy __builtin_memcpy
78 #endif
80 #ifndef __maybe_unused
81 # define __maybe_unused __attribute__ ((__unused__))
82 #endif
84 #ifndef noinline
85 # define noinline __attribute__((noinline))
86 #endif
88 #ifndef __noreturn
89 # define __noreturn __attribute__((noreturn))
90 #endif
92 #ifndef __hidden
93 # define __hidden __attribute__((visibility("hidden")))
94 #endif
96 #ifndef __pure
97 # define __pure __attribute__ ((pure))
98 #endif
100 #ifndef __force
101 # define __force /* unimplemented */
102 #endif
104 #ifndef force_cast
105 # define force_cast(type, arg) ((type) (arg))
106 #endif
108 #ifndef max
109 # define max(a, b) \
110 ({ \
111 typeof (a) _a = (a); \
112 typeof (b) _b = (b); \
113 _a > _b ? _a : _b; \
115 #endif /* max */
117 #ifndef min
118 # define min(a, b) \
119 ({ \
120 typeof (a) _a = (a); \
121 typeof (b) _b = (b); \
122 _a < _b ? _a : _b; \
124 #endif /* min */
126 #ifndef ispow2
127 # define ispow2(x) ({ !!((x) && !((x) & ((x) - 1))); })
128 #endif
130 #ifndef offsetof
131 # define offsetof(type, member) ((size_t) &((type *) 0)->member)
132 #endif
134 #ifndef container_of
135 # define container_of(ptr, type, member) \
136 ({ \
137 const typeof(((type *) 0)->member) * __mptr = (ptr); \
138 (type *) ((char *) __mptr - offsetof(type, member)); \
140 #endif
142 #ifndef array_size
143 # define array_size(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
144 #endif
146 #ifndef __must_be_array
147 # define __must_be_array(x) \
148 build_bug_on_zero(__builtin_types_compatible_p(typeof(x), \
149 typeof(&x[0])))
150 #endif
152 #ifndef build_bug_on_zero
153 # define build_bug_on_zero(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
154 #endif
156 #ifndef build_bug_on
157 # define build_bug_on(e) ((void)sizeof(char[1 - 2*!!(e)]))
158 #endif
160 #ifndef bug_on
161 # define bug_on(cond) assert(!(cond))
162 #endif
164 #ifndef bug
165 # define bug() assert(0)
166 #endif
168 #define PAGE_SIZE (getpagesize())
169 #define PAGE_MASK (~(PAGE_SIZE - 1))
170 #define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)
172 #if __BYTE_ORDER == __LITTLE_ENDIAN
173 static inline uint64_t htonll(uint64_t x)
175 return bswap_64(x);
178 static inline uint64_t ntohll(uint64_t x)
180 return bswap_64(x);
182 #elif __BYTE_ORDER == __BIG_ENDIAN
183 static inline uint64_t htonll(uint64_t x)
185 return x;
188 static inline uint64_t ntohll(uint64_t x)
190 return x;
192 #else
193 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
194 #endif
195 #ifndef ___constant_swab16
196 # define ___constant_swab16(x) ((__u16)( \
197 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
198 (((__u16)(x) & (__u16)0xff00U) >> 8)))
199 #endif
200 #ifndef ___constant_swab32
201 # define ___constant_swab32(x) ((__u32)( \
202 (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
203 (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
204 (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
205 (((__u32)(x) & (__u32)0xff000000UL) >> 24)))
206 #endif
207 #if __BYTE_ORDER == __LITTLE_ENDIAN
208 static inline u16 cpu_to_be16(u16 val)
210 return bswap_16(val);
213 static inline u32 cpu_to_be32(u32 val)
215 return bswap_32(val);
218 static inline u64 cpu_to_be64(u64 val)
220 return bswap_64(val);
223 static inline u16 cpu_to_le16(u16 val)
225 return val;
228 static inline u32 cpu_to_le32(u32 val)
230 return val;
233 static inline u64 cpu_to_le64(u64 val)
235 return val;
238 # ifndef __constant_htonl
239 # define __constant_htonl(x) ((__force __be32)___constant_swab32((x)))
240 # endif
241 # ifndef __constant_ntohl
242 # define __constant_ntohl(x) ___constant_swab32((__force __be32)(x))
243 # endif
244 # ifndef __constant_htons
245 # define __constant_htons(x) ((__force __be16)___constant_swab16((x)))
246 # endif
247 # ifndef __constant_ntohs
248 # define __constant_ntohs(x) ___constant_swab16((__force __be16)(x))
249 # endif
250 #elif __BYTE_ORDER == __BIG_ENDIAN
251 static inline u16 cpu_to_be16(u16 val)
253 return val;
256 static inline u32 cpu_to_be32(u32 val)
258 return val;
261 static inline u64 cpu_to_be64(u64 val)
263 return val;
266 static inline u16 cpu_to_le16(u16 val)
268 return bswap_16(val);
271 static inline u32 cpu_to_le32(u32 val)
273 return bswap_32(val);
276 static inline u64 cpu_to_le64(u64 val)
278 return bswap_64(val);
281 # ifndef __constant_htonl
282 # define __constant_htonl(x) ((__force __be32)(__u32)(x))
283 # endif
284 # ifndef __constant_ntohl
285 # define __constant_ntohl(x) ((__force __u32)(__be32)(x))
286 # endif
287 # ifndef __constant_htons
288 # define __constant_htons(x) ((__force __be16)(__u16)(x))
289 # endif
290 # ifndef __constant_ntohs
291 # define __constant_ntohs(x) ((__force __u16)(__be16)(x))
292 # endif
293 #else
294 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
295 #endif
297 #define le64_to_cpu cpu_to_le64
298 #define le32_to_cpu cpu_to_le32
299 #define le16_to_cpu cpu_to_le16
300 #define be64_to_cpu cpu_to_be64
301 #define be32_to_cpu cpu_to_be32
302 #define be16_to_cpu cpu_to_be16
304 #undef memset
305 #undef memcpy
307 #define memset fmemset
308 #define memcpy fmemcpy
310 #if defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || \
311 defined(_M_X64) || defined(__amd64)
312 # define CO_IN_CACHE_SHIFT 7
313 #elif defined(__i386__) || defined(__x86__) || defined(__X86__) || \
314 defined(_M_IX86) || defined(__i386)
315 # define CO_IN_CACHE_SHIFT 7
316 #elif defined(__ia64__) || defined(__IA64__) || defined(__M_IA64)
317 # define CO_IN_CACHE_SHIFT 6
318 #elif defined(__SPU__)
319 # define CO_IN_CACHE_SHIFT 7
320 #elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
321 defined(_ARCH_PPC64)
322 # define CO_IN_CACHE_SHIFT 8
323 #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || \
324 defined(_ARCH_PPC)
325 # define CO_IN_CACHE_SHIFT 7
326 #elif defined(__sparcv9__) || defined(__sparcv9)
327 # define CO_IN_CACHE_SHIFT 6
328 #elif defined(__sparc_v8__)
329 # define CO_IN_CACHE_SHIFT 5
330 #elif defined(__sparc__) || defined(__sparc)
331 # define CO_IN_CACHE_SHIFT 5
332 #elif defined(__ARM_EABI__)
333 # define CO_IN_CACHE_SHIFT 5
334 #elif defined(__arm__)
335 # define CO_IN_CACHE_SHIFT 5
336 #elif defined(__mips__) || defined(__mips) || defined(__MIPS__)
337 # if defined(_ABIO32)
338 # define CO_IN_CACHE_SHIFT 5
339 # elif defined(_ABIN32)
340 # define CO_IN_CACHE_SHIFT 5
341 # else
342 # define CO_IN_CACHE_SHIFT 6
343 # endif
344 #else
345 # define CO_IN_CACHE_SHIFT 5
346 #endif
348 #ifndef TP_STATUS_TS_SOFTWARE
349 # define TP_STATUS_TS_SOFTWARE (1 << 29)
350 #endif
352 #ifndef TP_STATUS_TS_SYS_HARDWARE
353 # define TP_STATUS_TS_SYS_HARDWARE (1 << 30)
354 #endif
356 #ifndef TP_STATUS_TS_RAW_HARDWARE
357 # define TP_STATUS_TS_RAW_HARDWARE (1 << 31)
358 #endif
360 #ifndef POLLRDNORM
361 # define POLLRDNORM 0x0040
362 #endif
364 #ifndef POLLWRNORM
365 # define POLLWRNORM 0x0100
366 #endif
368 #ifndef POLLRDHUP
369 # define POLLRDHUP 0x2000
370 #endif
372 #endif /* BUILT_IN_H */