21 #include <rte_compat.h>
22 #include <rte_config.h>
31 #ifndef RTE_TOOLCHAIN_MSVC
33 #define typeof __typeof__
43 #ifdef RTE_TOOLCHAIN_MSVC
81 #ifdef RTE_TOOLCHAIN_MSVC
82 #define __rte_constant(e) _Generic((1 ? (void *) ((e) * 0ll) : (int *) 0), int * : 1, void * : 0)
84 #define __rte_constant(e) __extension__(__builtin_constant_p(e))
93 #define RTE_CC_IS_GNU 0
96 #elif defined __GNUC__
99 #define RTE_CC_IS_GNU 1
102 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
118 #ifdef RTE_TOOLCHAIN_MSVC
119 #define __rte_aligned(a) __declspec(align(a))
121 #define __rte_aligned(a) __attribute__((__aligned__(a)))
124 #ifdef RTE_ARCH_STRICT_ALIGN
129 typedef uint64_t unaligned_uint64_t;
130 typedef uint32_t unaligned_uint32_t;
131 typedef uint16_t unaligned_uint16_t;
141 #ifdef RTE_TOOLCHAIN_MSVC
142 #define __rte_packed RTE_DEPRECATED(__rte_packed)
144 #define __rte_packed (RTE_DEPRECATED(__rte_packed) __attribute__((__packed__)))
154 #ifdef RTE_TOOLCHAIN_MSVC
155 #define __rte_packed_begin __pragma(pack(push, 1))
156 #define __rte_packed_end __pragma(pack(pop))
158 #define __rte_packed_begin
159 #define __rte_packed_end __attribute__((__packed__))
165 #ifdef RTE_TOOLCHAIN_MSVC
166 #define __rte_may_alias
168 #define __rte_may_alias __attribute__((__may_alias__))
172 #ifdef RTE_TOOLCHAIN_MSVC
173 #define __rte_deprecated
174 #define __rte_deprecated_msg(msg)
176 #define __rte_deprecated __attribute__((__deprecated__))
177 #define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg)))
183 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
184 #define RTE_PRAGMA(x) _Pragma(#x)
185 #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
186 #define RTE_DEPRECATED(x) RTE_PRAGMA_WARNING(#x is deprecated)
188 #define RTE_DEPRECATED(x)
195 #if !defined(RTE_TOOLCHAIN_MSVC)
196 #define __rte_diagnostic_push _Pragma("GCC diagnostic push")
197 #define __rte_diagnostic_pop _Pragma("GCC diagnostic pop")
199 #define __rte_diagnostic_push
200 #define __rte_diagnostic_pop
207 #if !defined(RTE_TOOLCHAIN_MSVC)
208 #define __rte_diagnostic_ignored_wcast_qual _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
210 #define __rte_diagnostic_ignored_wcast_qual
216 #ifdef RTE_TOOLCHAIN_MSVC
217 #define __rte_weak RTE_DEPRECATED(__rte_weak)
219 #define __rte_weak RTE_DEPRECATED(__rte_weak) __attribute__((__weak__))
225 #ifdef RTE_TOOLCHAIN_MSVC
228 #define __rte_pure __attribute__((pure))
234 #ifdef RTE_TOOLCHAIN_MSVC
237 #define __rte_used __attribute__((used))
245 #ifdef RTE_TOOLCHAIN_MSVC
248 #define __rte_unused __attribute__((__unused__))
254 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
255 #define __rte_restrict __restrict
257 #define __rte_restrict restrict
264 #define RTE_SET_USED(x) (void)(x)
273 #ifdef RTE_TOOLCHAIN_MSVC
274 #define __rte_format_printf(format_index, first_arg)
277 #define __rte_format_printf(format_index, first_arg) \
278 __attribute__((format(gnu_printf, format_index, first_arg)))
280 #define __rte_format_printf(format_index, first_arg) \
281 __attribute__((format(printf, format_index, first_arg)))
288 #ifdef RTE_TOOLCHAIN_MSVC
289 #define __rte_section(name) \
290 __pragma(data_seg(name)) __declspec(allocate(name))
292 #define __rte_section(name) \
293 __attribute__((section(name)))
301 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
302 #define __rte_alloc_size(...) \
303 __attribute__((alloc_size(__VA_ARGS__)))
305 #define __rte_alloc_size(...)
314 #if defined(RTE_CC_GCC)
315 #define __rte_alloc_align(argno) \
316 __attribute__((alloc_align(argno)))
318 #define __rte_alloc_align(argno)
325 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
326 #define __rte_malloc __attribute__((__malloc__))
335 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 110000)
336 #define __rte_dealloc(dealloc, argno) \
337 __attribute__((malloc(dealloc, argno)))
339 #define __rte_dealloc(dealloc, argno)
342 #define RTE_PRIORITY_LOG 101
343 #define RTE_PRIORITY_BUS 110
344 #define RTE_PRIORITY_CLASS 120
345 #define RTE_PRIORITY_LAST 65535
347 #define RTE_PRIO(prio) \
348 RTE_PRIORITY_ ## prio
359 #ifndef RTE_INIT_PRIO
360 #ifndef RTE_TOOLCHAIN_MSVC
361 #define RTE_INIT_PRIO(func, prio) \
362 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
365 typedef int(__cdecl *_PIFV)(void);
367 #define CTOR_SECTION_LOG ".CRT$XIB"
368 #define CTOR_SECTION_BUS ".CRT$XIC"
369 #define CTOR_SECTION_CLASS ".CRT$XID"
370 #define CTOR_SECTION_LAST ".CRT$XIY"
372 #define CTOR_PRIORITY_TO_SECTION(priority) CTOR_SECTION_ ## priority
374 #define RTE_INIT_PRIO(name, priority) \
375 static void name(void); \
376 static int __cdecl name ## _thunk(void) { name(); return 0; } \
377 __pragma(const_seg(CTOR_PRIORITY_TO_SECTION(priority))) \
378 __declspec(allocate(CTOR_PRIORITY_TO_SECTION(priority))) \
379 _PIFV name ## _pointer = &name ## _thunk; \
380 __pragma(const_seg()) \
381 static void name(void)
393 #define RTE_INIT(func) \
394 RTE_INIT_PRIO(func, LAST)
405 #ifndef RTE_FINI_PRIO
406 #ifndef RTE_TOOLCHAIN_MSVC
407 #define RTE_FINI_PRIO(func, prio) \
408 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
410 #define DTOR_SECTION_LOG "mydtor$B"
411 #define DTOR_SECTION_BUS "mydtor$C"
412 #define DTOR_SECTION_CLASS "mydtor$D"
413 #define DTOR_SECTION_LAST "mydtor$Y"
415 #define DTOR_PRIORITY_TO_SECTION(priority) DTOR_SECTION_ ## priority
417 #define RTE_FINI_PRIO(name, priority) \
418 static void name(void); \
419 __pragma(const_seg(DTOR_PRIORITY_TO_SECTION(priority))) \
420 __declspec(allocate(DTOR_PRIORITY_TO_SECTION(priority))) void *name ## _pointer = &name; \
421 __pragma(const_seg()) \
422 static void name(void)
434 #define RTE_FINI(func) \
435 RTE_FINI_PRIO(func, LAST)
440 #ifdef RTE_TOOLCHAIN_MSVC
441 #define __rte_noreturn
443 #define __rte_noreturn __attribute__((noreturn))
449 #if defined(RTE_TOOLCHAIN_GCC) || defined(RTE_TOOLCHAIN_CLANG)
450 #define __rte_unreachable() __extension__(__builtin_unreachable())
452 #define __rte_unreachable() __assume(0)
478 #ifdef RTE_TOOLCHAIN_MSVC
479 #define __rte_warn_unused_result
481 #define __rte_warn_unused_result __attribute__((warn_unused_result))
503 #ifdef RTE_TOOLCHAIN_MSVC
504 #define __rte_always_inline __forceinline
506 #define __rte_always_inline inline __attribute__((always_inline))
516 #ifdef RTE_TOOLCHAIN_MSVC
517 #define __rte_noinline __declspec(noinline)
519 #define __rte_noinline __attribute__((noinline))
531 #ifdef RTE_TOOLCHAIN_MSVC
534 #define __rte_hot __attribute__((hot))
548 #ifdef RTE_TOOLCHAIN_MSVC
551 #define __rte_cold __attribute__((cold))
560 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 130000)
561 #define __rte_assume(condition) __attribute__((assume(condition)))
562 #elif defined(RTE_TOOLCHAIN_GCC)
563 #define __rte_assume(condition) do { if (!(condition)) __rte_unreachable(); } while (0)
564 #elif defined(RTE_TOOLCHAIN_CLANG)
565 #define __rte_assume(condition) __extension__(__builtin_assume(condition))
567 #define __rte_assume(condition) __assume(condition)
573 #ifdef RTE_MALLOC_ASAN
575 #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
577 #define __rte_no_asan __attribute__((no_sanitize_address))
580 #define __rte_no_asan
588 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
593 #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
600 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
608 #define RTE_PTR_UNQUAL(X) ((void *)(uintptr_t)(X))
627 #define RTE_CAST_PTR(type, ptr) ((type)(uintptr_t)(ptr))
632 #define RTE_CAST_FIELD(var, field, type) \
633 (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
644 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
645 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
653 #define RTE_ALIGN_FLOOR(val, align) \
654 (typeof(val))((val) & (~((typeof(val))((align) - 1))))
662 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
663 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
671 #define RTE_ALIGN_CEIL(val, align) \
672 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
681 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
690 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
697 #define RTE_ALIGN_MUL_CEIL(v, mul) \
698 ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
705 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
706 (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
713 #define RTE_ALIGN_MUL_NEAR(v, mul) \
715 typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
716 typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
717 (ceil - (v)) > ((v) - floor) ? floor : ceil; \
734 return ((uintptr_t)ptr & (align - 1)) == 0;
740 #if !defined(static_assert) && !defined(__cplusplus)
741 #define static_assert _Static_assert
750 #define RTE_BUILD_BUG_ON(condition) do { static_assert(!(condition), #condition); } while (0)
755 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
758 #define RTE_CACHE_LINE_ROUNDUP(size) RTE_ALIGN_CEIL(size, RTE_CACHE_LINE_SIZE)
761 #if RTE_CACHE_LINE_SIZE == 64
762 #define RTE_CACHE_LINE_SIZE_LOG2 6
763 #elif RTE_CACHE_LINE_SIZE == 128
764 #define RTE_CACHE_LINE_SIZE_LOG2 7
766 #error "Unsupported cache line size"
770 #define RTE_CACHE_LINE_MIN_SIZE 64
773 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
776 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
778 #define _RTE_CACHE_GUARD_HELPER2(unique) \
779 alignas(RTE_CACHE_LINE_SIZE) \
780 char cache_guard_ ## unique[RTE_CACHE_LINE_SIZE * RTE_CACHE_GUARD_LINES]
781 #define _RTE_CACHE_GUARD_HELPER1(unique) _RTE_CACHE_GUARD_HELPER2(unique)
793 #define RTE_CACHE_GUARD _RTE_CACHE_GUARD_HELPER1(__LINE__)
799 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
809 #define RTE_BAD_IOVA ((rte_iova_t)-1)
813 #ifndef RTE_TOOLCHAIN_MSVC
833 #define RTE_MIN(a, b) \
835 typeof (a) _a_min = (a); \
836 typeof (b) _b_min = (b); \
837 _a_min < _b_min ? _a_min : _b_min; \
843 #define RTE_MIN3(a, b, c) \
845 typeof (a) _a_min3 = (a); \
846 typeof (b) _b_min3 = (b); \
847 typeof (c) _c_min3 = (c); \
848 _a_min3 < _b_min3 ? \
849 (_a_min3 < _c_min3 ? _a_min3 : _c_min3) : \
850 (_b_min3 < _c_min3 ? _b_min3 : _c_min3); \
860 #define RTE_MIN_T(a, b, t) \
861 ((t)(a) < (t)(b) ? (t)(a) : (t)(b))
866 #define RTE_MAX(a, b) \
868 typeof (a) _a_max = (a); \
869 typeof (b) _b_max = (b); \
870 _a_max > _b_max ? _a_max : _b_max; \
876 #define RTE_MAX3(a, b, c) \
878 typeof (a) _a_max3 = (a); \
879 typeof (b) _b_max3 = (b); \
880 typeof (c) _c_max3 = (c); \
881 _a_max3 > _b_max3 ? \
882 (_a_max3 > _c_max3 ? _a_max3 : _c_max3) : \
883 (_b_max3 > _c_max3 ? _b_max3 : _c_max3); \
893 #define RTE_MAX_T(a, b, t) \
894 ((t)(a) > (t)(b) ? (t)(a) : (t)(b))
900 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
918 #ifdef RTE_TOOLCHAIN_MSVC
919 #define container_of(ptr, type, member) \
920 ((type *)((uintptr_t)(ptr) - offsetof(type, member)))
922 #define container_of(ptr, type, member) __extension__ ({ \
923 const typeof(((type *)0)->member) *_ptr = (ptr); \
924 __rte_unused type *_target_ptr = \
926 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
932 #define RTE_SWAP(a, b) \
949 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
951 #define _RTE_STR(x) #x
953 #define RTE_STR(x) _RTE_STR(x)
956 #define _RTE_CONCAT2(a, b) a ## b
957 #define RTE_CONCAT(a, b) _RTE_CONCAT2(a, b)
964 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
965 #define RTE_FMT_HEAD(fmt, ...) fmt
966 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
969 #define RTE_LEN2MASK(ln, tp) \
970 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
973 #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)
__rte_experimental char * rte_size_to_str(char *buf, int buf_size, uint64_t count, bool use_iec, const char *unit)
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]