DPDK  19.05.0
rte_common.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2019 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 /* OS specific include */
28 #include <rte_os.h>
29 
30 #ifndef typeof
31 #define typeof __typeof__
32 #endif
33 
34 #ifndef asm
35 #define asm __asm__
36 #endif
37 
39 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
40 #define RTE_STD_C11 __extension__
41 #else
42 #define RTE_STD_C11
43 #endif
44 
46 #ifdef RTE_TOOLCHAIN_GCC
47 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
48  __GNUC_PATCHLEVEL__)
49 #endif
50 
51 #ifdef RTE_ARCH_STRICT_ALIGN
52 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
53 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
54 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
55 #else
56 typedef uint64_t unaligned_uint64_t;
57 typedef uint32_t unaligned_uint32_t;
58 typedef uint16_t unaligned_uint16_t;
59 #endif
60 
64 #define __rte_aligned(a) __attribute__((__aligned__(a)))
65 
69 #define __rte_packed __attribute__((__packed__))
70 
71 /******* Macro to mark functions and fields scheduled for removal *****/
72 #define __rte_deprecated __attribute__((__deprecated__))
73 
77 #define __rte_weak __attribute__((__weak__))
78 
79 /*********** Macros to eliminate unused variable warnings ********/
80 
84 #define __rte_unused __attribute__((__unused__))
85 
90 #define RTE_SET_USED(x) (void)(x)
91 
92 #define RTE_PRIORITY_LOG 101
93 #define RTE_PRIORITY_BUS 110
94 #define RTE_PRIORITY_CLASS 120
95 #define RTE_PRIORITY_LAST 65535
96 
97 #define RTE_PRIO(prio) \
98  RTE_PRIORITY_ ## prio
99 
109 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
110 #define RTE_INIT_PRIO(func, prio) \
111 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
112 #endif
113 
122 #define RTE_INIT(func) \
123  RTE_INIT_PRIO(func, LAST)
124 
134 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
135 #define RTE_FINI_PRIO(func, prio) \
136 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
137 #endif
138 
147 #define RTE_FINI(func) \
148  RTE_FINI_PRIO(func, LAST)
149 
153 #define __rte_always_inline inline __attribute__((always_inline))
154 
158 #define __rte_noinline __attribute__((noinline))
159 
160 /*********** Macros for pointer arithmetic ********/
161 
165 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
166 
170 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
171 
177 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
178 
182 #define RTE_CAST_FIELD(var, field, type) \
183  (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
184 
185 /*********** Macros/static functions for doing alignment ********/
186 
187 
194 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
195  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
196 
203 #define RTE_ALIGN_FLOOR(val, align) \
204  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
205 
212 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
213  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
214 
221 #define RTE_ALIGN_CEIL(val, align) \
222  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
223 
231 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
232 
240 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
241 
247 #define RTE_ALIGN_MUL_CEIL(v, mul) \
248  (((v + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
249 
255 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
256  ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
257 
263 #define RTE_ALIGN_MUL_NEAR(v, mul) \
264  ({ \
265  typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
266  typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
267  (ceil - v) > (v - floor) ? floor : ceil; \
268  })
269 
281 static inline int
282 rte_is_aligned(void *ptr, unsigned align)
283 {
284  return RTE_PTR_ALIGN(ptr, align) == ptr;
285 }
286 
287 /*********** Macros for compile type checks ********/
288 
292 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
293 
304 static inline uint32_t
305 rte_combine32ms1b(register uint32_t x)
306 {
307  x |= x >> 1;
308  x |= x >> 2;
309  x |= x >> 4;
310  x |= x >> 8;
311  x |= x >> 16;
312 
313  return x;
314 }
315 
326 static inline uint64_t
327 rte_combine64ms1b(register uint64_t v)
328 {
329  v |= v >> 1;
330  v |= v >> 2;
331  v |= v >> 4;
332  v |= v >> 8;
333  v |= v >> 16;
334  v |= v >> 32;
335 
336  return v;
337 }
338 
339 /*********** Macros to work with powers of 2 ********/
340 
344 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
345 
352 static inline int
353 rte_is_power_of_2(uint32_t n)
354 {
355  return n && !(n & (n - 1));
356 }
357 
367 static inline uint32_t
368 rte_align32pow2(uint32_t x)
369 {
370  x--;
371  x = rte_combine32ms1b(x);
372 
373  return x + 1;
374 }
375 
385 static inline uint32_t
387 {
388  x = rte_combine32ms1b(x);
389 
390  return x - (x >> 1);
391 }
392 
402 static inline uint64_t
403 rte_align64pow2(uint64_t v)
404 {
405  v--;
406  v = rte_combine64ms1b(v);
407 
408  return v + 1;
409 }
410 
420 static inline uint64_t
422 {
423  v = rte_combine64ms1b(v);
424 
425  return v - (v >> 1);
426 }
427 
428 /*********** Macros for calculating min and max **********/
429 
433 #define RTE_MIN(a, b) \
434  __extension__ ({ \
435  typeof (a) _a = (a); \
436  typeof (b) _b = (b); \
437  _a < _b ? _a : _b; \
438  })
439 
443 #define RTE_MAX(a, b) \
444  __extension__ ({ \
445  typeof (a) _a = (a); \
446  typeof (b) _b = (b); \
447  _a > _b ? _a : _b; \
448  })
449 
450 /*********** Other general functions / macros ********/
451 
463 static inline uint32_t
464 rte_bsf32(uint32_t v)
465 {
466  return (uint32_t)__builtin_ctz(v);
467 }
468 
483 static inline int
484 rte_bsf32_safe(uint64_t v, uint32_t *pos)
485 {
486  if (v == 0)
487  return 0;
488 
489  *pos = rte_bsf32(v);
490  return 1;
491 }
492 
501 static inline uint32_t
502 rte_log2_u32(uint32_t v)
503 {
504  if (v == 0)
505  return 0;
506  v = rte_align32pow2(v);
507  return rte_bsf32(v);
508 }
509 
510 
522 static inline int
523 rte_fls_u32(uint32_t x)
524 {
525  return (x == 0) ? 0 : 32 - __builtin_clz(x);
526 }
527 
539 static inline int
540 rte_bsf64(uint64_t v)
541 {
542  return (uint32_t)__builtin_ctzll(v);
543 }
544 
559 static inline int
560 rte_bsf64_safe(uint64_t v, uint32_t *pos)
561 {
562  if (v == 0)
563  return 0;
564 
565  *pos = rte_bsf64(v);
566  return 1;
567 }
568 
581 static inline int
582 rte_fls_u64(uint64_t x)
583 {
584  return (x == 0) ? 0 : 64 - __builtin_clzll(x);
585 }
586 
595 static inline uint32_t
596 rte_log2_u64(uint64_t v)
597 {
598  if (v == 0)
599  return 0;
600  v = rte_align64pow2(v);
601  /* we checked for v being 0 already, so no undefined behavior */
602  return rte_bsf64(v);
603 }
604 
605 #ifndef offsetof
606 
607 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
608 #endif
609 
624 #ifndef container_of
625 #define container_of(ptr, type, member) __extension__ ({ \
626  const typeof(((type *)0)->member) *_ptr = (ptr); \
627  __attribute__((unused)) type *_target_ptr = \
628  (type *)(ptr); \
629  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
630  })
631 #endif
632 
633 #define _RTE_STR(x) #x
634 
635 #define RTE_STR(x) _RTE_STR(x)
636 
642 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
643 #define RTE_FMT_HEAD(fmt, ...) fmt
644 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
645 
647 #define RTE_LEN2MASK(ln, tp) \
648  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
649 
651 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
652 
667 static inline uint64_t
668 rte_str_to_size(const char *str)
669 {
670  char *endptr;
671  unsigned long long size;
672 
673  while (isspace((int)*str))
674  str++;
675  if (*str == '-')
676  return 0;
677 
678  errno = 0;
679  size = strtoull(str, &endptr, 0);
680  if (errno)
681  return 0;
682 
683  if (*endptr == ' ')
684  endptr++; /* allow 1 space gap */
685 
686  switch (*endptr){
687  case 'G': case 'g': size *= 1024; /* fall-through */
688  case 'M': case 'm': size *= 1024; /* fall-through */
689  case 'K': case 'k': size *= 1024; /* fall-through */
690  default:
691  break;
692  }
693  return size;
694 }
695 
709 void
710 rte_exit(int exit_code, const char *format, ...)
711  __attribute__((noreturn))
712  __attribute__((format(printf, 2, 3)));
713 
714 #ifdef __cplusplus
715 }
716 #endif
717 
718 #endif