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