DPDK  19.02.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 #define RTE_INIT_PRIO(func, prio) \
107 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
108 
117 #define RTE_INIT(func) \
118  RTE_INIT_PRIO(func, LAST)
119 
129 #define RTE_FINI_PRIO(func, prio) \
130 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
131 
140 #define RTE_FINI(func) \
141  RTE_FINI_PRIO(func, LAST)
142 
146 #define __rte_always_inline inline __attribute__((always_inline))
147 
151 #define __rte_noinline __attribute__((noinline))
152 
153 /*********** Macros for pointer arithmetic ********/
154 
158 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
159 
163 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
164 
170 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
171 
175 #define RTE_CAST_FIELD(var, field, type) \
176  (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
177 
178 /*********** Macros/static functions for doing alignment ********/
179 
180 
187 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
188  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
189 
196 #define RTE_ALIGN_FLOOR(val, align) \
197  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
198 
205 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
206  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
207 
214 #define RTE_ALIGN_CEIL(val, align) \
215  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
216 
224 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
225 
233 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
234 
240 #define RTE_ALIGN_MUL_CEIL(v, mul) \
241  (((v + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
242 
248 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
249  ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
250 
262 static inline int
263 rte_is_aligned(void *ptr, unsigned align)
264 {
265  return RTE_PTR_ALIGN(ptr, align) == ptr;
266 }
267 
268 /*********** Macros for compile type checks ********/
269 
273 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
274 
285 static inline uint32_t
286 rte_combine32ms1b(register uint32_t x)
287 {
288  x |= x >> 1;
289  x |= x >> 2;
290  x |= x >> 4;
291  x |= x >> 8;
292  x |= x >> 16;
293 
294  return x;
295 }
296 
307 static inline uint64_t
308 rte_combine64ms1b(register uint64_t v)
309 {
310  v |= v >> 1;
311  v |= v >> 2;
312  v |= v >> 4;
313  v |= v >> 8;
314  v |= v >> 16;
315  v |= v >> 32;
316 
317  return v;
318 }
319 
320 /*********** Macros to work with powers of 2 ********/
321 
325 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
326 
333 static inline int
334 rte_is_power_of_2(uint32_t n)
335 {
336  return n && !(n & (n - 1));
337 }
338 
348 static inline uint32_t
349 rte_align32pow2(uint32_t x)
350 {
351  x--;
352  x = rte_combine32ms1b(x);
353 
354  return x + 1;
355 }
356 
366 static inline uint32_t
368 {
369  x = rte_combine32ms1b(x);
370 
371  return x - (x >> 1);
372 }
373 
383 static inline uint64_t
384 rte_align64pow2(uint64_t v)
385 {
386  v--;
387  v = rte_combine64ms1b(v);
388 
389  return v + 1;
390 }
391 
401 static inline uint64_t
403 {
404  v = rte_combine64ms1b(v);
405 
406  return v - (v >> 1);
407 }
408 
409 /*********** Macros for calculating min and max **********/
410 
414 #define RTE_MIN(a, b) \
415  __extension__ ({ \
416  typeof (a) _a = (a); \
417  typeof (b) _b = (b); \
418  _a < _b ? _a : _b; \
419  })
420 
424 #define RTE_MAX(a, b) \
425  __extension__ ({ \
426  typeof (a) _a = (a); \
427  typeof (b) _b = (b); \
428  _a > _b ? _a : _b; \
429  })
430 
431 /*********** Other general functions / macros ********/
432 
444 static inline uint32_t
445 rte_bsf32(uint32_t v)
446 {
447  return (uint32_t)__builtin_ctz(v);
448 }
449 
464 static inline int
465 rte_bsf32_safe(uint64_t v, uint32_t *pos)
466 {
467  if (v == 0)
468  return 0;
469 
470  *pos = rte_bsf32(v);
471  return 1;
472 }
473 
482 static inline uint32_t
483 rte_log2_u32(uint32_t v)
484 {
485  if (v == 0)
486  return 0;
487  v = rte_align32pow2(v);
488  return rte_bsf32(v);
489 }
490 
491 
503 static inline int
504 rte_fls_u32(uint32_t x)
505 {
506  return (x == 0) ? 0 : 32 - __builtin_clz(x);
507 }
508 
520 static inline int
521 rte_bsf64(uint64_t v)
522 {
523  return (uint32_t)__builtin_ctzll(v);
524 }
525 
540 static inline int
541 rte_bsf64_safe(uint64_t v, uint32_t *pos)
542 {
543  if (v == 0)
544  return 0;
545 
546  *pos = rte_bsf64(v);
547  return 1;
548 }
549 
562 static inline int
563 rte_fls_u64(uint64_t x)
564 {
565  return (x == 0) ? 0 : 64 - __builtin_clzll(x);
566 }
567 
576 static inline uint32_t
577 rte_log2_u64(uint64_t v)
578 {
579  if (v == 0)
580  return 0;
581  v = rte_align64pow2(v);
582  /* we checked for v being 0 already, so no undefined behavior */
583  return rte_bsf64(v);
584 }
585 
586 #ifndef offsetof
587 
588 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
589 #endif
590 
605 #ifndef container_of
606 #define container_of(ptr, type, member) __extension__ ({ \
607  const typeof(((type *)0)->member) *_ptr = (ptr); \
608  __attribute__((unused)) type *_target_ptr = \
609  (type *)(ptr); \
610  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
611  })
612 #endif
613 
614 #define _RTE_STR(x) #x
615 
616 #define RTE_STR(x) _RTE_STR(x)
617 
623 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
624 #define RTE_FMT_HEAD(fmt, ...) fmt
625 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
626 
628 #define RTE_LEN2MASK(ln, tp) \
629  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
630 
632 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
633 
648 static inline uint64_t
649 rte_str_to_size(const char *str)
650 {
651  char *endptr;
652  unsigned long long size;
653 
654  while (isspace((int)*str))
655  str++;
656  if (*str == '-')
657  return 0;
658 
659  errno = 0;
660  size = strtoull(str, &endptr, 0);
661  if (errno)
662  return 0;
663 
664  if (*endptr == ' ')
665  endptr++; /* allow 1 space gap */
666 
667  switch (*endptr){
668  case 'G': case 'g': size *= 1024; /* fall-through */
669  case 'M': case 'm': size *= 1024; /* fall-through */
670  case 'K': case 'k': size *= 1024; /* fall-through */
671  default:
672  break;
673  }
674  return size;
675 }
676 
690 void
691 rte_exit(int exit_code, const char *format, ...)
692  __attribute__((noreturn))
693  __attribute__((format(printf, 2, 3)));
694 
695 #ifdef __cplusplus
696 }
697 #endif
698 
699 #endif