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