4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #ifndef _RTE_COMMON_H_
35 #define _RTE_COMMON_H_
40 * Generic, commonly-used macro and inline function definitions
54 #include <rte_config.h>
57 #define typeof __typeof__
64 /** C extension macro for environments lacking C11 features. */
65 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
66 #define RTE_STD_C11 __extension__
71 /** Define GCC_VERSION **/
72 #ifdef RTE_TOOLCHAIN_GCC
73 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
77 #ifdef RTE_ARCH_STRICT_ALIGN
78 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
79 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
80 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
82 typedef uint64_t unaligned_uint64_t;
83 typedef uint32_t unaligned_uint32_t;
84 typedef uint16_t unaligned_uint16_t;
90 #define __rte_aligned(a) __attribute__((__aligned__(a)))
93 * Force a structure to be packed
95 #define __rte_packed __attribute__((__packed__))
97 /******* Macro to mark functions and fields scheduled for removal *****/
98 #define __rte_deprecated __attribute__((__deprecated__))
100 /*********** Macros to eliminate unused variable warnings ********/
103 * short definition to mark a function parameter unused
105 #define __rte_unused __attribute__((__unused__))
108 * definition to mark a variable or function parameter as used so
109 * as to avoid a compiler warning
111 #define RTE_SET_USED(x) (void)(x)
114 * Run function before main() with low priority.
116 * The constructor will be run after prioritized constructors.
119 * Constructor function.
121 #define RTE_INIT(func) \
122 static void __attribute__((constructor, used)) func(void)
125 * Run function before main() with high priority.
128 * Constructor function.
130 * Priority number must be above 100.
131 * Lowest number is the first to run.
133 #define RTE_INIT_PRIO(func, prio) \
134 static void __attribute__((constructor(prio), used)) func(void)
137 * Force a function to be inlined
139 #define __rte_always_inline inline __attribute__((always_inline))
142 * Force a function to be noinlined
144 #define __rte_noinline __attribute__((noinline))
146 /*********** Macros for pointer arithmetic ********/
149 * add a byte-value offset from a pointer
151 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
154 * subtract a byte-value offset from a pointer
156 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
159 * get the difference between two pointer values, i.e. how far apart
160 * in bytes are the locations they point two. It is assumed that
161 * ptr1 is greater than ptr2.
163 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
165 /*********** Macros/static functions for doing alignment ********/
169 * Macro to align a pointer to a given power-of-two. The resultant
170 * pointer will be a pointer of the same type as the first parameter, and
171 * point to an address no higher than the first parameter. Second parameter
172 * must be a power-of-two value.
174 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
175 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
178 * Macro to align a value to a given power-of-two. The resultant value
179 * will be of the same type as the first parameter, and will be no
180 * bigger than the first parameter. Second parameter must be a
181 * power-of-two value.
183 #define RTE_ALIGN_FLOOR(val, align) \
184 (typeof(val))((val) & (~((typeof(val))((align) - 1))))
187 * Macro to align a pointer to a given power-of-two. The resultant
188 * pointer will be a pointer of the same type as the first parameter, and
189 * point to an address no lower than the first parameter. Second parameter
190 * must be a power-of-two value.
192 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
193 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
196 * Macro to align a value to a given power-of-two. The resultant value
197 * will be of the same type as the first parameter, and will be no lower
198 * than the first parameter. Second parameter must be a power-of-two
201 #define RTE_ALIGN_CEIL(val, align) \
202 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
205 * Macro to align a pointer to a given power-of-two. The resultant
206 * pointer will be a pointer of the same type as the first parameter, and
207 * point to an address no lower than the first parameter. Second parameter
208 * must be a power-of-two value.
209 * This function is the same as RTE_PTR_ALIGN_CEIL
211 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
214 * Macro to align a value to a given power-of-two. The resultant
215 * value will be of the same type as the first parameter, and
216 * will be no lower than the first parameter. Second parameter
217 * must be a power-of-two value.
218 * This function is the same as RTE_ALIGN_CEIL
220 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
223 * Checks if a pointer is aligned to a given power-of-two value
226 * The pointer whose alignment is to be checked
228 * The power-of-two value to which the ptr should be aligned
231 * True(1) where the pointer is correctly aligned, false(0) otherwise
234 rte_is_aligned(void *ptr, unsigned align)
236 return RTE_PTR_ALIGN(ptr, align) == ptr;
239 /*********** Macros for compile type checks ********/
242 * Triggers an error at compilation time if the condition is true.
245 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
247 extern int RTE_BUILD_BUG_ON_detected_error;
248 #define RTE_BUILD_BUG_ON(condition) do { \
249 ((void)sizeof(char[1 - 2*!!(condition)])); \
251 RTE_BUILD_BUG_ON_detected_error = 1; \
255 /*********** Macros to work with powers of 2 ********/
258 * Returns true if n is a power of 2
261 * @return 1 if true, 0 otherwise
264 rte_is_power_of_2(uint32_t n)
266 return n && !(n & (n - 1));
270 * Aligns input parameter to the next power of 2
273 * The integer value to algin
276 * Input parameter aligned to the next power of 2
278 static inline uint32_t
279 rte_align32pow2(uint32_t x)
292 * Aligns 64b input parameter to the next power of 2
295 * The 64b value to align
298 * Input parameter aligned to the next power of 2
300 static inline uint64_t
301 rte_align64pow2(uint64_t v)
314 /*********** Macros for calculating min and max **********/
317 * Macro to return the minimum of two numbers
319 #define RTE_MIN(a, b) \
321 typeof (a) _a = (a); \
322 typeof (b) _b = (b); \
327 * Macro to return the maximum of two numbers
329 #define RTE_MAX(a, b) \
331 typeof (a) _a = (a); \
332 typeof (b) _b = (b); \
336 /*********** Other general functions / macros ********/
339 * Searches the input parameter for the least significant set bit
340 * (starting from zero).
341 * If a least significant 1 bit is found, its bit index is returned.
342 * If the content of the input parameter is zero, then the content of the return
343 * value is undefined.
345 * input parameter, should not be zero.
347 * least significant set bit in the input parameter.
349 static inline uint32_t
350 rte_bsf32(uint32_t v)
352 return __builtin_ctz(v);
356 * Return the rounded-up log2 of a integer.
359 * The input parameter.
361 * The rounded-up log2 of the input, or 0 if the input is 0.
363 static inline uint32_t
364 rte_log2_u32(uint32_t v)
368 v = rte_align32pow2(v);
373 /** Return the offset of a field in a structure. */
374 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
378 * Return pointer to the wrapping struct instance.
388 * struct child *x = obtain(...);
389 * struct wrapper *w = container_of(x, struct wrapper, c);
392 #define container_of(ptr, type, member) __extension__ ({ \
393 const typeof(((type *)0)->member) *_ptr = (ptr); \
394 __attribute__((unused)) type *_target_ptr = \
396 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
400 #define _RTE_STR(x) #x
401 /** Take a macro value and get a string version of it */
402 #define RTE_STR(x) _RTE_STR(x)
405 * ISO C helpers to modify format strings using variadic macros.
406 * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
407 * An empty %s argument is appended to avoid a dangling comma.
409 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
410 #define RTE_FMT_HEAD(fmt, ...) fmt
411 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
413 /** Mask value of type "tp" for the first "ln" bit set. */
414 #define RTE_LEN2MASK(ln, tp) \
415 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
417 /** Number of elements in the array. */
418 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
421 * Converts a numeric string to the equivalent uint64_t value.
422 * As well as straight number conversion, also recognises the suffixes
423 * k, m and g for kilobytes, megabytes and gigabytes respectively.
425 * If a negative number is passed in i.e. a string with the first non-black
426 * character being "-", zero is returned. Zero is also returned in the case of
427 * an error with the strtoull call in the function.
430 * String containing number to convert.
434 static inline uint64_t
435 rte_str_to_size(const char *str)
438 unsigned long long size;
440 while (isspace((int)*str))
446 size = strtoull(str, &endptr, 0);
451 endptr++; /* allow 1 space gap */
454 case 'G': case 'g': size *= 1024; /* fall-through */
455 case 'M': case 'm': size *= 1024; /* fall-through */
456 case 'K': case 'k': size *= 1024; /* fall-through */
464 * Function to terminate the application immediately, printing an error
465 * message and returning the exit_code back to the shell.
467 * This function never returns
470 * The exit code to be returned by the application
472 * The format string to be used for printing the message. This can include
473 * printf format characters which will be expanded using any further parameters
477 rte_exit(int exit_code, const char *format, ...)
478 __attribute__((noreturn))
479 __attribute__((format(printf, 2, 3)));