4 * Copyright(c) 2010-2012 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.
35 #ifndef _RTE_COMMON_H_
36 #define _RTE_COMMON_H_
41 * Generic, commonly-used macro and inline function definitions
55 /*********** Macros to eliminate unused variable warnings ********/
58 * short definition to mark a function parameter unused
60 #define __rte_unused __attribute__((__unused__))
63 * definition to mark a variable or function parameter as used so
64 * as to avoid a compiler warning
66 #define RTE_SET_USED(x) (void)(x)
68 /*********** Macros for pointer arithmetic ********/
71 * add a byte-value offset from a pointer
73 #define RTE_PTR_ADD(ptr, x) ((typeof(ptr))((uintptr_t)ptr + (x)))
76 * subtract a byte-value offset from a pointer
78 #define RTE_PTR_SUB(ptr, x) ((typeof(ptr))((uintptr_t)ptr - (x)))
81 * get the difference between two pointer values, i.e. how far apart
82 * in bytes are the locations they point two. It is assumed that
83 * ptr1 is greater than ptr2.
85 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
87 /*********** Macros/static functions for doing alignment ********/
90 * Function which rounds an unsigned int down to a given power-of-two value.
91 * Takes uintptr_t types as parameters, as this type of operation is most
92 * commonly done for pointer alignment. (See also RTE_ALIGN_FLOOR,
93 * RTE_ALIGN_CEIL, RTE_ALIGN, RTE_PTR_ALIGN_FLOOR, RTE_PTR_ALIGN_CEL,
94 * RTE_PTR_ALIGN macros)
96 * The value to be rounded down
98 * The power-of-two of which the result must be a multiple.
100 * Function returns a properly aligned value where align is a power-of-two.
101 * If align is not a power-of-two, result will be incorrect.
103 static inline uintptr_t
104 rte_align_floor_int(uintptr_t ptr, uintptr_t align)
106 return (ptr & ~(align - 1));
110 * Macro to align a pointer to a given power-of-two. The resultant
111 * pointer will be a pointer of the same type as the first parameter, and
112 * point to an address no higher than the first parameter. Second parameter
113 * must be a power-of-two value.
115 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
116 (typeof(ptr))rte_align_floor_int((uintptr_t)ptr, align)
119 * Macro to align a value to a given power-of-two. The resultant value
120 * will be of the same type as the first parameter, and will be no
121 * bigger than the first parameter. Second parameter must be a
122 * power-of-two value.
124 #define RTE_ALIGN_FLOOR(val, align) \
125 (typeof(val))(val & (~((typeof(val))(align - 1))))
128 * Macro to align a pointer to a given power-of-two. The resultant
129 * pointer will be a pointer of the same type as the first parameter, and
130 * point to an address no lower than the first parameter. Second parameter
131 * must be a power-of-two value.
133 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
134 RTE_PTR_ALIGN_FLOOR(RTE_PTR_ADD(ptr, align - 1), align)
137 * Macro to align a value to a given power-of-two. The resultant value
138 * will be of the same type as the first parameter, and will be no lower
139 * than the first parameter. Second parameter must be a power-of-two
142 #define RTE_ALIGN_CEIL(val, align) \
143 RTE_ALIGN_FLOOR((val + ((typeof(val)) align - 1)), align)
146 * Macro to align a pointer to a given power-of-two. The resultant
147 * pointer will be a pointer of the same type as the first parameter, and
148 * point to an address no lower than the first parameter. Second parameter
149 * must be a power-of-two value.
150 * This function is the same as RTE_PTR_ALIGN_CEIL
152 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
155 * Macro to align a value to a given power-of-two. The resultant
156 * value will be of the same type as the first parameter, and
157 * will be no lower than the first parameter. Second parameter
158 * must be a power-of-two value.
159 * This function is the same as RTE_ALIGN_CEIL
161 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
164 * Checks if a pointer is aligned to a given power-of-two value
167 * The pointer whose alignment is to be checked
169 * The power-of-two value to which the ptr should be aligned
172 * True(1) where the pointer is correctly aligned, false(0) otherwise
175 rte_is_aligned(void *ptr, unsigned align)
177 return RTE_PTR_ALIGN(ptr, align) == ptr;
180 /*********** Macros for compile type checks ********/
183 * Triggers an error at compilation time if the condition is true.
186 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
188 extern int RTE_BUILD_BUG_ON_detected_error;
189 #define RTE_BUILD_BUG_ON(condition) do { \
190 ((void)sizeof(char[1 - 2*!!(condition)])); \
192 RTE_BUILD_BUG_ON_detected_error = 1; \
196 /*********** Macros to work with powers of 2 ********/
199 * Returns true if n is a power of 2
202 * @return 1 if true, 0 otherwise
205 rte_is_power_of_2(uint32_t n)
207 return ((n-1) & n) == 0;
211 * Aligns input parameter to the next power of 2
214 * The integer value to algin
217 * Input parameter aligned to the next power of 2
219 static inline uint32_t
220 rte_align32pow2(uint32_t x)
232 /*********** Macros for calculating min and max **********/
235 * Macro to return the minimum of two numbers
237 #define RTE_MIN(a, b) ({ \
238 typeof (a) _a = (a); \
239 typeof (b) _b = (b); \
244 * Macro to return the maximum of two numbers
246 #define RTE_MAX(a, b) ({ \
247 typeof (a) _a = (a); \
248 typeof (b) _b = (b); \
252 /*********** Other general functions / macros ********/
255 * PAUSE instruction for tight loops (avoid busy waiting)
260 asm volatile ("pause");
264 * Searches the input parameter for the least significant set bit
265 * (starting from zero).
266 * If a least significant 1 bit is found, its bit index is returned.
267 * If the content of the input paramer is zero, then the content of the return
268 * value is undefined.
270 * input parameter, should not be zero.
272 * least significant set bit in the input parameter.
274 static inline uint32_t
275 rte_bsf32(uint32_t v)
284 /** Return the offset of a field in a structure. */
285 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
288 #define _RTE_STR(x) #x
289 /** Take a macro value and get a string version of it */
290 #define RTE_STR(x) _RTE_STR(x)
293 * Converts a numeric string to the equivalent uint64_t value.
294 * As well as straight number conversion, also recognises the suffixes
295 * k, m and g for kilobytes, megabytes and gigabytes respectively.
297 * If a negative number is passed in i.e. a string with the first non-black
298 * character being "-", zero is returned. Zero is also returned in the case of
299 * an error with the strtoull call in the function.
302 * String containing number to convert.
306 static inline uint64_t
307 rte_str_to_size(const char *str)
310 unsigned long long size;
312 while (isspace((int)*str))
318 size = strtoull(str, &endptr, 0);
323 endptr++; /* allow 1 space gap */
326 case 'G': case 'g': size *= 1024; /* fall-through */
327 case 'M': case 'm': size *= 1024; /* fall-through */
328 case 'K': case 'k': size *= 1024; /* fall-through */
336 * Function to terminate the application immediately, printing an error
337 * message and returning the exit_code back to the shell.
339 * This function never returns
342 * The exit code to be returned by the application
344 * The format string to be used for printing the message. This can include
345 * printf format characters which will be expanded using any further parameters
349 rte_exit(int exit_code, const char *format, ...)
350 __attribute__((noreturn))
351 __attribute__((format(printf, 2, 3)));