DPDK  18.11.0-rc3
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 #ifndef __OPTIMIZE__
274 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
275 #else
276 extern int RTE_BUILD_BUG_ON_detected_error;
277 #define RTE_BUILD_BUG_ON(condition) do { \
278  ((void)sizeof(char[1 - 2*!!(condition)])); \
279  if (condition) \
280  RTE_BUILD_BUG_ON_detected_error = 1; \
281 } while(0)
282 #endif
283 
294 static inline uint32_t
295 rte_combine32ms1b(register uint32_t x)
296 {
297  x |= x >> 1;
298  x |= x >> 2;
299  x |= x >> 4;
300  x |= x >> 8;
301  x |= x >> 16;
302 
303  return x;
304 }
305 
316 static inline uint64_t
317 rte_combine64ms1b(register uint64_t v)
318 {
319  v |= v >> 1;
320  v |= v >> 2;
321  v |= v >> 4;
322  v |= v >> 8;
323  v |= v >> 16;
324  v |= v >> 32;
325 
326  return v;
327 }
328 
329 /*********** Macros to work with powers of 2 ********/
330 
334 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
335 
342 static inline int
343 rte_is_power_of_2(uint32_t n)
344 {
345  return n && !(n & (n - 1));
346 }
347 
357 static inline uint32_t
358 rte_align32pow2(uint32_t x)
359 {
360  x--;
361  x = rte_combine32ms1b(x);
362 
363  return x + 1;
364 }
365 
375 static inline uint32_t
377 {
378  x = rte_combine32ms1b(x);
379 
380  return x - (x >> 1);
381 }
382 
392 static inline uint64_t
393 rte_align64pow2(uint64_t v)
394 {
395  v--;
396  v = rte_combine64ms1b(v);
397 
398  return v + 1;
399 }
400 
410 static inline uint64_t
412 {
413  v = rte_combine64ms1b(v);
414 
415  return v - (v >> 1);
416 }
417 
418 /*********** Macros for calculating min and max **********/
419 
423 #define RTE_MIN(a, b) \
424  __extension__ ({ \
425  typeof (a) _a = (a); \
426  typeof (b) _b = (b); \
427  _a < _b ? _a : _b; \
428  })
429 
433 #define RTE_MAX(a, b) \
434  __extension__ ({ \
435  typeof (a) _a = (a); \
436  typeof (b) _b = (b); \
437  _a > _b ? _a : _b; \
438  })
439 
440 /*********** Other general functions / macros ********/
441 
453 static inline uint32_t
454 rte_bsf32(uint32_t v)
455 {
456  return (uint32_t)__builtin_ctz(v);
457 }
458 
467 static inline uint32_t
468 rte_log2_u32(uint32_t v)
469 {
470  if (v == 0)
471  return 0;
472  v = rte_align32pow2(v);
473  return rte_bsf32(v);
474 }
475 
476 
488 static inline int
489 rte_fls_u32(uint32_t x)
490 {
491  return (x == 0) ? 0 : 32 - __builtin_clz(x);
492 }
493 
494 
495 #ifndef offsetof
496 
497 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
498 #endif
499 
514 #ifndef container_of
515 #define container_of(ptr, type, member) __extension__ ({ \
516  const typeof(((type *)0)->member) *_ptr = (ptr); \
517  __attribute__((unused)) type *_target_ptr = \
518  (type *)(ptr); \
519  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
520  })
521 #endif
522 
523 #define _RTE_STR(x) #x
524 
525 #define RTE_STR(x) _RTE_STR(x)
526 
532 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
533 #define RTE_FMT_HEAD(fmt, ...) fmt
534 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
535 
537 #define RTE_LEN2MASK(ln, tp) \
538  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
539 
541 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
542 
557 static inline uint64_t
558 rte_str_to_size(const char *str)
559 {
560  char *endptr;
561  unsigned long long size;
562 
563  while (isspace((int)*str))
564  str++;
565  if (*str == '-')
566  return 0;
567 
568  errno = 0;
569  size = strtoull(str, &endptr, 0);
570  if (errno)
571  return 0;
572 
573  if (*endptr == ' ')
574  endptr++; /* allow 1 space gap */
575 
576  switch (*endptr){
577  case 'G': case 'g': size *= 1024; /* fall-through */
578  case 'M': case 'm': size *= 1024; /* fall-through */
579  case 'K': case 'k': size *= 1024; /* fall-through */
580  default:
581  break;
582  }
583  return size;
584 }
585 
599 void
600 rte_exit(int exit_code, const char *format, ...)
601  __attribute__((noreturn))
602  __attribute__((format(printf, 2, 3)));
603 
604 #ifdef __cplusplus
605 }
606 #endif
607 
608 #endif