dissector: we're in default case anyways
[netsniff-ng.git] / built_in.h
blobb48193e1cf5781521da290be33e1273d8868ea69
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009-2012 Daniel Borkmann.
5 * Parts taken from the Linux kernel, GPL, version 2.
6 * Subject to the GPL, version 2.
7 */
9 #ifndef BUILT_IN_H
10 #define BUILT_IN_H
12 #include <linux/if_packet.h>
13 #include <assert.h>
14 #include <endian.h>
15 #include <byteswap.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 /* /sys/devices/system/cpu/cpuX/cache/indexX/coherency_line_size */
25 #if defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || \
26 defined(_M_X64) || defined(__amd64)
27 # define CO_IN_CACHE_SHIFT 7
28 #elif defined(__i386__) || defined(__x86__) || defined(__X86__) || \
29 defined(_M_IX86) || defined(__i386)
30 # define CO_IN_CACHE_SHIFT 7
31 #elif defined(__ia64__) || defined(__IA64__) || defined(__M_IA64)
32 # define CO_IN_CACHE_SHIFT 6
33 #elif defined(__SPU__)
34 # define CO_IN_CACHE_SHIFT 7
35 #elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
36 defined(_ARCH_PPC64)
37 # define CO_IN_CACHE_SHIFT 8
38 #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || \
39 defined(_ARCH_PPC)
40 # define CO_IN_CACHE_SHIFT 7
41 #elif defined(__sparcv9__) || defined(__sparcv9)
42 # define CO_IN_CACHE_SHIFT 6
43 #elif defined(__sparc_v8__)
44 # define CO_IN_CACHE_SHIFT 5
45 #elif defined(__sparc__) || defined(__sparc)
46 # define CO_IN_CACHE_SHIFT 5
47 #elif defined(__ARM_EABI__)
48 # define CO_IN_CACHE_SHIFT 5
49 #elif defined(__arm__)
50 # define CO_IN_CACHE_SHIFT 5
51 #elif defined(__mips__) || defined(__mips) || defined(__MIPS__)
52 # if defined(_ABIO32)
53 # define CO_IN_CACHE_SHIFT 5
54 # elif defined(_ABIN32)
55 # define CO_IN_CACHE_SHIFT 5
56 # else
57 # define CO_IN_CACHE_SHIFT 6
58 # endif
59 #else
60 # define CO_IN_CACHE_SHIFT 5
61 #endif
63 #ifndef CO_CACHE_LINE_SIZE
64 # define CO_CACHE_LINE_SIZE (1 << CO_IN_CACHE_SHIFT)
65 #endif
67 #ifndef MAX_CPUS
68 # define MAX_CPUS 32
69 #endif
71 #ifndef __aligned_16
72 # define __aligned_16 __attribute__((aligned(16)))
73 #endif
75 #ifndef __cacheline_aligned
76 # define __cacheline_aligned __attribute__((aligned(CO_CACHE_LINE_SIZE)))
77 #endif
79 #ifndef __aligned_tpacket
80 # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
81 #endif
83 #ifndef __align_tpacket
84 # define __align_tpacket(x) __attribute__((aligned(TPACKET_ALIGN(x))))
85 #endif
87 #ifndef __check_format_printf
88 # define __check_format_printf(pos_fmtstr, pos_fmtargs) \
89 __attribute__ ((format (printf, (pos_fmtstr), (pos_fmtargs))))
90 #endif
92 #ifndef __packed
93 # define __packed __attribute__((packed))
94 #endif
96 #ifndef round_up
97 # define round_up(x, alignment) (((x) + (alignment) - 1) & ~((alignment) - 1))
98 #endif
100 #ifndef round_up_cacheline
101 # define round_up_cacheline(x) round_up((x), CO_CACHE_LINE_SIZE)
102 #endif
104 #ifndef likely
105 # define likely(x) __builtin_expect(!!(x), 1)
106 #endif
108 #ifndef unlikely
109 # define unlikely(x) __builtin_expect(!!(x), 0)
110 #endif
112 #ifndef constant
113 # define constant(x) __builtin_constant_p(x)
114 #endif
116 #ifndef fmemset
117 # define fmemset __builtin_memset
118 #endif
120 #ifndef fmemcpy
121 # define fmemcpy __builtin_memcpy
122 #endif
124 #ifndef atomic_cmp_swp
125 # define atomic_cmp_swp __sync_val_compare_and_swap
126 #endif
128 #ifndef __deprecated
129 # define __deprecated /* unimplemented */
130 #endif
132 #ifndef unreachable
133 # define unreachable() do { } while (1)
134 #endif
136 #ifndef __maybe_unused
137 # define __maybe_unused __attribute__ ((__unused__))
138 #endif
140 #ifndef noinline
141 # define noinline __attribute__((noinline))
142 #endif
144 #ifndef __always_inline
145 # define __always_inline inline
146 #endif
148 #ifndef __hidden
149 # define __hidden __attribute__((visibility("hidden")))
150 #endif
152 #ifndef __pure
153 # define __pure __attribute__ ((pure))
154 #endif
156 #ifndef __force
157 # define __force /* unimplemented */
158 #endif
160 #ifndef force_cast
161 # define force_cast(type, arg) ((type) (arg))
162 #endif
164 #ifndef access_once
165 # define access_once(x) (*(volatile typeof(x) *) &(x))
166 #endif
168 #ifndef max
169 # define max(a, b) \
170 ({ \
171 typeof (a) _a = (a); \
172 typeof (b) _b = (b); \
173 _a > _b ? _a : _b; \
175 #endif /* max */
177 #ifndef min
178 # define min(a, b) \
179 ({ \
180 typeof (a) _a = (a); \
181 typeof (b) _b = (b); \
182 _a < _b ? _a : _b; \
184 #endif /* min */
186 #ifndef ispow2
187 # define ispow2(x) ({ !!((x) && !((x) & ((x) - 1))); })
188 #endif
190 #ifndef offsetof
191 # define offsetof(type, member) ((size_t) &((type *) 0)->member)
192 #endif
194 #ifndef container_of
195 # define container_of(ptr, type, member) \
196 ({ \
197 const typeof(((type *) 0)->member) * __mptr = (ptr); \
198 (type *) ((char *) __mptr - offsetof(type, member)); \
200 #endif
202 #ifndef array_size
203 # define array_size(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
204 #endif
206 #ifndef __must_be_array
207 # define __must_be_array(x) \
208 build_bug_on_zero(__builtin_types_compatible_p(typeof(x), \
209 typeof(&x[0])))
210 #endif
212 #ifndef build_bug_on_zero
213 # define build_bug_on_zero(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
214 #endif
216 #ifndef bug_on
217 # define bug_on(cond) assert(!(cond))
218 #endif
220 #ifndef bug
221 # define bug() assert(0)
222 #endif
224 #define PAGE_SIZE (getpagesize())
225 #define PAGE_MASK (~(PAGE_SIZE - 1))
226 #define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)
228 #if __BYTE_ORDER == __LITTLE_ENDIAN
229 static inline uint64_t htonll(uint64_t x)
231 return bswap_64(x);
234 static inline uint64_t ntohll(uint64_t x)
236 return bswap_64(x);
238 #elif __BYTE_ORDER == __BIG_ENDIAN
239 static inline uint64_t htonll(uint64_t x)
241 return x;
244 static inline uint64_t ntohll(uint64_t x)
246 return x;
248 #else
249 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
250 #endif
251 #ifndef ___constant_swab16
252 # define ___constant_swab16(x) ((__u16)( \
253 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
254 (((__u16)(x) & (__u16)0xff00U) >> 8)))
255 #endif
256 #ifndef ___constant_swab32
257 # define ___constant_swab32(x) ((__u32)( \
258 (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
259 (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
260 (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
261 (((__u32)(x) & (__u32)0xff000000UL) >> 24)))
262 #endif
263 #if __BYTE_ORDER == __LITTLE_ENDIAN
264 static inline u16 cpu_to_be16(u16 val)
266 return bswap_16(val);
269 static inline u32 cpu_to_be32(u32 val)
271 return bswap_32(val);
274 static inline u64 cpu_to_be64(u64 val)
276 return bswap_64(val);
279 static inline u16 cpu_to_le16(u16 val)
281 return val;
284 static inline u32 cpu_to_le32(u32 val)
286 return val;
289 static inline u64 cpu_to_le64(u64 val)
291 return val;
294 # ifndef __constant_htonl
295 # define __constant_htonl(x) ((__force __be32)___constant_swab32((x)))
296 # endif
297 # ifndef __constant_ntohl
298 # define __constant_ntohl(x) ___constant_swab32((__force __be32)(x))
299 # endif
300 # ifndef __constant_htons
301 # define __constant_htons(x) ((__force __be16)___constant_swab16((x)))
302 # endif
303 # ifndef __constant_ntohs
304 # define __constant_ntohs(x) ___constant_swab16((__force __be16)(x))
305 # endif
306 #elif __BYTE_ORDER == __BIG_ENDIAN
307 static inline u16 cpu_to_be16(u16 val)
309 return val;
312 static inline u32 cpu_to_be32(u32 val)
314 return val;
317 static inline u64 cpu_to_be64(u64 val)
319 return val;
322 static inline u16 cpu_to_le16(u16 val)
324 return bswap_16(val);
327 static inline u32 cpu_to_le32(u32 val)
329 return bswap_32(val);
332 static inline u64 cpu_to_le64(u64 val)
334 return bswap_64(val);
337 # ifndef __constant_htonl
338 # define __constant_htonl(x) ((__force __be32)(__u32)(x))
339 # endif
340 # ifndef __constant_ntohl
341 # define __constant_ntohl(x) ((__force __u32)(__be32)(x))
342 # endif
343 # ifndef __constant_htons
344 # define __constant_htons(x) ((__force __be16)(__u16)(x))
345 # endif
346 # ifndef __constant_ntohs
347 # define __constant_ntohs(x) ((__force __u16)(__be16)(x))
348 # endif
349 #else
350 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
351 #endif
353 #define le64_to_cpu cpu_to_le64
354 #define le32_to_cpu cpu_to_le32
355 #define le16_to_cpu cpu_to_le16
356 #define be64_to_cpu cpu_to_be64
357 #define be32_to_cpu cpu_to_be32
358 #define be16_to_cpu cpu_to_be16
360 #undef memset
361 #undef memcpy
363 #define memset fmemset
364 #define memcpy fmemcpy
366 #endif /* BUILT_IN_H */