xref: /dpdk/lib/eal/include/rte_common.h (revision e9fd1ebf981f361844aea9ec94e17f4bda5e1479)
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>
21*e9fd1ebfSTyler Retzlaff #include <stdint.h>
22*e9fd1ebfSTyler 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
4251574a4fSTyler Retzlaff #define __extension__
4351574a4fSTyler Retzlaff #endif
4451574a4fSTyler Retzlaff 
4599a2dd95SBruce Richardson /*
4699a2dd95SBruce Richardson  * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
4799a2dd95SBruce Richardson  * while a host application (like pmdinfogen) may have another compiler.
4899a2dd95SBruce Richardson  * RTE_CC_IS_GNU is true if the file is compiled with GCC,
4999a2dd95SBruce Richardson  * no matter it is a target or host application.
5099a2dd95SBruce Richardson  */
5199a2dd95SBruce Richardson #define RTE_CC_IS_GNU 0
5299a2dd95SBruce Richardson #if defined __clang__
5399a2dd95SBruce Richardson #define RTE_CC_CLANG
5499a2dd95SBruce Richardson #elif defined __INTEL_COMPILER
5599a2dd95SBruce Richardson #define RTE_CC_ICC
5699a2dd95SBruce Richardson #elif defined __GNUC__
5799a2dd95SBruce Richardson #define RTE_CC_GCC
5899a2dd95SBruce Richardson #undef RTE_CC_IS_GNU
5999a2dd95SBruce Richardson #define RTE_CC_IS_GNU 1
6099a2dd95SBruce Richardson #endif
6199a2dd95SBruce Richardson #if RTE_CC_IS_GNU
6299a2dd95SBruce Richardson #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 +	\
6399a2dd95SBruce Richardson 		__GNUC_PATCHLEVEL__)
6499a2dd95SBruce Richardson #endif
6599a2dd95SBruce Richardson 
6699a2dd95SBruce Richardson /**
6799a2dd95SBruce Richardson  * Force alignment
6899a2dd95SBruce Richardson  */
6951574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
7051574a4fSTyler Retzlaff #define __rte_aligned(a)
7151574a4fSTyler Retzlaff #else
7299a2dd95SBruce Richardson #define __rte_aligned(a) __attribute__((__aligned__(a)))
7351574a4fSTyler Retzlaff #endif
7499a2dd95SBruce Richardson 
7599a2dd95SBruce Richardson #ifdef RTE_ARCH_STRICT_ALIGN
7699a2dd95SBruce Richardson typedef uint64_t unaligned_uint64_t __rte_aligned(1);
7799a2dd95SBruce Richardson typedef uint32_t unaligned_uint32_t __rte_aligned(1);
7899a2dd95SBruce Richardson typedef uint16_t unaligned_uint16_t __rte_aligned(1);
7999a2dd95SBruce Richardson #else
8099a2dd95SBruce Richardson typedef uint64_t unaligned_uint64_t;
8199a2dd95SBruce Richardson typedef uint32_t unaligned_uint32_t;
8299a2dd95SBruce Richardson typedef uint16_t unaligned_uint16_t;
8399a2dd95SBruce Richardson #endif
8499a2dd95SBruce Richardson 
8599a2dd95SBruce Richardson /**
8699a2dd95SBruce Richardson  * Force a structure to be packed
8799a2dd95SBruce Richardson  */
8851574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
8951574a4fSTyler Retzlaff #define __rte_packed
9051574a4fSTyler Retzlaff #else
9199a2dd95SBruce Richardson #define __rte_packed __attribute__((__packed__))
9251574a4fSTyler Retzlaff #endif
9399a2dd95SBruce Richardson 
9400901e4dSLuc Pelletier /**
9500901e4dSLuc Pelletier  * Macro to mark a type that is not subject to type-based aliasing rules
9600901e4dSLuc Pelletier  */
9751574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
9851574a4fSTyler Retzlaff #define __rte_may_alias
9951574a4fSTyler Retzlaff #else
10000901e4dSLuc Pelletier #define __rte_may_alias __attribute__((__may_alias__))
10151574a4fSTyler Retzlaff #endif
10200901e4dSLuc Pelletier 
10399a2dd95SBruce Richardson /******* Macro to mark functions and fields scheduled for removal *****/
10451574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
10551574a4fSTyler Retzlaff #define __rte_deprecated
10651574a4fSTyler Retzlaff #define __rte_deprecated_msg(msg)
10751574a4fSTyler Retzlaff #else
10899a2dd95SBruce Richardson #define __rte_deprecated	__attribute__((__deprecated__))
10999a2dd95SBruce Richardson #define __rte_deprecated_msg(msg)	__attribute__((__deprecated__(msg)))
11051574a4fSTyler Retzlaff #endif
11199a2dd95SBruce Richardson 
11299a2dd95SBruce Richardson /**
11399a2dd95SBruce Richardson  *  Macro to mark macros and defines scheduled for removal
11499a2dd95SBruce Richardson  */
11599a2dd95SBruce Richardson #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
11699a2dd95SBruce Richardson #define RTE_PRAGMA(x)  _Pragma(#x)
11799a2dd95SBruce Richardson #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
11899a2dd95SBruce Richardson #define RTE_DEPRECATED(x)  RTE_PRAGMA_WARNING(#x is deprecated)
11999a2dd95SBruce Richardson #else
12099a2dd95SBruce Richardson #define RTE_DEPRECATED(x)
12199a2dd95SBruce Richardson #endif
12299a2dd95SBruce Richardson 
12399a2dd95SBruce Richardson /**
12499a2dd95SBruce Richardson  * Mark a function or variable to a weak reference.
12599a2dd95SBruce Richardson  */
12699a2dd95SBruce Richardson #define __rte_weak __attribute__((__weak__))
12799a2dd95SBruce Richardson 
12899a2dd95SBruce Richardson /**
12999a2dd95SBruce Richardson  * Force symbol to be generated even if it appears to be unused.
13099a2dd95SBruce Richardson  */
13151574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
13251574a4fSTyler Retzlaff #define __rte_used
13351574a4fSTyler Retzlaff #else
13499a2dd95SBruce Richardson #define __rte_used __attribute__((used))
13551574a4fSTyler Retzlaff #endif
13699a2dd95SBruce Richardson 
13799a2dd95SBruce Richardson /*********** Macros to eliminate unused variable warnings ********/
13899a2dd95SBruce Richardson 
13999a2dd95SBruce Richardson /**
14099a2dd95SBruce Richardson  * short definition to mark a function parameter unused
14199a2dd95SBruce Richardson  */
14251574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
14351574a4fSTyler Retzlaff #define __rte_unused
14451574a4fSTyler Retzlaff #else
14599a2dd95SBruce Richardson #define __rte_unused __attribute__((__unused__))
14651574a4fSTyler Retzlaff #endif
14799a2dd95SBruce Richardson 
14899a2dd95SBruce Richardson /**
14999a2dd95SBruce Richardson  * Mark pointer as restricted with regard to pointer aliasing.
15099a2dd95SBruce Richardson  */
15199a2dd95SBruce Richardson #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
15299a2dd95SBruce Richardson #define __rte_restrict __restrict
15399a2dd95SBruce Richardson #else
15499a2dd95SBruce Richardson #define __rte_restrict restrict
15599a2dd95SBruce Richardson #endif
15699a2dd95SBruce Richardson 
15799a2dd95SBruce Richardson /**
15899a2dd95SBruce Richardson  * definition to mark a variable or function parameter as used so
15999a2dd95SBruce Richardson  * as to avoid a compiler warning
16099a2dd95SBruce Richardson  */
16199a2dd95SBruce Richardson #define RTE_SET_USED(x) (void)(x)
16299a2dd95SBruce Richardson 
16399a2dd95SBruce Richardson /**
16499a2dd95SBruce Richardson  * Check format string and its arguments at compile-time.
16599a2dd95SBruce Richardson  *
16699a2dd95SBruce Richardson  * GCC on Windows assumes MS-specific format string by default,
16799a2dd95SBruce Richardson  * even if the underlying stdio implementation is ANSI-compliant,
16899a2dd95SBruce Richardson  * so this must be overridden.
16999a2dd95SBruce Richardson  */
17051574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
17151574a4fSTyler Retzlaff #define __rte_format_printf(format_index, first_arg)
17251574a4fSTyler Retzlaff #else
17399a2dd95SBruce Richardson #if RTE_CC_IS_GNU
17499a2dd95SBruce Richardson #define __rte_format_printf(format_index, first_arg) \
17599a2dd95SBruce Richardson 	__attribute__((format(gnu_printf, format_index, first_arg)))
17699a2dd95SBruce Richardson #else
17799a2dd95SBruce Richardson #define __rte_format_printf(format_index, first_arg) \
17899a2dd95SBruce Richardson 	__attribute__((format(printf, format_index, first_arg)))
17999a2dd95SBruce Richardson #endif
18051574a4fSTyler Retzlaff #endif
18199a2dd95SBruce Richardson 
18299a2dd95SBruce Richardson /**
18374fff67aSTyler Retzlaff  * Specify data or function section/segment.
18474fff67aSTyler Retzlaff  */
18574fff67aSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
18674fff67aSTyler Retzlaff #define __rte_section(name) \
18774fff67aSTyler Retzlaff 	__pragma(data_seg(name)) __declspec(allocate(name))
18874fff67aSTyler Retzlaff #else
18974fff67aSTyler Retzlaff #define __rte_section(name) \
19074fff67aSTyler Retzlaff 	__attribute__((section(name)))
19174fff67aSTyler Retzlaff #endif
19274fff67aSTyler Retzlaff 
19374fff67aSTyler Retzlaff /**
19499a2dd95SBruce Richardson  * Tells compiler that the function returns a value that points to
19599a2dd95SBruce Richardson  * memory, where the size is given by the one or two arguments.
19699a2dd95SBruce Richardson  * Used by compiler to validate object size.
19799a2dd95SBruce Richardson  */
19899a2dd95SBruce Richardson #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
19999a2dd95SBruce Richardson #define __rte_alloc_size(...) \
20099a2dd95SBruce Richardson 	__attribute__((alloc_size(__VA_ARGS__)))
20199a2dd95SBruce Richardson #else
20299a2dd95SBruce Richardson #define __rte_alloc_size(...)
20399a2dd95SBruce Richardson #endif
20499a2dd95SBruce Richardson 
20599a2dd95SBruce Richardson #define RTE_PRIORITY_LOG 101
20699a2dd95SBruce Richardson #define RTE_PRIORITY_BUS 110
20799a2dd95SBruce Richardson #define RTE_PRIORITY_CLASS 120
20899a2dd95SBruce Richardson #define RTE_PRIORITY_LAST 65535
20999a2dd95SBruce Richardson 
21099a2dd95SBruce Richardson #define RTE_PRIO(prio) \
21199a2dd95SBruce Richardson 	RTE_PRIORITY_ ## prio
21299a2dd95SBruce Richardson 
21399a2dd95SBruce Richardson /**
21499a2dd95SBruce Richardson  * Run function before main() with high priority.
21599a2dd95SBruce Richardson  *
21699a2dd95SBruce Richardson  * @param func
21799a2dd95SBruce Richardson  *   Constructor function.
21899a2dd95SBruce Richardson  * @param prio
21999a2dd95SBruce Richardson  *   Priority number must be above 100.
22099a2dd95SBruce Richardson  *   Lowest number is the first to run.
22199a2dd95SBruce Richardson  */
22299a2dd95SBruce Richardson #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
22364eff943STyler Retzlaff #ifndef RTE_TOOLCHAIN_MSVC
22499a2dd95SBruce Richardson #define RTE_INIT_PRIO(func, prio) \
22599a2dd95SBruce Richardson static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
22664eff943STyler Retzlaff #else
22764eff943STyler Retzlaff /* definition from the Microsoft CRT */
22864eff943STyler Retzlaff typedef int(__cdecl *_PIFV)(void);
22964eff943STyler Retzlaff 
23064eff943STyler Retzlaff #define CTOR_SECTION_LOG ".CRT$XIB"
23164eff943STyler Retzlaff #define CTOR_SECTION_BUS ".CRT$XIC"
23264eff943STyler Retzlaff #define CTOR_SECTION_CLASS ".CRT$XID"
23364eff943STyler Retzlaff #define CTOR_SECTION_LAST ".CRT$XIY"
23464eff943STyler Retzlaff 
23564eff943STyler Retzlaff #define CTOR_PRIORITY_TO_SECTION(priority) CTOR_SECTION_ ## priority
23664eff943STyler Retzlaff 
23764eff943STyler Retzlaff #define RTE_INIT_PRIO(name, priority) \
23864eff943STyler Retzlaff 	static void name(void); \
23964eff943STyler Retzlaff 	static int __cdecl name ## _thunk(void) { name(); return 0; } \
24064eff943STyler Retzlaff 	__pragma(const_seg(CTOR_PRIORITY_TO_SECTION(priority))) \
24164eff943STyler Retzlaff 	__declspec(allocate(CTOR_PRIORITY_TO_SECTION(priority))) \
24264eff943STyler Retzlaff 	    _PIFV name ## _pointer = &name ## _thunk; \
24364eff943STyler Retzlaff 	__pragma(const_seg()) \
24464eff943STyler Retzlaff 	static void name(void)
24564eff943STyler Retzlaff #endif
24699a2dd95SBruce Richardson #endif
24799a2dd95SBruce Richardson 
24899a2dd95SBruce Richardson /**
24999a2dd95SBruce Richardson  * Run function before main() with low priority.
25099a2dd95SBruce Richardson  *
25199a2dd95SBruce Richardson  * The constructor will be run after prioritized constructors.
25299a2dd95SBruce Richardson  *
25399a2dd95SBruce Richardson  * @param func
25499a2dd95SBruce Richardson  *   Constructor function.
25599a2dd95SBruce Richardson  */
25699a2dd95SBruce Richardson #define RTE_INIT(func) \
25799a2dd95SBruce Richardson 	RTE_INIT_PRIO(func, LAST)
25899a2dd95SBruce Richardson 
25999a2dd95SBruce Richardson /**
26099a2dd95SBruce Richardson  * Run after main() with low priority.
26199a2dd95SBruce Richardson  *
26299a2dd95SBruce Richardson  * @param func
26399a2dd95SBruce Richardson  *   Destructor function name.
26499a2dd95SBruce Richardson  * @param prio
26599a2dd95SBruce Richardson  *   Priority number must be above 100.
26699a2dd95SBruce Richardson  *   Lowest number is the last to run.
26799a2dd95SBruce Richardson  */
26899a2dd95SBruce Richardson #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
26964eff943STyler Retzlaff #ifndef RTE_TOOLCHAIN_MSVC
27099a2dd95SBruce Richardson #define RTE_FINI_PRIO(func, prio) \
27199a2dd95SBruce Richardson static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
27264eff943STyler Retzlaff #else
27364eff943STyler Retzlaff #define DTOR_SECTION_LOG "mydtor$B"
27464eff943STyler Retzlaff #define DTOR_SECTION_BUS "mydtor$C"
27564eff943STyler Retzlaff #define DTOR_SECTION_CLASS "mydtor$D"
27664eff943STyler Retzlaff #define DTOR_SECTION_LAST "mydtor$Y"
27764eff943STyler Retzlaff 
27864eff943STyler Retzlaff #define DTOR_PRIORITY_TO_SECTION(priority) DTOR_SECTION_ ## priority
27964eff943STyler Retzlaff 
28064eff943STyler Retzlaff #define RTE_FINI_PRIO(name, priority) \
28164eff943STyler Retzlaff 	static void name(void); \
28264eff943STyler Retzlaff 	__pragma(const_seg(DTOR_PRIORITY_TO_SECTION(priority))) \
28364eff943STyler Retzlaff 	__declspec(allocate(DTOR_PRIORITY_TO_SECTION(priority))) name ## _pointer = &name; \
28464eff943STyler Retzlaff 	__pragma(const_seg()) \
28564eff943STyler Retzlaff 	static void name(void)
28664eff943STyler Retzlaff #endif
28799a2dd95SBruce Richardson #endif
28899a2dd95SBruce Richardson 
28999a2dd95SBruce Richardson /**
29099a2dd95SBruce Richardson  * Run after main() with high priority.
29199a2dd95SBruce Richardson  *
29299a2dd95SBruce Richardson  * The destructor will be run *before* prioritized destructors.
29399a2dd95SBruce Richardson  *
29499a2dd95SBruce Richardson  * @param func
29599a2dd95SBruce Richardson  *   Destructor function name.
29699a2dd95SBruce Richardson  */
29799a2dd95SBruce Richardson #define RTE_FINI(func) \
29899a2dd95SBruce Richardson 	RTE_FINI_PRIO(func, LAST)
29999a2dd95SBruce Richardson 
30099a2dd95SBruce Richardson /**
30199a2dd95SBruce Richardson  * Hint never returning function
30299a2dd95SBruce Richardson  */
30351574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
30451574a4fSTyler Retzlaff #define __rte_noreturn
30551574a4fSTyler Retzlaff #else
30699a2dd95SBruce Richardson #define __rte_noreturn __attribute__((noreturn))
30751574a4fSTyler Retzlaff #endif
30899a2dd95SBruce Richardson 
30999a2dd95SBruce Richardson /**
310eb13e558SMattias Rönnblom  * Issue a warning in case the function's return value is ignored.
311eb13e558SMattias Rönnblom  *
312eb13e558SMattias Rönnblom  * The use of this attribute should be restricted to cases where
313eb13e558SMattias Rönnblom  * ignoring the marked function's return value is almost always a
314eb13e558SMattias Rönnblom  * bug. With GCC, some effort is required to make clear that ignoring
315eb13e558SMattias Rönnblom  * the return value is intentional. The usual void-casting method to
316eb13e558SMattias Rönnblom  * mark something unused as used does not suppress the warning with
317eb13e558SMattias Rönnblom  * this compiler.
318eb13e558SMattias Rönnblom  *
319eb13e558SMattias Rönnblom  * @code{.c}
320eb13e558SMattias Rönnblom  * __rte_warn_unused_result int foo();
321eb13e558SMattias Rönnblom  *
322eb13e558SMattias Rönnblom  * void ignore_foo_result(void) {
323eb13e558SMattias Rönnblom  *         foo(); // generates a warning with all compilers
324eb13e558SMattias Rönnblom  *
325eb13e558SMattias Rönnblom  *         (void)foo(); // still generates the warning with GCC (but not clang)
326eb13e558SMattias Rönnblom  *
327eb13e558SMattias Rönnblom  *         int unused __rte_unused;
328eb13e558SMattias Rönnblom  *         unused = foo(); // does the trick with all compilers
329eb13e558SMattias Rönnblom  *  }
330eb13e558SMattias Rönnblom  * @endcode
331eb13e558SMattias Rönnblom  */
33251574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
33351574a4fSTyler Retzlaff #define __rte_warn_unused_result
33451574a4fSTyler Retzlaff #else
335eb13e558SMattias Rönnblom #define __rte_warn_unused_result __attribute__((warn_unused_result))
33651574a4fSTyler Retzlaff #endif
337eb13e558SMattias Rönnblom 
338eb13e558SMattias Rönnblom /**
33999a2dd95SBruce Richardson  * Force a function to be inlined
34099a2dd95SBruce Richardson  */
34151574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
34251574a4fSTyler Retzlaff #define __rte_always_inline
34351574a4fSTyler Retzlaff #else
34499a2dd95SBruce Richardson #define __rte_always_inline inline __attribute__((always_inline))
34551574a4fSTyler Retzlaff #endif
34699a2dd95SBruce Richardson 
34799a2dd95SBruce Richardson /**
34899a2dd95SBruce Richardson  * Force a function to be noinlined
34999a2dd95SBruce Richardson  */
35099a2dd95SBruce Richardson #define __rte_noinline __attribute__((noinline))
35199a2dd95SBruce Richardson 
35299a2dd95SBruce Richardson /**
35399a2dd95SBruce Richardson  * Hint function in the hot path
35499a2dd95SBruce Richardson  */
35599a2dd95SBruce Richardson #define __rte_hot __attribute__((hot))
35699a2dd95SBruce Richardson 
35799a2dd95SBruce Richardson /**
35899a2dd95SBruce Richardson  * Hint function in the cold path
35999a2dd95SBruce Richardson  */
36051574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
36151574a4fSTyler Retzlaff #define __rte_cold
36251574a4fSTyler Retzlaff #else
36399a2dd95SBruce Richardson #define __rte_cold __attribute__((cold))
36451574a4fSTyler Retzlaff #endif
36599a2dd95SBruce Richardson 
36648ff13efSDavid Marchand /**
36748ff13efSDavid Marchand  * Disable AddressSanitizer on some code
36848ff13efSDavid Marchand  */
36948ff13efSDavid Marchand #ifdef RTE_MALLOC_ASAN
37048ff13efSDavid Marchand #ifdef RTE_CC_CLANG
37148ff13efSDavid Marchand #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
37248ff13efSDavid Marchand #else
37348ff13efSDavid Marchand #define __rte_no_asan __attribute__((no_sanitize_address))
37448ff13efSDavid Marchand #endif
37548ff13efSDavid Marchand #else /* ! RTE_MALLOC_ASAN */
37648ff13efSDavid Marchand #define __rte_no_asan
37748ff13efSDavid Marchand #endif
37848ff13efSDavid Marchand 
37999a2dd95SBruce Richardson /*********** Macros for pointer arithmetic ********/
38099a2dd95SBruce Richardson 
38199a2dd95SBruce Richardson /**
38299a2dd95SBruce Richardson  * add a byte-value offset to a pointer
38399a2dd95SBruce Richardson  */
38499a2dd95SBruce Richardson #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
38599a2dd95SBruce Richardson 
38699a2dd95SBruce Richardson /**
38799a2dd95SBruce Richardson  * subtract a byte-value offset from a pointer
38899a2dd95SBruce Richardson  */
3891a7374c9SDmitry Kozlyuk #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
39099a2dd95SBruce Richardson 
39199a2dd95SBruce Richardson /**
39299a2dd95SBruce Richardson  * get the difference between two pointer values, i.e. how far apart
39399a2dd95SBruce Richardson  * in bytes are the locations they point two. It is assumed that
39499a2dd95SBruce Richardson  * ptr1 is greater than ptr2.
39599a2dd95SBruce Richardson  */
39699a2dd95SBruce Richardson #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
39799a2dd95SBruce Richardson 
39899a2dd95SBruce Richardson /**
39999a2dd95SBruce Richardson  * Workaround to cast a const field of a structure to non-const type.
40099a2dd95SBruce Richardson  */
40199a2dd95SBruce Richardson #define RTE_CAST_FIELD(var, field, type) \
40299a2dd95SBruce Richardson 	(*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
40399a2dd95SBruce Richardson 
40499a2dd95SBruce Richardson /*********** Macros/static functions for doing alignment ********/
40599a2dd95SBruce Richardson 
40699a2dd95SBruce Richardson 
40799a2dd95SBruce Richardson /**
40899a2dd95SBruce Richardson  * Macro to align a pointer to a given power-of-two. The resultant
40999a2dd95SBruce Richardson  * pointer will be a pointer of the same type as the first parameter, and
41099a2dd95SBruce Richardson  * point to an address no higher than the first parameter. Second parameter
41199a2dd95SBruce Richardson  * must be a power-of-two value.
41299a2dd95SBruce Richardson  */
41399a2dd95SBruce Richardson #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
4141a7374c9SDmitry Kozlyuk 	((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
41599a2dd95SBruce Richardson 
41699a2dd95SBruce Richardson /**
41799a2dd95SBruce Richardson  * Macro to align a value to a given power-of-two. The resultant value
41899a2dd95SBruce Richardson  * will be of the same type as the first parameter, and will be no
41999a2dd95SBruce Richardson  * bigger than the first parameter. Second parameter must be a
42099a2dd95SBruce Richardson  * power-of-two value.
42199a2dd95SBruce Richardson  */
42299a2dd95SBruce Richardson #define RTE_ALIGN_FLOOR(val, align) \
42399a2dd95SBruce Richardson 	(typeof(val))((val) & (~((typeof(val))((align) - 1))))
42499a2dd95SBruce Richardson 
42599a2dd95SBruce Richardson /**
42699a2dd95SBruce Richardson  * Macro to align a pointer to a given power-of-two. The resultant
42799a2dd95SBruce Richardson  * pointer will be a pointer of the same type as the first parameter, and
42899a2dd95SBruce Richardson  * point to an address no lower than the first parameter. Second parameter
42999a2dd95SBruce Richardson  * must be a power-of-two value.
43099a2dd95SBruce Richardson  */
43199a2dd95SBruce Richardson #define RTE_PTR_ALIGN_CEIL(ptr, align) \
43299a2dd95SBruce Richardson 	RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
43399a2dd95SBruce Richardson 
43499a2dd95SBruce Richardson /**
43599a2dd95SBruce Richardson  * Macro to align a value to a given power-of-two. The resultant value
43699a2dd95SBruce Richardson  * will be of the same type as the first parameter, and will be no lower
43799a2dd95SBruce Richardson  * than the first parameter. Second parameter must be a power-of-two
43899a2dd95SBruce Richardson  * value.
43999a2dd95SBruce Richardson  */
44099a2dd95SBruce Richardson #define RTE_ALIGN_CEIL(val, align) \
44199a2dd95SBruce Richardson 	RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
44299a2dd95SBruce Richardson 
44399a2dd95SBruce Richardson /**
44499a2dd95SBruce Richardson  * Macro to align a pointer to a given power-of-two. The resultant
44599a2dd95SBruce Richardson  * pointer will be a pointer of the same type as the first parameter, and
44699a2dd95SBruce Richardson  * point to an address no lower than the first parameter. Second parameter
44799a2dd95SBruce Richardson  * must be a power-of-two value.
44899a2dd95SBruce Richardson  * This function is the same as RTE_PTR_ALIGN_CEIL
44999a2dd95SBruce Richardson  */
45099a2dd95SBruce Richardson #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
45199a2dd95SBruce Richardson 
45299a2dd95SBruce Richardson /**
45399a2dd95SBruce Richardson  * Macro to align a value to a given power-of-two. The resultant
45499a2dd95SBruce Richardson  * value will be of the same type as the first parameter, and
45599a2dd95SBruce Richardson  * will be no lower than the first parameter. Second parameter
45699a2dd95SBruce Richardson  * must be a power-of-two value.
45799a2dd95SBruce Richardson  * This function is the same as RTE_ALIGN_CEIL
45899a2dd95SBruce Richardson  */
45999a2dd95SBruce Richardson #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
46099a2dd95SBruce Richardson 
46199a2dd95SBruce Richardson /**
46299a2dd95SBruce Richardson  * Macro to align a value to the multiple of given value. The resultant
46399a2dd95SBruce Richardson  * value will be of the same type as the first parameter and will be no lower
46499a2dd95SBruce Richardson  * than the first parameter.
46599a2dd95SBruce Richardson  */
46699a2dd95SBruce Richardson #define RTE_ALIGN_MUL_CEIL(v, mul) \
46799a2dd95SBruce Richardson 	((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
46899a2dd95SBruce Richardson 
46999a2dd95SBruce Richardson /**
47099a2dd95SBruce Richardson  * Macro to align a value to the multiple of given value. The resultant
47199a2dd95SBruce Richardson  * value will be of the same type as the first parameter and will be no higher
47299a2dd95SBruce Richardson  * than the first parameter.
47399a2dd95SBruce Richardson  */
47499a2dd95SBruce Richardson #define RTE_ALIGN_MUL_FLOOR(v, mul) \
47599a2dd95SBruce Richardson 	(((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
47699a2dd95SBruce Richardson 
47799a2dd95SBruce Richardson /**
47899a2dd95SBruce Richardson  * Macro to align value to the nearest multiple of the given value.
47999a2dd95SBruce Richardson  * The resultant value might be greater than or less than the first parameter
48099a2dd95SBruce Richardson  * whichever difference is the lowest.
48199a2dd95SBruce Richardson  */
48299a2dd95SBruce Richardson #define RTE_ALIGN_MUL_NEAR(v, mul)				\
483a24456c2STyler Retzlaff 	__extension__ ({					\
48499a2dd95SBruce Richardson 		typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul);	\
48599a2dd95SBruce Richardson 		typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul);	\
48699a2dd95SBruce Richardson 		(ceil - (v)) > ((v) - floor) ? floor : ceil;	\
48799a2dd95SBruce Richardson 	})
48899a2dd95SBruce Richardson 
48999a2dd95SBruce Richardson /**
49099a2dd95SBruce Richardson  * Checks if a pointer is aligned to a given power-of-two value
49199a2dd95SBruce Richardson  *
49299a2dd95SBruce Richardson  * @param ptr
49399a2dd95SBruce Richardson  *   The pointer whose alignment is to be checked
49499a2dd95SBruce Richardson  * @param align
49599a2dd95SBruce Richardson  *   The power-of-two value to which the ptr should be aligned
49699a2dd95SBruce Richardson  *
49799a2dd95SBruce Richardson  * @return
49899a2dd95SBruce Richardson  *   True(1) where the pointer is correctly aligned, false(0) otherwise
49999a2dd95SBruce Richardson  */
50099a2dd95SBruce Richardson static inline int
501f398ebd7SMorten Brørup rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
50299a2dd95SBruce Richardson {
503f398ebd7SMorten Brørup 	return ((uintptr_t)ptr & (align - 1)) == 0;
50499a2dd95SBruce Richardson }
50599a2dd95SBruce Richardson 
50699a2dd95SBruce Richardson /*********** Macros for compile type checks ********/
50799a2dd95SBruce Richardson 
508537caad2SStephen Hemminger /* Workaround for toolchain issues with missing C11 macro in FreeBSD */
509537caad2SStephen Hemminger #if !defined(static_assert) && !defined(__cplusplus)
510537caad2SStephen Hemminger #define	static_assert	_Static_assert
511537caad2SStephen Hemminger #endif
512537caad2SStephen Hemminger 
51399a2dd95SBruce Richardson /**
51499a2dd95SBruce Richardson  * Triggers an error at compilation time if the condition is true.
515537caad2SStephen Hemminger  *
516537caad2SStephen Hemminger  * The do { } while(0) exists to workaround a bug in clang (#55821)
517537caad2SStephen Hemminger  * where it would not handle _Static_assert in a switch case.
51899a2dd95SBruce Richardson  */
519537caad2SStephen Hemminger #define RTE_BUILD_BUG_ON(condition) do { static_assert(!(condition), #condition); } while (0)
52099a2dd95SBruce Richardson 
52199a2dd95SBruce Richardson /*********** Cache line related macros ********/
52299a2dd95SBruce Richardson 
52399a2dd95SBruce Richardson /** Cache line mask. */
52499a2dd95SBruce Richardson #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
52599a2dd95SBruce Richardson 
52699a2dd95SBruce Richardson /** Return the first cache-aligned value greater or equal to size. */
527107dc066SDmitry Kozlyuk #define RTE_CACHE_LINE_ROUNDUP(size) RTE_ALIGN_CEIL(size, RTE_CACHE_LINE_SIZE)
52899a2dd95SBruce Richardson 
52999a2dd95SBruce Richardson /** Cache line size in terms of log2 */
53099a2dd95SBruce Richardson #if RTE_CACHE_LINE_SIZE == 64
53199a2dd95SBruce Richardson #define RTE_CACHE_LINE_SIZE_LOG2 6
53299a2dd95SBruce Richardson #elif RTE_CACHE_LINE_SIZE == 128
53399a2dd95SBruce Richardson #define RTE_CACHE_LINE_SIZE_LOG2 7
53499a2dd95SBruce Richardson #else
53599a2dd95SBruce Richardson #error "Unsupported cache line size"
53699a2dd95SBruce Richardson #endif
53799a2dd95SBruce Richardson 
53899a2dd95SBruce Richardson /** Minimum Cache line size. */
53999a2dd95SBruce Richardson #define RTE_CACHE_LINE_MIN_SIZE 64
54099a2dd95SBruce Richardson 
54199a2dd95SBruce Richardson /** Force alignment to cache line. */
54251574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
54351574a4fSTyler Retzlaff #define __rte_cache_aligned
54451574a4fSTyler Retzlaff #else
54599a2dd95SBruce Richardson #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
54651574a4fSTyler Retzlaff #endif
54799a2dd95SBruce Richardson 
54899a2dd95SBruce Richardson /** Force minimum cache line alignment. */
54999a2dd95SBruce Richardson #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
55099a2dd95SBruce Richardson 
55165f600c0SMorten Brørup #define _RTE_CACHE_GUARD_HELPER2(unique) \
552*e9fd1ebfSTyler Retzlaff 	alignas(RTE_CACHE_LINE_SIZE) \
553*e9fd1ebfSTyler Retzlaff 	char cache_guard_ ## unique[RTE_CACHE_LINE_SIZE * RTE_CACHE_GUARD_LINES]
55465f600c0SMorten Brørup #define _RTE_CACHE_GUARD_HELPER1(unique) _RTE_CACHE_GUARD_HELPER2(unique)
55565f600c0SMorten Brørup /**
55665f600c0SMorten Brørup  * Empty cache lines, to guard against false sharing-like effects
55765f600c0SMorten Brørup  * on systems with a next-N-lines hardware prefetcher.
55865f600c0SMorten Brørup  *
55965f600c0SMorten Brørup  * Use as spacing between data accessed by different lcores,
56065f600c0SMorten Brørup  * to prevent cache thrashing on hardware with speculative prefetching.
56165f600c0SMorten Brørup  */
56265f600c0SMorten Brørup #define RTE_CACHE_GUARD _RTE_CACHE_GUARD_HELPER1(__COUNTER__)
56365f600c0SMorten Brørup 
56499a2dd95SBruce Richardson /*********** PA/IOVA type definitions ********/
56599a2dd95SBruce Richardson 
56699a2dd95SBruce Richardson /** Physical address */
56799a2dd95SBruce Richardson typedef uint64_t phys_addr_t;
56899a2dd95SBruce Richardson #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
56999a2dd95SBruce Richardson 
57099a2dd95SBruce Richardson /**
57199a2dd95SBruce Richardson  * IO virtual address type.
57299a2dd95SBruce Richardson  * When the physical addressing mode (IOVA as PA) is in use,
57399a2dd95SBruce Richardson  * the translation from an IO virtual address (IOVA) to a physical address
57499a2dd95SBruce Richardson  * is a direct mapping, i.e. the same value.
57599a2dd95SBruce Richardson  * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
57699a2dd95SBruce Richardson  */
57799a2dd95SBruce Richardson typedef uint64_t rte_iova_t;
57899a2dd95SBruce Richardson #define RTE_BAD_IOVA ((rte_iova_t)-1)
57999a2dd95SBruce Richardson 
58099a2dd95SBruce Richardson /*********** Structure alignment markers ********/
58199a2dd95SBruce Richardson 
582fc9fa366STyler Retzlaff #ifndef RTE_TOOLCHAIN_MSVC
583fc9fa366STyler Retzlaff 
58499a2dd95SBruce Richardson /** Generic marker for any place in a structure. */
58599a2dd95SBruce Richardson __extension__ typedef void    *RTE_MARKER[0];
58699a2dd95SBruce Richardson /** Marker for 1B alignment in a structure. */
58799a2dd95SBruce Richardson __extension__ typedef uint8_t  RTE_MARKER8[0];
58899a2dd95SBruce Richardson /** Marker for 2B alignment in a structure. */
58999a2dd95SBruce Richardson __extension__ typedef uint16_t RTE_MARKER16[0];
59099a2dd95SBruce Richardson /** Marker for 4B alignment in a structure. */
59199a2dd95SBruce Richardson __extension__ typedef uint32_t RTE_MARKER32[0];
59299a2dd95SBruce Richardson /** Marker for 8B alignment in a structure. */
59399a2dd95SBruce Richardson __extension__ typedef uint64_t RTE_MARKER64[0];
59499a2dd95SBruce Richardson 
595fc9fa366STyler Retzlaff #endif
596fc9fa366STyler Retzlaff 
59799a2dd95SBruce Richardson /*********** Macros for calculating min and max **********/
59899a2dd95SBruce Richardson 
59999a2dd95SBruce Richardson /**
60099a2dd95SBruce Richardson  * Macro to return the minimum of two numbers
60199a2dd95SBruce Richardson  */
60299a2dd95SBruce Richardson #define RTE_MIN(a, b) \
60399a2dd95SBruce Richardson 	__extension__ ({ \
60499a2dd95SBruce Richardson 		typeof (a) _a = (a); \
60599a2dd95SBruce Richardson 		typeof (b) _b = (b); \
60699a2dd95SBruce Richardson 		_a < _b ? _a : _b; \
60799a2dd95SBruce Richardson 	})
60899a2dd95SBruce Richardson 
60999a2dd95SBruce Richardson /**
610ac718edaSStephen Hemminger  * Macro to return the minimum of two numbers
611ac718edaSStephen Hemminger  *
612ac718edaSStephen Hemminger  * As opposed to RTE_MIN, it does not use temporary variables so it is not safe
613ac718edaSStephen Hemminger  * if a or b is an expression. Yet it is guaranteed to be constant for use in
614ac718edaSStephen Hemminger  * static_assert().
615ac718edaSStephen Hemminger  */
616ac718edaSStephen Hemminger #define RTE_MIN_T(a, b, t) \
617ac718edaSStephen Hemminger 	((t)(a) < (t)(b) ? (t)(a) : (t)(b))
618ac718edaSStephen Hemminger 
619ac718edaSStephen Hemminger /**
62099a2dd95SBruce Richardson  * Macro to return the maximum of two numbers
62199a2dd95SBruce Richardson  */
62299a2dd95SBruce Richardson #define RTE_MAX(a, b) \
62399a2dd95SBruce Richardson 	__extension__ ({ \
62499a2dd95SBruce Richardson 		typeof (a) _a = (a); \
62599a2dd95SBruce Richardson 		typeof (b) _b = (b); \
62699a2dd95SBruce Richardson 		_a > _b ? _a : _b; \
62799a2dd95SBruce Richardson 	})
62899a2dd95SBruce Richardson 
629ac718edaSStephen Hemminger /**
630ac718edaSStephen Hemminger  * Macro to return the maximum of two numbers
631ac718edaSStephen Hemminger  *
632ac718edaSStephen Hemminger  * As opposed to RTE_MAX, it does not use temporary variables so it is not safe
633ac718edaSStephen Hemminger  * if a or b is an expression. Yet it is guaranteed to be constant for use in
634ac718edaSStephen Hemminger  * static_assert().
635ac718edaSStephen Hemminger  */
636ac718edaSStephen Hemminger #define RTE_MAX_T(a, b, t) \
637ac718edaSStephen Hemminger 	((t)(a) > (t)(b) ? (t)(a) : (t)(b))
638ac718edaSStephen Hemminger 
63999a2dd95SBruce Richardson /*********** Other general functions / macros ********/
64099a2dd95SBruce Richardson 
64199a2dd95SBruce Richardson #ifndef offsetof
64299a2dd95SBruce Richardson /** Return the offset of a field in a structure. */
64399a2dd95SBruce Richardson #define offsetof(TYPE, MEMBER)  __builtin_offsetof (TYPE, MEMBER)
64499a2dd95SBruce Richardson #endif
64599a2dd95SBruce Richardson 
64699a2dd95SBruce Richardson /**
64799a2dd95SBruce Richardson  * Return pointer to the wrapping struct instance.
64899a2dd95SBruce Richardson  *
64999a2dd95SBruce Richardson  * Example:
65099a2dd95SBruce Richardson  *
65199a2dd95SBruce Richardson  *  struct wrapper {
65299a2dd95SBruce Richardson  *      ...
65399a2dd95SBruce Richardson  *      struct child c;
65499a2dd95SBruce Richardson  *      ...
65599a2dd95SBruce Richardson  *  };
65699a2dd95SBruce Richardson  *
65799a2dd95SBruce Richardson  *  struct child *x = obtain(...);
65899a2dd95SBruce Richardson  *  struct wrapper *w = container_of(x, struct wrapper, c);
65999a2dd95SBruce Richardson  */
66099a2dd95SBruce Richardson #ifndef container_of
66151574a4fSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
66251574a4fSTyler Retzlaff #define container_of(ptr, type, member) \
66351574a4fSTyler Retzlaff 			((type *)((uintptr_t)(ptr) - offsetof(type, member)))
66451574a4fSTyler Retzlaff #else
66599a2dd95SBruce Richardson #define container_of(ptr, type, member)	__extension__ ({		\
66699a2dd95SBruce Richardson 			const typeof(((type *)0)->member) *_ptr = (ptr); \
66799a2dd95SBruce Richardson 			__rte_unused type *_target_ptr =	\
66899a2dd95SBruce Richardson 				(type *)(ptr);				\
66999a2dd95SBruce Richardson 			(type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
67099a2dd95SBruce Richardson 		})
67199a2dd95SBruce Richardson #endif
67251574a4fSTyler Retzlaff #endif
67399a2dd95SBruce Richardson 
674a0a388a8SShijith Thotton /** Swap two variables. */
675a0a388a8SShijith Thotton #define RTE_SWAP(a, b) \
676a0a388a8SShijith Thotton 	__extension__ ({ \
677a0a388a8SShijith Thotton 		typeof (a) _a = a; \
678a0a388a8SShijith Thotton 		a = b; \
679a0a388a8SShijith Thotton 		b = _a; \
680a0a388a8SShijith Thotton 	})
681a0a388a8SShijith Thotton 
68299a2dd95SBruce Richardson /**
68399a2dd95SBruce Richardson  * Get the size of a field in a structure.
68499a2dd95SBruce Richardson  *
68599a2dd95SBruce Richardson  * @param type
68699a2dd95SBruce Richardson  *   The type of the structure.
68799a2dd95SBruce Richardson  * @param field
68899a2dd95SBruce Richardson  *   The field in the structure.
68999a2dd95SBruce Richardson  * @return
69099a2dd95SBruce Richardson  *   The size of the field in the structure, in bytes.
69199a2dd95SBruce Richardson  */
69299a2dd95SBruce Richardson #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
69399a2dd95SBruce Richardson 
69499a2dd95SBruce Richardson #define _RTE_STR(x) #x
69599a2dd95SBruce Richardson /** Take a macro value and get a string version of it */
69699a2dd95SBruce Richardson #define RTE_STR(x) _RTE_STR(x)
69799a2dd95SBruce Richardson 
69899a2dd95SBruce Richardson /**
69999a2dd95SBruce Richardson  * ISO C helpers to modify format strings using variadic macros.
70099a2dd95SBruce Richardson  * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
70199a2dd95SBruce Richardson  * An empty %s argument is appended to avoid a dangling comma.
70299a2dd95SBruce Richardson  */
70399a2dd95SBruce Richardson #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
70499a2dd95SBruce Richardson #define RTE_FMT_HEAD(fmt, ...) fmt
70599a2dd95SBruce Richardson #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
70699a2dd95SBruce Richardson 
70799a2dd95SBruce Richardson /** Mask value of type "tp" for the first "ln" bit set. */
70899a2dd95SBruce Richardson #define	RTE_LEN2MASK(ln, tp)	\
70999a2dd95SBruce Richardson 	((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
71099a2dd95SBruce Richardson 
71199a2dd95SBruce Richardson /** Number of elements in the array. */
71299a2dd95SBruce Richardson #define	RTE_DIM(a)	(sizeof (a) / sizeof ((a)[0]))
71399a2dd95SBruce Richardson 
71499a2dd95SBruce Richardson /**
71599a2dd95SBruce Richardson  * Converts a numeric string to the equivalent uint64_t value.
71699a2dd95SBruce Richardson  * As well as straight number conversion, also recognises the suffixes
71799a2dd95SBruce Richardson  * k, m and g for kilobytes, megabytes and gigabytes respectively.
71899a2dd95SBruce Richardson  *
71999a2dd95SBruce Richardson  * If a negative number is passed in  i.e. a string with the first non-black
72099a2dd95SBruce Richardson  * character being "-", zero is returned. Zero is also returned in the case of
72199a2dd95SBruce Richardson  * an error with the strtoull call in the function.
72299a2dd95SBruce Richardson  *
72399a2dd95SBruce Richardson  * @param str
72499a2dd95SBruce Richardson  *     String containing number to convert.
72599a2dd95SBruce Richardson  * @return
72699a2dd95SBruce Richardson  *     Number.
72799a2dd95SBruce Richardson  */
728347623c9SDmitry Kozlyuk uint64_t
729347623c9SDmitry Kozlyuk rte_str_to_size(const char *str);
73099a2dd95SBruce Richardson 
73199a2dd95SBruce Richardson /**
73299a2dd95SBruce Richardson  * Function to terminate the application immediately, printing an error
73399a2dd95SBruce Richardson  * message and returning the exit_code back to the shell.
73499a2dd95SBruce Richardson  *
73599a2dd95SBruce Richardson  * This function never returns
73699a2dd95SBruce Richardson  *
73799a2dd95SBruce Richardson  * @param exit_code
73899a2dd95SBruce Richardson  *     The exit code to be returned by the application
73999a2dd95SBruce Richardson  * @param format
74099a2dd95SBruce Richardson  *     The format string to be used for printing the message. This can include
74199a2dd95SBruce Richardson  *     printf format characters which will be expanded using any further parameters
74299a2dd95SBruce Richardson  *     to the function.
74399a2dd95SBruce Richardson  */
74499a2dd95SBruce Richardson __rte_noreturn void
74599a2dd95SBruce Richardson rte_exit(int exit_code, const char *format, ...)
74699a2dd95SBruce Richardson 	__rte_format_printf(2, 3);
74799a2dd95SBruce Richardson 
74899a2dd95SBruce Richardson #ifdef __cplusplus
74999a2dd95SBruce Richardson }
75099a2dd95SBruce Richardson #endif
75199a2dd95SBruce Richardson 
75299a2dd95SBruce Richardson #endif
753