DPDK  2.1.0
rte_common.h
Go to the documentation of this file.
1 /*-
2  * BSD LICENSE
3  *
4  * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of Intel Corporation nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef _RTE_COMMON_H_
35 #define _RTE_COMMON_H_
36 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 #include <stdint.h>
49 #include <stdlib.h>
50 #include <ctype.h>
51 #include <errno.h>
52 #include <limits.h>
53 
54 #ifndef typeof
55 #define typeof __typeof__
56 #endif
57 
58 #ifndef asm
59 #define asm __asm__
60 #endif
61 
62 #ifdef RTE_ARCH_STRICT_ALIGN
63 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
64 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
65 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
66 #else
67 typedef uint64_t unaligned_uint64_t;
68 typedef uint32_t unaligned_uint32_t;
69 typedef uint16_t unaligned_uint16_t;
70 #endif
71 
72 /*********** Macros to eliminate unused variable warnings ********/
73 
77 #define __rte_unused __attribute__((__unused__))
78 
83 #define RTE_SET_USED(x) (void)(x)
84 
85 /*********** Macros for pointer arithmetic ********/
86 
90 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
91 
95 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
96 
102 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
103 
104 /*********** Macros/static functions for doing alignment ********/
105 
106 
113 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
114  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
115 
122 #define RTE_ALIGN_FLOOR(val, align) \
123  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
124 
131 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
132  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
133 
140 #define RTE_ALIGN_CEIL(val, align) \
141  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
142 
150 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
151 
159 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
160 
172 static inline int
173 rte_is_aligned(void *ptr, unsigned align)
174 {
175  return RTE_PTR_ALIGN(ptr, align) == ptr;
176 }
177 
178 /*********** Macros for compile type checks ********/
179 
183 #ifndef __OPTIMIZE__
184 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
185 #else
186 extern int RTE_BUILD_BUG_ON_detected_error;
187 #define RTE_BUILD_BUG_ON(condition) do { \
188  ((void)sizeof(char[1 - 2*!!(condition)])); \
189  if (condition) \
190  RTE_BUILD_BUG_ON_detected_error = 1; \
191 } while(0)
192 #endif
193 
194 /*********** Macros to work with powers of 2 ********/
195 
202 static inline int
203 rte_is_power_of_2(uint32_t n)
204 {
205  return n && !(n & (n - 1));
206 }
207 
217 static inline uint32_t
218 rte_align32pow2(uint32_t x)
219 {
220  x--;
221  x |= x >> 1;
222  x |= x >> 2;
223  x |= x >> 4;
224  x |= x >> 8;
225  x |= x >> 16;
226 
227  return x + 1;
228 }
229 
239 static inline uint64_t
240 rte_align64pow2(uint64_t v)
241 {
242  v--;
243  v |= v >> 1;
244  v |= v >> 2;
245  v |= v >> 4;
246  v |= v >> 8;
247  v |= v >> 16;
248  v |= v >> 32;
249 
250  return v + 1;
251 }
252 
253 /*********** Macros for calculating min and max **********/
254 
258 #define RTE_MIN(a, b) ({ \
259  typeof (a) _a = (a); \
260  typeof (b) _b = (b); \
261  _a < _b ? _a : _b; \
262  })
263 
267 #define RTE_MAX(a, b) ({ \
268  typeof (a) _a = (a); \
269  typeof (b) _b = (b); \
270  _a > _b ? _a : _b; \
271  })
272 
273 /*********** Other general functions / macros ********/
274 
275 #ifdef __SSE2__
276 #include <emmintrin.h>
280 static inline void
281 rte_pause (void)
282 {
283  _mm_pause();
284 }
285 #else
286 static inline void
287 rte_pause(void) {}
288 #endif
289 
301 static inline uint32_t
302 rte_bsf32(uint32_t v)
303 {
304  return __builtin_ctz(v);
305 }
306 
307 #ifndef offsetof
308 
309 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
310 #endif
311 
312 #define _RTE_STR(x) #x
313 
314 #define RTE_STR(x) _RTE_STR(x)
315 
317 #define RTE_LEN2MASK(ln, tp) \
318  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
319 
321 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
322 
337 static inline uint64_t
338 rte_str_to_size(const char *str)
339 {
340  char *endptr;
341  unsigned long long size;
342 
343  while (isspace((int)*str))
344  str++;
345  if (*str == '-')
346  return 0;
347 
348  errno = 0;
349  size = strtoull(str, &endptr, 0);
350  if (errno)
351  return 0;
352 
353  if (*endptr == ' ')
354  endptr++; /* allow 1 space gap */
355 
356  switch (*endptr){
357  case 'G': case 'g': size *= 1024; /* fall-through */
358  case 'M': case 'm': size *= 1024; /* fall-through */
359  case 'K': case 'k': size *= 1024; /* fall-through */
360  default:
361  break;
362  }
363  return size;
364 }
365 
379 void
380 rte_exit(int exit_code, const char *format, ...)
381  __attribute__((noreturn))
382  __attribute__((format(printf, 2, 3)));
383 
384 #ifdef __cplusplus
385 }
386 #endif
387 
388 #endif