DPDK  18.08.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 
71 /*********** Macros to eliminate unused variable warnings ********/
72 
76 #define __rte_unused __attribute__((__unused__))
77 
82 #define RTE_SET_USED(x) (void)(x)
83 
84 #define RTE_PRIORITY_LOG 101
85 #define RTE_PRIORITY_BUS 110
86 #define RTE_PRIORITY_CLASS 120
87 #define RTE_PRIORITY_LAST 65535
88 
89 #define RTE_PRIO(prio) \
90  RTE_PRIORITY_ ## prio
91 
101 #define RTE_INIT_PRIO(func, prio) \
102 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
103 
112 #define RTE_INIT(func) \
113  RTE_INIT_PRIO(func, LAST)
114 
124 #define RTE_FINI_PRIO(func, prio) \
125 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
126 
135 #define RTE_FINI(func) \
136  RTE_FINI_PRIO(func, LAST)
137 
141 #define __rte_always_inline inline __attribute__((always_inline))
142 
146 #define __rte_noinline __attribute__((noinline))
147 
148 /*********** Macros for pointer arithmetic ********/
149 
153 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
154 
158 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
159 
165 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
166 
167 /*********** Macros/static functions for doing alignment ********/
168 
169 
176 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
177  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
178 
185 #define RTE_ALIGN_FLOOR(val, align) \
186  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
187 
194 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
195  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
196 
203 #define RTE_ALIGN_CEIL(val, align) \
204  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
205 
213 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
214 
222 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
223 
229 #define RTE_ALIGN_MUL_CEIL(v, mul) \
230  (((v + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
231 
237 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
238  ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
239 
251 static inline int
252 rte_is_aligned(void *ptr, unsigned align)
253 {
254  return RTE_PTR_ALIGN(ptr, align) == ptr;
255 }
256 
257 /*********** Macros for compile type checks ********/
258 
262 #ifndef __OPTIMIZE__
263 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
264 #else
265 extern int RTE_BUILD_BUG_ON_detected_error;
266 #define RTE_BUILD_BUG_ON(condition) do { \
267  ((void)sizeof(char[1 - 2*!!(condition)])); \
268  if (condition) \
269  RTE_BUILD_BUG_ON_detected_error = 1; \
270 } while(0)
271 #endif
272 
283 static inline uint32_t
284 rte_combine32ms1b(register uint32_t x)
285 {
286  x |= x >> 1;
287  x |= x >> 2;
288  x |= x >> 4;
289  x |= x >> 8;
290  x |= x >> 16;
291 
292  return x;
293 }
294 
305 static inline uint64_t
306 rte_combine64ms1b(register uint64_t v)
307 {
308  v |= v >> 1;
309  v |= v >> 2;
310  v |= v >> 4;
311  v |= v >> 8;
312  v |= v >> 16;
313  v |= v >> 32;
314 
315  return v;
316 }
317 
318 /*********** Macros to work with powers of 2 ********/
319 
323 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
324 
331 static inline int
332 rte_is_power_of_2(uint32_t n)
333 {
334  return n && !(n & (n - 1));
335 }
336 
346 static inline uint32_t
347 rte_align32pow2(uint32_t x)
348 {
349  x--;
350  x = rte_combine32ms1b(x);
351 
352  return x + 1;
353 }
354 
364 static inline uint32_t
366 {
367  x = rte_combine32ms1b(x);
368 
369  return x - (x >> 1);
370 }
371 
381 static inline uint64_t
382 rte_align64pow2(uint64_t v)
383 {
384  v--;
385  v = rte_combine64ms1b(v);
386 
387  return v + 1;
388 }
389 
399 static inline uint64_t
401 {
402  v = rte_combine64ms1b(v);
403 
404  return v - (v >> 1);
405 }
406 
407 /*********** Macros for calculating min and max **********/
408 
412 #define RTE_MIN(a, b) \
413  __extension__ ({ \
414  typeof (a) _a = (a); \
415  typeof (b) _b = (b); \
416  _a < _b ? _a : _b; \
417  })
418 
422 #define RTE_MAX(a, b) \
423  __extension__ ({ \
424  typeof (a) _a = (a); \
425  typeof (b) _b = (b); \
426  _a > _b ? _a : _b; \
427  })
428 
429 /*********** Other general functions / macros ********/
430 
442 static inline uint32_t
443 rte_bsf32(uint32_t v)
444 {
445  return (uint32_t)__builtin_ctz(v);
446 }
447 
456 static inline uint32_t
457 rte_log2_u32(uint32_t v)
458 {
459  if (v == 0)
460  return 0;
461  v = rte_align32pow2(v);
462  return rte_bsf32(v);
463 }
464 
465 #ifndef offsetof
466 
467 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
468 #endif
469 
484 #ifndef container_of
485 #define container_of(ptr, type, member) __extension__ ({ \
486  const typeof(((type *)0)->member) *_ptr = (ptr); \
487  __attribute__((unused)) type *_target_ptr = \
488  (type *)(ptr); \
489  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
490  })
491 #endif
492 
493 #define _RTE_STR(x) #x
494 
495 #define RTE_STR(x) _RTE_STR(x)
496 
502 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
503 #define RTE_FMT_HEAD(fmt, ...) fmt
504 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
505 
507 #define RTE_LEN2MASK(ln, tp) \
508  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
509 
511 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
512 
527 static inline uint64_t
528 rte_str_to_size(const char *str)
529 {
530  char *endptr;
531  unsigned long long size;
532 
533  while (isspace((int)*str))
534  str++;
535  if (*str == '-')
536  return 0;
537 
538  errno = 0;
539  size = strtoull(str, &endptr, 0);
540  if (errno)
541  return 0;
542 
543  if (*endptr == ' ')
544  endptr++; /* allow 1 space gap */
545 
546  switch (*endptr){
547  case 'G': case 'g': size *= 1024; /* fall-through */
548  case 'M': case 'm': size *= 1024; /* fall-through */
549  case 'K': case 'k': size *= 1024; /* fall-through */
550  default:
551  break;
552  }
553  return size;
554 }
555 
569 void
570 rte_exit(int exit_code, const char *format, ...)
571  __attribute__((noreturn))
572  __attribute__((format(printf, 2, 3)));
573 
574 #ifdef __cplusplus
575 }
576 #endif
577 
578 #endif