xref: /dpdk/lib/eal/include/rte_common.h (revision d065725b6fb99bb4ebbcd6ee5fa4628886d7bee3)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2010-2019 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #ifndef _RTE_COMMON_H_
699a2dd95SBruce Richardson #define _RTE_COMMON_H_
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson /**
999a2dd95SBruce Richardson  * @file
1099a2dd95SBruce Richardson  *
1199a2dd95SBruce Richardson  * Generic, commonly-used macro and inline function definitions
1299a2dd95SBruce Richardson  * for DPDK.
1399a2dd95SBruce Richardson  */
1499a2dd95SBruce Richardson 
1599a2dd95SBruce Richardson #ifdef __cplusplus
1699a2dd95SBruce Richardson extern "C" {
1799a2dd95SBruce Richardson #endif
1899a2dd95SBruce Richardson 
19537caad2SStephen Hemminger #include <assert.h>
2099a2dd95SBruce Richardson #include <limits.h>
21e9fd1ebfSTyler Retzlaff #include <stdint.h>
22e9fd1ebfSTyler Retzlaff #include <stdalign.h>
2399a2dd95SBruce Richardson 
2499a2dd95SBruce Richardson #include <rte_config.h>
2599a2dd95SBruce Richardson 
2699a2dd95SBruce Richardson /* OS specific include */
2799a2dd95SBruce Richardson #include <rte_os.h>
2899a2dd95SBruce Richardson 
29e7afce06STyler Retzlaff #ifndef RTE_TOOLCHAIN_MSVC
3099a2dd95SBruce Richardson #ifndef typeof
3199a2dd95SBruce Richardson #define typeof __typeof__
3299a2dd95SBruce Richardson #endif
33e7afce06STyler Retzlaff #endif
3499a2dd95SBruce Richardson 
3599a2dd95SBruce Richardson #ifndef __cplusplus
3699a2dd95SBruce Richardson #ifndef asm
3799a2dd95SBruce Richardson #define asm __asm__
3899a2dd95SBruce Richardson #endif
3999a2dd95SBruce Richardson #endif
4099a2dd95SBruce Richardson 
4151574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
42*d065725bSTyler Retzlaff #ifdef __cplusplus
4351574a4fSTyler Retzlaff #define __extension__
4451574a4fSTyler Retzlaff #endif
45*d065725bSTyler Retzlaff #endif
4651574a4fSTyler Retzlaff 
4799a2dd95SBruce Richardson /*
4899a2dd95SBruce Richardson  * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
4999a2dd95SBruce Richardson  * while a host application (like pmdinfogen) may have another compiler.
5099a2dd95SBruce Richardson  * RTE_CC_IS_GNU is true if the file is compiled with GCC,
5199a2dd95SBruce Richardson  * no matter it is a target or host application.
5299a2dd95SBruce Richardson  */
5399a2dd95SBruce Richardson #define RTE_CC_IS_GNU 0
5499a2dd95SBruce Richardson #if defined __clang__
5599a2dd95SBruce Richardson #define RTE_CC_CLANG
5699a2dd95SBruce Richardson #elif defined __INTEL_COMPILER
5799a2dd95SBruce Richardson #define RTE_CC_ICC
5899a2dd95SBruce Richardson #elif defined __GNUC__
5999a2dd95SBruce Richardson #define RTE_CC_GCC
6099a2dd95SBruce Richardson #undef RTE_CC_IS_GNU
6199a2dd95SBruce Richardson #define RTE_CC_IS_GNU 1
6299a2dd95SBruce Richardson #endif
6399a2dd95SBruce Richardson #if RTE_CC_IS_GNU
6499a2dd95SBruce Richardson #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 +	\
6599a2dd95SBruce Richardson 		__GNUC_PATCHLEVEL__)
6699a2dd95SBruce Richardson #endif
6799a2dd95SBruce Richardson 
6899a2dd95SBruce Richardson /**
69c6552d9aSTyler Retzlaff  * Force type alignment
70c6552d9aSTyler Retzlaff  *
71c6552d9aSTyler Retzlaff  * This macro should be used when alignment of a struct or union type
72c6552d9aSTyler Retzlaff  * is required. For toolchain compatibility it should appear between
73c6552d9aSTyler Retzlaff  * the {struct,union} keyword and tag. e.g.
74c6552d9aSTyler Retzlaff  *
75c6552d9aSTyler Retzlaff  *   struct __rte_aligned(8) tag { ... };
76c6552d9aSTyler Retzlaff  *
77c6552d9aSTyler Retzlaff  * If alignment of an object/variable is required then this macro should
78c6552d9aSTyler Retzlaff  * not be used, instead prefer C11 alignas(a).
7999a2dd95SBruce Richardson  */
8051574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
81c6552d9aSTyler Retzlaff #define __rte_aligned(a) __declspec(align(a))
8251574a4fSTyler Retzlaff #else
8399a2dd95SBruce Richardson #define __rte_aligned(a) __attribute__((__aligned__(a)))
8451574a4fSTyler Retzlaff #endif
8599a2dd95SBruce Richardson 
8699a2dd95SBruce Richardson #ifdef RTE_ARCH_STRICT_ALIGN
8799a2dd95SBruce Richardson typedef uint64_t unaligned_uint64_t __rte_aligned(1);
8899a2dd95SBruce Richardson typedef uint32_t unaligned_uint32_t __rte_aligned(1);
8999a2dd95SBruce Richardson typedef uint16_t unaligned_uint16_t __rte_aligned(1);
9099a2dd95SBruce Richardson #else
9199a2dd95SBruce Richardson typedef uint64_t unaligned_uint64_t;
9299a2dd95SBruce Richardson typedef uint32_t unaligned_uint32_t;
9399a2dd95SBruce Richardson typedef uint16_t unaligned_uint16_t;
9499a2dd95SBruce Richardson #endif
9599a2dd95SBruce Richardson 
9699a2dd95SBruce Richardson /**
9799a2dd95SBruce Richardson  * Force a structure to be packed
9899a2dd95SBruce Richardson  */
9951574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
10051574a4fSTyler Retzlaff #define __rte_packed
10151574a4fSTyler Retzlaff #else
10299a2dd95SBruce Richardson #define __rte_packed __attribute__((__packed__))
10351574a4fSTyler Retzlaff #endif
10499a2dd95SBruce Richardson 
10500901e4dSLuc Pelletier /**
10600901e4dSLuc Pelletier  * Macro to mark a type that is not subject to type-based aliasing rules
10700901e4dSLuc Pelletier  */
10851574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
10951574a4fSTyler Retzlaff #define __rte_may_alias
11051574a4fSTyler Retzlaff #else
11100901e4dSLuc Pelletier #define __rte_may_alias __attribute__((__may_alias__))
11251574a4fSTyler Retzlaff #endif
11300901e4dSLuc Pelletier 
11499a2dd95SBruce Richardson /******* Macro to mark functions and fields scheduled for removal *****/
11551574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
11651574a4fSTyler Retzlaff #define __rte_deprecated
11751574a4fSTyler Retzlaff #define __rte_deprecated_msg(msg)
11851574a4fSTyler Retzlaff #else
11999a2dd95SBruce Richardson #define __rte_deprecated	__attribute__((__deprecated__))
12099a2dd95SBruce Richardson #define __rte_deprecated_msg(msg)	__attribute__((__deprecated__(msg)))
12151574a4fSTyler Retzlaff #endif
12299a2dd95SBruce Richardson 
12399a2dd95SBruce Richardson /**
12499a2dd95SBruce Richardson  *  Macro to mark macros and defines scheduled for removal
12599a2dd95SBruce Richardson  */
12699a2dd95SBruce Richardson #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
12799a2dd95SBruce Richardson #define RTE_PRAGMA(x)  _Pragma(#x)
12899a2dd95SBruce Richardson #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
12999a2dd95SBruce Richardson #define RTE_DEPRECATED(x)  RTE_PRAGMA_WARNING(#x is deprecated)
13099a2dd95SBruce Richardson #else
13199a2dd95SBruce Richardson #define RTE_DEPRECATED(x)
13299a2dd95SBruce Richardson #endif
13399a2dd95SBruce Richardson 
13499a2dd95SBruce Richardson /**
13599a2dd95SBruce Richardson  * Mark a function or variable to a weak reference.
13699a2dd95SBruce Richardson  */
13799a2dd95SBruce Richardson #define __rte_weak __attribute__((__weak__))
13899a2dd95SBruce Richardson 
13999a2dd95SBruce Richardson /**
14099a2dd95SBruce Richardson  * Force symbol to be generated even if it appears to be unused.
14199a2dd95SBruce Richardson  */
14251574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
14351574a4fSTyler Retzlaff #define __rte_used
14451574a4fSTyler Retzlaff #else
14599a2dd95SBruce Richardson #define __rte_used __attribute__((used))
14651574a4fSTyler Retzlaff #endif
14799a2dd95SBruce Richardson 
14899a2dd95SBruce Richardson /*********** Macros to eliminate unused variable warnings ********/
14999a2dd95SBruce Richardson 
15099a2dd95SBruce Richardson /**
15199a2dd95SBruce Richardson  * short definition to mark a function parameter unused
15299a2dd95SBruce Richardson  */
15351574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
15451574a4fSTyler Retzlaff #define __rte_unused
15551574a4fSTyler Retzlaff #else
15699a2dd95SBruce Richardson #define __rte_unused __attribute__((__unused__))
15751574a4fSTyler Retzlaff #endif
15899a2dd95SBruce Richardson 
15999a2dd95SBruce Richardson /**
16099a2dd95SBruce Richardson  * Mark pointer as restricted with regard to pointer aliasing.
16199a2dd95SBruce Richardson  */
16299a2dd95SBruce Richardson #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
16399a2dd95SBruce Richardson #define __rte_restrict __restrict
16499a2dd95SBruce Richardson #else
16599a2dd95SBruce Richardson #define __rte_restrict restrict
16699a2dd95SBruce Richardson #endif
16799a2dd95SBruce Richardson 
16899a2dd95SBruce Richardson /**
16999a2dd95SBruce Richardson  * definition to mark a variable or function parameter as used so
17099a2dd95SBruce Richardson  * as to avoid a compiler warning
17199a2dd95SBruce Richardson  */
17299a2dd95SBruce Richardson #define RTE_SET_USED(x) (void)(x)
17399a2dd95SBruce Richardson 
17499a2dd95SBruce Richardson /**
17599a2dd95SBruce Richardson  * Check format string and its arguments at compile-time.
17699a2dd95SBruce Richardson  *
17799a2dd95SBruce Richardson  * GCC on Windows assumes MS-specific format string by default,
17899a2dd95SBruce Richardson  * even if the underlying stdio implementation is ANSI-compliant,
17999a2dd95SBruce Richardson  * so this must be overridden.
18099a2dd95SBruce Richardson  */
18151574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
18251574a4fSTyler Retzlaff #define __rte_format_printf(format_index, first_arg)
18351574a4fSTyler Retzlaff #else
18499a2dd95SBruce Richardson #if RTE_CC_IS_GNU
18599a2dd95SBruce Richardson #define __rte_format_printf(format_index, first_arg) \
18699a2dd95SBruce Richardson 	__attribute__((format(gnu_printf, format_index, first_arg)))
18799a2dd95SBruce Richardson #else
18899a2dd95SBruce Richardson #define __rte_format_printf(format_index, first_arg) \
18999a2dd95SBruce Richardson 	__attribute__((format(printf, format_index, first_arg)))
19099a2dd95SBruce Richardson #endif
19151574a4fSTyler Retzlaff #endif
19299a2dd95SBruce Richardson 
19399a2dd95SBruce Richardson /**
19474fff67aSTyler Retzlaff  * Specify data or function section/segment.
19574fff67aSTyler Retzlaff  */
19674fff67aSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
19774fff67aSTyler Retzlaff #define __rte_section(name) \
19874fff67aSTyler Retzlaff 	__pragma(data_seg(name)) __declspec(allocate(name))
19974fff67aSTyler Retzlaff #else
20074fff67aSTyler Retzlaff #define __rte_section(name) \
20174fff67aSTyler Retzlaff 	__attribute__((section(name)))
20274fff67aSTyler Retzlaff #endif
20374fff67aSTyler Retzlaff 
20474fff67aSTyler Retzlaff /**
20599a2dd95SBruce Richardson  * Tells compiler that the function returns a value that points to
20699a2dd95SBruce Richardson  * memory, where the size is given by the one or two arguments.
20799a2dd95SBruce Richardson  * Used by compiler to validate object size.
20899a2dd95SBruce Richardson  */
20999a2dd95SBruce Richardson #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
21099a2dd95SBruce Richardson #define __rte_alloc_size(...) \
21199a2dd95SBruce Richardson 	__attribute__((alloc_size(__VA_ARGS__)))
21299a2dd95SBruce Richardson #else
21399a2dd95SBruce Richardson #define __rte_alloc_size(...)
21499a2dd95SBruce Richardson #endif
21599a2dd95SBruce Richardson 
21699a2dd95SBruce Richardson #define RTE_PRIORITY_LOG 101
21799a2dd95SBruce Richardson #define RTE_PRIORITY_BUS 110
21899a2dd95SBruce Richardson #define RTE_PRIORITY_CLASS 120
21999a2dd95SBruce Richardson #define RTE_PRIORITY_LAST 65535
22099a2dd95SBruce Richardson 
22199a2dd95SBruce Richardson #define RTE_PRIO(prio) \
22299a2dd95SBruce Richardson 	RTE_PRIORITY_ ## prio
22399a2dd95SBruce Richardson 
22499a2dd95SBruce Richardson /**
22599a2dd95SBruce Richardson  * Run function before main() with high priority.
22699a2dd95SBruce Richardson  *
22799a2dd95SBruce Richardson  * @param func
22899a2dd95SBruce Richardson  *   Constructor function.
22999a2dd95SBruce Richardson  * @param prio
23099a2dd95SBruce Richardson  *   Priority number must be above 100.
23199a2dd95SBruce Richardson  *   Lowest number is the first to run.
23299a2dd95SBruce Richardson  */
23399a2dd95SBruce Richardson #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
23464eff943STyler Retzlaff #ifndef RTE_TOOLCHAIN_MSVC
23599a2dd95SBruce Richardson #define RTE_INIT_PRIO(func, prio) \
23699a2dd95SBruce Richardson static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
23764eff943STyler Retzlaff #else
23864eff943STyler Retzlaff /* definition from the Microsoft CRT */
23964eff943STyler Retzlaff typedef int(__cdecl *_PIFV)(void);
24064eff943STyler Retzlaff 
24164eff943STyler Retzlaff #define CTOR_SECTION_LOG ".CRT$XIB"
24264eff943STyler Retzlaff #define CTOR_SECTION_BUS ".CRT$XIC"
24364eff943STyler Retzlaff #define CTOR_SECTION_CLASS ".CRT$XID"
24464eff943STyler Retzlaff #define CTOR_SECTION_LAST ".CRT$XIY"
24564eff943STyler Retzlaff 
24664eff943STyler Retzlaff #define CTOR_PRIORITY_TO_SECTION(priority) CTOR_SECTION_ ## priority
24764eff943STyler Retzlaff 
24864eff943STyler Retzlaff #define RTE_INIT_PRIO(name, priority) \
24964eff943STyler Retzlaff 	static void name(void); \
25064eff943STyler Retzlaff 	static int __cdecl name ## _thunk(void) { name(); return 0; } \
25164eff943STyler Retzlaff 	__pragma(const_seg(CTOR_PRIORITY_TO_SECTION(priority))) \
25264eff943STyler Retzlaff 	__declspec(allocate(CTOR_PRIORITY_TO_SECTION(priority))) \
25364eff943STyler Retzlaff 	    _PIFV name ## _pointer = &name ## _thunk; \
25464eff943STyler Retzlaff 	__pragma(const_seg()) \
25564eff943STyler Retzlaff 	static void name(void)
25664eff943STyler Retzlaff #endif
25799a2dd95SBruce Richardson #endif
25899a2dd95SBruce Richardson 
25999a2dd95SBruce Richardson /**
26099a2dd95SBruce Richardson  * Run function before main() with low priority.
26199a2dd95SBruce Richardson  *
26299a2dd95SBruce Richardson  * The constructor will be run after prioritized constructors.
26399a2dd95SBruce Richardson  *
26499a2dd95SBruce Richardson  * @param func
26599a2dd95SBruce Richardson  *   Constructor function.
26699a2dd95SBruce Richardson  */
26799a2dd95SBruce Richardson #define RTE_INIT(func) \
26899a2dd95SBruce Richardson 	RTE_INIT_PRIO(func, LAST)
26999a2dd95SBruce Richardson 
27099a2dd95SBruce Richardson /**
27199a2dd95SBruce Richardson  * Run after main() with low priority.
27299a2dd95SBruce Richardson  *
27399a2dd95SBruce Richardson  * @param func
27499a2dd95SBruce Richardson  *   Destructor function name.
27599a2dd95SBruce Richardson  * @param prio
27699a2dd95SBruce Richardson  *   Priority number must be above 100.
27799a2dd95SBruce Richardson  *   Lowest number is the last to run.
27899a2dd95SBruce Richardson  */
27999a2dd95SBruce Richardson #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
28064eff943STyler Retzlaff #ifndef RTE_TOOLCHAIN_MSVC
28199a2dd95SBruce Richardson #define RTE_FINI_PRIO(func, prio) \
28299a2dd95SBruce Richardson static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
28364eff943STyler Retzlaff #else
28464eff943STyler Retzlaff #define DTOR_SECTION_LOG "mydtor$B"
28564eff943STyler Retzlaff #define DTOR_SECTION_BUS "mydtor$C"
28664eff943STyler Retzlaff #define DTOR_SECTION_CLASS "mydtor$D"
28764eff943STyler Retzlaff #define DTOR_SECTION_LAST "mydtor$Y"
28864eff943STyler Retzlaff 
28964eff943STyler Retzlaff #define DTOR_PRIORITY_TO_SECTION(priority) DTOR_SECTION_ ## priority
29064eff943STyler Retzlaff 
29164eff943STyler Retzlaff #define RTE_FINI_PRIO(name, priority) \
29264eff943STyler Retzlaff 	static void name(void); \
29364eff943STyler Retzlaff 	__pragma(const_seg(DTOR_PRIORITY_TO_SECTION(priority))) \
29464eff943STyler Retzlaff 	__declspec(allocate(DTOR_PRIORITY_TO_SECTION(priority))) name ## _pointer = &name; \
29564eff943STyler Retzlaff 	__pragma(const_seg()) \
29664eff943STyler Retzlaff 	static void name(void)
29764eff943STyler Retzlaff #endif
29899a2dd95SBruce Richardson #endif
29999a2dd95SBruce Richardson 
30099a2dd95SBruce Richardson /**
30199a2dd95SBruce Richardson  * Run after main() with high priority.
30299a2dd95SBruce Richardson  *
30399a2dd95SBruce Richardson  * The destructor will be run *before* prioritized destructors.
30499a2dd95SBruce Richardson  *
30599a2dd95SBruce Richardson  * @param func
30699a2dd95SBruce Richardson  *   Destructor function name.
30799a2dd95SBruce Richardson  */
30899a2dd95SBruce Richardson #define RTE_FINI(func) \
30999a2dd95SBruce Richardson 	RTE_FINI_PRIO(func, LAST)
31099a2dd95SBruce Richardson 
31199a2dd95SBruce Richardson /**
31299a2dd95SBruce Richardson  * Hint never returning function
31399a2dd95SBruce Richardson  */
31451574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
31551574a4fSTyler Retzlaff #define __rte_noreturn
31651574a4fSTyler Retzlaff #else
31799a2dd95SBruce Richardson #define __rte_noreturn __attribute__((noreturn))
31851574a4fSTyler Retzlaff #endif
31999a2dd95SBruce Richardson 
32099a2dd95SBruce Richardson /**
321eb13e558SMattias Rönnblom  * Issue a warning in case the function's return value is ignored.
322eb13e558SMattias Rönnblom  *
323eb13e558SMattias Rönnblom  * The use of this attribute should be restricted to cases where
324eb13e558SMattias Rönnblom  * ignoring the marked function's return value is almost always a
325eb13e558SMattias Rönnblom  * bug. With GCC, some effort is required to make clear that ignoring
326eb13e558SMattias Rönnblom  * the return value is intentional. The usual void-casting method to
327eb13e558SMattias Rönnblom  * mark something unused as used does not suppress the warning with
328eb13e558SMattias Rönnblom  * this compiler.
329eb13e558SMattias Rönnblom  *
330eb13e558SMattias Rönnblom  * @code{.c}
331eb13e558SMattias Rönnblom  * __rte_warn_unused_result int foo();
332eb13e558SMattias Rönnblom  *
333eb13e558SMattias Rönnblom  * void ignore_foo_result(void) {
334eb13e558SMattias Rönnblom  *         foo(); // generates a warning with all compilers
335eb13e558SMattias Rönnblom  *
336eb13e558SMattias Rönnblom  *         (void)foo(); // still generates the warning with GCC (but not clang)
337eb13e558SMattias Rönnblom  *
338eb13e558SMattias Rönnblom  *         int unused __rte_unused;
339eb13e558SMattias Rönnblom  *         unused = foo(); // does the trick with all compilers
340eb13e558SMattias Rönnblom  *  }
341eb13e558SMattias Rönnblom  * @endcode
342eb13e558SMattias Rönnblom  */
34351574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
34451574a4fSTyler Retzlaff #define __rte_warn_unused_result
34551574a4fSTyler Retzlaff #else
346eb13e558SMattias Rönnblom #define __rte_warn_unused_result __attribute__((warn_unused_result))
34751574a4fSTyler Retzlaff #endif
348eb13e558SMattias Rönnblom 
349eb13e558SMattias Rönnblom /**
35099a2dd95SBruce Richardson  * Force a function to be inlined
35199a2dd95SBruce Richardson  */
35251574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
35351574a4fSTyler Retzlaff #define __rte_always_inline
35451574a4fSTyler Retzlaff #else
35599a2dd95SBruce Richardson #define __rte_always_inline inline __attribute__((always_inline))
35651574a4fSTyler Retzlaff #endif
35799a2dd95SBruce Richardson 
35899a2dd95SBruce Richardson /**
35999a2dd95SBruce Richardson  * Force a function to be noinlined
36099a2dd95SBruce Richardson  */
36199a2dd95SBruce Richardson #define __rte_noinline __attribute__((noinline))
36299a2dd95SBruce Richardson 
36399a2dd95SBruce Richardson /**
36499a2dd95SBruce Richardson  * Hint function in the hot path
36599a2dd95SBruce Richardson  */
36699a2dd95SBruce Richardson #define __rte_hot __attribute__((hot))
36799a2dd95SBruce Richardson 
36899a2dd95SBruce Richardson /**
36999a2dd95SBruce Richardson  * Hint function in the cold path
37099a2dd95SBruce Richardson  */
37151574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
37251574a4fSTyler Retzlaff #define __rte_cold
37351574a4fSTyler Retzlaff #else
37499a2dd95SBruce Richardson #define __rte_cold __attribute__((cold))
37551574a4fSTyler Retzlaff #endif
37699a2dd95SBruce Richardson 
37748ff13efSDavid Marchand /**
37848ff13efSDavid Marchand  * Disable AddressSanitizer on some code
37948ff13efSDavid Marchand  */
38048ff13efSDavid Marchand #ifdef RTE_MALLOC_ASAN
38148ff13efSDavid Marchand #ifdef RTE_CC_CLANG
38248ff13efSDavid Marchand #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
38348ff13efSDavid Marchand #else
38448ff13efSDavid Marchand #define __rte_no_asan __attribute__((no_sanitize_address))
38548ff13efSDavid Marchand #endif
38648ff13efSDavid Marchand #else /* ! RTE_MALLOC_ASAN */
38748ff13efSDavid Marchand #define __rte_no_asan
38848ff13efSDavid Marchand #endif
38948ff13efSDavid Marchand 
39099a2dd95SBruce Richardson /*********** Macros for pointer arithmetic ********/
39199a2dd95SBruce Richardson 
39299a2dd95SBruce Richardson /**
39399a2dd95SBruce Richardson  * add a byte-value offset to a pointer
39499a2dd95SBruce Richardson  */
39599a2dd95SBruce Richardson #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
39699a2dd95SBruce Richardson 
39799a2dd95SBruce Richardson /**
39899a2dd95SBruce Richardson  * subtract a byte-value offset from a pointer
39999a2dd95SBruce Richardson  */
4001a7374c9SDmitry Kozlyuk #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
40199a2dd95SBruce Richardson 
40299a2dd95SBruce Richardson /**
40399a2dd95SBruce Richardson  * get the difference between two pointer values, i.e. how far apart
40499a2dd95SBruce Richardson  * in bytes are the locations they point two. It is assumed that
40599a2dd95SBruce Richardson  * ptr1 is greater than ptr2.
40699a2dd95SBruce Richardson  */
40799a2dd95SBruce Richardson #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
40899a2dd95SBruce Richardson 
40999a2dd95SBruce Richardson /**
41099a2dd95SBruce Richardson  * Workaround to cast a const field of a structure to non-const type.
41199a2dd95SBruce Richardson  */
41299a2dd95SBruce Richardson #define RTE_CAST_FIELD(var, field, type) \
41399a2dd95SBruce Richardson 	(*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
41499a2dd95SBruce Richardson 
41599a2dd95SBruce Richardson /*********** Macros/static functions for doing alignment ********/
41699a2dd95SBruce Richardson 
41799a2dd95SBruce Richardson 
41899a2dd95SBruce Richardson /**
41999a2dd95SBruce Richardson  * Macro to align a pointer to a given power-of-two. The resultant
42099a2dd95SBruce Richardson  * pointer will be a pointer of the same type as the first parameter, and
42199a2dd95SBruce Richardson  * point to an address no higher than the first parameter. Second parameter
42299a2dd95SBruce Richardson  * must be a power-of-two value.
42399a2dd95SBruce Richardson  */
42499a2dd95SBruce Richardson #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
4251a7374c9SDmitry Kozlyuk 	((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
42699a2dd95SBruce Richardson 
42799a2dd95SBruce Richardson /**
42899a2dd95SBruce Richardson  * Macro to align a value to a given power-of-two. The resultant value
42999a2dd95SBruce Richardson  * will be of the same type as the first parameter, and will be no
43099a2dd95SBruce Richardson  * bigger than the first parameter. Second parameter must be a
43199a2dd95SBruce Richardson  * power-of-two value.
43299a2dd95SBruce Richardson  */
43399a2dd95SBruce Richardson #define RTE_ALIGN_FLOOR(val, align) \
43499a2dd95SBruce Richardson 	(typeof(val))((val) & (~((typeof(val))((align) - 1))))
43599a2dd95SBruce Richardson 
43699a2dd95SBruce Richardson /**
43799a2dd95SBruce Richardson  * Macro to align a pointer to a given power-of-two. The resultant
43899a2dd95SBruce Richardson  * pointer will be a pointer of the same type as the first parameter, and
43999a2dd95SBruce Richardson  * point to an address no lower than the first parameter. Second parameter
44099a2dd95SBruce Richardson  * must be a power-of-two value.
44199a2dd95SBruce Richardson  */
44299a2dd95SBruce Richardson #define RTE_PTR_ALIGN_CEIL(ptr, align) \
44399a2dd95SBruce Richardson 	RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
44499a2dd95SBruce Richardson 
44599a2dd95SBruce Richardson /**
44699a2dd95SBruce Richardson  * Macro to align a value to a given power-of-two. The resultant value
44799a2dd95SBruce Richardson  * will be of the same type as the first parameter, and will be no lower
44899a2dd95SBruce Richardson  * than the first parameter. Second parameter must be a power-of-two
44999a2dd95SBruce Richardson  * value.
45099a2dd95SBruce Richardson  */
45199a2dd95SBruce Richardson #define RTE_ALIGN_CEIL(val, align) \
45299a2dd95SBruce Richardson 	RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
45399a2dd95SBruce Richardson 
45499a2dd95SBruce Richardson /**
45599a2dd95SBruce Richardson  * Macro to align a pointer to a given power-of-two. The resultant
45699a2dd95SBruce Richardson  * pointer will be a pointer of the same type as the first parameter, and
45799a2dd95SBruce Richardson  * point to an address no lower than the first parameter. Second parameter
45899a2dd95SBruce Richardson  * must be a power-of-two value.
45999a2dd95SBruce Richardson  * This function is the same as RTE_PTR_ALIGN_CEIL
46099a2dd95SBruce Richardson  */
46199a2dd95SBruce Richardson #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
46299a2dd95SBruce Richardson 
46399a2dd95SBruce Richardson /**
46499a2dd95SBruce Richardson  * Macro to align a value to a given power-of-two. The resultant
46599a2dd95SBruce Richardson  * value will be of the same type as the first parameter, and
46699a2dd95SBruce Richardson  * will be no lower than the first parameter. Second parameter
46799a2dd95SBruce Richardson  * must be a power-of-two value.
46899a2dd95SBruce Richardson  * This function is the same as RTE_ALIGN_CEIL
46999a2dd95SBruce Richardson  */
47099a2dd95SBruce Richardson #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
47199a2dd95SBruce Richardson 
47299a2dd95SBruce Richardson /**
47399a2dd95SBruce Richardson  * Macro to align a value to the multiple of given value. The resultant
47499a2dd95SBruce Richardson  * value will be of the same type as the first parameter and will be no lower
47599a2dd95SBruce Richardson  * than the first parameter.
47699a2dd95SBruce Richardson  */
47799a2dd95SBruce Richardson #define RTE_ALIGN_MUL_CEIL(v, mul) \
47899a2dd95SBruce Richardson 	((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
47999a2dd95SBruce Richardson 
48099a2dd95SBruce Richardson /**
48199a2dd95SBruce Richardson  * Macro to align a value to the multiple of given value. The resultant
48299a2dd95SBruce Richardson  * value will be of the same type as the first parameter and will be no higher
48399a2dd95SBruce Richardson  * than the first parameter.
48499a2dd95SBruce Richardson  */
48599a2dd95SBruce Richardson #define RTE_ALIGN_MUL_FLOOR(v, mul) \
48699a2dd95SBruce Richardson 	(((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
48799a2dd95SBruce Richardson 
48899a2dd95SBruce Richardson /**
48999a2dd95SBruce Richardson  * Macro to align value to the nearest multiple of the given value.
49099a2dd95SBruce Richardson  * The resultant value might be greater than or less than the first parameter
49199a2dd95SBruce Richardson  * whichever difference is the lowest.
49299a2dd95SBruce Richardson  */
49399a2dd95SBruce Richardson #define RTE_ALIGN_MUL_NEAR(v, mul)				\
494a24456c2STyler Retzlaff 	__extension__ ({					\
49599a2dd95SBruce Richardson 		typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul);	\
49699a2dd95SBruce Richardson 		typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul);	\
49799a2dd95SBruce Richardson 		(ceil - (v)) > ((v) - floor) ? floor : ceil;	\
49899a2dd95SBruce Richardson 	})
49999a2dd95SBruce Richardson 
50099a2dd95SBruce Richardson /**
50199a2dd95SBruce Richardson  * Checks if a pointer is aligned to a given power-of-two value
50299a2dd95SBruce Richardson  *
50399a2dd95SBruce Richardson  * @param ptr
50499a2dd95SBruce Richardson  *   The pointer whose alignment is to be checked
50599a2dd95SBruce Richardson  * @param align
50699a2dd95SBruce Richardson  *   The power-of-two value to which the ptr should be aligned
50799a2dd95SBruce Richardson  *
50899a2dd95SBruce Richardson  * @return
50999a2dd95SBruce Richardson  *   True(1) where the pointer is correctly aligned, false(0) otherwise
51099a2dd95SBruce Richardson  */
51199a2dd95SBruce Richardson static inline int
512f398ebd7SMorten Brørup rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
51399a2dd95SBruce Richardson {
514f398ebd7SMorten Brørup 	return ((uintptr_t)ptr & (align - 1)) == 0;
51599a2dd95SBruce Richardson }
51699a2dd95SBruce Richardson 
51799a2dd95SBruce Richardson /*********** Macros for compile type checks ********/
51899a2dd95SBruce Richardson 
519537caad2SStephen Hemminger /* Workaround for toolchain issues with missing C11 macro in FreeBSD */
520537caad2SStephen Hemminger #if !defined(static_assert) && !defined(__cplusplus)
521537caad2SStephen Hemminger #define	static_assert	_Static_assert
522537caad2SStephen Hemminger #endif
523537caad2SStephen Hemminger 
52499a2dd95SBruce Richardson /**
52599a2dd95SBruce Richardson  * Triggers an error at compilation time if the condition is true.
526537caad2SStephen Hemminger  *
527537caad2SStephen Hemminger  * The do { } while(0) exists to workaround a bug in clang (#55821)
528537caad2SStephen Hemminger  * where it would not handle _Static_assert in a switch case.
52999a2dd95SBruce Richardson  */
530537caad2SStephen Hemminger #define RTE_BUILD_BUG_ON(condition) do { static_assert(!(condition), #condition); } while (0)
53199a2dd95SBruce Richardson 
53299a2dd95SBruce Richardson /*********** Cache line related macros ********/
53399a2dd95SBruce Richardson 
53499a2dd95SBruce Richardson /** Cache line mask. */
53599a2dd95SBruce Richardson #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
53699a2dd95SBruce Richardson 
53799a2dd95SBruce Richardson /** Return the first cache-aligned value greater or equal to size. */
538107dc066SDmitry Kozlyuk #define RTE_CACHE_LINE_ROUNDUP(size) RTE_ALIGN_CEIL(size, RTE_CACHE_LINE_SIZE)
53999a2dd95SBruce Richardson 
54099a2dd95SBruce Richardson /** Cache line size in terms of log2 */
54199a2dd95SBruce Richardson #if RTE_CACHE_LINE_SIZE == 64
54299a2dd95SBruce Richardson #define RTE_CACHE_LINE_SIZE_LOG2 6
54399a2dd95SBruce Richardson #elif RTE_CACHE_LINE_SIZE == 128
54499a2dd95SBruce Richardson #define RTE_CACHE_LINE_SIZE_LOG2 7
54599a2dd95SBruce Richardson #else
54699a2dd95SBruce Richardson #error "Unsupported cache line size"
54799a2dd95SBruce Richardson #endif
54899a2dd95SBruce Richardson 
54999a2dd95SBruce Richardson /** Minimum Cache line size. */
55099a2dd95SBruce Richardson #define RTE_CACHE_LINE_MIN_SIZE 64
55199a2dd95SBruce Richardson 
55299a2dd95SBruce Richardson /** Force alignment to cache line. */
55399a2dd95SBruce Richardson #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
55499a2dd95SBruce Richardson 
55599a2dd95SBruce Richardson /** Force minimum cache line alignment. */
55699a2dd95SBruce Richardson #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
55799a2dd95SBruce Richardson 
55865f600c0SMorten Brørup #define _RTE_CACHE_GUARD_HELPER2(unique) \
559e9fd1ebfSTyler Retzlaff 	alignas(RTE_CACHE_LINE_SIZE) \
560e9fd1ebfSTyler Retzlaff 	char cache_guard_ ## unique[RTE_CACHE_LINE_SIZE * RTE_CACHE_GUARD_LINES]
56165f600c0SMorten Brørup #define _RTE_CACHE_GUARD_HELPER1(unique) _RTE_CACHE_GUARD_HELPER2(unique)
56265f600c0SMorten Brørup /**
56365f600c0SMorten Brørup  * Empty cache lines, to guard against false sharing-like effects
56465f600c0SMorten Brørup  * on systems with a next-N-lines hardware prefetcher.
56565f600c0SMorten Brørup  *
56665f600c0SMorten Brørup  * Use as spacing between data accessed by different lcores,
56765f600c0SMorten Brørup  * to prevent cache thrashing on hardware with speculative prefetching.
56865f600c0SMorten Brørup  */
56965f600c0SMorten Brørup #define RTE_CACHE_GUARD _RTE_CACHE_GUARD_HELPER1(__COUNTER__)
57065f600c0SMorten Brørup 
57199a2dd95SBruce Richardson /*********** PA/IOVA type definitions ********/
57299a2dd95SBruce Richardson 
57399a2dd95SBruce Richardson /** Physical address */
57499a2dd95SBruce Richardson typedef uint64_t phys_addr_t;
57599a2dd95SBruce Richardson #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
57699a2dd95SBruce Richardson 
57799a2dd95SBruce Richardson /**
57899a2dd95SBruce Richardson  * IO virtual address type.
57999a2dd95SBruce Richardson  * When the physical addressing mode (IOVA as PA) is in use,
58099a2dd95SBruce Richardson  * the translation from an IO virtual address (IOVA) to a physical address
58199a2dd95SBruce Richardson  * is a direct mapping, i.e. the same value.
58299a2dd95SBruce Richardson  * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
58399a2dd95SBruce Richardson  */
58499a2dd95SBruce Richardson typedef uint64_t rte_iova_t;
58599a2dd95SBruce Richardson #define RTE_BAD_IOVA ((rte_iova_t)-1)
58699a2dd95SBruce Richardson 
58799a2dd95SBruce Richardson /*********** Structure alignment markers ********/
58899a2dd95SBruce Richardson 
589fc9fa366STyler Retzlaff #ifndef RTE_TOOLCHAIN_MSVC
590fc9fa366STyler Retzlaff 
59199a2dd95SBruce Richardson /** Generic marker for any place in a structure. */
59299a2dd95SBruce Richardson __extension__ typedef void    *RTE_MARKER[0];
59399a2dd95SBruce Richardson /** Marker for 1B alignment in a structure. */
59499a2dd95SBruce Richardson __extension__ typedef uint8_t  RTE_MARKER8[0];
59599a2dd95SBruce Richardson /** Marker for 2B alignment in a structure. */
59699a2dd95SBruce Richardson __extension__ typedef uint16_t RTE_MARKER16[0];
59799a2dd95SBruce Richardson /** Marker for 4B alignment in a structure. */
59899a2dd95SBruce Richardson __extension__ typedef uint32_t RTE_MARKER32[0];
59999a2dd95SBruce Richardson /** Marker for 8B alignment in a structure. */
60099a2dd95SBruce Richardson __extension__ typedef uint64_t RTE_MARKER64[0];
60199a2dd95SBruce Richardson 
602fc9fa366STyler Retzlaff #endif
603fc9fa366STyler Retzlaff 
60499a2dd95SBruce Richardson /*********** Macros for calculating min and max **********/
60599a2dd95SBruce Richardson 
60699a2dd95SBruce Richardson /**
60799a2dd95SBruce Richardson  * Macro to return the minimum of two numbers
60899a2dd95SBruce Richardson  */
60999a2dd95SBruce Richardson #define RTE_MIN(a, b) \
61099a2dd95SBruce Richardson 	__extension__ ({ \
61199a2dd95SBruce Richardson 		typeof (a) _a = (a); \
61299a2dd95SBruce Richardson 		typeof (b) _b = (b); \
61399a2dd95SBruce Richardson 		_a < _b ? _a : _b; \
61499a2dd95SBruce Richardson 	})
61599a2dd95SBruce Richardson 
61699a2dd95SBruce Richardson /**
617ac718edaSStephen Hemminger  * Macro to return the minimum of two numbers
618ac718edaSStephen Hemminger  *
619ac718edaSStephen Hemminger  * As opposed to RTE_MIN, it does not use temporary variables so it is not safe
620ac718edaSStephen Hemminger  * if a or b is an expression. Yet it is guaranteed to be constant for use in
621ac718edaSStephen Hemminger  * static_assert().
622ac718edaSStephen Hemminger  */
623ac718edaSStephen Hemminger #define RTE_MIN_T(a, b, t) \
624ac718edaSStephen Hemminger 	((t)(a) < (t)(b) ? (t)(a) : (t)(b))
625ac718edaSStephen Hemminger 
626ac718edaSStephen Hemminger /**
62799a2dd95SBruce Richardson  * Macro to return the maximum of two numbers
62899a2dd95SBruce Richardson  */
62999a2dd95SBruce Richardson #define RTE_MAX(a, b) \
63099a2dd95SBruce Richardson 	__extension__ ({ \
63199a2dd95SBruce Richardson 		typeof (a) _a = (a); \
63299a2dd95SBruce Richardson 		typeof (b) _b = (b); \
63399a2dd95SBruce Richardson 		_a > _b ? _a : _b; \
63499a2dd95SBruce Richardson 	})
63599a2dd95SBruce Richardson 
636ac718edaSStephen Hemminger /**
637ac718edaSStephen Hemminger  * Macro to return the maximum of two numbers
638ac718edaSStephen Hemminger  *
639ac718edaSStephen Hemminger  * As opposed to RTE_MAX, it does not use temporary variables so it is not safe
640ac718edaSStephen Hemminger  * if a or b is an expression. Yet it is guaranteed to be constant for use in
641ac718edaSStephen Hemminger  * static_assert().
642ac718edaSStephen Hemminger  */
643ac718edaSStephen Hemminger #define RTE_MAX_T(a, b, t) \
644ac718edaSStephen Hemminger 	((t)(a) > (t)(b) ? (t)(a) : (t)(b))
645ac718edaSStephen Hemminger 
64699a2dd95SBruce Richardson /*********** Other general functions / macros ********/
64799a2dd95SBruce Richardson 
64899a2dd95SBruce Richardson #ifndef offsetof
64999a2dd95SBruce Richardson /** Return the offset of a field in a structure. */
65099a2dd95SBruce Richardson #define offsetof(TYPE, MEMBER)  __builtin_offsetof (TYPE, MEMBER)
65199a2dd95SBruce Richardson #endif
65299a2dd95SBruce Richardson 
65399a2dd95SBruce Richardson /**
65499a2dd95SBruce Richardson  * Return pointer to the wrapping struct instance.
65599a2dd95SBruce Richardson  *
65699a2dd95SBruce Richardson  * Example:
65799a2dd95SBruce Richardson  *
65899a2dd95SBruce Richardson  *  struct wrapper {
65999a2dd95SBruce Richardson  *      ...
66099a2dd95SBruce Richardson  *      struct child c;
66199a2dd95SBruce Richardson  *      ...
66299a2dd95SBruce Richardson  *  };
66399a2dd95SBruce Richardson  *
66499a2dd95SBruce Richardson  *  struct child *x = obtain(...);
66599a2dd95SBruce Richardson  *  struct wrapper *w = container_of(x, struct wrapper, c);
66699a2dd95SBruce Richardson  */
66799a2dd95SBruce Richardson #ifndef container_of
66851574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
66951574a4fSTyler Retzlaff #define container_of(ptr, type, member) \
67051574a4fSTyler Retzlaff 			((type *)((uintptr_t)(ptr) - offsetof(type, member)))
67151574a4fSTyler Retzlaff #else
67299a2dd95SBruce Richardson #define container_of(ptr, type, member)	__extension__ ({		\
67399a2dd95SBruce Richardson 			const typeof(((type *)0)->member) *_ptr = (ptr); \
67499a2dd95SBruce Richardson 			__rte_unused type *_target_ptr =	\
67599a2dd95SBruce Richardson 				(type *)(ptr);				\
67699a2dd95SBruce Richardson 			(type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
67799a2dd95SBruce Richardson 		})
67899a2dd95SBruce Richardson #endif
67951574a4fSTyler Retzlaff #endif
68099a2dd95SBruce Richardson 
681a0a388a8SShijith Thotton /** Swap two variables. */
682a0a388a8SShijith Thotton #define RTE_SWAP(a, b) \
683a0a388a8SShijith Thotton 	__extension__ ({ \
684a0a388a8SShijith Thotton 		typeof (a) _a = a; \
685a0a388a8SShijith Thotton 		a = b; \
686a0a388a8SShijith Thotton 		b = _a; \
687a0a388a8SShijith Thotton 	})
688a0a388a8SShijith Thotton 
68999a2dd95SBruce Richardson /**
69099a2dd95SBruce Richardson  * Get the size of a field in a structure.
69199a2dd95SBruce Richardson  *
69299a2dd95SBruce Richardson  * @param type
69399a2dd95SBruce Richardson  *   The type of the structure.
69499a2dd95SBruce Richardson  * @param field
69599a2dd95SBruce Richardson  *   The field in the structure.
69699a2dd95SBruce Richardson  * @return
69799a2dd95SBruce Richardson  *   The size of the field in the structure, in bytes.
69899a2dd95SBruce Richardson  */
69999a2dd95SBruce Richardson #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
70099a2dd95SBruce Richardson 
70199a2dd95SBruce Richardson #define _RTE_STR(x) #x
70299a2dd95SBruce Richardson /** Take a macro value and get a string version of it */
70399a2dd95SBruce Richardson #define RTE_STR(x) _RTE_STR(x)
70499a2dd95SBruce Richardson 
70599a2dd95SBruce Richardson /**
70699a2dd95SBruce Richardson  * ISO C helpers to modify format strings using variadic macros.
70799a2dd95SBruce Richardson  * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
70899a2dd95SBruce Richardson  * An empty %s argument is appended to avoid a dangling comma.
70999a2dd95SBruce Richardson  */
71099a2dd95SBruce Richardson #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
71199a2dd95SBruce Richardson #define RTE_FMT_HEAD(fmt, ...) fmt
71299a2dd95SBruce Richardson #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
71399a2dd95SBruce Richardson 
71499a2dd95SBruce Richardson /** Mask value of type "tp" for the first "ln" bit set. */
71599a2dd95SBruce Richardson #define	RTE_LEN2MASK(ln, tp)	\
71699a2dd95SBruce Richardson 	((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
71799a2dd95SBruce Richardson 
71899a2dd95SBruce Richardson /** Number of elements in the array. */
71999a2dd95SBruce Richardson #define	RTE_DIM(a)	(sizeof (a) / sizeof ((a)[0]))
72099a2dd95SBruce Richardson 
72199a2dd95SBruce Richardson /**
72299a2dd95SBruce Richardson  * Converts a numeric string to the equivalent uint64_t value.
72399a2dd95SBruce Richardson  * As well as straight number conversion, also recognises the suffixes
72499a2dd95SBruce Richardson  * k, m and g for kilobytes, megabytes and gigabytes respectively.
72599a2dd95SBruce Richardson  *
72699a2dd95SBruce Richardson  * If a negative number is passed in  i.e. a string with the first non-black
72799a2dd95SBruce Richardson  * character being "-", zero is returned. Zero is also returned in the case of
72899a2dd95SBruce Richardson  * an error with the strtoull call in the function.
72999a2dd95SBruce Richardson  *
73099a2dd95SBruce Richardson  * @param str
73199a2dd95SBruce Richardson  *     String containing number to convert.
73299a2dd95SBruce Richardson  * @return
73399a2dd95SBruce Richardson  *     Number.
73499a2dd95SBruce Richardson  */
735347623c9SDmitry Kozlyuk uint64_t
736347623c9SDmitry Kozlyuk rte_str_to_size(const char *str);
73799a2dd95SBruce Richardson 
73899a2dd95SBruce Richardson /**
73999a2dd95SBruce Richardson  * Function to terminate the application immediately, printing an error
74099a2dd95SBruce Richardson  * message and returning the exit_code back to the shell.
74199a2dd95SBruce Richardson  *
74299a2dd95SBruce Richardson  * This function never returns
74399a2dd95SBruce Richardson  *
74499a2dd95SBruce Richardson  * @param exit_code
74599a2dd95SBruce Richardson  *     The exit code to be returned by the application
74699a2dd95SBruce Richardson  * @param format
74799a2dd95SBruce Richardson  *     The format string to be used for printing the message. This can include
74899a2dd95SBruce Richardson  *     printf format characters which will be expanded using any further parameters
74999a2dd95SBruce Richardson  *     to the function.
75099a2dd95SBruce Richardson  */
75199a2dd95SBruce Richardson __rte_noreturn void
75299a2dd95SBruce Richardson rte_exit(int exit_code, const char *format, ...)
75399a2dd95SBruce Richardson 	__rte_format_printf(2, 3);
75499a2dd95SBruce Richardson 
75599a2dd95SBruce Richardson #ifdef __cplusplus
75699a2dd95SBruce Richardson }
75799a2dd95SBruce Richardson #endif
75899a2dd95SBruce Richardson 
75999a2dd95SBruce Richardson #endif
760