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