DPDK  18.11.11
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(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(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 
458 static inline uint32_t
459 rte_log2_u32(uint32_t v)
460 {
461  if (v == 0)
462  return 0;
463  v = rte_align32pow2(v);
464  return rte_bsf32(v);
465 }
466 
467 
479 static inline int
480 rte_fls_u32(uint32_t x)
481 {
482  return (x == 0) ? 0 : 32 - __builtin_clz(x);
483 }
484 
499 static inline int
500 rte_bsf64_safe(uint64_t v, uint32_t *pos)
501 {
502  if (v == 0)
503  return 0;
504 
505  *pos = __builtin_ctzll(v);
506  return 1;
507 }
508 
509 #ifndef offsetof
510 
511 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
512 #endif
513 
528 #ifndef container_of
529 #define container_of(ptr, type, member) __extension__ ({ \
530  const typeof(((type *)0)->member) *_ptr = (ptr); \
531  __attribute__((unused)) type *_target_ptr = \
532  (type *)(ptr); \
533  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
534  })
535 #endif
536 
537 #define _RTE_STR(x) #x
538 
539 #define RTE_STR(x) _RTE_STR(x)
540 
546 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
547 #define RTE_FMT_HEAD(fmt, ...) fmt
548 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
549 
551 #define RTE_LEN2MASK(ln, tp) \
552  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
553 
555 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
556 
571 static inline uint64_t
572 rte_str_to_size(const char *str)
573 {
574  char *endptr;
575  unsigned long long size;
576 
577  while (isspace((int)*str))
578  str++;
579  if (*str == '-')
580  return 0;
581 
582  errno = 0;
583  size = strtoull(str, &endptr, 0);
584  if (errno)
585  return 0;
586 
587  if (*endptr == ' ')
588  endptr++; /* allow 1 space gap */
589 
590  switch (*endptr){
591  case 'G': case 'g': size *= 1024; /* fall-through */
592  case 'M': case 'm': size *= 1024; /* fall-through */
593  case 'K': case 'k': size *= 1024; /* fall-through */
594  default:
595  break;
596  }
597  return size;
598 }
599 
613 void
614 rte_exit(int exit_code, const char *format, ...)
615  __attribute__((noreturn))
616  __attribute__((format(printf, 2, 3)));
617 
618 #ifdef __cplusplus
619 }
620 #endif
621 
622 #endif
static int rte_is_aligned(void *ptr, unsigned align)
Definition: rte_common.h:263
static uint64_t rte_combine64ms1b(uint64_t v)
Definition: rte_common.h:308
static int rte_fls_u32(uint32_t x)
Definition: rte_common.h:480
static uint32_t rte_combine32ms1b(uint32_t x)
Definition: rte_common.h:286
static uint64_t rte_align64pow2(uint64_t v)
Definition: rte_common.h:384
static uint32_t rte_log2_u32(uint32_t v)
Definition: rte_common.h:459
static uint32_t rte_bsf32(uint32_t v)
Definition: rte_common.h:445
static uint64_t rte_align64prevpow2(uint64_t v)
Definition: rte_common.h:402
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:349
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:500
uint64_t unaligned_uint64_t
Definition: rte_common.h:53
#define RTE_PTR_ALIGN(ptr, align)
Definition: rte_common.h:224
static int rte_is_power_of_2(uint32_t n)
Definition: rte_common.h:334
void rte_exit(int exit_code, const char *format,...)
static uint32_t rte_align32prevpow2(uint32_t x)
Definition: rte_common.h:367
static uint64_t rte_str_to_size(const char *str)
Definition: rte_common.h:572