proto_ipv6_mobility_hdr.h: Headerfixing
[netsniff-ng.git] / src / built_in.h
blobe1a46c84c0234d023bff8baac95a3a947cad9ddf
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009-2012 Daniel Borkmann.
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 <stdint.h>
17 /* /sys/devices/system/cpu/cpuX/cache/indexX/coherency_line_size */
19 #if defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || \
20 defined(_M_X64) || defined(__amd64)
21 # define CO_IN_CACHE_SHIFT 7
22 #elif defined(__i386__) || defined(__x86__) || defined(__X86__) || \
23 defined(_M_IX86) || defined(__i386)
24 # define CO_IN_CACHE_SHIFT 7
25 #elif defined(__ia64__) || defined(__IA64__) || defined(__M_IA64)
26 # define CO_IN_CACHE_SHIFT 6
27 #elif defined(__SPU__)
28 # define CO_IN_CACHE_SHIFT 7
29 #elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
30 defined(_ARCH_PPC64)
31 # define CO_IN_CACHE_SHIFT 8
32 #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || \
33 defined(_ARCH_PPC)
34 # define CO_IN_CACHE_SHIFT 7
35 #elif defined(__sparcv9__) || defined(__sparcv9)
36 # define CO_IN_CACHE_SHIFT 6
37 #elif defined(__sparc_v8__)
38 # define CO_IN_CACHE_SHIFT 5
39 #elif defined(__sparc__) || defined(__sparc)
40 # define CO_IN_CACHE_SHIFT 5
41 #elif defined(__ARM_EABI__)
42 # define CO_IN_CACHE_SHIFT 5
43 #elif defined(__arm__)
44 # define CO_IN_CACHE_SHIFT 5
45 #elif defined(__mips__) || defined(__mips) || defined(__MIPS__)
46 # if defined(_ABIO32)
47 # define CO_IN_CACHE_SHIFT 5
48 # elif defined(_ABIN32)
49 # define CO_IN_CACHE_SHIFT 5
50 # else
51 # define CO_IN_CACHE_SHIFT 6
52 # endif
53 #else
54 # define CO_IN_CACHE_SHIFT 5
55 #endif
57 #ifndef CO_CACHE_LINE_SIZE
58 # define CO_CACHE_LINE_SIZE (1 << CO_IN_CACHE_SHIFT)
59 #endif
61 #ifndef __aligned_16
62 # define __aligned_16 __attribute__((aligned(16)))
63 #endif
65 #ifndef __cacheline_aligned
66 # define __cacheline_aligned __attribute__((aligned(CO_CACHE_LINE_SIZE)))
67 #endif
69 #ifndef __aligned_tpacket
70 # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
71 #endif
73 #ifndef __packed
74 # define __packed __attribute__((packed))
75 #endif
77 #ifndef round_up
78 # define round_up(x, alignment) (((x) + (alignment) - 1) & ~((alignment) - 1))
79 #endif
81 #ifndef round_up_cacheline
82 # define round_up_cacheline(x) round_up((x), CO_CACHE_LINE_SIZE)
83 #endif
85 #ifndef likely
86 # define likely(x) __builtin_expect(!!(x), 1)
87 #endif
89 #ifndef unlikely
90 # define unlikely(x) __builtin_expect(!!(x), 0)
91 #endif
93 #ifndef prefetch_rd_hi
94 # define prefetch_rd_hi(addr) __builtin_prefetch(addr, 0, 3)
95 #endif
97 #ifndef prefetch_rd_lo
98 # define prefetch_rd_lo(addr) __builtin_prefetch(addr, 0, 0)
99 #endif
101 #ifndef prefetch_wr_hi
102 # define prefetch_wr_hi(addr) __builtin_prefetch(addr, 1, 3)
103 #endif
105 #ifndef prefetch_wr_lo
106 # define prefetch_wr_lo(addr) __builtin_prefetch(addr, 1, 0)
107 #endif
109 #ifndef fmemset
110 # define fmemset __builtin_memset
111 #endif
113 #ifndef fmemcpy
114 # define fmemcpy __builtin_memcpy
115 #endif
117 #ifndef atomic_cmp_swp
118 # define atomic_cmp_swp __sync_val_compare_and_swap
119 #endif
121 #ifndef __deprecated
122 # define __deprecated /* unimplemented */
123 #endif
125 #ifndef unreachable
126 # define unreachable() do { } while (1)
127 #endif
129 #ifndef __read_mostly
130 # define __read_mostly __attribute__((__section__(".data.read_mostly")))
131 #endif
133 #ifndef noinline
134 # define noinline __attribute__((noinline))
135 #endif
137 #ifndef __always_inline
138 # define __always_inline inline
139 #endif
141 #ifndef __hidden
142 # define __hidden __attribute__((visibility("hidden")))
143 #endif
145 #ifndef __pure
146 # define __pure __attribute__ ((pure))
147 #endif
149 #ifndef force_cast
150 # define force_cast(type, arg) ((type) (arg))
151 #endif
153 #ifndef access_once
154 # define access_once(x) (*(volatile typeof(x) *) &(x))
155 #endif
157 #ifndef max
158 # define max(a, b) \
159 ({ \
160 typeof (a) _a = (a); \
161 typeof (b) _b = (b); \
162 _a > _b ? _a : _b; \
164 #endif /* max */
166 #ifndef min
167 # define min(a, b) \
168 ({ \
169 typeof (a) _a = (a); \
170 typeof (b) _b = (b); \
171 _a < _b ? _a : _b; \
173 #endif /* min */
175 #ifndef ispow2
176 # define ispow2(x) ({ !!((x) && !((x) & ((x) - 1))); })
177 #endif
179 #ifndef offsetof
180 # define offsetof(type, member) ((size_t) &((type *) 0)->member)
181 #endif
183 #ifndef container_of
184 # define container_of(ptr, type, member) \
185 ({ \
186 const typeof(((type *) 0)->member) * __mptr = (ptr); \
187 (type *) ((char *) __mptr - offsetof(type, member)); \
189 #endif
191 #ifndef array_size
192 # define array_size(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
193 #endif
195 #ifndef __must_be_array
196 # define __must_be_array(x) \
197 build_bug_on_zero(__builtin_types_compatible_p(typeof(x), \
198 typeof(&x[0])))
199 #endif
201 #ifndef build_bug_on_zero
202 # define build_bug_on_zero(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
203 #endif
205 #ifndef bug_on
206 # define bug_on(cond) assert(!(cond))
207 #endif
209 #ifndef bug
210 # define bug assert(0)
211 #endif
213 #if __BYTE_ORDER == __LITTLE_ENDIAN
214 static inline uint64_t htonll(uint64_t x)
216 return bswap_64(x);
219 static inline uint64_t ntohll(uint64_t x)
221 return bswap_64(x);
223 #elif __BYTE_ORDER == __BIG_ENDIAN
224 static inline uint64_t htonll(uint64_t x)
226 return x;
229 static inline uint64_t ntohll(uint64_t x)
231 return x;
233 #else
234 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
235 #endif
237 #endif /* BUILT_IN_H */