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
55 #define typeof __typeof__
62 /** C extension macro for environments lacking C11 features. */
63 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
64 #define RTE_STD_C11 __extension__
69 /** Define GCC_VERSION **/
70 #ifdef RTE_TOOLCHAIN_GCC
71 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
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)));
80 typedef uint64_t unaligned_uint64_t;
81 typedef uint32_t unaligned_uint32_t;
82 typedef uint16_t unaligned_uint16_t;
88 #define __rte_aligned(a) __attribute__((__aligned__(a)))
91 * Force a structure to be packed
93 #define __rte_packed __attribute__((__packed__))
95 /******* Macro to mark functions and fields scheduled for removal *****/
96 #define __rte_deprecated __attribute__((__deprecated__))
98 /*********** Macros to eliminate unused variable warnings ********/
101 * short definition to mark a function parameter unused
103 #define __rte_unused __attribute__((__unused__))
106 * definition to mark a variable or function parameter as used so
107 * as to avoid a compiler warning
109 #define RTE_SET_USED(x) (void)(x)
112 * Force a function to be inlined
114 #define __rte_always_inline inline __attribute__((always_inline))
117 * Force a function to be noinlined
119 #define __rte_noinline __attribute__((noinline))
121 /*********** Macros for pointer arithmetic ********/
124 * add a byte-value offset from a pointer
126 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
129 * subtract a byte-value offset from a pointer
131 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
134 * get the difference between two pointer values, i.e. how far apart
135 * in bytes are the locations they point two. It is assumed that
136 * ptr1 is greater than ptr2.
138 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
140 /*********** Macros/static functions for doing alignment ********/
144 * Macro to align a pointer to a given power-of-two. The resultant
145 * pointer will be a pointer of the same type as the first parameter, and
146 * point to an address no higher than the first parameter. Second parameter
147 * must be a power-of-two value.
149 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
150 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
153 * Macro to align a value to a given power-of-two. The resultant value
154 * will be of the same type as the first parameter, and will be no
155 * bigger than the first parameter. Second parameter must be a
156 * power-of-two value.
158 #define RTE_ALIGN_FLOOR(val, align) \
159 (typeof(val))((val) & (~((typeof(val))((align) - 1))))
162 * Macro to align a pointer to a given power-of-two. The resultant
163 * pointer will be a pointer of the same type as the first parameter, and
164 * point to an address no lower than the first parameter. Second parameter
165 * must be a power-of-two value.
167 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
168 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
171 * Macro to align a value to a given power-of-two. The resultant value
172 * will be of the same type as the first parameter, and will be no lower
173 * than the first parameter. Second parameter must be a power-of-two
176 #define RTE_ALIGN_CEIL(val, align) \
177 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
180 * Macro to align a pointer to a given power-of-two. The resultant
181 * pointer will be a pointer of the same type as the first parameter, and
182 * point to an address no lower than the first parameter. Second parameter
183 * must be a power-of-two value.
184 * This function is the same as RTE_PTR_ALIGN_CEIL
186 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
189 * Macro to align a value to a given power-of-two. The resultant
190 * value will be of the same type as the first parameter, and
191 * will be no lower than the first parameter. Second parameter
192 * must be a power-of-two value.
193 * This function is the same as RTE_ALIGN_CEIL
195 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
198 * Checks if a pointer is aligned to a given power-of-two value
201 * The pointer whose alignment is to be checked
203 * The power-of-two value to which the ptr should be aligned
206 * True(1) where the pointer is correctly aligned, false(0) otherwise
209 rte_is_aligned(void *ptr, unsigned align)
211 return RTE_PTR_ALIGN(ptr, align) == ptr;
214 /*********** Macros for compile type checks ********/
217 * Triggers an error at compilation time if the condition is true.
220 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
222 extern int RTE_BUILD_BUG_ON_detected_error;
223 #define RTE_BUILD_BUG_ON(condition) do { \
224 ((void)sizeof(char[1 - 2*!!(condition)])); \
226 RTE_BUILD_BUG_ON_detected_error = 1; \
230 /*********** Macros to work with powers of 2 ********/
233 * Returns true if n is a power of 2
236 * @return 1 if true, 0 otherwise
239 rte_is_power_of_2(uint32_t n)
241 return n && !(n & (n - 1));
245 * Aligns input parameter to the next power of 2
248 * The integer value to algin
251 * Input parameter aligned to the next power of 2
253 static inline uint32_t
254 rte_align32pow2(uint32_t x)
267 * Aligns 64b input parameter to the next power of 2
270 * The 64b value to align
273 * Input parameter aligned to the next power of 2
275 static inline uint64_t
276 rte_align64pow2(uint64_t v)
289 /*********** Macros for calculating min and max **********/
292 * Macro to return the minimum of two numbers
294 #define RTE_MIN(a, b) \
296 typeof (a) _a = (a); \
297 typeof (b) _b = (b); \
302 * Macro to return the maximum of two numbers
304 #define RTE_MAX(a, b) \
306 typeof (a) _a = (a); \
307 typeof (b) _b = (b); \
311 /*********** Other general functions / macros ********/
314 * Searches the input parameter for the least significant set bit
315 * (starting from zero).
316 * If a least significant 1 bit is found, its bit index is returned.
317 * If the content of the input parameter is zero, then the content of the return
318 * value is undefined.
320 * input parameter, should not be zero.
322 * least significant set bit in the input parameter.
324 static inline uint32_t
325 rte_bsf32(uint32_t v)
327 return __builtin_ctz(v);
331 * Return the rounded-up log2 of a integer.
334 * The input parameter.
336 * The rounded-up log2 of the input, or 0 if the input is 0.
338 static inline uint32_t
339 rte_log2_u32(uint32_t v)
343 v = rte_align32pow2(v);
348 /** Return the offset of a field in a structure. */
349 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
353 * Return pointer to the wrapping struct instance.
363 * struct child *x = obtain(...);
364 * struct wrapper *w = container_of(x, struct wrapper, c);
367 #define container_of(ptr, type, member) __extension__ ({ \
368 const typeof(((type *)0)->member) *_ptr = (ptr); \
369 __attribute__((unused)) type *_target_ptr = \
371 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
375 #define _RTE_STR(x) #x
376 /** Take a macro value and get a string version of it */
377 #define RTE_STR(x) _RTE_STR(x)
380 * ISO C helpers to modify format strings using variadic macros.
381 * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
382 * An empty %s argument is appended to avoid a dangling comma.
384 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
385 #define RTE_FMT_HEAD(fmt, ...) fmt
386 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
388 /** Mask value of type "tp" for the first "ln" bit set. */
389 #define RTE_LEN2MASK(ln, tp) \
390 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
392 /** Number of elements in the array. */
393 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
396 * Converts a numeric string to the equivalent uint64_t value.
397 * As well as straight number conversion, also recognises the suffixes
398 * k, m and g for kilobytes, megabytes and gigabytes respectively.
400 * If a negative number is passed in i.e. a string with the first non-black
401 * character being "-", zero is returned. Zero is also returned in the case of
402 * an error with the strtoull call in the function.
405 * String containing number to convert.
409 static inline uint64_t
410 rte_str_to_size(const char *str)
413 unsigned long long size;
415 while (isspace((int)*str))
421 size = strtoull(str, &endptr, 0);
426 endptr++; /* allow 1 space gap */
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 */
439 * Function to terminate the application immediately, printing an error
440 * message and returning the exit_code back to the shell.
442 * This function never returns
445 * The exit code to be returned by the application
447 * The format string to be used for printing the message. This can include
448 * printf format characters which will be expanded using any further parameters
452 rte_exit(int exit_code, const char *format, ...)
453 __attribute__((noreturn))
454 __attribute__((format(printf, 2, 3)));