eal/freebsd: fix use of newer cpuset macros
[dpdk.git] / lib / eal / include / rte_common.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2019 Intel Corporation
3  */
4
5 #ifndef _RTE_COMMON_H_
6 #define _RTE_COMMON_H_
7
8 /**
9  * @file
10  *
11  * Generic, commonly-used macro and inline function definitions
12  * for DPDK.
13  */
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <limits.h>
24
25 #include <rte_config.h>
26
27 /* OS specific include */
28 #include <rte_os.h>
29
30 #ifndef typeof
31 #define typeof __typeof__
32 #endif
33
34 #ifndef __cplusplus
35 #ifndef asm
36 #define asm __asm__
37 #endif
38 #endif
39
40 /** C extension macro for environments lacking C11 features. */
41 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
42 #define RTE_STD_C11 __extension__
43 #else
44 #define RTE_STD_C11
45 #endif
46
47 /*
48  * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
49  * while a host application (like pmdinfogen) may have another compiler.
50  * RTE_CC_IS_GNU is true if the file is compiled with GCC,
51  * no matter it is a target or host application.
52  */
53 #define RTE_CC_IS_GNU 0
54 #if defined __clang__
55 #define RTE_CC_CLANG
56 #elif defined __INTEL_COMPILER
57 #define RTE_CC_ICC
58 #elif defined __GNUC__
59 #define RTE_CC_GCC
60 #undef RTE_CC_IS_GNU
61 #define RTE_CC_IS_GNU 1
62 #endif
63 #if RTE_CC_IS_GNU
64 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 +  \
65                 __GNUC_PATCHLEVEL__)
66 #endif
67
68 /**
69  * Force alignment
70  */
71 #define __rte_aligned(a) __attribute__((__aligned__(a)))
72
73 #ifdef RTE_ARCH_STRICT_ALIGN
74 typedef uint64_t unaligned_uint64_t __rte_aligned(1);
75 typedef uint32_t unaligned_uint32_t __rte_aligned(1);
76 typedef uint16_t unaligned_uint16_t __rte_aligned(1);
77 #else
78 typedef uint64_t unaligned_uint64_t;
79 typedef uint32_t unaligned_uint32_t;
80 typedef uint16_t unaligned_uint16_t;
81 #endif
82
83 /**
84  * Force a structure to be packed
85  */
86 #define __rte_packed __attribute__((__packed__))
87
88 /**
89  * Macro to mark a type that is not subject to type-based aliasing rules
90  */
91 #define __rte_may_alias __attribute__((__may_alias__))
92
93 /******* Macro to mark functions and fields scheduled for removal *****/
94 #define __rte_deprecated        __attribute__((__deprecated__))
95 #define __rte_deprecated_msg(msg)       __attribute__((__deprecated__(msg)))
96
97 /**
98  *  Macro to mark macros and defines scheduled for removal
99  */
100 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
101 #define RTE_PRAGMA(x)  _Pragma(#x)
102 #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
103 #define RTE_DEPRECATED(x)  RTE_PRAGMA_WARNING(#x is deprecated)
104 #else
105 #define RTE_DEPRECATED(x)
106 #endif
107
108 /**
109  * Mark a function or variable to a weak reference.
110  */
111 #define __rte_weak __attribute__((__weak__))
112
113 /**
114  * Force symbol to be generated even if it appears to be unused.
115  */
116 #define __rte_used __attribute__((used))
117
118 /*********** Macros to eliminate unused variable warnings ********/
119
120 /**
121  * short definition to mark a function parameter unused
122  */
123 #define __rte_unused __attribute__((__unused__))
124
125 /**
126  * Mark pointer as restricted with regard to pointer aliasing.
127  */
128 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
129 #define __rte_restrict __restrict
130 #else
131 #define __rte_restrict restrict
132 #endif
133
134 /**
135  * definition to mark a variable or function parameter as used so
136  * as to avoid a compiler warning
137  */
138 #define RTE_SET_USED(x) (void)(x)
139
140 /**
141  * Check format string and its arguments at compile-time.
142  *
143  * GCC on Windows assumes MS-specific format string by default,
144  * even if the underlying stdio implementation is ANSI-compliant,
145  * so this must be overridden.
146  */
147 #if RTE_CC_IS_GNU
148 #define __rte_format_printf(format_index, first_arg) \
149         __attribute__((format(gnu_printf, format_index, first_arg)))
150 #else
151 #define __rte_format_printf(format_index, first_arg) \
152         __attribute__((format(printf, format_index, first_arg)))
153 #endif
154
155 /**
156  * Tells compiler that the function returns a value that points to
157  * memory, where the size is given by the one or two arguments.
158  * Used by compiler to validate object size.
159  */
160 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
161 #define __rte_alloc_size(...) \
162         __attribute__((alloc_size(__VA_ARGS__)))
163 #else
164 #define __rte_alloc_size(...)
165 #endif
166
167 #define RTE_PRIORITY_LOG 101
168 #define RTE_PRIORITY_BUS 110
169 #define RTE_PRIORITY_CLASS 120
170 #define RTE_PRIORITY_LAST 65535
171
172 #define RTE_PRIO(prio) \
173         RTE_PRIORITY_ ## prio
174
175 /**
176  * Run function before main() with high priority.
177  *
178  * @param func
179  *   Constructor function.
180  * @param prio
181  *   Priority number must be above 100.
182  *   Lowest number is the first to run.
183  */
184 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
185 #define RTE_INIT_PRIO(func, prio) \
186 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
187 #endif
188
189 /**
190  * Run function before main() with low priority.
191  *
192  * The constructor will be run after prioritized constructors.
193  *
194  * @param func
195  *   Constructor function.
196  */
197 #define RTE_INIT(func) \
198         RTE_INIT_PRIO(func, LAST)
199
200 /**
201  * Run after main() with low priority.
202  *
203  * @param func
204  *   Destructor function name.
205  * @param prio
206  *   Priority number must be above 100.
207  *   Lowest number is the last to run.
208  */
209 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
210 #define RTE_FINI_PRIO(func, prio) \
211 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
212 #endif
213
214 /**
215  * Run after main() with high priority.
216  *
217  * The destructor will be run *before* prioritized destructors.
218  *
219  * @param func
220  *   Destructor function name.
221  */
222 #define RTE_FINI(func) \
223         RTE_FINI_PRIO(func, LAST)
224
225 /**
226  * Hint never returning function
227  */
228 #define __rte_noreturn __attribute__((noreturn))
229
230 /**
231  * Issue a warning in case the function's return value is ignored.
232  *
233  * The use of this attribute should be restricted to cases where
234  * ignoring the marked function's return value is almost always a
235  * bug. With GCC, some effort is required to make clear that ignoring
236  * the return value is intentional. The usual void-casting method to
237  * mark something unused as used does not suppress the warning with
238  * this compiler.
239  *
240  * @code{.c}
241  * __rte_warn_unused_result int foo();
242  *
243  * void ignore_foo_result(void) {
244  *         foo(); // generates a warning with all compilers
245  *
246  *         (void)foo(); // still generates the warning with GCC (but not clang)
247  *
248  *         int unused __rte_unused;
249  *         unused = foo(); // does the trick with all compilers
250  *  }
251  * @endcode
252  */
253 #define __rte_warn_unused_result __attribute__((warn_unused_result))
254
255 /**
256  * Force a function to be inlined
257  */
258 #define __rte_always_inline inline __attribute__((always_inline))
259
260 /**
261  * Force a function to be noinlined
262  */
263 #define __rte_noinline __attribute__((noinline))
264
265 /**
266  * Hint function in the hot path
267  */
268 #define __rte_hot __attribute__((hot))
269
270 /**
271  * Hint function in the cold path
272  */
273 #define __rte_cold __attribute__((cold))
274
275 /**
276  * Disable AddressSanitizer on some code
277  */
278 #ifdef RTE_MALLOC_ASAN
279 #ifdef RTE_CC_CLANG
280 #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
281 #else
282 #define __rte_no_asan __attribute__((no_sanitize_address))
283 #endif
284 #else /* ! RTE_MALLOC_ASAN */
285 #define __rte_no_asan
286 #endif
287
288 /*********** Macros for pointer arithmetic ********/
289
290 /**
291  * add a byte-value offset to a pointer
292  */
293 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
294
295 /**
296  * subtract a byte-value offset from a pointer
297  */
298 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
299
300 /**
301  * get the difference between two pointer values, i.e. how far apart
302  * in bytes are the locations they point two. It is assumed that
303  * ptr1 is greater than ptr2.
304  */
305 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
306
307 /**
308  * Workaround to cast a const field of a structure to non-const type.
309  */
310 #define RTE_CAST_FIELD(var, field, type) \
311         (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
312
313 /*********** Macros/static functions for doing alignment ********/
314
315
316 /**
317  * Macro to align a pointer to a given power-of-two. The resultant
318  * pointer will be a pointer of the same type as the first parameter, and
319  * point to an address no higher than the first parameter. Second parameter
320  * must be a power-of-two value.
321  */
322 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
323         ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
324
325 /**
326  * Macro to align a value to a given power-of-two. The resultant value
327  * will be of the same type as the first parameter, and will be no
328  * bigger than the first parameter. Second parameter must be a
329  * power-of-two value.
330  */
331 #define RTE_ALIGN_FLOOR(val, align) \
332         (typeof(val))((val) & (~((typeof(val))((align) - 1))))
333
334 /**
335  * Macro to align a pointer to a given power-of-two. The resultant
336  * pointer will be a pointer of the same type as the first parameter, and
337  * point to an address no lower than the first parameter. Second parameter
338  * must be a power-of-two value.
339  */
340 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
341         RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
342
343 /**
344  * Macro to align a value to a given power-of-two. The resultant value
345  * will be of the same type as the first parameter, and will be no lower
346  * than the first parameter. Second parameter must be a power-of-two
347  * value.
348  */
349 #define RTE_ALIGN_CEIL(val, align) \
350         RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
351
352 /**
353  * Macro to align a pointer to a given power-of-two. The resultant
354  * pointer will be a pointer of the same type as the first parameter, and
355  * point to an address no lower than the first parameter. Second parameter
356  * must be a power-of-two value.
357  * This function is the same as RTE_PTR_ALIGN_CEIL
358  */
359 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
360
361 /**
362  * Macro to align a value to a given power-of-two. The resultant
363  * value will be of the same type as the first parameter, and
364  * will be no lower than the first parameter. Second parameter
365  * must be a power-of-two value.
366  * This function is the same as RTE_ALIGN_CEIL
367  */
368 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
369
370 /**
371  * Macro to align a value to the multiple of given value. The resultant
372  * value will be of the same type as the first parameter and will be no lower
373  * than the first parameter.
374  */
375 #define RTE_ALIGN_MUL_CEIL(v, mul) \
376         ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
377
378 /**
379  * Macro to align a value to the multiple of given value. The resultant
380  * value will be of the same type as the first parameter and will be no higher
381  * than the first parameter.
382  */
383 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
384         (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
385
386 /**
387  * Macro to align value to the nearest multiple of the given value.
388  * The resultant value might be greater than or less than the first parameter
389  * whichever difference is the lowest.
390  */
391 #define RTE_ALIGN_MUL_NEAR(v, mul)                              \
392         ({                                                      \
393                 typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul);    \
394                 typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul);  \
395                 (ceil - (v)) > ((v) - floor) ? floor : ceil;    \
396         })
397
398 /**
399  * Checks if a pointer is aligned to a given power-of-two value
400  *
401  * @param ptr
402  *   The pointer whose alignment is to be checked
403  * @param align
404  *   The power-of-two value to which the ptr should be aligned
405  *
406  * @return
407  *   True(1) where the pointer is correctly aligned, false(0) otherwise
408  */
409 static inline int
410 rte_is_aligned(void *ptr, unsigned align)
411 {
412         return RTE_PTR_ALIGN(ptr, align) == ptr;
413 }
414
415 /*********** Macros for compile type checks ********/
416
417 /**
418  * Triggers an error at compilation time if the condition is true.
419  */
420 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
421
422 /*********** Cache line related macros ********/
423
424 /** Cache line mask. */
425 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
426
427 /** Return the first cache-aligned value greater or equal to size. */
428 #define RTE_CACHE_LINE_ROUNDUP(size) \
429         (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / \
430         RTE_CACHE_LINE_SIZE))
431
432 /** Cache line size in terms of log2 */
433 #if RTE_CACHE_LINE_SIZE == 64
434 #define RTE_CACHE_LINE_SIZE_LOG2 6
435 #elif RTE_CACHE_LINE_SIZE == 128
436 #define RTE_CACHE_LINE_SIZE_LOG2 7
437 #else
438 #error "Unsupported cache line size"
439 #endif
440
441 /** Minimum Cache line size. */
442 #define RTE_CACHE_LINE_MIN_SIZE 64
443
444 /** Force alignment to cache line. */
445 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
446
447 /** Force minimum cache line alignment. */
448 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
449
450 /*********** PA/IOVA type definitions ********/
451
452 /** Physical address */
453 typedef uint64_t phys_addr_t;
454 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
455
456 /**
457  * IO virtual address type.
458  * When the physical addressing mode (IOVA as PA) is in use,
459  * the translation from an IO virtual address (IOVA) to a physical address
460  * is a direct mapping, i.e. the same value.
461  * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
462  */
463 typedef uint64_t rte_iova_t;
464 #define RTE_BAD_IOVA ((rte_iova_t)-1)
465
466 /*********** Structure alignment markers ********/
467
468 /** Generic marker for any place in a structure. */
469 __extension__ typedef void    *RTE_MARKER[0];
470 /** Marker for 1B alignment in a structure. */
471 __extension__ typedef uint8_t  RTE_MARKER8[0];
472 /** Marker for 2B alignment in a structure. */
473 __extension__ typedef uint16_t RTE_MARKER16[0];
474 /** Marker for 4B alignment in a structure. */
475 __extension__ typedef uint32_t RTE_MARKER32[0];
476 /** Marker for 8B alignment in a structure. */
477 __extension__ typedef uint64_t RTE_MARKER64[0];
478
479 /**
480  * Combines 32b inputs most significant set bits into the least
481  * significant bits to construct a value with the same MSBs as x
482  * but all 1's under it.
483  *
484  * @param x
485  *    The integer whose MSBs need to be combined with its LSBs
486  * @return
487  *    The combined value.
488  */
489 static inline uint32_t
490 rte_combine32ms1b(uint32_t x)
491 {
492         x |= x >> 1;
493         x |= x >> 2;
494         x |= x >> 4;
495         x |= x >> 8;
496         x |= x >> 16;
497
498         return x;
499 }
500
501 /**
502  * Combines 64b inputs most significant set bits into the least
503  * significant bits to construct a value with the same MSBs as x
504  * but all 1's under it.
505  *
506  * @param v
507  *    The integer whose MSBs need to be combined with its LSBs
508  * @return
509  *    The combined value.
510  */
511 static inline uint64_t
512 rte_combine64ms1b(uint64_t v)
513 {
514         v |= v >> 1;
515         v |= v >> 2;
516         v |= v >> 4;
517         v |= v >> 8;
518         v |= v >> 16;
519         v |= v >> 32;
520
521         return v;
522 }
523
524 /*********** Macros to work with powers of 2 ********/
525
526 /**
527  * Macro to return 1 if n is a power of 2, 0 otherwise
528  */
529 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
530
531 /**
532  * Returns true if n is a power of 2
533  * @param n
534  *     Number to check
535  * @return 1 if true, 0 otherwise
536  */
537 static inline int
538 rte_is_power_of_2(uint32_t n)
539 {
540         return n && !(n & (n - 1));
541 }
542
543 /**
544  * Aligns input parameter to the next power of 2
545  *
546  * @param x
547  *   The integer value to align
548  *
549  * @return
550  *   Input parameter aligned to the next power of 2
551  */
552 static inline uint32_t
553 rte_align32pow2(uint32_t x)
554 {
555         x--;
556         x = rte_combine32ms1b(x);
557
558         return x + 1;
559 }
560
561 /**
562  * Aligns input parameter to the previous power of 2
563  *
564  * @param x
565  *   The integer value to align
566  *
567  * @return
568  *   Input parameter aligned to the previous power of 2
569  */
570 static inline uint32_t
571 rte_align32prevpow2(uint32_t x)
572 {
573         x = rte_combine32ms1b(x);
574
575         return x - (x >> 1);
576 }
577
578 /**
579  * Aligns 64b input parameter to the next power of 2
580  *
581  * @param v
582  *   The 64b value to align
583  *
584  * @return
585  *   Input parameter aligned to the next power of 2
586  */
587 static inline uint64_t
588 rte_align64pow2(uint64_t v)
589 {
590         v--;
591         v = rte_combine64ms1b(v);
592
593         return v + 1;
594 }
595
596 /**
597  * Aligns 64b input parameter to the previous power of 2
598  *
599  * @param v
600  *   The 64b value to align
601  *
602  * @return
603  *   Input parameter aligned to the previous power of 2
604  */
605 static inline uint64_t
606 rte_align64prevpow2(uint64_t v)
607 {
608         v = rte_combine64ms1b(v);
609
610         return v - (v >> 1);
611 }
612
613 /*********** Macros for calculating min and max **********/
614
615 /**
616  * Macro to return the minimum of two numbers
617  */
618 #define RTE_MIN(a, b) \
619         __extension__ ({ \
620                 typeof (a) _a = (a); \
621                 typeof (b) _b = (b); \
622                 _a < _b ? _a : _b; \
623         })
624
625 /**
626  * Macro to return the maximum of two numbers
627  */
628 #define RTE_MAX(a, b) \
629         __extension__ ({ \
630                 typeof (a) _a = (a); \
631                 typeof (b) _b = (b); \
632                 _a > _b ? _a : _b; \
633         })
634
635 /*********** Other general functions / macros ********/
636
637 /**
638  * Searches the input parameter for the least significant set bit
639  * (starting from zero).
640  * If a least significant 1 bit is found, its bit index is returned.
641  * If the content of the input parameter is zero, then the content of the return
642  * value is undefined.
643  * @param v
644  *     input parameter, should not be zero.
645  * @return
646  *     least significant set bit in the input parameter.
647  */
648 static inline uint32_t
649 rte_bsf32(uint32_t v)
650 {
651         return (uint32_t)__builtin_ctz(v);
652 }
653
654 /**
655  * Searches the input parameter for the least significant set bit
656  * (starting from zero). Safe version (checks for input parameter being zero).
657  *
658  * @warning ``pos`` must be a valid pointer. It is not checked!
659  *
660  * @param v
661  *     The input parameter.
662  * @param pos
663  *     If ``v`` was not 0, this value will contain position of least significant
664  *     bit within the input parameter.
665  * @return
666  *     Returns 0 if ``v`` was 0, otherwise returns 1.
667  */
668 static inline int
669 rte_bsf32_safe(uint32_t v, uint32_t *pos)
670 {
671         if (v == 0)
672                 return 0;
673
674         *pos = rte_bsf32(v);
675         return 1;
676 }
677
678 /**
679  * Return the rounded-up log2 of a integer.
680  *
681  * @note Contrary to the logarithm mathematical operation,
682  * rte_log2_u32(0) == 0 and not -inf.
683  *
684  * @param v
685  *     The input parameter.
686  * @return
687  *     The rounded-up log2 of the input, or 0 if the input is 0.
688  */
689 static inline uint32_t
690 rte_log2_u32(uint32_t v)
691 {
692         if (v == 0)
693                 return 0;
694         v = rte_align32pow2(v);
695         return rte_bsf32(v);
696 }
697
698
699 /**
700  * Return the last (most-significant) bit set.
701  *
702  * @note The last (most significant) bit is at position 32.
703  * @note rte_fls_u32(0) = 0, rte_fls_u32(1) = 1, rte_fls_u32(0x80000000) = 32
704  *
705  * @param x
706  *     The input parameter.
707  * @return
708  *     The last (most-significant) bit set, or 0 if the input is 0.
709  */
710 static inline int
711 rte_fls_u32(uint32_t x)
712 {
713         return (x == 0) ? 0 : 32 - __builtin_clz(x);
714 }
715
716 /**
717  * Searches the input parameter for the least significant set bit
718  * (starting from zero).
719  * If a least significant 1 bit is found, its bit index is returned.
720  * If the content of the input parameter is zero, then the content of the return
721  * value is undefined.
722  * @param v
723  *     input parameter, should not be zero.
724  * @return
725  *     least significant set bit in the input parameter.
726  */
727 static inline int
728 rte_bsf64(uint64_t v)
729 {
730         return (uint32_t)__builtin_ctzll(v);
731 }
732
733 /**
734  * Searches the input parameter for the least significant set bit
735  * (starting from zero). Safe version (checks for input parameter being zero).
736  *
737  * @warning ``pos`` must be a valid pointer. It is not checked!
738  *
739  * @param v
740  *     The input parameter.
741  * @param pos
742  *     If ``v`` was not 0, this value will contain position of least significant
743  *     bit within the input parameter.
744  * @return
745  *     Returns 0 if ``v`` was 0, otherwise returns 1.
746  */
747 static inline int
748 rte_bsf64_safe(uint64_t v, uint32_t *pos)
749 {
750         if (v == 0)
751                 return 0;
752
753         *pos = rte_bsf64(v);
754         return 1;
755 }
756
757 /**
758  * Return the last (most-significant) bit set.
759  *
760  * @note The last (most significant) bit is at position 64.
761  * @note rte_fls_u64(0) = 0, rte_fls_u64(1) = 1,
762  *       rte_fls_u64(0x8000000000000000) = 64
763  *
764  * @param x
765  *     The input parameter.
766  * @return
767  *     The last (most-significant) bit set, or 0 if the input is 0.
768  */
769 static inline int
770 rte_fls_u64(uint64_t x)
771 {
772         return (x == 0) ? 0 : 64 - __builtin_clzll(x);
773 }
774
775 /**
776  * Return the rounded-up log2 of a 64-bit integer.
777  *
778  * @note Contrary to the logarithm mathematical operation,
779  * rte_log2_u64(0) == 0 and not -inf.
780  *
781  * @param v
782  *     The input parameter.
783  * @return
784  *     The rounded-up log2 of the input, or 0 if the input is 0.
785  */
786 static inline uint32_t
787 rte_log2_u64(uint64_t v)
788 {
789         if (v == 0)
790                 return 0;
791         v = rte_align64pow2(v);
792         /* we checked for v being 0 already, so no undefined behavior */
793         return rte_bsf64(v);
794 }
795
796 #ifndef offsetof
797 /** Return the offset of a field in a structure. */
798 #define offsetof(TYPE, MEMBER)  __builtin_offsetof (TYPE, MEMBER)
799 #endif
800
801 /**
802  * Return pointer to the wrapping struct instance.
803  *
804  * Example:
805  *
806  *  struct wrapper {
807  *      ...
808  *      struct child c;
809  *      ...
810  *  };
811  *
812  *  struct child *x = obtain(...);
813  *  struct wrapper *w = container_of(x, struct wrapper, c);
814  */
815 #ifndef container_of
816 #define container_of(ptr, type, member) __extension__ ({                \
817                         const typeof(((type *)0)->member) *_ptr = (ptr); \
818                         __rte_unused type *_target_ptr =        \
819                                 (type *)(ptr);                          \
820                         (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
821                 })
822 #endif
823
824 /** Swap two variables. */
825 #define RTE_SWAP(a, b) \
826         __extension__ ({ \
827                 typeof (a) _a = a; \
828                 a = b; \
829                 b = _a; \
830         })
831
832 /**
833  * Get the size of a field in a structure.
834  *
835  * @param type
836  *   The type of the structure.
837  * @param field
838  *   The field in the structure.
839  * @return
840  *   The size of the field in the structure, in bytes.
841  */
842 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
843
844 #define _RTE_STR(x) #x
845 /** Take a macro value and get a string version of it */
846 #define RTE_STR(x) _RTE_STR(x)
847
848 /**
849  * ISO C helpers to modify format strings using variadic macros.
850  * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
851  * An empty %s argument is appended to avoid a dangling comma.
852  */
853 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
854 #define RTE_FMT_HEAD(fmt, ...) fmt
855 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
856
857 /** Mask value of type "tp" for the first "ln" bit set. */
858 #define RTE_LEN2MASK(ln, tp)    \
859         ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
860
861 /** Number of elements in the array. */
862 #define RTE_DIM(a)      (sizeof (a) / sizeof ((a)[0]))
863
864 /**
865  * Converts a numeric string to the equivalent uint64_t value.
866  * As well as straight number conversion, also recognises the suffixes
867  * k, m and g for kilobytes, megabytes and gigabytes respectively.
868  *
869  * If a negative number is passed in  i.e. a string with the first non-black
870  * character being "-", zero is returned. Zero is also returned in the case of
871  * an error with the strtoull call in the function.
872  *
873  * @param str
874  *     String containing number to convert.
875  * @return
876  *     Number.
877  */
878 static inline uint64_t
879 rte_str_to_size(const char *str)
880 {
881         char *endptr;
882         unsigned long long size;
883
884         while (isspace((int)*str))
885                 str++;
886         if (*str == '-')
887                 return 0;
888
889         errno = 0;
890         size = strtoull(str, &endptr, 0);
891         if (errno)
892                 return 0;
893
894         if (*endptr == ' ')
895                 endptr++; /* allow 1 space gap */
896
897         switch (*endptr){
898         case 'G': case 'g': size *= 1024; /* fall-through */
899         case 'M': case 'm': size *= 1024; /* fall-through */
900         case 'K': case 'k': size *= 1024; /* fall-through */
901         default:
902                 break;
903         }
904         return size;
905 }
906
907 /**
908  * Function to terminate the application immediately, printing an error
909  * message and returning the exit_code back to the shell.
910  *
911  * This function never returns
912  *
913  * @param exit_code
914  *     The exit code to be returned by the application
915  * @param format
916  *     The format string to be used for printing the message. This can include
917  *     printf format characters which will be expanded using any further parameters
918  *     to the function.
919  */
920 __rte_noreturn void
921 rte_exit(int exit_code, const char *format, ...)
922         __rte_format_printf(2, 3);
923
924 #ifdef __cplusplus
925 }
926 #endif
927
928 #endif