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