20 #include <rte_config.h>
29 #ifndef RTE_TOOLCHAIN_MSVC
31 #define typeof __typeof__
41 #ifdef RTE_TOOLCHAIN_MSVC
47 #ifdef RTE_TOOLCHAIN_MSVC
48 #define __rte_constant(e) 0
50 #define __rte_constant(e) __extension__(__builtin_constant_p(e))
59 #define RTE_CC_IS_GNU 0
62 #elif defined __INTEL_COMPILER
64 #elif defined __GNUC__
67 #define RTE_CC_IS_GNU 1
70 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
86 #ifdef RTE_TOOLCHAIN_MSVC
87 #define __rte_aligned(a) __declspec(align(a))
89 #define __rte_aligned(a) __attribute__((__aligned__(a)))
92 #ifdef RTE_ARCH_STRICT_ALIGN
97 typedef uint64_t unaligned_uint64_t;
98 typedef uint32_t unaligned_uint32_t;
99 typedef uint16_t unaligned_uint16_t;
105 #ifdef RTE_TOOLCHAIN_MSVC
108 #define __rte_packed __attribute__((__packed__))
114 #ifdef RTE_TOOLCHAIN_MSVC
115 #define __rte_may_alias
117 #define __rte_may_alias __attribute__((__may_alias__))
121 #ifdef RTE_TOOLCHAIN_MSVC
122 #define __rte_deprecated
123 #define __rte_deprecated_msg(msg)
125 #define __rte_deprecated __attribute__((__deprecated__))
126 #define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg)))
132 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
133 #define RTE_PRAGMA(x) _Pragma(#x)
134 #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
135 #define RTE_DEPRECATED(x) RTE_PRAGMA_WARNING(#x is deprecated)
137 #define RTE_DEPRECATED(x)
143 #define __rte_weak __attribute__((__weak__))
148 #ifdef RTE_TOOLCHAIN_MSVC
151 #define __rte_pure __attribute__((pure))
157 #ifdef RTE_TOOLCHAIN_MSVC
160 #define __rte_used __attribute__((used))
168 #ifdef RTE_TOOLCHAIN_MSVC
171 #define __rte_unused __attribute__((__unused__))
177 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
178 #define __rte_restrict __restrict
180 #define __rte_restrict restrict
187 #define RTE_SET_USED(x) (void)(x)
196 #ifdef RTE_TOOLCHAIN_MSVC
197 #define __rte_format_printf(format_index, first_arg)
200 #define __rte_format_printf(format_index, first_arg) \
201 __attribute__((format(gnu_printf, format_index, first_arg)))
203 #define __rte_format_printf(format_index, first_arg) \
204 __attribute__((format(printf, format_index, first_arg)))
211 #ifdef RTE_TOOLCHAIN_MSVC
212 #define __rte_section(name) \
213 __pragma(data_seg(name)) __declspec(allocate(name))
215 #define __rte_section(name) \
216 __attribute__((section(name)))
224 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
225 #define __rte_alloc_size(...) \
226 __attribute__((alloc_size(__VA_ARGS__)))
228 #define __rte_alloc_size(...)
237 #if defined(RTE_CC_GCC)
238 #define __rte_alloc_align(argno) \
239 __attribute__((alloc_align(argno)))
241 #define __rte_alloc_align(argno)
248 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
249 #define __rte_malloc __attribute__((__malloc__))
258 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 110000)
259 #define __rte_dealloc(dealloc, argno) \
260 __attribute__((malloc(dealloc, argno)))
262 #define __rte_dealloc(dealloc, argno)
265 #define RTE_PRIORITY_LOG 101
266 #define RTE_PRIORITY_BUS 110
267 #define RTE_PRIORITY_CLASS 120
268 #define RTE_PRIORITY_LAST 65535
270 #define RTE_PRIO(prio) \
271 RTE_PRIORITY_ ## prio
282 #ifndef RTE_INIT_PRIO
283 #ifndef RTE_TOOLCHAIN_MSVC
284 #define RTE_INIT_PRIO(func, prio) \
285 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
288 typedef int(__cdecl *_PIFV)(void);
290 #define CTOR_SECTION_LOG ".CRT$XIB"
291 #define CTOR_SECTION_BUS ".CRT$XIC"
292 #define CTOR_SECTION_CLASS ".CRT$XID"
293 #define CTOR_SECTION_LAST ".CRT$XIY"
295 #define CTOR_PRIORITY_TO_SECTION(priority) CTOR_SECTION_ ## priority
297 #define RTE_INIT_PRIO(name, priority) \
298 static void name(void); \
299 static int __cdecl name ## _thunk(void) { name(); return 0; } \
300 __pragma(const_seg(CTOR_PRIORITY_TO_SECTION(priority))) \
301 __declspec(allocate(CTOR_PRIORITY_TO_SECTION(priority))) \
302 _PIFV name ## _pointer = &name ## _thunk; \
303 __pragma(const_seg()) \
304 static void name(void)
316 #define RTE_INIT(func) \
317 RTE_INIT_PRIO(func, LAST)
328 #ifndef RTE_FINI_PRIO
329 #ifndef RTE_TOOLCHAIN_MSVC
330 #define RTE_FINI_PRIO(func, prio) \
331 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
333 #define DTOR_SECTION_LOG "mydtor$B"
334 #define DTOR_SECTION_BUS "mydtor$C"
335 #define DTOR_SECTION_CLASS "mydtor$D"
336 #define DTOR_SECTION_LAST "mydtor$Y"
338 #define DTOR_PRIORITY_TO_SECTION(priority) DTOR_SECTION_ ## priority
340 #define RTE_FINI_PRIO(name, priority) \
341 static void name(void); \
342 __pragma(const_seg(DTOR_PRIORITY_TO_SECTION(priority))) \
343 __declspec(allocate(DTOR_PRIORITY_TO_SECTION(priority))) void *name ## _pointer = &name; \
344 __pragma(const_seg()) \
345 static void name(void)
357 #define RTE_FINI(func) \
358 RTE_FINI_PRIO(func, LAST)
363 #ifdef RTE_TOOLCHAIN_MSVC
364 #define __rte_noreturn
366 #define __rte_noreturn __attribute__((noreturn))
372 #if defined(RTE_TOOLCHAIN_GCC) || defined(RTE_TOOLCHAIN_CLANG)
373 #define __rte_unreachable() __extension__(__builtin_unreachable())
375 #define __rte_unreachable() __assume(0)
401 #ifdef RTE_TOOLCHAIN_MSVC
402 #define __rte_warn_unused_result
404 #define __rte_warn_unused_result __attribute__((warn_unused_result))
410 #ifdef RTE_TOOLCHAIN_MSVC
411 #define __rte_always_inline
413 #define __rte_always_inline inline __attribute__((always_inline))
419 #define __rte_noinline __attribute__((noinline))
424 #define __rte_hot __attribute__((hot))
429 #ifdef RTE_TOOLCHAIN_MSVC
432 #define __rte_cold __attribute__((cold))
441 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 130000)
442 #define __rte_assume(condition) __attribute__((assume(condition)))
443 #elif defined(RTE_TOOLCHAIN_GCC)
444 #define __rte_assume(condition) do { if (!(condition)) __rte_unreachable(); } while (0)
445 #elif defined(RTE_TOOLCHAIN_CLANG)
446 #define __rte_assume(condition) __extension__(__builtin_assume(condition))
448 #define __rte_assume(condition) __assume(condition)
454 #ifdef RTE_MALLOC_ASAN
456 #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
458 #define __rte_no_asan __attribute__((no_sanitize_address))
461 #define __rte_no_asan
469 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
474 #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
481 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
486 #define RTE_CAST_FIELD(var, field, type) \
487 (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
498 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
499 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
507 #define RTE_ALIGN_FLOOR(val, align) \
508 (typeof(val))((val) & (~((typeof(val))((align) - 1))))
516 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
517 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
525 #define RTE_ALIGN_CEIL(val, align) \
526 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
535 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
544 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
551 #define RTE_ALIGN_MUL_CEIL(v, mul) \
552 ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
559 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
560 (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
567 #define RTE_ALIGN_MUL_NEAR(v, mul) \
569 typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
570 typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
571 (ceil - (v)) > ((v) - floor) ? floor : ceil; \
588 return ((uintptr_t)ptr & (align - 1)) == 0;
594 #if !defined(static_assert) && !defined(__cplusplus)
595 #define static_assert _Static_assert
604 #define RTE_BUILD_BUG_ON(condition) do { static_assert(!(condition), #condition); } while (0)
609 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
612 #define RTE_CACHE_LINE_ROUNDUP(size) RTE_ALIGN_CEIL(size, RTE_CACHE_LINE_SIZE)
615 #if RTE_CACHE_LINE_SIZE == 64
616 #define RTE_CACHE_LINE_SIZE_LOG2 6
617 #elif RTE_CACHE_LINE_SIZE == 128
618 #define RTE_CACHE_LINE_SIZE_LOG2 7
620 #error "Unsupported cache line size"
624 #define RTE_CACHE_LINE_MIN_SIZE 64
627 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
630 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
632 #define _RTE_CACHE_GUARD_HELPER2(unique) \
633 alignas(RTE_CACHE_LINE_SIZE) \
634 char cache_guard_ ## unique[RTE_CACHE_LINE_SIZE * RTE_CACHE_GUARD_LINES]
635 #define _RTE_CACHE_GUARD_HELPER1(unique) _RTE_CACHE_GUARD_HELPER2(unique)
643 #define RTE_CACHE_GUARD _RTE_CACHE_GUARD_HELPER1(__COUNTER__)
649 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
659 #define RTE_BAD_IOVA ((rte_iova_t)-1)
663 #ifndef RTE_TOOLCHAIN_MSVC
683 #define RTE_MIN(a, b) \
685 typeof (a) _a = (a); \
686 typeof (b) _b = (b); \
697 #define RTE_MIN_T(a, b, t) \
698 ((t)(a) < (t)(b) ? (t)(a) : (t)(b))
703 #define RTE_MAX(a, b) \
705 typeof (a) _a = (a); \
706 typeof (b) _b = (b); \
717 #define RTE_MAX_T(a, b, t) \
718 ((t)(a) > (t)(b) ? (t)(a) : (t)(b))
724 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
742 #ifdef RTE_TOOLCHAIN_MSVC
743 #define container_of(ptr, type, member) \
744 ((type *)((uintptr_t)(ptr) - offsetof(type, member)))
746 #define container_of(ptr, type, member) __extension__ ({ \
747 const typeof(((type *)0)->member) *_ptr = (ptr); \
748 __rte_unused type *_target_ptr = \
750 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
756 #define RTE_SWAP(a, b) \
773 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
775 #define _RTE_STR(x) #x
777 #define RTE_STR(x) _RTE_STR(x)
784 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
785 #define RTE_FMT_HEAD(fmt, ...) fmt
786 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
789 #define RTE_LEN2MASK(ln, tp) \
790 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
793 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
uint64_t rte_str_to_size(const char *str)
__extension__ typedef uint64_t RTE_MARKER64[0]
__extension__ typedef uint8_t RTE_MARKER8[0]
__extension__ typedef uint32_t RTE_MARKER32[0]
__extension__ typedef uint16_t RTE_MARKER16[0]
#define __rte_format_printf(format_index, first_arg)
static int rte_is_aligned(const void *const __rte_restrict ptr, const unsigned int align)
__rte_noreturn void rte_exit(int exit_code, const char *format,...) __rte_format_printf(2
__extension__ typedef void * RTE_MARKER[0]