1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 * Generic, commonly-used macro and inline function definitions
25 #include <rte_config.h>
28 #define typeof __typeof__
35 /** C extension macro for environments lacking C11 features. */
36 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
37 #define RTE_STD_C11 __extension__
42 /** Define GCC_VERSION **/
43 #ifdef RTE_TOOLCHAIN_GCC
44 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
48 #ifdef RTE_ARCH_STRICT_ALIGN
49 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
50 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
51 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
53 typedef uint64_t unaligned_uint64_t;
54 typedef uint32_t unaligned_uint32_t;
55 typedef uint16_t unaligned_uint16_t;
61 #define __rte_aligned(a) __attribute__((__aligned__(a)))
64 * Force a structure to be packed
66 #define __rte_packed __attribute__((__packed__))
68 /******* Macro to mark functions and fields scheduled for removal *****/
69 #define __rte_deprecated __attribute__((__deprecated__))
71 /*********** Macros to eliminate unused variable warnings ********/
74 * short definition to mark a function parameter unused
76 #define __rte_unused __attribute__((__unused__))
79 * definition to mark a variable or function parameter as used so
80 * as to avoid a compiler warning
82 #define RTE_SET_USED(x) (void)(x)
85 * Run function before main() with low priority.
87 * The constructor will be run after prioritized constructors.
90 * Constructor function.
92 #define RTE_INIT(func) \
93 static void __attribute__((constructor, used)) func(void)
96 * Run function before main() with high priority.
99 * Constructor function.
101 * Priority number must be above 100.
102 * Lowest number is the first to run.
104 #define RTE_INIT_PRIO(func, prio) \
105 static void __attribute__((constructor(prio), used)) func(void)
108 * Force a function to be inlined
110 #define __rte_always_inline inline __attribute__((always_inline))
113 * Force a function to be noinlined
115 #define __rte_noinline __attribute__((noinline))
117 /*********** Macros for pointer arithmetic ********/
120 * add a byte-value offset from a pointer
122 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
125 * subtract a byte-value offset from a pointer
127 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
130 * get the difference between two pointer values, i.e. how far apart
131 * in bytes are the locations they point two. It is assumed that
132 * ptr1 is greater than ptr2.
134 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
136 /*********** Macros/static functions for doing alignment ********/
140 * Macro to align a pointer to a given power-of-two. The resultant
141 * pointer will be a pointer of the same type as the first parameter, and
142 * point to an address no higher than the first parameter. Second parameter
143 * must be a power-of-two value.
145 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
146 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
149 * Macro to align a value to a given power-of-two. The resultant value
150 * will be of the same type as the first parameter, and will be no
151 * bigger than the first parameter. Second parameter must be a
152 * power-of-two value.
154 #define RTE_ALIGN_FLOOR(val, align) \
155 (typeof(val))((val) & (~((typeof(val))((align) - 1))))
158 * Macro to align a pointer to a given power-of-two. The resultant
159 * pointer will be a pointer of the same type as the first parameter, and
160 * point to an address no lower than the first parameter. Second parameter
161 * must be a power-of-two value.
163 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
164 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
167 * Macro to align a value to a given power-of-two. The resultant value
168 * will be of the same type as the first parameter, and will be no lower
169 * than the first parameter. Second parameter must be a power-of-two
172 #define RTE_ALIGN_CEIL(val, align) \
173 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
176 * Macro to align a pointer to a given power-of-two. The resultant
177 * pointer will be a pointer of the same type as the first parameter, and
178 * point to an address no lower than the first parameter. Second parameter
179 * must be a power-of-two value.
180 * This function is the same as RTE_PTR_ALIGN_CEIL
182 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
185 * Macro to align a value to a given power-of-two. The resultant
186 * value will be of the same type as the first parameter, and
187 * will be no lower than the first parameter. Second parameter
188 * must be a power-of-two value.
189 * This function is the same as RTE_ALIGN_CEIL
191 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
194 * Macro to align a value to the multiple of given value. The resultant
195 * value will be of the same type as the first parameter and will be no lower
196 * than the first parameter.
198 #define RTE_ALIGN_MUL_CEIL(v, mul) \
199 (((v + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
202 * Macro to align a value to the multiple of given value. The resultant
203 * value will be of the same type as the first parameter and will be no higher
204 * than the first parameter.
206 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
207 ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
210 * Checks if a pointer is aligned to a given power-of-two value
213 * The pointer whose alignment is to be checked
215 * The power-of-two value to which the ptr should be aligned
218 * True(1) where the pointer is correctly aligned, false(0) otherwise
221 rte_is_aligned(void *ptr, unsigned align)
223 return RTE_PTR_ALIGN(ptr, align) == ptr;
226 /*********** Macros for compile type checks ********/
229 * Triggers an error at compilation time if the condition is true.
232 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
234 extern int RTE_BUILD_BUG_ON_detected_error;
235 #define RTE_BUILD_BUG_ON(condition) do { \
236 ((void)sizeof(char[1 - 2*!!(condition)])); \
238 RTE_BUILD_BUG_ON_detected_error = 1; \
242 /*********** Macros to work with powers of 2 ********/
245 * Returns true if n is a power of 2
248 * @return 1 if true, 0 otherwise
251 rte_is_power_of_2(uint32_t n)
253 return n && !(n & (n - 1));
257 * Aligns input parameter to the next power of 2
260 * The integer value to algin
263 * Input parameter aligned to the next power of 2
265 static inline uint32_t
266 rte_align32pow2(uint32_t x)
279 * Aligns 64b input parameter to the next power of 2
282 * The 64b value to align
285 * Input parameter aligned to the next power of 2
287 static inline uint64_t
288 rte_align64pow2(uint64_t v)
301 /*********** Macros for calculating min and max **********/
304 * Macro to return the minimum of two numbers
306 #define RTE_MIN(a, b) \
308 typeof (a) _a = (a); \
309 typeof (b) _b = (b); \
314 * Macro to return the maximum of two numbers
316 #define RTE_MAX(a, b) \
318 typeof (a) _a = (a); \
319 typeof (b) _b = (b); \
323 /*********** Other general functions / macros ********/
326 * Searches the input parameter for the least significant set bit
327 * (starting from zero).
328 * If a least significant 1 bit is found, its bit index is returned.
329 * If the content of the input parameter is zero, then the content of the return
330 * value is undefined.
332 * input parameter, should not be zero.
334 * least significant set bit in the input parameter.
336 static inline uint32_t
337 rte_bsf32(uint32_t v)
339 return __builtin_ctz(v);
343 * Return the rounded-up log2 of a integer.
346 * The input parameter.
348 * The rounded-up log2 of the input, or 0 if the input is 0.
350 static inline uint32_t
351 rte_log2_u32(uint32_t v)
355 v = rte_align32pow2(v);
360 /** Return the offset of a field in a structure. */
361 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
365 * Return pointer to the wrapping struct instance.
375 * struct child *x = obtain(...);
376 * struct wrapper *w = container_of(x, struct wrapper, c);
379 #define container_of(ptr, type, member) __extension__ ({ \
380 const typeof(((type *)0)->member) *_ptr = (ptr); \
381 __attribute__((unused)) type *_target_ptr = \
383 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
387 #define _RTE_STR(x) #x
388 /** Take a macro value and get a string version of it */
389 #define RTE_STR(x) _RTE_STR(x)
392 * ISO C helpers to modify format strings using variadic macros.
393 * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
394 * An empty %s argument is appended to avoid a dangling comma.
396 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
397 #define RTE_FMT_HEAD(fmt, ...) fmt
398 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
400 /** Mask value of type "tp" for the first "ln" bit set. */
401 #define RTE_LEN2MASK(ln, tp) \
402 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
404 /** Number of elements in the array. */
405 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
408 * Converts a numeric string to the equivalent uint64_t value.
409 * As well as straight number conversion, also recognises the suffixes
410 * k, m and g for kilobytes, megabytes and gigabytes respectively.
412 * If a negative number is passed in i.e. a string with the first non-black
413 * character being "-", zero is returned. Zero is also returned in the case of
414 * an error with the strtoull call in the function.
417 * String containing number to convert.
421 static inline uint64_t
422 rte_str_to_size(const char *str)
425 unsigned long long size;
427 while (isspace((int)*str))
433 size = strtoull(str, &endptr, 0);
438 endptr++; /* allow 1 space gap */
441 case 'G': case 'g': size *= 1024; /* fall-through */
442 case 'M': case 'm': size *= 1024; /* fall-through */
443 case 'K': case 'k': size *= 1024; /* fall-through */
451 * Function to terminate the application immediately, printing an error
452 * message and returning the exit_code back to the shell.
454 * This function never returns
457 * The exit code to be returned by the application
459 * The format string to be used for printing the message. This can include
460 * printf format characters which will be expanded using any further parameters
464 rte_exit(int exit_code, const char *format, ...)
465 __attribute__((noreturn))
466 __attribute__((format(printf, 2, 3)));