DPDK  18.05.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_LAST 65535
87 
88 #define RTE_PRIO(prio) \
89  RTE_PRIORITY_ ## prio
90 
100 #define RTE_INIT_PRIO(func, prio) \
101 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
102 
111 #define RTE_INIT(func) \
112  RTE_INIT_PRIO(func, LAST)
113 
117 #define __rte_always_inline inline __attribute__((always_inline))
118 
122 #define __rte_noinline __attribute__((noinline))
123 
124 /*********** Macros for pointer arithmetic ********/
125 
129 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
130 
134 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
135 
141 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
142 
143 /*********** Macros/static functions for doing alignment ********/
144 
145 
152 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
153  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
154 
161 #define RTE_ALIGN_FLOOR(val, align) \
162  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
163 
170 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
171  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
172 
179 #define RTE_ALIGN_CEIL(val, align) \
180  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
181 
189 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
190 
198 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
199 
205 #define RTE_ALIGN_MUL_CEIL(v, mul) \
206  (((v + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
207 
213 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
214  ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
215 
227 static inline int
228 rte_is_aligned(void *ptr, unsigned align)
229 {
230  return RTE_PTR_ALIGN(ptr, align) == ptr;
231 }
232 
233 /*********** Macros for compile type checks ********/
234 
238 #ifndef __OPTIMIZE__
239 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
240 #else
241 extern int RTE_BUILD_BUG_ON_detected_error;
242 #define RTE_BUILD_BUG_ON(condition) do { \
243  ((void)sizeof(char[1 - 2*!!(condition)])); \
244  if (condition) \
245  RTE_BUILD_BUG_ON_detected_error = 1; \
246 } while(0)
247 #endif
248 
259 static inline uint32_t
260 rte_combine32ms1b(register uint32_t x)
261 {
262  x |= x >> 1;
263  x |= x >> 2;
264  x |= x >> 4;
265  x |= x >> 8;
266  x |= x >> 16;
267 
268  return x;
269 }
270 
281 static inline uint64_t
282 rte_combine64ms1b(register uint64_t v)
283 {
284  v |= v >> 1;
285  v |= v >> 2;
286  v |= v >> 4;
287  v |= v >> 8;
288  v |= v >> 16;
289  v |= v >> 32;
290 
291  return v;
292 }
293 
294 /*********** Macros to work with powers of 2 ********/
295 
302 static inline int
303 rte_is_power_of_2(uint32_t n)
304 {
305  return n && !(n & (n - 1));
306 }
307 
317 static inline uint32_t
318 rte_align32pow2(uint32_t x)
319 {
320  x--;
321  x = rte_combine32ms1b(x);
322 
323  return x + 1;
324 }
325 
335 static inline uint32_t
337 {
338  x = rte_combine32ms1b(x);
339 
340  return x - (x >> 1);
341 }
342 
352 static inline uint64_t
353 rte_align64pow2(uint64_t v)
354 {
355  v--;
356  v = rte_combine64ms1b(v);
357 
358  return v + 1;
359 }
360 
370 static inline uint64_t
372 {
373  v = rte_combine64ms1b(v);
374 
375  return v - (v >> 1);
376 }
377 
378 /*********** Macros for calculating min and max **********/
379 
383 #define RTE_MIN(a, b) \
384  __extension__ ({ \
385  typeof (a) _a = (a); \
386  typeof (b) _b = (b); \
387  _a < _b ? _a : _b; \
388  })
389 
393 #define RTE_MAX(a, b) \
394  __extension__ ({ \
395  typeof (a) _a = (a); \
396  typeof (b) _b = (b); \
397  _a > _b ? _a : _b; \
398  })
399 
400 /*********** Other general functions / macros ********/
401 
413 static inline uint32_t
414 rte_bsf32(uint32_t v)
415 {
416  return (uint32_t)__builtin_ctz(v);
417 }
418 
427 static inline uint32_t
428 rte_log2_u32(uint32_t v)
429 {
430  if (v == 0)
431  return 0;
432  v = rte_align32pow2(v);
433  return rte_bsf32(v);
434 }
435 
436 #ifndef offsetof
437 
438 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
439 #endif
440 
455 #ifndef container_of
456 #define container_of(ptr, type, member) __extension__ ({ \
457  const typeof(((type *)0)->member) *_ptr = (ptr); \
458  __attribute__((unused)) type *_target_ptr = \
459  (type *)(ptr); \
460  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
461  })
462 #endif
463 
464 #define _RTE_STR(x) #x
465 
466 #define RTE_STR(x) _RTE_STR(x)
467 
473 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
474 #define RTE_FMT_HEAD(fmt, ...) fmt
475 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
476 
478 #define RTE_LEN2MASK(ln, tp) \
479  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
480 
482 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
483 
498 static inline uint64_t
499 rte_str_to_size(const char *str)
500 {
501  char *endptr;
502  unsigned long long size;
503 
504  while (isspace((int)*str))
505  str++;
506  if (*str == '-')
507  return 0;
508 
509  errno = 0;
510  size = strtoull(str, &endptr, 0);
511  if (errno)
512  return 0;
513 
514  if (*endptr == ' ')
515  endptr++; /* allow 1 space gap */
516 
517  switch (*endptr){
518  case 'G': case 'g': size *= 1024; /* fall-through */
519  case 'M': case 'm': size *= 1024; /* fall-through */
520  case 'K': case 'k': size *= 1024; /* fall-through */
521  default:
522  break;
523  }
524  return size;
525 }
526 
540 void
541 rte_exit(int exit_code, const char *format, ...)
542  __attribute__((noreturn))
543  __attribute__((format(printf, 2, 3)));
544 
545 #ifdef __cplusplus
546 }
547 #endif
548 
549 #endif