DPDK  17.05.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 
69 #ifdef RTE_ARCH_STRICT_ALIGN
70 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
71 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
72 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
73 #else
74 typedef uint64_t unaligned_uint64_t;
75 typedef uint32_t unaligned_uint32_t;
76 typedef uint16_t unaligned_uint16_t;
77 #endif
78 
82 #define __rte_aligned(a) __attribute__((__aligned__(a)))
83 
87 #define __rte_packed __attribute__((__packed__))
88 
89 /******* Macro to mark functions and fields scheduled for removal *****/
90 #define __rte_deprecated __attribute__((__deprecated__))
91 
92 /*********** Macros to eliminate unused variable warnings ********/
93 
97 #define __rte_unused __attribute__((__unused__))
98 
103 #define RTE_SET_USED(x) (void)(x)
104 
105 /*********** Macros for pointer arithmetic ********/
106 
110 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
111 
115 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
116 
122 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
123 
124 /*********** Macros/static functions for doing alignment ********/
125 
126 
133 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
134  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
135 
142 #define RTE_ALIGN_FLOOR(val, align) \
143  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
144 
151 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
152  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
153 
160 #define RTE_ALIGN_CEIL(val, align) \
161  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
162 
170 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
171 
179 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
180 
192 static inline int
193 rte_is_aligned(void *ptr, unsigned align)
194 {
195  return RTE_PTR_ALIGN(ptr, align) == ptr;
196 }
197 
198 /*********** Macros for compile type checks ********/
199 
203 #ifndef __OPTIMIZE__
204 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
205 #else
206 extern int RTE_BUILD_BUG_ON_detected_error;
207 #define RTE_BUILD_BUG_ON(condition) do { \
208  ((void)sizeof(char[1 - 2*!!(condition)])); \
209  if (condition) \
210  RTE_BUILD_BUG_ON_detected_error = 1; \
211 } while(0)
212 #endif
213 
214 /*********** Macros to work with powers of 2 ********/
215 
222 static inline int
223 rte_is_power_of_2(uint32_t n)
224 {
225  return n && !(n & (n - 1));
226 }
227 
237 static inline uint32_t
238 rte_align32pow2(uint32_t x)
239 {
240  x--;
241  x |= x >> 1;
242  x |= x >> 2;
243  x |= x >> 4;
244  x |= x >> 8;
245  x |= x >> 16;
246 
247  return x + 1;
248 }
249 
259 static inline uint64_t
260 rte_align64pow2(uint64_t v)
261 {
262  v--;
263  v |= v >> 1;
264  v |= v >> 2;
265  v |= v >> 4;
266  v |= v >> 8;
267  v |= v >> 16;
268  v |= v >> 32;
269 
270  return v + 1;
271 }
272 
273 /*********** Macros for calculating min and max **********/
274 
278 #define RTE_MIN(a, b) \
279  __extension__ ({ \
280  typeof (a) _a = (a); \
281  typeof (b) _b = (b); \
282  _a < _b ? _a : _b; \
283  })
284 
288 #define RTE_MAX(a, b) \
289  __extension__ ({ \
290  typeof (a) _a = (a); \
291  typeof (b) _b = (b); \
292  _a > _b ? _a : _b; \
293  })
294 
295 /*********** Other general functions / macros ********/
296 
297 #ifdef __SSE2__
298 #include <emmintrin.h>
302 static inline void
303 rte_pause (void)
304 {
305  _mm_pause();
306 }
307 #else
308 static inline void
309 rte_pause(void) {}
310 #endif
311 
323 static inline uint32_t
324 rte_bsf32(uint32_t v)
325 {
326  return __builtin_ctz(v);
327 }
328 
329 #ifndef offsetof
330 
331 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
332 #endif
333 
348 #ifndef container_of
349 #define container_of(ptr, type, member) __extension__ ({ \
350  const typeof(((type *)0)->member) *_ptr = (ptr); \
351  __attribute__((unused)) type *_target_ptr = \
352  (type *)(ptr); \
353  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
354  })
355 #endif
356 
357 #define _RTE_STR(x) #x
358 
359 #define RTE_STR(x) _RTE_STR(x)
360 
366 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
367 #define RTE_FMT_HEAD(fmt, ...) fmt
368 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
369 
371 #define RTE_LEN2MASK(ln, tp) \
372  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
373 
375 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
376 
391 static inline uint64_t
392 rte_str_to_size(const char *str)
393 {
394  char *endptr;
395  unsigned long long size;
396 
397  while (isspace((int)*str))
398  str++;
399  if (*str == '-')
400  return 0;
401 
402  errno = 0;
403  size = strtoull(str, &endptr, 0);
404  if (errno)
405  return 0;
406 
407  if (*endptr == ' ')
408  endptr++; /* allow 1 space gap */
409 
410  switch (*endptr){
411  case 'G': case 'g': size *= 1024; /* fall-through */
412  case 'M': case 'm': size *= 1024; /* fall-through */
413  case 'K': case 'k': size *= 1024; /* fall-through */
414  default:
415  break;
416  }
417  return size;
418 }
419 
433 void
434 rte_exit(int exit_code, const char *format, ...)
435  __attribute__((noreturn))
436  __attribute__((format(printf, 2, 3)));
437 
438 #ifdef __cplusplus
439 }
440 #endif
441 
442 #endif