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