DPDK  17.08.2
rte_common.h
Go to the documentation of this file.
1 /*-
2  * BSD LICENSE
3  *
4  * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of Intel Corporation nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef _RTE_COMMON_H_
35 #define _RTE_COMMON_H_
36 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 #include <stdint.h>
49 #include <stdlib.h>
50 #include <ctype.h>
51 #include <errno.h>
52 #include <limits.h>
53 
54 #ifndef typeof
55 #define typeof __typeof__
56 #endif
57 
58 #ifndef asm
59 #define asm __asm__
60 #endif
61 
63 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
64 #define RTE_STD_C11 __extension__
65 #else
66 #define RTE_STD_C11
67 #endif
68 
70 #ifdef RTE_TOOLCHAIN_GCC
71 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
72  __GNUC_PATCHLEVEL__)
73 #endif
74 
75 #ifdef RTE_ARCH_STRICT_ALIGN
76 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
77 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
78 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
79 #else
80 typedef uint64_t unaligned_uint64_t;
81 typedef uint32_t unaligned_uint32_t;
82 typedef uint16_t unaligned_uint16_t;
83 #endif
84 
88 #define __rte_aligned(a) __attribute__((__aligned__(a)))
89 
93 #define __rte_packed __attribute__((__packed__))
94 
95 /******* Macro to mark functions and fields scheduled for removal *****/
96 #define __rte_deprecated __attribute__((__deprecated__))
97 
98 /*********** Macros to eliminate unused variable warnings ********/
99 
103 #define __rte_unused __attribute__((__unused__))
104 
109 #define RTE_SET_USED(x) (void)(x)
110 
114 #define __rte_always_inline inline __attribute__((always_inline))
115 
119 #define __rte_noinline __attribute__((noinline))
120 
121 /*********** Macros for pointer arithmetic ********/
122 
126 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
127 
131 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
132 
138 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
139 
140 /*********** Macros/static functions for doing alignment ********/
141 
142 
149 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
150  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
151 
158 #define RTE_ALIGN_FLOOR(val, align) \
159  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
160 
167 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
168  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
169 
176 #define RTE_ALIGN_CEIL(val, align) \
177  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
178 
186 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
187 
195 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
196 
208 static inline int
209 rte_is_aligned(void *ptr, unsigned align)
210 {
211  return RTE_PTR_ALIGN(ptr, align) == ptr;
212 }
213 
214 /*********** Macros for compile type checks ********/
215 
219 #ifndef __OPTIMIZE__
220 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
221 #else
222 extern int RTE_BUILD_BUG_ON_detected_error;
223 #define RTE_BUILD_BUG_ON(condition) do { \
224  ((void)sizeof(char[1 - 2*!!(condition)])); \
225  if (condition) \
226  RTE_BUILD_BUG_ON_detected_error = 1; \
227 } while(0)
228 #endif
229 
230 /*********** Macros to work with powers of 2 ********/
231 
238 static inline int
239 rte_is_power_of_2(uint32_t n)
240 {
241  return n && !(n & (n - 1));
242 }
243 
253 static inline uint32_t
254 rte_align32pow2(uint32_t x)
255 {
256  x--;
257  x |= x >> 1;
258  x |= x >> 2;
259  x |= x >> 4;
260  x |= x >> 8;
261  x |= x >> 16;
262 
263  return x + 1;
264 }
265 
275 static inline uint64_t
276 rte_align64pow2(uint64_t v)
277 {
278  v--;
279  v |= v >> 1;
280  v |= v >> 2;
281  v |= v >> 4;
282  v |= v >> 8;
283  v |= v >> 16;
284  v |= v >> 32;
285 
286  return v + 1;
287 }
288 
289 /*********** Macros for calculating min and max **********/
290 
294 #define RTE_MIN(a, b) \
295  __extension__ ({ \
296  typeof (a) _a = (a); \
297  typeof (b) _b = (b); \
298  _a < _b ? _a : _b; \
299  })
300 
304 #define RTE_MAX(a, b) \
305  __extension__ ({ \
306  typeof (a) _a = (a); \
307  typeof (b) _b = (b); \
308  _a > _b ? _a : _b; \
309  })
310 
311 /*********** Other general functions / macros ********/
312 
324 static inline uint32_t
325 rte_bsf32(uint32_t v)
326 {
327  return __builtin_ctz(v);
328 }
329 
338 static inline uint32_t
339 rte_log2_u32(uint32_t v)
340 {
341  if (v == 0)
342  return 0;
343  v = rte_align32pow2(v);
344  return rte_bsf32(v);
345 }
346 
347 #ifndef offsetof
348 
349 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
350 #endif
351 
366 #ifndef container_of
367 #define container_of(ptr, type, member) __extension__ ({ \
368  const typeof(((type *)0)->member) *_ptr = (ptr); \
369  __attribute__((unused)) type *_target_ptr = \
370  (type *)(ptr); \
371  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
372  })
373 #endif
374 
375 #define _RTE_STR(x) #x
376 
377 #define RTE_STR(x) _RTE_STR(x)
378 
384 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
385 #define RTE_FMT_HEAD(fmt, ...) fmt
386 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
387 
389 #define RTE_LEN2MASK(ln, tp) \
390  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
391 
393 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
394 
409 static inline uint64_t
410 rte_str_to_size(const char *str)
411 {
412  char *endptr;
413  unsigned long long size;
414 
415  while (isspace((int)*str))
416  str++;
417  if (*str == '-')
418  return 0;
419 
420  errno = 0;
421  size = strtoull(str, &endptr, 0);
422  if (errno)
423  return 0;
424 
425  if (*endptr == ' ')
426  endptr++; /* allow 1 space gap */
427 
428  switch (*endptr){
429  case 'G': case 'g': size *= 1024; /* fall-through */
430  case 'M': case 'm': size *= 1024; /* fall-through */
431  case 'K': case 'k': size *= 1024; /* fall-through */
432  default:
433  break;
434  }
435  return size;
436 }
437 
451 void
452 rte_exit(int exit_code, const char *format, ...)
453  __attribute__((noreturn))
454  __attribute__((format(printf, 2, 3)));
455 
456 #ifdef __cplusplus
457 }
458 #endif
459 
460 #endif