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