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