DPDK  18.11.0
rte_common.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #ifndef _RTE_COMMON_H_
6 #define _RTE_COMMON_H_
7 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <limits.h>
24 
25 #include <rte_config.h>
26 
27 #ifndef typeof
28 #define typeof __typeof__
29 #endif
30 
31 #ifndef asm
32 #define asm __asm__
33 #endif
34 
36 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
37 #define RTE_STD_C11 __extension__
38 #else
39 #define RTE_STD_C11
40 #endif
41 
43 #ifdef RTE_TOOLCHAIN_GCC
44 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
45  __GNUC_PATCHLEVEL__)
46 #endif
47 
48 #ifdef RTE_ARCH_STRICT_ALIGN
49 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
50 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
51 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
52 #else
53 typedef uint64_t unaligned_uint64_t;
54 typedef uint32_t unaligned_uint32_t;
55 typedef uint16_t unaligned_uint16_t;
56 #endif
57 
61 #define __rte_aligned(a) __attribute__((__aligned__(a)))
62 
66 #define __rte_packed __attribute__((__packed__))
67 
68 /******* Macro to mark functions and fields scheduled for removal *****/
69 #define __rte_deprecated __attribute__((__deprecated__))
70 
74 #define __rte_weak __attribute__((__weak__))
75 
76 /*********** Macros to eliminate unused variable warnings ********/
77 
81 #define __rte_unused __attribute__((__unused__))
82 
87 #define RTE_SET_USED(x) (void)(x)
88 
89 #define RTE_PRIORITY_LOG 101
90 #define RTE_PRIORITY_BUS 110
91 #define RTE_PRIORITY_CLASS 120
92 #define RTE_PRIORITY_LAST 65535
93 
94 #define RTE_PRIO(prio) \
95  RTE_PRIORITY_ ## prio
96 
106 #if defined(__x86_64__) || defined(__i386__)
107 #define RTE_INIT_PRIO(func, prio) \
108 static void \
109  __attribute__((constructor(RTE_PRIO(prio)), used)) \
110  __attribute__((target ("sse2"))) \
111  __attribute__((target ("no-sse3"))) \
112  __attribute__((target ("no-sse4"))) \
113  func(void)
114 #else
115 #define RTE_INIT_PRIO(func, prio) \
116 static void \
117  __attribute__((constructor(RTE_PRIO(prio)), used)) \
118  func(void)
119 #endif
120 
121 
130 #define RTE_INIT(func) \
131  RTE_INIT_PRIO(func, LAST)
132 
142 #define RTE_FINI_PRIO(func, prio) \
143 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
144 
153 #define RTE_FINI(func) \
154  RTE_FINI_PRIO(func, LAST)
155 
159 #define __rte_always_inline inline __attribute__((always_inline))
160 
164 #define __rte_noinline __attribute__((noinline))
165 
166 /*********** Macros for pointer arithmetic ********/
167 
171 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
172 
176 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
177 
183 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
184 
188 #define RTE_CAST_FIELD(var, field, type) \
189  (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
190 
191 /*********** Macros/static functions for doing alignment ********/
192 
193 
200 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
201  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
202 
209 #define RTE_ALIGN_FLOOR(val, align) \
210  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
211 
218 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
219  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
220 
227 #define RTE_ALIGN_CEIL(val, align) \
228  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
229 
237 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
238 
246 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
247 
253 #define RTE_ALIGN_MUL_CEIL(v, mul) \
254  (((v + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
255 
261 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
262  ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
263 
275 static inline int
276 rte_is_aligned(void *ptr, unsigned align)
277 {
278  return RTE_PTR_ALIGN(ptr, align) == ptr;
279 }
280 
281 /*********** Macros for compile type checks ********/
282 
286 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
287 
298 static inline uint32_t
299 rte_combine32ms1b(register uint32_t x)
300 {
301  x |= x >> 1;
302  x |= x >> 2;
303  x |= x >> 4;
304  x |= x >> 8;
305  x |= x >> 16;
306 
307  return x;
308 }
309 
320 static inline uint64_t
321 rte_combine64ms1b(register uint64_t v)
322 {
323  v |= v >> 1;
324  v |= v >> 2;
325  v |= v >> 4;
326  v |= v >> 8;
327  v |= v >> 16;
328  v |= v >> 32;
329 
330  return v;
331 }
332 
333 /*********** Macros to work with powers of 2 ********/
334 
338 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
339 
346 static inline int
347 rte_is_power_of_2(uint32_t n)
348 {
349  return n && !(n & (n - 1));
350 }
351 
361 static inline uint32_t
362 rte_align32pow2(uint32_t x)
363 {
364  x--;
365  x = rte_combine32ms1b(x);
366 
367  return x + 1;
368 }
369 
379 static inline uint32_t
381 {
382  x = rte_combine32ms1b(x);
383 
384  return x - (x >> 1);
385 }
386 
396 static inline uint64_t
397 rte_align64pow2(uint64_t v)
398 {
399  v--;
400  v = rte_combine64ms1b(v);
401 
402  return v + 1;
403 }
404 
414 static inline uint64_t
416 {
417  v = rte_combine64ms1b(v);
418 
419  return v - (v >> 1);
420 }
421 
422 /*********** Macros for calculating min and max **********/
423 
427 #define RTE_MIN(a, b) \
428  __extension__ ({ \
429  typeof (a) _a = (a); \
430  typeof (b) _b = (b); \
431  _a < _b ? _a : _b; \
432  })
433 
437 #define RTE_MAX(a, b) \
438  __extension__ ({ \
439  typeof (a) _a = (a); \
440  typeof (b) _b = (b); \
441  _a > _b ? _a : _b; \
442  })
443 
444 /*********** Other general functions / macros ********/
445 
457 static inline uint32_t
458 rte_bsf32(uint32_t v)
459 {
460  return (uint32_t)__builtin_ctz(v);
461 }
462 
471 static inline uint32_t
472 rte_log2_u32(uint32_t v)
473 {
474  if (v == 0)
475  return 0;
476  v = rte_align32pow2(v);
477  return rte_bsf32(v);
478 }
479 
480 
492 static inline int
493 rte_fls_u32(uint32_t x)
494 {
495  return (x == 0) ? 0 : 32 - __builtin_clz(x);
496 }
497 
512 static inline int
513 rte_bsf64_safe(uint64_t v, uint32_t *pos)
514 {
515  if (v == 0)
516  return 0;
517 
518  *pos = __builtin_ctzll(v);
519  return 1;
520 }
521 
522 #ifndef offsetof
523 
524 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
525 #endif
526 
541 #ifndef container_of
542 #define container_of(ptr, type, member) __extension__ ({ \
543  const typeof(((type *)0)->member) *_ptr = (ptr); \
544  __attribute__((unused)) type *_target_ptr = \
545  (type *)(ptr); \
546  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
547  })
548 #endif
549 
550 #define _RTE_STR(x) #x
551 
552 #define RTE_STR(x) _RTE_STR(x)
553 
559 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
560 #define RTE_FMT_HEAD(fmt, ...) fmt
561 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
562 
564 #define RTE_LEN2MASK(ln, tp) \
565  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
566 
568 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
569 
584 static inline uint64_t
585 rte_str_to_size(const char *str)
586 {
587  char *endptr;
588  unsigned long long size;
589 
590  while (isspace((int)*str))
591  str++;
592  if (*str == '-')
593  return 0;
594 
595  errno = 0;
596  size = strtoull(str, &endptr, 0);
597  if (errno)
598  return 0;
599 
600  if (*endptr == ' ')
601  endptr++; /* allow 1 space gap */
602 
603  switch (*endptr){
604  case 'G': case 'g': size *= 1024; /* fall-through */
605  case 'M': case 'm': size *= 1024; /* fall-through */
606  case 'K': case 'k': size *= 1024; /* fall-through */
607  default:
608  break;
609  }
610  return size;
611 }
612 
626 void
627 rte_exit(int exit_code, const char *format, ...)
628  __attribute__((noreturn))
629  __attribute__((format(printf, 2, 3)));
630 
631 #ifdef __cplusplus
632 }
633 #endif
634 
635 #endif
static int rte_is_aligned(void *ptr, unsigned align)
Definition: rte_common.h:276
static int rte_fls_u32(uint32_t x)
Definition: rte_common.h:493
static uint64_t rte_combine64ms1b(register uint64_t v)
Definition: rte_common.h:321
static uint64_t rte_align64pow2(uint64_t v)
Definition: rte_common.h:397
static uint32_t rte_log2_u32(uint32_t v)
Definition: rte_common.h:472
static uint32_t rte_bsf32(uint32_t v)
Definition: rte_common.h:458
static uint64_t rte_align64prevpow2(uint64_t v)
Definition: rte_common.h:415
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:362
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:513
uint64_t unaligned_uint64_t
Definition: rte_common.h:53
#define RTE_PTR_ALIGN(ptr, align)
Definition: rte_common.h:237
static int rte_is_power_of_2(uint32_t n)
Definition: rte_common.h:347
void rte_exit(int exit_code, const char *format,...)
static uint32_t rte_align32prevpow2(uint32_t x)
Definition: rte_common.h:380
static uint64_t rte_str_to_size(const char *str)
Definition: rte_common.h:585
static uint32_t rte_combine32ms1b(register uint32_t x)
Definition: rte_common.h:299