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