proto_ipv4: don't trim length of pkt_buff
[netsniff-ng.git] / src / built_in.h
blobd5a292d3201f6f4de91bae2c2fb2d5a6476e9b7c
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 EXPORT_SYMBOL
126 # define EXPORT_SYMBOL(x) /* empty, just for readability */
127 #endif
129 #ifndef unreachable
130 # define unreachable() do { } while (1)
131 #endif
133 #ifndef __read_mostly
134 # define __read_mostly __attribute__((__section__(".data.read_mostly")))
135 #endif
137 #ifndef __unused
138 # define __unused __attribute__ ((__unused__))
139 #endif
141 #ifndef noinline
142 # define noinline __attribute__((noinline))
143 #endif
145 #ifndef __always_inline
146 # define __always_inline inline
147 #endif
149 #ifndef __hidden
150 # define __hidden __attribute__((visibility("hidden")))
151 #endif
153 #ifndef __pure
154 # define __pure __attribute__ ((pure))
155 #endif
157 #ifndef force_cast
158 # define force_cast(type, arg) ((type) (arg))
159 #endif
161 #ifndef access_once
162 # define access_once(x) (*(volatile typeof(x) *) &(x))
163 #endif
165 #ifndef max
166 # define max(a, b) \
167 ({ \
168 typeof (a) _a = (a); \
169 typeof (b) _b = (b); \
170 _a > _b ? _a : _b; \
172 #endif /* max */
174 #ifndef min
175 # define min(a, b) \
176 ({ \
177 typeof (a) _a = (a); \
178 typeof (b) _b = (b); \
179 _a < _b ? _a : _b; \
181 #endif /* min */
183 #ifndef ispow2
184 # define ispow2(x) ({ !!((x) && !((x) & ((x) - 1))); })
185 #endif
187 #ifndef offsetof
188 # define offsetof(type, member) ((size_t) &((type *) 0)->member)
189 #endif
191 #ifndef container_of
192 # define container_of(ptr, type, member) \
193 ({ \
194 const typeof(((type *) 0)->member) * __mptr = (ptr); \
195 (type *) ((char *) __mptr - offsetof(type, member)); \
197 #endif
199 #ifndef array_size
200 # define array_size(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
201 #endif
203 #ifndef __must_be_array
204 # define __must_be_array(x) \
205 build_bug_on_zero(__builtin_types_compatible_p(typeof(x), \
206 typeof(&x[0])))
207 #endif
209 #ifndef build_bug_on_zero
210 # define build_bug_on_zero(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
211 #endif
213 #ifndef bug_on
214 # define bug_on(cond) assert(!(cond))
215 #endif
217 #ifndef bug
218 # define bug assert(0)
219 #endif
221 #if __BYTE_ORDER == __LITTLE_ENDIAN
222 static inline uint64_t htonll(uint64_t x)
224 return bswap_64(x);
227 static inline uint64_t ntohll(uint64_t x)
229 return bswap_64(x);
231 #elif __BYTE_ORDER == __BIG_ENDIAN
232 static inline uint64_t htonll(uint64_t x)
234 return x;
237 static inline uint64_t ntohll(uint64_t x)
239 return x;
241 #else
242 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
243 #endif
245 typedef uint64_t u64;
246 typedef uint32_t u32;
247 typedef uint16_t u16;
248 typedef uint8_t u8;
250 #endif /* BUILT_IN_H */