xref: /dpdk/lib/eal/include/rte_common.h (revision b6f30094e511c22f038907a507d16f7cc022c682)
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 <assert.h>
20 #include <stdint.h>
21 #include <limits.h>
22 
23 #include <rte_config.h>
24 
25 /* OS specific include */
26 #include <rte_os.h>
27 
28 #ifndef RTE_TOOLCHAIN_MSVC
29 #ifndef typeof
30 #define typeof __typeof__
31 #endif
32 #endif
33 
34 #ifndef __cplusplus
35 #ifndef asm
36 #define asm __asm__
37 #endif
38 #endif
39 
40 #ifdef RTE_TOOLCHAIN_MSVC
41 #define __extension__
42 #endif
43 
44 /*
45  * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
46  * while a host application (like pmdinfogen) may have another compiler.
47  * RTE_CC_IS_GNU is true if the file is compiled with GCC,
48  * no matter it is a target or host application.
49  */
50 #define RTE_CC_IS_GNU 0
51 #if defined __clang__
52 #define RTE_CC_CLANG
53 #elif defined __INTEL_COMPILER
54 #define RTE_CC_ICC
55 #elif defined __GNUC__
56 #define RTE_CC_GCC
57 #undef RTE_CC_IS_GNU
58 #define RTE_CC_IS_GNU 1
59 #endif
60 #if RTE_CC_IS_GNU
61 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 +	\
62 		__GNUC_PATCHLEVEL__)
63 #endif
64 
65 /**
66  * Force alignment
67  */
68 #ifdef RTE_TOOLCHAIN_MSVC
69 #define __rte_aligned(a)
70 #else
71 #define __rte_aligned(a) __attribute__((__aligned__(a)))
72 #endif
73 
74 #ifdef RTE_ARCH_STRICT_ALIGN
75 typedef uint64_t unaligned_uint64_t __rte_aligned(1);
76 typedef uint32_t unaligned_uint32_t __rte_aligned(1);
77 typedef uint16_t unaligned_uint16_t __rte_aligned(1);
78 #else
79 typedef uint64_t unaligned_uint64_t;
80 typedef uint32_t unaligned_uint32_t;
81 typedef uint16_t unaligned_uint16_t;
82 #endif
83 
84 /**
85  * Force a structure to be packed
86  */
87 #ifdef RTE_TOOLCHAIN_MSVC
88 #define __rte_packed
89 #else
90 #define __rte_packed __attribute__((__packed__))
91 #endif
92 
93 /**
94  * Macro to mark a type that is not subject to type-based aliasing rules
95  */
96 #ifdef RTE_TOOLCHAIN_MSVC
97 #define __rte_may_alias
98 #else
99 #define __rte_may_alias __attribute__((__may_alias__))
100 #endif
101 
102 /******* Macro to mark functions and fields scheduled for removal *****/
103 #ifdef RTE_TOOLCHAIN_MSVC
104 #define __rte_deprecated
105 #define __rte_deprecated_msg(msg)
106 #else
107 #define __rte_deprecated	__attribute__((__deprecated__))
108 #define __rte_deprecated_msg(msg)	__attribute__((__deprecated__(msg)))
109 #endif
110 
111 /**
112  *  Macro to mark macros and defines scheduled for removal
113  */
114 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
115 #define RTE_PRAGMA(x)  _Pragma(#x)
116 #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
117 #define RTE_DEPRECATED(x)  RTE_PRAGMA_WARNING(#x is deprecated)
118 #else
119 #define RTE_DEPRECATED(x)
120 #endif
121 
122 /**
123  * Mark a function or variable to a weak reference.
124  */
125 #define __rte_weak __attribute__((__weak__))
126 
127 /**
128  * Force symbol to be generated even if it appears to be unused.
129  */
130 #ifdef RTE_TOOLCHAIN_MSVC
131 #define __rte_used
132 #else
133 #define __rte_used __attribute__((used))
134 #endif
135 
136 /*********** Macros to eliminate unused variable warnings ********/
137 
138 /**
139  * short definition to mark a function parameter unused
140  */
141 #ifdef RTE_TOOLCHAIN_MSVC
142 #define __rte_unused
143 #else
144 #define __rte_unused __attribute__((__unused__))
145 #endif
146 
147 /**
148  * Mark pointer as restricted with regard to pointer aliasing.
149  */
150 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
151 #define __rte_restrict __restrict
152 #else
153 #define __rte_restrict restrict
154 #endif
155 
156 /**
157  * definition to mark a variable or function parameter as used so
158  * as to avoid a compiler warning
159  */
160 #define RTE_SET_USED(x) (void)(x)
161 
162 /**
163  * Check format string and its arguments at compile-time.
164  *
165  * GCC on Windows assumes MS-specific format string by default,
166  * even if the underlying stdio implementation is ANSI-compliant,
167  * so this must be overridden.
168  */
169 #ifdef RTE_TOOLCHAIN_MSVC
170 #define __rte_format_printf(format_index, first_arg)
171 #else
172 #if RTE_CC_IS_GNU
173 #define __rte_format_printf(format_index, first_arg) \
174 	__attribute__((format(gnu_printf, format_index, first_arg)))
175 #else
176 #define __rte_format_printf(format_index, first_arg) \
177 	__attribute__((format(printf, format_index, first_arg)))
178 #endif
179 #endif
180 
181 /**
182  * Specify data or function section/segment.
183  */
184 #ifdef RTE_TOOLCHAIN_MSVC
185 #define __rte_section(name) \
186 	__pragma(data_seg(name)) __declspec(allocate(name))
187 #else
188 #define __rte_section(name) \
189 	__attribute__((section(name)))
190 #endif
191 
192 /**
193  * Tells compiler that the function returns a value that points to
194  * memory, where the size is given by the one or two arguments.
195  * Used by compiler to validate object size.
196  */
197 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
198 #define __rte_alloc_size(...) \
199 	__attribute__((alloc_size(__VA_ARGS__)))
200 #else
201 #define __rte_alloc_size(...)
202 #endif
203 
204 #define RTE_PRIORITY_LOG 101
205 #define RTE_PRIORITY_BUS 110
206 #define RTE_PRIORITY_CLASS 120
207 #define RTE_PRIORITY_LAST 65535
208 
209 #define RTE_PRIO(prio) \
210 	RTE_PRIORITY_ ## prio
211 
212 /**
213  * Run function before main() with high priority.
214  *
215  * @param func
216  *   Constructor function.
217  * @param prio
218  *   Priority number must be above 100.
219  *   Lowest number is the first to run.
220  */
221 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
222 #ifndef RTE_TOOLCHAIN_MSVC
223 #define RTE_INIT_PRIO(func, prio) \
224 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
225 #else
226 /* definition from the Microsoft CRT */
227 typedef int(__cdecl *_PIFV)(void);
228 
229 #define CTOR_SECTION_LOG ".CRT$XIB"
230 #define CTOR_SECTION_BUS ".CRT$XIC"
231 #define CTOR_SECTION_CLASS ".CRT$XID"
232 #define CTOR_SECTION_LAST ".CRT$XIY"
233 
234 #define CTOR_PRIORITY_TO_SECTION(priority) CTOR_SECTION_ ## priority
235 
236 #define RTE_INIT_PRIO(name, priority) \
237 	static void name(void); \
238 	static int __cdecl name ## _thunk(void) { name(); return 0; } \
239 	__pragma(const_seg(CTOR_PRIORITY_TO_SECTION(priority))) \
240 	__declspec(allocate(CTOR_PRIORITY_TO_SECTION(priority))) \
241 	    _PIFV name ## _pointer = &name ## _thunk; \
242 	__pragma(const_seg()) \
243 	static void name(void)
244 #endif
245 #endif
246 
247 /**
248  * Run function before main() with low priority.
249  *
250  * The constructor will be run after prioritized constructors.
251  *
252  * @param func
253  *   Constructor function.
254  */
255 #define RTE_INIT(func) \
256 	RTE_INIT_PRIO(func, LAST)
257 
258 /**
259  * Run after main() with low priority.
260  *
261  * @param func
262  *   Destructor function name.
263  * @param prio
264  *   Priority number must be above 100.
265  *   Lowest number is the last to run.
266  */
267 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
268 #ifndef RTE_TOOLCHAIN_MSVC
269 #define RTE_FINI_PRIO(func, prio) \
270 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
271 #else
272 #define DTOR_SECTION_LOG "mydtor$B"
273 #define DTOR_SECTION_BUS "mydtor$C"
274 #define DTOR_SECTION_CLASS "mydtor$D"
275 #define DTOR_SECTION_LAST "mydtor$Y"
276 
277 #define DTOR_PRIORITY_TO_SECTION(priority) DTOR_SECTION_ ## priority
278 
279 #define RTE_FINI_PRIO(name, priority) \
280 	static void name(void); \
281 	__pragma(const_seg(DTOR_PRIORITY_TO_SECTION(priority))) \
282 	__declspec(allocate(DTOR_PRIORITY_TO_SECTION(priority))) name ## _pointer = &name; \
283 	__pragma(const_seg()) \
284 	static void name(void)
285 #endif
286 #endif
287 
288 /**
289  * Run after main() with high priority.
290  *
291  * The destructor will be run *before* prioritized destructors.
292  *
293  * @param func
294  *   Destructor function name.
295  */
296 #define RTE_FINI(func) \
297 	RTE_FINI_PRIO(func, LAST)
298 
299 /**
300  * Hint never returning function
301  */
302 #ifdef RTE_TOOLCHAIN_MSVC
303 #define __rte_noreturn
304 #else
305 #define __rte_noreturn __attribute__((noreturn))
306 #endif
307 
308 /**
309  * Issue a warning in case the function's return value is ignored.
310  *
311  * The use of this attribute should be restricted to cases where
312  * ignoring the marked function's return value is almost always a
313  * bug. With GCC, some effort is required to make clear that ignoring
314  * the return value is intentional. The usual void-casting method to
315  * mark something unused as used does not suppress the warning with
316  * this compiler.
317  *
318  * @code{.c}
319  * __rte_warn_unused_result int foo();
320  *
321  * void ignore_foo_result(void) {
322  *         foo(); // generates a warning with all compilers
323  *
324  *         (void)foo(); // still generates the warning with GCC (but not clang)
325  *
326  *         int unused __rte_unused;
327  *         unused = foo(); // does the trick with all compilers
328  *  }
329  * @endcode
330  */
331 #ifdef RTE_TOOLCHAIN_MSVC
332 #define __rte_warn_unused_result
333 #else
334 #define __rte_warn_unused_result __attribute__((warn_unused_result))
335 #endif
336 
337 /**
338  * Force a function to be inlined
339  */
340 #ifdef RTE_TOOLCHAIN_MSVC
341 #define __rte_always_inline
342 #else
343 #define __rte_always_inline inline __attribute__((always_inline))
344 #endif
345 
346 /**
347  * Force a function to be noinlined
348  */
349 #define __rte_noinline __attribute__((noinline))
350 
351 /**
352  * Hint function in the hot path
353  */
354 #define __rte_hot __attribute__((hot))
355 
356 /**
357  * Hint function in the cold path
358  */
359 #ifdef RTE_TOOLCHAIN_MSVC
360 #define __rte_cold
361 #else
362 #define __rte_cold __attribute__((cold))
363 #endif
364 
365 /**
366  * Disable AddressSanitizer on some code
367  */
368 #ifdef RTE_MALLOC_ASAN
369 #ifdef RTE_CC_CLANG
370 #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
371 #else
372 #define __rte_no_asan __attribute__((no_sanitize_address))
373 #endif
374 #else /* ! RTE_MALLOC_ASAN */
375 #define __rte_no_asan
376 #endif
377 
378 /*********** Macros for pointer arithmetic ********/
379 
380 /**
381  * add a byte-value offset to a pointer
382  */
383 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
384 
385 /**
386  * subtract a byte-value offset from a pointer
387  */
388 #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
389 
390 /**
391  * get the difference between two pointer values, i.e. how far apart
392  * in bytes are the locations they point two. It is assumed that
393  * ptr1 is greater than ptr2.
394  */
395 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
396 
397 /**
398  * Workaround to cast a const field of a structure to non-const type.
399  */
400 #define RTE_CAST_FIELD(var, field, type) \
401 	(*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
402 
403 /*********** Macros/static functions for doing alignment ********/
404 
405 
406 /**
407  * Macro to align a pointer to a given power-of-two. The resultant
408  * pointer will be a pointer of the same type as the first parameter, and
409  * point to an address no higher than the first parameter. Second parameter
410  * must be a power-of-two value.
411  */
412 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
413 	((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
414 
415 /**
416  * Macro to align a value to a given power-of-two. The resultant value
417  * will be of the same type as the first parameter, and will be no
418  * bigger than the first parameter. Second parameter must be a
419  * power-of-two value.
420  */
421 #define RTE_ALIGN_FLOOR(val, align) \
422 	(typeof(val))((val) & (~((typeof(val))((align) - 1))))
423 
424 /**
425  * Macro to align a pointer to a given power-of-two. The resultant
426  * pointer will be a pointer of the same type as the first parameter, and
427  * point to an address no lower than the first parameter. Second parameter
428  * must be a power-of-two value.
429  */
430 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
431 	RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
432 
433 /**
434  * Macro to align a value to a given power-of-two. The resultant value
435  * will be of the same type as the first parameter, and will be no lower
436  * than the first parameter. Second parameter must be a power-of-two
437  * value.
438  */
439 #define RTE_ALIGN_CEIL(val, align) \
440 	RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
441 
442 /**
443  * Macro to align a pointer to a given power-of-two. The resultant
444  * pointer will be a pointer of the same type as the first parameter, and
445  * point to an address no lower than the first parameter. Second parameter
446  * must be a power-of-two value.
447  * This function is the same as RTE_PTR_ALIGN_CEIL
448  */
449 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
450 
451 /**
452  * Macro to align a value to a given power-of-two. The resultant
453  * value will be of the same type as the first parameter, and
454  * will be no lower than the first parameter. Second parameter
455  * must be a power-of-two value.
456  * This function is the same as RTE_ALIGN_CEIL
457  */
458 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
459 
460 /**
461  * Macro to align a value to the multiple of given value. The resultant
462  * value will be of the same type as the first parameter and will be no lower
463  * than the first parameter.
464  */
465 #define RTE_ALIGN_MUL_CEIL(v, mul) \
466 	((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
467 
468 /**
469  * Macro to align a value to the multiple of given value. The resultant
470  * value will be of the same type as the first parameter and will be no higher
471  * than the first parameter.
472  */
473 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
474 	(((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
475 
476 /**
477  * Macro to align value to the nearest multiple of the given value.
478  * The resultant value might be greater than or less than the first parameter
479  * whichever difference is the lowest.
480  */
481 #define RTE_ALIGN_MUL_NEAR(v, mul)				\
482 	__extension__ ({					\
483 		typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul);	\
484 		typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul);	\
485 		(ceil - (v)) > ((v) - floor) ? floor : ceil;	\
486 	})
487 
488 /**
489  * Checks if a pointer is aligned to a given power-of-two value
490  *
491  * @param ptr
492  *   The pointer whose alignment is to be checked
493  * @param align
494  *   The power-of-two value to which the ptr should be aligned
495  *
496  * @return
497  *   True(1) where the pointer is correctly aligned, false(0) otherwise
498  */
499 static inline int
500 rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
501 {
502 	return ((uintptr_t)ptr & (align - 1)) == 0;
503 }
504 
505 /*********** Macros for compile type checks ********/
506 
507 /* Workaround for toolchain issues with missing C11 macro in FreeBSD */
508 #if !defined(static_assert) && !defined(__cplusplus)
509 #define	static_assert	_Static_assert
510 #endif
511 
512 /**
513  * Triggers an error at compilation time if the condition is true.
514  *
515  * The do { } while(0) exists to workaround a bug in clang (#55821)
516  * where it would not handle _Static_assert in a switch case.
517  */
518 #define RTE_BUILD_BUG_ON(condition) do { static_assert(!(condition), #condition); } while (0)
519 
520 /*********** Cache line related macros ********/
521 
522 /** Cache line mask. */
523 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
524 
525 /** Return the first cache-aligned value greater or equal to size. */
526 #define RTE_CACHE_LINE_ROUNDUP(size) RTE_ALIGN_CEIL(size, RTE_CACHE_LINE_SIZE)
527 
528 /** Cache line size in terms of log2 */
529 #if RTE_CACHE_LINE_SIZE == 64
530 #define RTE_CACHE_LINE_SIZE_LOG2 6
531 #elif RTE_CACHE_LINE_SIZE == 128
532 #define RTE_CACHE_LINE_SIZE_LOG2 7
533 #else
534 #error "Unsupported cache line size"
535 #endif
536 
537 /** Minimum Cache line size. */
538 #define RTE_CACHE_LINE_MIN_SIZE 64
539 
540 /** Force alignment to cache line. */
541 #ifdef RTE_TOOLCHAIN_MSVC
542 #define __rte_cache_aligned
543 #else
544 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
545 #endif
546 
547 /** Force minimum cache line alignment. */
548 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
549 
550 #define _RTE_CACHE_GUARD_HELPER2(unique) \
551 	char cache_guard_ ## unique[RTE_CACHE_LINE_SIZE * RTE_CACHE_GUARD_LINES] \
552 	__rte_cache_aligned
553 #define _RTE_CACHE_GUARD_HELPER1(unique) _RTE_CACHE_GUARD_HELPER2(unique)
554 /**
555  * Empty cache lines, to guard against false sharing-like effects
556  * on systems with a next-N-lines hardware prefetcher.
557  *
558  * Use as spacing between data accessed by different lcores,
559  * to prevent cache thrashing on hardware with speculative prefetching.
560  */
561 #define RTE_CACHE_GUARD _RTE_CACHE_GUARD_HELPER1(__COUNTER__)
562 
563 /*********** PA/IOVA type definitions ********/
564 
565 /** Physical address */
566 typedef uint64_t phys_addr_t;
567 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
568 
569 /**
570  * IO virtual address type.
571  * When the physical addressing mode (IOVA as PA) is in use,
572  * the translation from an IO virtual address (IOVA) to a physical address
573  * is a direct mapping, i.e. the same value.
574  * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
575  */
576 typedef uint64_t rte_iova_t;
577 #define RTE_BAD_IOVA ((rte_iova_t)-1)
578 
579 /*********** Structure alignment markers ********/
580 
581 #ifndef RTE_TOOLCHAIN_MSVC
582 
583 /** Generic marker for any place in a structure. */
584 __extension__ typedef void    *RTE_MARKER[0];
585 /** Marker for 1B alignment in a structure. */
586 __extension__ typedef uint8_t  RTE_MARKER8[0];
587 /** Marker for 2B alignment in a structure. */
588 __extension__ typedef uint16_t RTE_MARKER16[0];
589 /** Marker for 4B alignment in a structure. */
590 __extension__ typedef uint32_t RTE_MARKER32[0];
591 /** Marker for 8B alignment in a structure. */
592 __extension__ typedef uint64_t RTE_MARKER64[0];
593 
594 #endif
595 
596 /*********** Macros for calculating min and max **********/
597 
598 /**
599  * Macro to return the minimum of two numbers
600  */
601 #define RTE_MIN(a, b) \
602 	__extension__ ({ \
603 		typeof (a) _a = (a); \
604 		typeof (b) _b = (b); \
605 		_a < _b ? _a : _b; \
606 	})
607 
608 /**
609  * Macro to return the minimum of two numbers
610  *
611  * As opposed to RTE_MIN, it does not use temporary variables so it is not safe
612  * if a or b is an expression. Yet it is guaranteed to be constant for use in
613  * static_assert().
614  */
615 #define RTE_MIN_T(a, b, t) \
616 	((t)(a) < (t)(b) ? (t)(a) : (t)(b))
617 
618 /**
619  * Macro to return the maximum of two numbers
620  */
621 #define RTE_MAX(a, b) \
622 	__extension__ ({ \
623 		typeof (a) _a = (a); \
624 		typeof (b) _b = (b); \
625 		_a > _b ? _a : _b; \
626 	})
627 
628 /**
629  * Macro to return the maximum of two numbers
630  *
631  * As opposed to RTE_MAX, it does not use temporary variables so it is not safe
632  * if a or b is an expression. Yet it is guaranteed to be constant for use in
633  * static_assert().
634  */
635 #define RTE_MAX_T(a, b, t) \
636 	((t)(a) > (t)(b) ? (t)(a) : (t)(b))
637 
638 /*********** Other general functions / macros ********/
639 
640 #ifndef offsetof
641 /** Return the offset of a field in a structure. */
642 #define offsetof(TYPE, MEMBER)  __builtin_offsetof (TYPE, MEMBER)
643 #endif
644 
645 /**
646  * Return pointer to the wrapping struct instance.
647  *
648  * Example:
649  *
650  *  struct wrapper {
651  *      ...
652  *      struct child c;
653  *      ...
654  *  };
655  *
656  *  struct child *x = obtain(...);
657  *  struct wrapper *w = container_of(x, struct wrapper, c);
658  */
659 #ifndef container_of
660 #ifdef RTE_TOOLCHAIN_MSVC
661 #define container_of(ptr, type, member) \
662 			((type *)((uintptr_t)(ptr) - offsetof(type, member)))
663 #else
664 #define container_of(ptr, type, member)	__extension__ ({		\
665 			const typeof(((type *)0)->member) *_ptr = (ptr); \
666 			__rte_unused type *_target_ptr =	\
667 				(type *)(ptr);				\
668 			(type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
669 		})
670 #endif
671 #endif
672 
673 /** Swap two variables. */
674 #define RTE_SWAP(a, b) \
675 	__extension__ ({ \
676 		typeof (a) _a = a; \
677 		a = b; \
678 		b = _a; \
679 	})
680 
681 /**
682  * Get the size of a field in a structure.
683  *
684  * @param type
685  *   The type of the structure.
686  * @param field
687  *   The field in the structure.
688  * @return
689  *   The size of the field in the structure, in bytes.
690  */
691 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
692 
693 #define _RTE_STR(x) #x
694 /** Take a macro value and get a string version of it */
695 #define RTE_STR(x) _RTE_STR(x)
696 
697 /**
698  * ISO C helpers to modify format strings using variadic macros.
699  * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
700  * An empty %s argument is appended to avoid a dangling comma.
701  */
702 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
703 #define RTE_FMT_HEAD(fmt, ...) fmt
704 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
705 
706 /** Mask value of type "tp" for the first "ln" bit set. */
707 #define	RTE_LEN2MASK(ln, tp)	\
708 	((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
709 
710 /** Number of elements in the array. */
711 #define	RTE_DIM(a)	(sizeof (a) / sizeof ((a)[0]))
712 
713 /**
714  * Converts a numeric string to the equivalent uint64_t value.
715  * As well as straight number conversion, also recognises the suffixes
716  * k, m and g for kilobytes, megabytes and gigabytes respectively.
717  *
718  * If a negative number is passed in  i.e. a string with the first non-black
719  * character being "-", zero is returned. Zero is also returned in the case of
720  * an error with the strtoull call in the function.
721  *
722  * @param str
723  *     String containing number to convert.
724  * @return
725  *     Number.
726  */
727 uint64_t
728 rte_str_to_size(const char *str);
729 
730 /**
731  * Function to terminate the application immediately, printing an error
732  * message and returning the exit_code back to the shell.
733  *
734  * This function never returns
735  *
736  * @param exit_code
737  *     The exit code to be returned by the application
738  * @param format
739  *     The format string to be used for printing the message. This can include
740  *     printf format characters which will be expanded using any further parameters
741  *     to the function.
742  */
743 __rte_noreturn void
744 rte_exit(int exit_code, const char *format, ...)
745 	__rte_format_printf(2, 3);
746 
747 #ifdef __cplusplus
748 }
749 #endif
750 
751 #endif
752