bpf: use Linux' define of BPF_MAXINSNS
[netsniff-ng.git] / built_in.h
blob1cca2d78844c506bbc84b4a326dc38aa197fa870
1 #ifndef BUILT_IN_H
2 #define BUILT_IN_H
4 /* Parts taken from the Linux kernel, GPL, version 2. */
6 #include <linux/if_packet.h>
7 #include <assert.h>
8 #include <endian.h>
9 #include <byteswap.h>
10 #include <asm/byteorder.h>
11 #include <stdint.h>
13 typedef uint64_t u64;
14 typedef uint32_t u32;
15 typedef uint16_t u16;
16 typedef uint8_t u8;
18 #ifndef CO_CACHE_LINE_SIZE
19 # define CO_CACHE_LINE_SIZE (1 << CO_IN_CACHE_SHIFT)
20 #endif
22 #ifndef __aligned_16
23 # define __aligned_16 __attribute__((aligned(16)))
24 #endif
26 #ifndef __cacheline_aligned
27 # define __cacheline_aligned __attribute__((aligned(CO_CACHE_LINE_SIZE)))
28 #endif
30 #ifndef __aligned_tpacket
31 # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
32 #endif
34 #ifndef __align_tpacket
35 # define __align_tpacket(x) __attribute__((aligned(TPACKET_ALIGN(x))))
36 #endif
38 #ifndef __check_format_printf
39 # define __check_format_printf(pos_fmtstr, pos_fmtargs) \
40 __attribute__ ((format (printf, (pos_fmtstr), (pos_fmtargs))))
41 #endif
43 #ifndef __packed
44 # define __packed __attribute__((packed))
45 #endif
47 #ifndef round_up
48 # define round_up(x, alignment) (((x) + (alignment) - 1) & ~((alignment) - 1))
49 #endif
51 #ifndef round_up_cacheline
52 # define round_up_cacheline(x) round_up((x), CO_CACHE_LINE_SIZE)
53 #endif
55 #ifndef likely
56 # define likely(x) __builtin_expect(!!(x), 1)
57 #endif
59 #ifndef unlikely
60 # define unlikely(x) __builtin_expect(!!(x), 0)
61 #endif
63 #ifndef constant
64 # define constant(x) __builtin_constant_p(x)
65 #endif
67 #ifndef fmemset
68 # define fmemset __builtin_memset
69 #endif
71 #ifndef fmemcpy
72 # define fmemcpy __builtin_memcpy
73 #endif
75 #ifndef __maybe_unused
76 # define __maybe_unused __attribute__ ((__unused__))
77 #endif
79 #ifndef noinline
80 # define noinline __attribute__((noinline))
81 #endif
83 #ifndef __noreturn
84 # define __noreturn __attribute__((noreturn))
85 #endif
87 #ifndef __hidden
88 # define __hidden __attribute__((visibility("hidden")))
89 #endif
91 #ifndef __pure
92 # define __pure __attribute__ ((pure))
93 #endif
95 #ifndef __force
96 # define __force /* unimplemented */
97 #endif
99 #ifndef force_cast
100 # define force_cast(type, arg) ((type) (arg))
101 #endif
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 min
113 # define min(a, b) \
114 ({ \
115 typeof (a) _a = (a); \
116 typeof (b) _b = (b); \
117 _a < _b ? _a : _b; \
119 #endif /* min */
121 #ifndef ispow2
122 # define ispow2(x) ({ !!((x) && !((x) & ((x) - 1))); })
123 #endif
125 #ifndef offsetof
126 # define offsetof(type, member) ((size_t) &((type *) 0)->member)
127 #endif
129 #ifndef container_of
130 # define container_of(ptr, type, member) \
131 ({ \
132 const typeof(((type *) 0)->member) * __mptr = (ptr); \
133 (type *) ((char *) __mptr - offsetof(type, member)); \
135 #endif
137 #ifndef array_size
138 # define array_size(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
139 #endif
141 #ifndef __must_be_array
142 # define __must_be_array(x) \
143 build_bug_on_zero(__builtin_types_compatible_p(typeof(x), \
144 typeof(&x[0])))
145 #endif
147 #ifndef build_bug_on_zero
148 # define build_bug_on_zero(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
149 #endif
151 #ifndef build_bug_on
152 # define build_bug_on(e) ((void)sizeof(char[1 - 2*!!(e)]))
153 #endif
155 #ifndef bug_on
156 # define bug_on(cond) assert(!(cond))
157 #endif
159 #ifndef bug
160 # define bug() assert(0)
161 #endif
163 #define PAGE_SIZE (getpagesize())
164 #define PAGE_MASK (~(PAGE_SIZE - 1))
165 #define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)
167 #if __BYTE_ORDER == __LITTLE_ENDIAN
168 static inline uint64_t htonll(uint64_t x)
170 return bswap_64(x);
173 static inline uint64_t ntohll(uint64_t x)
175 return bswap_64(x);
177 #elif __BYTE_ORDER == __BIG_ENDIAN
178 static inline uint64_t htonll(uint64_t x)
180 return x;
183 static inline uint64_t ntohll(uint64_t x)
185 return x;
187 #else
188 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
189 #endif
190 #ifndef ___constant_swab16
191 # define ___constant_swab16(x) ((__u16)( \
192 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
193 (((__u16)(x) & (__u16)0xff00U) >> 8)))
194 #endif
195 #ifndef ___constant_swab32
196 # define ___constant_swab32(x) ((__u32)( \
197 (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
198 (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
199 (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
200 (((__u32)(x) & (__u32)0xff000000UL) >> 24)))
201 #endif
202 #if __BYTE_ORDER == __LITTLE_ENDIAN
203 static inline u16 cpu_to_be16(u16 val)
205 return bswap_16(val);
208 static inline u32 cpu_to_be32(u32 val)
210 return bswap_32(val);
213 static inline u64 cpu_to_be64(u64 val)
215 return bswap_64(val);
218 static inline u16 cpu_to_le16(u16 val)
220 return val;
223 static inline u32 cpu_to_le32(u32 val)
225 return val;
228 static inline u64 cpu_to_le64(u64 val)
230 return val;
233 # ifndef __constant_htonl
234 # define __constant_htonl(x) ((__force __be32)___constant_swab32((x)))
235 # endif
236 # ifndef __constant_ntohl
237 # define __constant_ntohl(x) ___constant_swab32((__force __be32)(x))
238 # endif
239 # ifndef __constant_htons
240 # define __constant_htons(x) ((__force __be16)___constant_swab16((x)))
241 # endif
242 # ifndef __constant_ntohs
243 # define __constant_ntohs(x) ___constant_swab16((__force __be16)(x))
244 # endif
245 #elif __BYTE_ORDER == __BIG_ENDIAN
246 static inline u16 cpu_to_be16(u16 val)
248 return val;
251 static inline u32 cpu_to_be32(u32 val)
253 return val;
256 static inline u64 cpu_to_be64(u64 val)
258 return val;
261 static inline u16 cpu_to_le16(u16 val)
263 return bswap_16(val);
266 static inline u32 cpu_to_le32(u32 val)
268 return bswap_32(val);
271 static inline u64 cpu_to_le64(u64 val)
273 return bswap_64(val);
276 # ifndef __constant_htonl
277 # define __constant_htonl(x) ((__force __be32)(__u32)(x))
278 # endif
279 # ifndef __constant_ntohl
280 # define __constant_ntohl(x) ((__force __u32)(__be32)(x))
281 # endif
282 # ifndef __constant_htons
283 # define __constant_htons(x) ((__force __be16)(__u16)(x))
284 # endif
285 # ifndef __constant_ntohs
286 # define __constant_ntohs(x) ((__force __u16)(__be16)(x))
287 # endif
288 #else
289 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
290 #endif
292 #define le64_to_cpu cpu_to_le64
293 #define le32_to_cpu cpu_to_le32
294 #define le16_to_cpu cpu_to_le16
295 #define be64_to_cpu cpu_to_be64
296 #define be32_to_cpu cpu_to_be32
297 #define be16_to_cpu cpu_to_be16
299 #undef memset
300 #undef memcpy
302 #define memset fmemset
303 #define memcpy fmemcpy
305 #if defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || \
306 defined(_M_X64) || defined(__amd64)
307 # define CO_IN_CACHE_SHIFT 7
308 #elif defined(__i386__) || defined(__x86__) || defined(__X86__) || \
309 defined(_M_IX86) || defined(__i386)
310 # define CO_IN_CACHE_SHIFT 7
311 #elif defined(__ia64__) || defined(__IA64__) || defined(__M_IA64)
312 # define CO_IN_CACHE_SHIFT 6
313 #elif defined(__SPU__)
314 # define CO_IN_CACHE_SHIFT 7
315 #elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
316 defined(_ARCH_PPC64)
317 # define CO_IN_CACHE_SHIFT 8
318 #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || \
319 defined(_ARCH_PPC)
320 # define CO_IN_CACHE_SHIFT 7
321 #elif defined(__sparcv9__) || defined(__sparcv9)
322 # define CO_IN_CACHE_SHIFT 6
323 #elif defined(__sparc_v8__)
324 # define CO_IN_CACHE_SHIFT 5
325 #elif defined(__sparc__) || defined(__sparc)
326 # define CO_IN_CACHE_SHIFT 5
327 #elif defined(__ARM_EABI__)
328 # define CO_IN_CACHE_SHIFT 5
329 #elif defined(__arm__)
330 # define CO_IN_CACHE_SHIFT 5
331 #elif defined(__mips__) || defined(__mips) || defined(__MIPS__)
332 # if defined(_ABIO32)
333 # define CO_IN_CACHE_SHIFT 5
334 # elif defined(_ABIN32)
335 # define CO_IN_CACHE_SHIFT 5
336 # else
337 # define CO_IN_CACHE_SHIFT 6
338 # endif
339 #else
340 # define CO_IN_CACHE_SHIFT 5
341 #endif
343 #ifndef TP_STATUS_TS_SOFTWARE
344 # define TP_STATUS_TS_SOFTWARE (1 << 29)
345 #endif
347 #ifndef TP_STATUS_TS_SYS_HARDWARE
348 # define TP_STATUS_TS_SYS_HARDWARE (1 << 30)
349 #endif
351 #ifndef TP_STATUS_TS_RAW_HARDWARE
352 # define TP_STATUS_TS_RAW_HARDWARE (1 << 31)
353 #endif
355 #ifndef POLLRDNORM
356 # define POLLRDNORM 0x0040
357 #endif
359 #ifndef POLLWRNORM
360 # define POLLWRNORM 0x0100
361 #endif
363 #ifndef POLLRDHUP
364 # define POLLRDHUP 0x2000
365 #endif
367 #endif /* BUILT_IN_H */