mausezahn: use getopt_long instead of getopt
[netsniff-ng.git] / built_in.h
blob24af1bc497cdef29540c5b0951938db0f913fb5b
1 #ifndef BUILT_IN_H
2 #define BUILT_IN_H
4 /* Parts taken from the Linux kernel, GPL, version 2. */
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <linux/if_packet.h>
9 #include <assert.h>
10 #include <endian.h>
11 #include <byteswap.h>
12 #include <asm/byteorder.h>
14 typedef uint64_t u64;
15 typedef uint32_t u32;
16 typedef uint16_t u16;
17 typedef uint8_t u8;
19 #ifndef CO_CACHE_LINE_SIZE
20 # define CO_CACHE_LINE_SIZE (1 << CO_IN_CACHE_SHIFT)
21 #endif
23 #ifndef __aligned_16
24 # define __aligned_16 __attribute__((aligned(16)))
25 #endif
27 #ifndef __cacheline_aligned
28 # define __cacheline_aligned __attribute__((aligned(CO_CACHE_LINE_SIZE)))
29 #endif
31 #ifndef __aligned_tpacket
32 # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
33 #endif
35 #ifndef __align_tpacket
36 # define __align_tpacket(x) __attribute__((aligned(TPACKET_ALIGN(x))))
37 #endif
39 #ifndef __check_format_printf
40 # define __check_format_printf(pos_fmtstr, pos_fmtargs) \
41 __attribute__ ((format (printf, (pos_fmtstr), (pos_fmtargs))))
42 #endif
44 #ifndef __packed
45 # define __packed __attribute__((packed))
46 #endif
48 #ifndef round_up
49 # define round_up(x, alignment) (((x) + (alignment) - 1) & ~((alignment) - 1))
50 #endif
52 #ifndef round_up_cacheline
53 # define round_up_cacheline(x) round_up((x), CO_CACHE_LINE_SIZE)
54 #endif
56 #ifndef likely
57 # define likely(x) __builtin_expect(!!(x), 1)
58 #endif
60 #ifndef unlikely
61 # define unlikely(x) __builtin_expect(!!(x), 0)
62 #endif
64 #ifndef constant
65 # define constant(x) __builtin_constant_p(x)
66 #endif
68 #ifndef __maybe_unused
69 # define __maybe_unused __attribute__((__unused__))
70 #endif
72 #ifndef __warn_unused_result
73 # define __warn_unused_result __attribute__((warn_unused_result))
74 #endif
76 #ifndef noinline
77 # define noinline __attribute__((noinline))
78 #endif
80 #ifndef __noreturn
81 # define __noreturn __attribute__((noreturn))
82 #endif
84 #ifndef __hidden
85 # define __hidden __attribute__((visibility("hidden")))
86 #endif
88 #ifndef __pure
89 # define __pure __attribute__ ((pure))
90 #endif
92 #ifndef __force
93 # define __force /* unimplemented */
94 #endif
96 /* see config_enabled et al. in linux/kconfig.h for details. */
97 #define __ARG_PLACEHOLDER_1 0,
98 #define is_defined(cfg) _is_defined(cfg)
99 #define _is_defined(value) __is_defined(__ARG_PLACEHOLDER_##value)
100 #define __is_defined(arg1_or_junk) ___is_defined(arg1_or_junk 1, 0)
101 #define ___is_defined(__ignored, val, ...) val
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 #define RUNTIME_PAGE_SIZE (sysconf(_SC_PAGE_SIZE))
182 #define PAGE_MASK (~(RUNTIME_PAGE_SIZE - 1))
183 #define PAGE_ALIGN(addr) (((addr) + RUNTIME_PAGE_SIZE - 1) & PAGE_MASK)
185 #if __BYTE_ORDER == __LITTLE_ENDIAN
186 static inline uint64_t htonll(uint64_t x)
188 return bswap_64(x);
191 static inline uint64_t ntohll(uint64_t x)
193 return bswap_64(x);
195 #elif __BYTE_ORDER == __BIG_ENDIAN
196 static inline uint64_t htonll(uint64_t x)
198 return x;
201 static inline uint64_t ntohll(uint64_t x)
203 return x;
205 #else
206 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
207 #endif
208 #ifndef ___constant_swab16
209 # define ___constant_swab16(x) ((__u16)( \
210 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
211 (((__u16)(x) & (__u16)0xff00U) >> 8)))
212 #endif
213 #ifndef ___constant_swab32
214 # define ___constant_swab32(x) ((__u32)( \
215 (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
216 (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
217 (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
218 (((__u32)(x) & (__u32)0xff000000UL) >> 24)))
219 #endif
220 #if __BYTE_ORDER == __LITTLE_ENDIAN
221 static inline u16 cpu_to_be16(u16 val)
223 return bswap_16(val);
226 static inline u32 cpu_to_be32(u32 val)
228 return bswap_32(val);
231 static inline u64 cpu_to_be64(u64 val)
233 return bswap_64(val);
236 static inline u16 cpu_to_le16(u16 val)
238 return val;
241 static inline u32 cpu_to_le32(u32 val)
243 return val;
246 static inline u64 cpu_to_le64(u64 val)
248 return val;
251 # ifndef __constant_htonl
252 # define __constant_htonl(x) ((__force __be32)___constant_swab32((x)))
253 # endif
254 # ifndef __constant_ntohl
255 # define __constant_ntohl(x) ___constant_swab32((__force __be32)(x))
256 # endif
257 # ifndef __constant_htons
258 # define __constant_htons(x) ((__force __be16)___constant_swab16((x)))
259 # endif
260 # ifndef __constant_ntohs
261 # define __constant_ntohs(x) ___constant_swab16((__force __be16)(x))
262 # endif
263 #elif __BYTE_ORDER == __BIG_ENDIAN
264 static inline u16 cpu_to_be16(u16 val)
266 return val;
269 static inline u32 cpu_to_be32(u32 val)
271 return val;
274 static inline u64 cpu_to_be64(u64 val)
276 return val;
279 static inline u16 cpu_to_le16(u16 val)
281 return bswap_16(val);
284 static inline u32 cpu_to_le32(u32 val)
286 return bswap_32(val);
289 static inline u64 cpu_to_le64(u64 val)
291 return bswap_64(val);
294 # ifndef __constant_htonl
295 # define __constant_htonl(x) ((__force __be32)(__u32)(x))
296 # endif
297 # ifndef __constant_ntohl
298 # define __constant_ntohl(x) ((__force __u32)(__be32)(x))
299 # endif
300 # ifndef __constant_htons
301 # define __constant_htons(x) ((__force __be16)(__u16)(x))
302 # endif
303 # ifndef __constant_ntohs
304 # define __constant_ntohs(x) ((__force __u16)(__be16)(x))
305 # endif
306 #else
307 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
308 #endif
310 #define le64_to_cpu cpu_to_le64
311 #define le32_to_cpu cpu_to_le32
312 #define le16_to_cpu cpu_to_le16
313 #define be64_to_cpu cpu_to_be64
314 #define be32_to_cpu cpu_to_be32
315 #define be16_to_cpu cpu_to_be16
317 #if defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || \
318 defined(_M_X64) || defined(__amd64)
319 # define CO_IN_CACHE_SHIFT 7
320 #elif defined(__i386__) || defined(__x86__) || defined(__X86__) || \
321 defined(_M_IX86) || defined(__i386)
322 # define CO_IN_CACHE_SHIFT 7
323 #elif defined(__ia64__) || defined(__IA64__) || defined(__M_IA64)
324 # define CO_IN_CACHE_SHIFT 6
325 #elif defined(__SPU__)
326 # define CO_IN_CACHE_SHIFT 7
327 #elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
328 defined(_ARCH_PPC64)
329 # define CO_IN_CACHE_SHIFT 8
330 #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || \
331 defined(_ARCH_PPC)
332 # define CO_IN_CACHE_SHIFT 7
333 #elif defined(__sparcv9__) || defined(__sparcv9)
334 # define CO_IN_CACHE_SHIFT 6
335 #elif defined(__sparc_v8__)
336 # define CO_IN_CACHE_SHIFT 5
337 #elif defined(__sparc__) || defined(__sparc)
338 # define CO_IN_CACHE_SHIFT 5
339 #elif defined(__ARM_EABI__)
340 # define CO_IN_CACHE_SHIFT 5
341 #elif defined(__arm__)
342 # define CO_IN_CACHE_SHIFT 5
343 #elif defined(__mips__) || defined(__mips) || defined(__MIPS__)
344 # if defined(_ABIO32)
345 # define CO_IN_CACHE_SHIFT 5
346 # elif defined(_ABIN32)
347 # define CO_IN_CACHE_SHIFT 5
348 # else
349 # define CO_IN_CACHE_SHIFT 6
350 # endif
351 #else
352 # define CO_IN_CACHE_SHIFT 5
353 #endif
355 #ifndef TP_STATUS_TS_SOFTWARE
356 # define TP_STATUS_TS_SOFTWARE (1 << 29)
357 #endif
359 #ifndef TP_STATUS_TS_SYS_HARDWARE
360 # define TP_STATUS_TS_SYS_HARDWARE (1 << 30)
361 #endif
363 #ifndef TP_STATUS_TS_RAW_HARDWARE
364 # define TP_STATUS_TS_RAW_HARDWARE (1 << 31)
365 #endif
367 #ifndef PACKET_QDISC_BYPASS
368 # define PACKET_QDISC_BYPASS 20
369 #endif
371 #ifndef ARPHRD_CAN
372 # define ARPHRD_CAN 280
373 #endif
375 #ifndef ARPHRD_IEEE802154_MONITOR
376 # define ARPHRD_IEEE802154_MONITOR 805
377 #endif
379 #ifndef ARPHRD_PHONET
380 # define ARPHRD_PHONET 820
381 #endif
383 #ifndef ARPHRD_PHONET_PIPE
384 # define ARPHRD_PHONET_PIPE 821
385 #endif
387 #ifndef ARPHRD_CAIF
388 # define ARPHRD_CAIF 822
389 #endif
391 #ifndef ARPHRD_IP6GRE
392 # define ARPHRD_IP6GRE 823
393 #endif
395 #ifndef ARPHRD_NETLINK
396 # define ARPHRD_NETLINK 824
397 #endif
399 #ifndef PACKET_USER
400 # define PACKET_USER 6
401 #endif
403 #ifndef PACKET_KERNEL
404 # define PACKET_KERNEL 7
405 #endif
407 #ifndef DEFFILEMODE
408 # define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) /* 0666 */
409 #endif
411 #endif /* BUILT_IN_H */