DPDK  16.07.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 
62 #ifdef RTE_ARCH_STRICT_ALIGN
63 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
64 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
65 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
66 #else
67 typedef uint64_t unaligned_uint64_t;
68 typedef uint32_t unaligned_uint32_t;
69 typedef uint16_t unaligned_uint16_t;
70 #endif
71 
75 #define __rte_aligned(a) __attribute__((__aligned__(a)))
76 
80 #define __rte_packed __attribute__((__packed__))
81 
82 /******* Macro to mark functions and fields scheduled for removal *****/
83 #define __rte_deprecated __attribute__((__deprecated__))
84 
85 /*********** Macros to eliminate unused variable warnings ********/
86 
90 #define __rte_unused __attribute__((__unused__))
91 
96 #define RTE_SET_USED(x) (void)(x)
97 
98 /*********** Macros for pointer arithmetic ********/
99 
103 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
104 
108 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
109 
115 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
116 
117 /*********** Macros/static functions for doing alignment ********/
118 
119 
126 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
127  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
128 
135 #define RTE_ALIGN_FLOOR(val, align) \
136  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
137 
144 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
145  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
146 
153 #define RTE_ALIGN_CEIL(val, align) \
154  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
155 
163 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
164 
172 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
173 
185 static inline int
186 rte_is_aligned(void *ptr, unsigned align)
187 {
188  return RTE_PTR_ALIGN(ptr, align) == ptr;
189 }
190 
191 /*********** Macros for compile type checks ********/
192 
196 #ifndef __OPTIMIZE__
197 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
198 #else
199 extern int RTE_BUILD_BUG_ON_detected_error;
200 #define RTE_BUILD_BUG_ON(condition) do { \
201  ((void)sizeof(char[1 - 2*!!(condition)])); \
202  if (condition) \
203  RTE_BUILD_BUG_ON_detected_error = 1; \
204 } while(0)
205 #endif
206 
207 /*********** Macros to work with powers of 2 ********/
208 
215 static inline int
216 rte_is_power_of_2(uint32_t n)
217 {
218  return n && !(n & (n - 1));
219 }
220 
230 static inline uint32_t
231 rte_align32pow2(uint32_t x)
232 {
233  x--;
234  x |= x >> 1;
235  x |= x >> 2;
236  x |= x >> 4;
237  x |= x >> 8;
238  x |= x >> 16;
239 
240  return x + 1;
241 }
242 
252 static inline uint64_t
253 rte_align64pow2(uint64_t v)
254 {
255  v--;
256  v |= v >> 1;
257  v |= v >> 2;
258  v |= v >> 4;
259  v |= v >> 8;
260  v |= v >> 16;
261  v |= v >> 32;
262 
263  return v + 1;
264 }
265 
266 /*********** Macros for calculating min and max **********/
267 
271 #define RTE_MIN(a, b) ({ \
272  typeof (a) _a = (a); \
273  typeof (b) _b = (b); \
274  _a < _b ? _a : _b; \
275  })
276 
280 #define RTE_MAX(a, b) ({ \
281  typeof (a) _a = (a); \
282  typeof (b) _b = (b); \
283  _a > _b ? _a : _b; \
284  })
285 
286 /*********** Other general functions / macros ********/
287 
288 #ifdef __SSE2__
289 #include <emmintrin.h>
293 static inline void
294 rte_pause (void)
295 {
296  _mm_pause();
297 }
298 #else
299 static inline void
300 rte_pause(void) {}
301 #endif
302 
314 static inline uint32_t
315 rte_bsf32(uint32_t v)
316 {
317  return __builtin_ctz(v);
318 }
319 
320 #ifndef offsetof
321 
322 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
323 #endif
324 
325 #define _RTE_STR(x) #x
326 
327 #define RTE_STR(x) _RTE_STR(x)
328 
330 #define RTE_LEN2MASK(ln, tp) \
331  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
332 
334 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
335 
350 static inline uint64_t
351 rte_str_to_size(const char *str)
352 {
353  char *endptr;
354  unsigned long long size;
355 
356  while (isspace((int)*str))
357  str++;
358  if (*str == '-')
359  return 0;
360 
361  errno = 0;
362  size = strtoull(str, &endptr, 0);
363  if (errno)
364  return 0;
365 
366  if (*endptr == ' ')
367  endptr++; /* allow 1 space gap */
368 
369  switch (*endptr){
370  case 'G': case 'g': size *= 1024; /* fall-through */
371  case 'M': case 'm': size *= 1024; /* fall-through */
372  case 'K': case 'k': size *= 1024; /* fall-through */
373  default:
374  break;
375  }
376  return size;
377 }
378 
392 void
393 rte_exit(int exit_code, const char *format, ...)
394  __attribute__((noreturn))
395  __attribute__((format(printf, 2, 3)));
396 
397 #ifdef __cplusplus
398 }
399 #endif
400 
401 #endif