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