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 
122 #if defined(__x86_64__) || defined(__i386__)
123 #define RTE_INIT_PRIO(func, prio) \
124 static void \
125  __attribute__((constructor(prio), used)) \
126  __attribute__((target ("sse2"))) \
127  __attribute__((target ("no-sse3"))) \
128  __attribute__((target ("no-sse4"))) \
129  func(void)
130 #else
131 #define RTE_INIT_PRIO(func, prio) \
132 static void \
133  __attribute__((constructor(prio), used)) \
134  func(void)
135 #endif
136 
144 #define RTE_INIT(func) \
145  RTE_INIT_PRIO(func, 65535)
146 
150 #define __rte_always_inline inline __attribute__((always_inline))
151 
155 #define __rte_noinline __attribute__((noinline))
156 
157 /*********** Macros for pointer arithmetic ********/
158 
162 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
163 
167 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
168 
174 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
175 
176 /*********** Macros/static functions for doing alignment ********/
177 
178 
185 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
186  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
187 
194 #define RTE_ALIGN_FLOOR(val, align) \
195  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
196 
203 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
204  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
205 
212 #define RTE_ALIGN_CEIL(val, align) \
213  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
214 
222 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
223 
231 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
232 
244 static inline int
245 rte_is_aligned(void *ptr, unsigned align)
246 {
247  return RTE_PTR_ALIGN(ptr, align) == ptr;
248 }
249 
250 /*********** Macros for compile type checks ********/
251 
255 #ifndef __OPTIMIZE__
256 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
257 #else
258 extern int RTE_BUILD_BUG_ON_detected_error;
259 #define RTE_BUILD_BUG_ON(condition) do { \
260  ((void)sizeof(char[1 - 2*!!(condition)])); \
261  if (condition) \
262  RTE_BUILD_BUG_ON_detected_error = 1; \
263 } while(0)
264 #endif
265 
266 /*********** Macros to work with powers of 2 ********/
267 
274 static inline int
275 rte_is_power_of_2(uint32_t n)
276 {
277  return n && !(n & (n - 1));
278 }
279 
289 static inline uint32_t
290 rte_align32pow2(uint32_t x)
291 {
292  x--;
293  x |= x >> 1;
294  x |= x >> 2;
295  x |= x >> 4;
296  x |= x >> 8;
297  x |= x >> 16;
298 
299  return x + 1;
300 }
301 
311 static inline uint64_t
312 rte_align64pow2(uint64_t v)
313 {
314  v--;
315  v |= v >> 1;
316  v |= v >> 2;
317  v |= v >> 4;
318  v |= v >> 8;
319  v |= v >> 16;
320  v |= v >> 32;
321 
322  return v + 1;
323 }
324 
325 /*********** Macros for calculating min and max **********/
326 
330 #define RTE_MIN(a, b) \
331  __extension__ ({ \
332  typeof (a) _a = (a); \
333  typeof (b) _b = (b); \
334  _a < _b ? _a : _b; \
335  })
336 
340 #define RTE_MAX(a, b) \
341  __extension__ ({ \
342  typeof (a) _a = (a); \
343  typeof (b) _b = (b); \
344  _a > _b ? _a : _b; \
345  })
346 
347 /*********** Other general functions / macros ********/
348 
360 static inline uint32_t
361 rte_bsf32(uint32_t v)
362 {
363  return __builtin_ctz(v);
364 }
365 
374 static inline uint32_t
375 rte_log2_u32(uint32_t v)
376 {
377  if (v == 0)
378  return 0;
379  v = rte_align32pow2(v);
380  return rte_bsf32(v);
381 }
382 
383 #ifndef offsetof
384 
385 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
386 #endif
387 
402 #ifndef container_of
403 #define container_of(ptr, type, member) __extension__ ({ \
404  const typeof(((type *)0)->member) *_ptr = (ptr); \
405  __attribute__((unused)) type *_target_ptr = \
406  (type *)(ptr); \
407  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
408  })
409 #endif
410 
411 #define _RTE_STR(x) #x
412 
413 #define RTE_STR(x) _RTE_STR(x)
414 
420 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
421 #define RTE_FMT_HEAD(fmt, ...) fmt
422 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
423 
425 #define RTE_LEN2MASK(ln, tp) \
426  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
427 
429 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
430 
445 static inline uint64_t
446 rte_str_to_size(const char *str)
447 {
448  char *endptr;
449  unsigned long long size;
450 
451  while (isspace((int)*str))
452  str++;
453  if (*str == '-')
454  return 0;
455 
456  errno = 0;
457  size = strtoull(str, &endptr, 0);
458  if (errno)
459  return 0;
460 
461  if (*endptr == ' ')
462  endptr++; /* allow 1 space gap */
463 
464  switch (*endptr){
465  case 'G': case 'g': size *= 1024; /* fall-through */
466  case 'M': case 'm': size *= 1024; /* fall-through */
467  case 'K': case 'k': size *= 1024; /* fall-through */
468  default:
469  break;
470  }
471  return size;
472 }
473 
487 void
488 rte_exit(int exit_code, const char *format, ...)
489  __attribute__((noreturn))
490  __attribute__((format(printf, 2, 3)));
491 
492 #ifdef __cplusplus
493 }
494 #endif
495 
496 #endif
static int rte_is_aligned(void *ptr, unsigned align)
Definition: rte_common.h:245
static uint64_t rte_align64pow2(uint64_t v)
Definition: rte_common.h:312
static uint32_t rte_log2_u32(uint32_t v)
Definition: rte_common.h:375
static uint32_t rte_bsf32(uint32_t v)
Definition: rte_common.h:361
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:290
uint64_t unaligned_uint64_t
Definition: rte_common.h:82
#define RTE_PTR_ALIGN(ptr, align)
Definition: rte_common.h:222
static int rte_is_power_of_2(uint32_t n)
Definition: rte_common.h:275
void rte_exit(int exit_code, const char *format,...)
static uint64_t rte_str_to_size(const char *str)
Definition: rte_common.h:446