DPDK  17.11.4
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 #include <rte_config.h>
55 
56 #ifndef typeof
57 #define typeof __typeof__
58 #endif
59 
60 #ifndef asm
61 #define asm __asm__
62 #endif
63 
65 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
66 #define RTE_STD_C11 __extension__
67 #else
68 #define RTE_STD_C11
69 #endif
70 
72 #ifdef RTE_TOOLCHAIN_GCC
73 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
74  __GNUC_PATCHLEVEL__)
75 #endif
76 
77 #ifdef RTE_ARCH_STRICT_ALIGN
78 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
79 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
80 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
81 #else
82 typedef uint64_t unaligned_uint64_t;
83 typedef uint32_t unaligned_uint32_t;
84 typedef uint16_t unaligned_uint16_t;
85 #endif
86 
90 #define __rte_aligned(a) __attribute__((__aligned__(a)))
91 
95 #define __rte_packed __attribute__((__packed__))
96 
97 /******* Macro to mark functions and fields scheduled for removal *****/
98 #define __rte_deprecated __attribute__((__deprecated__))
99 
100 /*********** Macros to eliminate unused variable warnings ********/
101 
105 #define __rte_unused __attribute__((__unused__))
106 
111 #define RTE_SET_USED(x) (void)(x)
112 
121 #define RTE_INIT(func) \
122 static void __attribute__((constructor, used)) func(void)
123 
133 #if defined(__x86_64__) || defined(__i386__)
134 #define RTE_INIT_PRIO(func, prio) \
135 static void \
136  __attribute__((constructor(prio), used)) \
137  __attribute__((target ("sse2"))) \
138  __attribute__((target ("no-sse3"))) \
139  __attribute__((target ("no-sse4"))) \
140  func(void)
141 #else
142 #define RTE_INIT_PRIO(func, prio) \
143 static void \
144  __attribute__((constructor(prio), used)) \
145  func(void)
146 #endif
147 
151 #define __rte_always_inline inline __attribute__((always_inline))
152 
156 #define __rte_noinline __attribute__((noinline))
157 
158 /*********** Macros for pointer arithmetic ********/
159 
163 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
164 
168 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
169 
175 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
176 
177 /*********** Macros/static functions for doing alignment ********/
178 
179 
186 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
187  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
188 
195 #define RTE_ALIGN_FLOOR(val, align) \
196  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
197 
204 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
205  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
206 
213 #define RTE_ALIGN_CEIL(val, align) \
214  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
215 
223 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
224 
232 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
233 
245 static inline int
246 rte_is_aligned(void *ptr, unsigned align)
247 {
248  return RTE_PTR_ALIGN(ptr, align) == ptr;
249 }
250 
251 /*********** Macros for compile type checks ********/
252 
256 #ifndef __OPTIMIZE__
257 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
258 #else
259 extern int RTE_BUILD_BUG_ON_detected_error;
260 #define RTE_BUILD_BUG_ON(condition) do { \
261  ((void)sizeof(char[1 - 2*!!(condition)])); \
262  if (condition) \
263  RTE_BUILD_BUG_ON_detected_error = 1; \
264 } while(0)
265 #endif
266 
267 /*********** Macros to work with powers of 2 ********/
268 
275 static inline int
276 rte_is_power_of_2(uint32_t n)
277 {
278  return n && !(n & (n - 1));
279 }
280 
290 static inline uint32_t
291 rte_align32pow2(uint32_t x)
292 {
293  x--;
294  x |= x >> 1;
295  x |= x >> 2;
296  x |= x >> 4;
297  x |= x >> 8;
298  x |= x >> 16;
299 
300  return x + 1;
301 }
302 
312 static inline uint64_t
313 rte_align64pow2(uint64_t v)
314 {
315  v--;
316  v |= v >> 1;
317  v |= v >> 2;
318  v |= v >> 4;
319  v |= v >> 8;
320  v |= v >> 16;
321  v |= v >> 32;
322 
323  return v + 1;
324 }
325 
326 /*********** Macros for calculating min and max **********/
327 
331 #define RTE_MIN(a, b) \
332  __extension__ ({ \
333  typeof (a) _a = (a); \
334  typeof (b) _b = (b); \
335  _a < _b ? _a : _b; \
336  })
337 
341 #define RTE_MAX(a, b) \
342  __extension__ ({ \
343  typeof (a) _a = (a); \
344  typeof (b) _b = (b); \
345  _a > _b ? _a : _b; \
346  })
347 
348 /*********** Other general functions / macros ********/
349 
361 static inline uint32_t
362 rte_bsf32(uint32_t v)
363 {
364  return __builtin_ctz(v);
365 }
366 
375 static inline uint32_t
376 rte_log2_u32(uint32_t v)
377 {
378  if (v == 0)
379  return 0;
380  v = rte_align32pow2(v);
381  return rte_bsf32(v);
382 }
383 
384 #ifndef offsetof
385 
386 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
387 #endif
388 
403 #ifndef container_of
404 #define container_of(ptr, type, member) __extension__ ({ \
405  const typeof(((type *)0)->member) *_ptr = (ptr); \
406  __attribute__((unused)) type *_target_ptr = \
407  (type *)(ptr); \
408  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
409  })
410 #endif
411 
412 #define _RTE_STR(x) #x
413 
414 #define RTE_STR(x) _RTE_STR(x)
415 
421 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
422 #define RTE_FMT_HEAD(fmt, ...) fmt
423 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
424 
426 #define RTE_LEN2MASK(ln, tp) \
427  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
428 
430 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
431 
446 static inline uint64_t
447 rte_str_to_size(const char *str)
448 {
449  char *endptr;
450  unsigned long long size;
451 
452  while (isspace((int)*str))
453  str++;
454  if (*str == '-')
455  return 0;
456 
457  errno = 0;
458  size = strtoull(str, &endptr, 0);
459  if (errno)
460  return 0;
461 
462  if (*endptr == ' ')
463  endptr++; /* allow 1 space gap */
464 
465  switch (*endptr){
466  case 'G': case 'g': size *= 1024; /* fall-through */
467  case 'M': case 'm': size *= 1024; /* fall-through */
468  case 'K': case 'k': size *= 1024; /* fall-through */
469  default:
470  break;
471  }
472  return size;
473 }
474 
488 void
489 rte_exit(int exit_code, const char *format, ...)
490  __attribute__((noreturn))
491  __attribute__((format(printf, 2, 3)));
492 
493 #ifdef __cplusplus
494 }
495 #endif
496 
497 #endif
static int rte_is_aligned(void *ptr, unsigned align)
Definition: rte_common.h:246
static uint64_t rte_align64pow2(uint64_t v)
Definition: rte_common.h:313
static uint32_t rte_log2_u32(uint32_t v)
Definition: rte_common.h:376
static uint32_t rte_bsf32(uint32_t v)
Definition: rte_common.h:362
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:291
uint64_t unaligned_uint64_t
Definition: rte_common.h:82
#define RTE_PTR_ALIGN(ptr, align)
Definition: rte_common.h:223
static int rte_is_power_of_2(uint32_t n)
Definition: rte_common.h:276
void rte_exit(int exit_code, const char *format,...)
static uint64_t rte_str_to_size(const char *str)
Definition: rte_common.h:447