DPDK  18.02.2
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 
92 #define RTE_INIT(func) \
93 static void __attribute__((constructor, used)) func(void)
94 
104 #define RTE_INIT_PRIO(func, prio) \
105 static void __attribute__((constructor(prio), used)) func(void)
106 
110 #define __rte_always_inline inline __attribute__((always_inline))
111 
115 #define __rte_noinline __attribute__((noinline))
116 
117 /*********** Macros for pointer arithmetic ********/
118 
122 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
123 
127 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
128 
134 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
135 
136 /*********** Macros/static functions for doing alignment ********/
137 
138 
145 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
146  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
147 
154 #define RTE_ALIGN_FLOOR(val, align) \
155  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
156 
163 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
164  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
165 
172 #define RTE_ALIGN_CEIL(val, align) \
173  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
174 
182 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
183 
191 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
192 
204 static inline int
205 rte_is_aligned(void *ptr, unsigned align)
206 {
207  return RTE_PTR_ALIGN(ptr, align) == ptr;
208 }
209 
210 /*********** Macros for compile type checks ********/
211 
215 #ifndef __OPTIMIZE__
216 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
217 #else
218 extern int RTE_BUILD_BUG_ON_detected_error;
219 #define RTE_BUILD_BUG_ON(condition) do { \
220  ((void)sizeof(char[1 - 2*!!(condition)])); \
221  if (condition) \
222  RTE_BUILD_BUG_ON_detected_error = 1; \
223 } while(0)
224 #endif
225 
226 /*********** Macros to work with powers of 2 ********/
227 
234 static inline int
235 rte_is_power_of_2(uint32_t n)
236 {
237  return n && !(n & (n - 1));
238 }
239 
249 static inline uint32_t
250 rte_align32pow2(uint32_t x)
251 {
252  x--;
253  x |= x >> 1;
254  x |= x >> 2;
255  x |= x >> 4;
256  x |= x >> 8;
257  x |= x >> 16;
258 
259  return x + 1;
260 }
261 
271 static inline uint64_t
272 rte_align64pow2(uint64_t v)
273 {
274  v--;
275  v |= v >> 1;
276  v |= v >> 2;
277  v |= v >> 4;
278  v |= v >> 8;
279  v |= v >> 16;
280  v |= v >> 32;
281 
282  return v + 1;
283 }
284 
285 /*********** Macros for calculating min and max **********/
286 
290 #define RTE_MIN(a, b) \
291  __extension__ ({ \
292  typeof (a) _a = (a); \
293  typeof (b) _b = (b); \
294  _a < _b ? _a : _b; \
295  })
296 
300 #define RTE_MAX(a, b) \
301  __extension__ ({ \
302  typeof (a) _a = (a); \
303  typeof (b) _b = (b); \
304  _a > _b ? _a : _b; \
305  })
306 
307 /*********** Other general functions / macros ********/
308 
320 static inline uint32_t
321 rte_bsf32(uint32_t v)
322 {
323  return (uint32_t)__builtin_ctz(v);
324 }
325 
334 static inline uint32_t
335 rte_log2_u32(uint32_t v)
336 {
337  if (v == 0)
338  return 0;
339  v = rte_align32pow2(v);
340  return rte_bsf32(v);
341 }
342 
343 #ifndef offsetof
344 
345 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
346 #endif
347 
362 #ifndef container_of
363 #define container_of(ptr, type, member) __extension__ ({ \
364  const typeof(((type *)0)->member) *_ptr = (ptr); \
365  __attribute__((unused)) type *_target_ptr = \
366  (type *)(ptr); \
367  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
368  })
369 #endif
370 
371 #define _RTE_STR(x) #x
372 
373 #define RTE_STR(x) _RTE_STR(x)
374 
380 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
381 #define RTE_FMT_HEAD(fmt, ...) fmt
382 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
383 
385 #define RTE_LEN2MASK(ln, tp) \
386  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
387 
389 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
390 
405 static inline uint64_t
406 rte_str_to_size(const char *str)
407 {
408  char *endptr;
409  unsigned long long size;
410 
411  while (isspace((int)*str))
412  str++;
413  if (*str == '-')
414  return 0;
415 
416  errno = 0;
417  size = strtoull(str, &endptr, 0);
418  if (errno)
419  return 0;
420 
421  if (*endptr == ' ')
422  endptr++; /* allow 1 space gap */
423 
424  switch (*endptr){
425  case 'G': case 'g': size *= 1024; /* fall-through */
426  case 'M': case 'm': size *= 1024; /* fall-through */
427  case 'K': case 'k': size *= 1024; /* fall-through */
428  default:
429  break;
430  }
431  return size;
432 }
433 
447 void
448 rte_exit(int exit_code, const char *format, ...)
449  __attribute__((noreturn))
450  __attribute__((format(printf, 2, 3)));
451 
452 #ifdef __cplusplus
453 }
454 #endif
455 
456 #endif