DPDK  18.08.1
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 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
263 
274 static inline uint32_t
275 rte_combine32ms1b(register uint32_t x)
276 {
277  x |= x >> 1;
278  x |= x >> 2;
279  x |= x >> 4;
280  x |= x >> 8;
281  x |= x >> 16;
282 
283  return x;
284 }
285 
296 static inline uint64_t
297 rte_combine64ms1b(register uint64_t v)
298 {
299  v |= v >> 1;
300  v |= v >> 2;
301  v |= v >> 4;
302  v |= v >> 8;
303  v |= v >> 16;
304  v |= v >> 32;
305 
306  return v;
307 }
308 
309 /*********** Macros to work with powers of 2 ********/
310 
314 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
315 
322 static inline int
323 rte_is_power_of_2(uint32_t n)
324 {
325  return n && !(n & (n - 1));
326 }
327 
337 static inline uint32_t
338 rte_align32pow2(uint32_t x)
339 {
340  x--;
341  x = rte_combine32ms1b(x);
342 
343  return x + 1;
344 }
345 
355 static inline uint32_t
357 {
358  x = rte_combine32ms1b(x);
359 
360  return x - (x >> 1);
361 }
362 
372 static inline uint64_t
373 rte_align64pow2(uint64_t v)
374 {
375  v--;
376  v = rte_combine64ms1b(v);
377 
378  return v + 1;
379 }
380 
390 static inline uint64_t
392 {
393  v = rte_combine64ms1b(v);
394 
395  return v - (v >> 1);
396 }
397 
398 /*********** Macros for calculating min and max **********/
399 
403 #define RTE_MIN(a, b) \
404  __extension__ ({ \
405  typeof (a) _a = (a); \
406  typeof (b) _b = (b); \
407  _a < _b ? _a : _b; \
408  })
409 
413 #define RTE_MAX(a, b) \
414  __extension__ ({ \
415  typeof (a) _a = (a); \
416  typeof (b) _b = (b); \
417  _a > _b ? _a : _b; \
418  })
419 
420 /*********** Other general functions / macros ********/
421 
433 static inline uint32_t
434 rte_bsf32(uint32_t v)
435 {
436  return (uint32_t)__builtin_ctz(v);
437 }
438 
447 static inline uint32_t
448 rte_log2_u32(uint32_t v)
449 {
450  if (v == 0)
451  return 0;
452  v = rte_align32pow2(v);
453  return rte_bsf32(v);
454 }
455 
456 #ifndef offsetof
457 
458 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
459 #endif
460 
475 #ifndef container_of
476 #define container_of(ptr, type, member) __extension__ ({ \
477  const typeof(((type *)0)->member) *_ptr = (ptr); \
478  __attribute__((unused)) type *_target_ptr = \
479  (type *)(ptr); \
480  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
481  })
482 #endif
483 
484 #define _RTE_STR(x) #x
485 
486 #define RTE_STR(x) _RTE_STR(x)
487 
493 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
494 #define RTE_FMT_HEAD(fmt, ...) fmt
495 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
496 
498 #define RTE_LEN2MASK(ln, tp) \
499  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
500 
502 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
503 
518 static inline uint64_t
519 rte_str_to_size(const char *str)
520 {
521  char *endptr;
522  unsigned long long size;
523 
524  while (isspace((int)*str))
525  str++;
526  if (*str == '-')
527  return 0;
528 
529  errno = 0;
530  size = strtoull(str, &endptr, 0);
531  if (errno)
532  return 0;
533 
534  if (*endptr == ' ')
535  endptr++; /* allow 1 space gap */
536 
537  switch (*endptr){
538  case 'G': case 'g': size *= 1024; /* fall-through */
539  case 'M': case 'm': size *= 1024; /* fall-through */
540  case 'K': case 'k': size *= 1024; /* fall-through */
541  default:
542  break;
543  }
544  return size;
545 }
546 
560 void
561 rte_exit(int exit_code, const char *format, ...)
562  __attribute__((noreturn))
563  __attribute__((format(printf, 2, 3)));
564 
565 #ifdef __cplusplus
566 }
567 #endif
568 
569 #endif