docs: add security onion
[netsniff-ng.git] / src / built_in.h
blob8ab36e6ebe8663a06ea48cc3609fbb06dbc24f37
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 typedef uint64_t u64;
18 typedef uint32_t u32;
19 typedef uint16_t u16;
20 typedef uint8_t u8;
22 /* /sys/devices/system/cpu/cpuX/cache/indexX/coherency_line_size */
24 #if defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || \
25 defined(_M_X64) || defined(__amd64)
26 # define CO_IN_CACHE_SHIFT 7
27 #elif defined(__i386__) || defined(__x86__) || defined(__X86__) || \
28 defined(_M_IX86) || defined(__i386)
29 # define CO_IN_CACHE_SHIFT 7
30 #elif defined(__ia64__) || defined(__IA64__) || defined(__M_IA64)
31 # define CO_IN_CACHE_SHIFT 6
32 #elif defined(__SPU__)
33 # define CO_IN_CACHE_SHIFT 7
34 #elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
35 defined(_ARCH_PPC64)
36 # define CO_IN_CACHE_SHIFT 8
37 #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || \
38 defined(_ARCH_PPC)
39 # define CO_IN_CACHE_SHIFT 7
40 #elif defined(__sparcv9__) || defined(__sparcv9)
41 # define CO_IN_CACHE_SHIFT 6
42 #elif defined(__sparc_v8__)
43 # define CO_IN_CACHE_SHIFT 5
44 #elif defined(__sparc__) || defined(__sparc)
45 # define CO_IN_CACHE_SHIFT 5
46 #elif defined(__ARM_EABI__)
47 # define CO_IN_CACHE_SHIFT 5
48 #elif defined(__arm__)
49 # define CO_IN_CACHE_SHIFT 5
50 #elif defined(__mips__) || defined(__mips) || defined(__MIPS__)
51 # if defined(_ABIO32)
52 # define CO_IN_CACHE_SHIFT 5
53 # elif defined(_ABIN32)
54 # define CO_IN_CACHE_SHIFT 5
55 # else
56 # define CO_IN_CACHE_SHIFT 6
57 # endif
58 #else
59 # define CO_IN_CACHE_SHIFT 5
60 #endif
62 #ifndef CO_CACHE_LINE_SIZE
63 # define CO_CACHE_LINE_SIZE (1 << CO_IN_CACHE_SHIFT)
64 #endif
66 #ifndef __aligned_16
67 # define __aligned_16 __attribute__((aligned(16)))
68 #endif
70 #ifndef __cacheline_aligned
71 # define __cacheline_aligned __attribute__((aligned(CO_CACHE_LINE_SIZE)))
72 #endif
74 #ifndef __aligned_tpacket
75 # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
76 #endif
78 #ifndef __check_format_printf
79 # define __check_format_printf(pos_fmtstr, pos_fmtargs) \
80 __attribute__ ((format (printf, (pos_fmtstr), (pos_fmtargs))))
81 #endif
83 #ifndef __packed
84 # define __packed __attribute__((packed))
85 #endif
87 #ifndef round_up
88 # define round_up(x, alignment) (((x) + (alignment) - 1) & ~((alignment) - 1))
89 #endif
91 #ifndef round_up_cacheline
92 # define round_up_cacheline(x) round_up((x), CO_CACHE_LINE_SIZE)
93 #endif
95 #ifndef likely
96 # define likely(x) __builtin_expect(!!(x), 1)
97 #endif
99 #ifndef unlikely
100 # define unlikely(x) __builtin_expect(!!(x), 0)
101 #endif
103 #ifndef constant
104 # define constant(x) __builtin_constant_p(x)
105 #endif
107 #ifndef prefetch_rd_hi
108 # define prefetch_rd_hi(addr) __builtin_prefetch(addr, 0, 3)
109 #endif
111 #ifndef prefetch_rd_lo
112 # define prefetch_rd_lo(addr) __builtin_prefetch(addr, 0, 0)
113 #endif
115 #ifndef prefetch_wr_hi
116 # define prefetch_wr_hi(addr) __builtin_prefetch(addr, 1, 3)
117 #endif
119 #ifndef prefetch_wr_lo
120 # define prefetch_wr_lo(addr) __builtin_prefetch(addr, 1, 0)
121 #endif
123 #ifndef fmemset
124 # define fmemset __builtin_memset
125 #endif
127 #ifndef fmemcpy
128 # define fmemcpy __builtin_memcpy
129 #endif
131 #ifndef atomic_cmp_swp
132 # define atomic_cmp_swp __sync_val_compare_and_swap
133 #endif
135 #ifndef __deprecated
136 # define __deprecated /* unimplemented */
137 #endif
139 #ifndef EXPORT_SYMBOL
140 # define EXPORT_SYMBOL(x) /* empty, just for readability */
141 #endif
143 #ifndef unreachable
144 # define unreachable() do { } while (1)
145 #endif
147 #ifndef __read_mostly
148 # define __read_mostly __attribute__((__section__(".data.read_mostly")))
149 #endif
151 #ifndef __unused
152 # define __unused __attribute__ ((__unused__))
153 #endif
155 #ifndef noinline
156 # define noinline __attribute__((noinline))
157 #endif
159 #ifndef __always_inline
160 # define __always_inline inline
161 #endif
163 #ifndef __hidden
164 # define __hidden __attribute__((visibility("hidden")))
165 #endif
167 #ifndef __pure
168 # define __pure __attribute__ ((pure))
169 #endif
171 #ifndef force_cast
172 # define force_cast(type, arg) ((type) (arg))
173 #endif
175 #ifndef access_once
176 # define access_once(x) (*(volatile typeof(x) *) &(x))
177 #endif
179 #ifndef max
180 # define max(a, b) \
181 ({ \
182 typeof (a) _a = (a); \
183 typeof (b) _b = (b); \
184 _a > _b ? _a : _b; \
186 #endif /* max */
188 #ifndef min
189 # define min(a, b) \
190 ({ \
191 typeof (a) _a = (a); \
192 typeof (b) _b = (b); \
193 _a < _b ? _a : _b; \
195 #endif /* min */
197 #ifndef ispow2
198 # define ispow2(x) ({ !!((x) && !((x) & ((x) - 1))); })
199 #endif
201 #ifndef offsetof
202 # define offsetof(type, member) ((size_t) &((type *) 0)->member)
203 #endif
205 #ifndef container_of
206 # define container_of(ptr, type, member) \
207 ({ \
208 const typeof(((type *) 0)->member) * __mptr = (ptr); \
209 (type *) ((char *) __mptr - offsetof(type, member)); \
211 #endif
213 #ifndef array_size
214 # define array_size(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
215 #endif
217 #ifndef __must_be_array
218 # define __must_be_array(x) \
219 build_bug_on_zero(__builtin_types_compatible_p(typeof(x), \
220 typeof(&x[0])))
221 #endif
223 #ifndef build_bug_on_zero
224 # define build_bug_on_zero(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
225 #endif
227 #ifndef bug_on
228 # define bug_on(cond) assert(!(cond))
229 #endif
231 #ifndef bug
232 # define bug assert(0)
233 #endif
235 #define PAGE_SIZE (getpagesize())
236 #define PAGE_MASK (~(PAGE_SIZE - 1))
237 #define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)
239 #if __BYTE_ORDER == __LITTLE_ENDIAN
240 static inline uint64_t htonll(uint64_t x)
242 return bswap_64(x);
245 static inline uint64_t ntohll(uint64_t x)
247 return bswap_64(x);
249 #elif __BYTE_ORDER == __BIG_ENDIAN
250 static inline uint64_t htonll(uint64_t x)
252 return x;
255 static inline uint64_t ntohll(uint64_t x)
257 return x;
259 #else
260 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
261 #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;
293 #elif __BYTE_ORDER == __BIG_ENDIAN
294 static inline u16 cpu_to_be16(u16 val)
296 return val;
299 static inline u32 cpu_to_be32(u32 val)
301 return val;
304 static inline u64 cpu_to_be64(u64 val)
306 return val;
309 static inline u16 cpu_to_le16(u16 val)
311 return bswap_16(val);
314 static inline u32 cpu_to_le32(u32 val)
316 return bswap_32(val);
319 static inline u64 cpu_to_le64(u64 val)
321 return bswap_64(val);
323 #else
324 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
325 #endif
327 #define le64_to_cpu cpu_to_le64
328 #define le32_to_cpu cpu_to_le32
329 #define le16_to_cpu cpu_to_le16
330 #define be64_to_cpu cpu_to_be64
331 #define be32_to_cpu cpu_to_be32
332 #define be16_to_cpu cpu_to_be16
334 #undef memset
335 #undef memcpy
337 #define memset fmemset
338 #define memcpy fmemcpy
340 #endif /* BUILT_IN_H */