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