25 #include <rte_config.h>
31 #define typeof __typeof__
41 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
42 #define RTE_STD_C11 __extension__
53 #define RTE_CC_IS_GNU 0
56 #elif defined __INTEL_COMPILER
58 #elif defined __GNUC__
61 #define RTE_CC_IS_GNU 1
64 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
71 #define __rte_aligned(a) __attribute__((__aligned__(a)))
73 #ifdef RTE_ARCH_STRICT_ALIGN
78 typedef uint64_t unaligned_uint64_t;
79 typedef uint32_t unaligned_uint32_t;
80 typedef uint16_t unaligned_uint16_t;
86 #define __rte_packed __attribute__((__packed__))
89 #define __rte_deprecated __attribute__((__deprecated__))
90 #define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg)))
95 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
96 #define RTE_PRAGMA(x) _Pragma(#x)
97 #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
98 #define RTE_DEPRECATED(x) RTE_PRAGMA_WARNING(#x is deprecated)
100 #define RTE_DEPRECATED(x)
106 #define __rte_weak __attribute__((__weak__))
111 #define __rte_used __attribute__((used))
118 #define __rte_unused __attribute__((__unused__))
123 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
124 #define __rte_restrict __restrict
126 #define __rte_restrict restrict
133 #define RTE_SET_USED(x) (void)(x)
143 #define __rte_format_printf(format_index, first_arg) \
144 __attribute__((format(gnu_printf, format_index, first_arg)))
146 #define __rte_format_printf(format_index, first_arg) \
147 __attribute__((format(printf, format_index, first_arg)))
155 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
156 #define __rte_alloc_size(...) \
157 __attribute__((alloc_size(__VA_ARGS__)))
159 #define __rte_alloc_size(...)
162 #define RTE_PRIORITY_LOG 101
163 #define RTE_PRIORITY_BUS 110
164 #define RTE_PRIORITY_CLASS 120
165 #define RTE_PRIORITY_LAST 65535
167 #define RTE_PRIO(prio) \
168 RTE_PRIORITY_ ## prio
179 #ifndef RTE_INIT_PRIO
180 #define RTE_INIT_PRIO(func, prio) \
181 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
192 #define RTE_INIT(func) \
193 RTE_INIT_PRIO(func, LAST)
204 #ifndef RTE_FINI_PRIO
205 #define RTE_FINI_PRIO(func, prio) \
206 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
217 #define RTE_FINI(func) \
218 RTE_FINI_PRIO(func, LAST)
223 #define __rte_noreturn __attribute__((noreturn))
228 #define __rte_always_inline inline __attribute__((always_inline))
233 #define __rte_noinline __attribute__((noinline))
238 #define __rte_hot __attribute__((hot))
243 #define __rte_cold __attribute__((cold))
250 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
255 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
262 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
267 #define RTE_CAST_FIELD(var, field, type) \
268 (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
279 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
280 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
288 #define RTE_ALIGN_FLOOR(val, align) \
289 (typeof(val))((val) & (~((typeof(val))((align) - 1))))
297 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
298 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
306 #define RTE_ALIGN_CEIL(val, align) \
307 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
316 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
325 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
332 #define RTE_ALIGN_MUL_CEIL(v, mul) \
333 ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
340 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
341 (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
348 #define RTE_ALIGN_MUL_NEAR(v, mul) \
350 typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
351 typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
352 (ceil - (v)) > ((v) - floor) ? floor : ceil; \
377 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
382 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
385 #define RTE_CACHE_LINE_ROUNDUP(size) \
386 (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / \
387 RTE_CACHE_LINE_SIZE))
390 #if RTE_CACHE_LINE_SIZE == 64
391 #define RTE_CACHE_LINE_SIZE_LOG2 6
392 #elif RTE_CACHE_LINE_SIZE == 128
393 #define RTE_CACHE_LINE_SIZE_LOG2 7
395 #error "Unsupported cache line size"
399 #define RTE_CACHE_LINE_MIN_SIZE 64
402 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
405 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
411 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
421 #define RTE_BAD_IOVA ((rte_iova_t)-1)
446 static inline uint32_t
468 static inline uint64_t
486 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
497 return n && !(n & (n - 1));
509 static inline uint32_t
527 static inline uint32_t
544 static inline uint64_t
562 static inline uint64_t
575 #define RTE_MIN(a, b) \
577 typeof (a) _a = (a); \
578 typeof (b) _b = (b); \
585 #define RTE_MAX(a, b) \
587 typeof (a) _a = (a); \
588 typeof (b) _b = (b); \
605 static inline uint32_t
608 return (uint32_t)__builtin_ctz(v);
646 static inline uint32_t
670 return (x == 0) ? 0 : 32 - __builtin_clz(x);
687 return (uint32_t)__builtin_ctzll(v);
729 return (x == 0) ? 0 : 64 - __builtin_clzll(x);
743 static inline uint32_t
755 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
773 #define container_of(ptr, type, member) __extension__ ({ \
774 const typeof(((type *)0)->member) *_ptr = (ptr); \
775 __rte_unused type *_target_ptr = \
777 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
782 #define RTE_SWAP(a, b) \
799 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
801 #define _RTE_STR(x) #x
803 #define RTE_STR(x) _RTE_STR(x)
810 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
811 #define RTE_FMT_HEAD(fmt, ...) fmt
812 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
815 #define RTE_LEN2MASK(ln, tp) \
816 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
819 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
835 static inline uint64_t
839 unsigned long long size;
841 while (isspace((
int)*str))
847 size = strtoull(str, &endptr, 0);
855 case 'G':
case 'g': size *= 1024;
856 case 'M':
case 'm': size *= 1024;
857 case 'K':
case 'k': size *= 1024;
static int rte_is_aligned(void *ptr, unsigned align)
__extension__ typedef uint64_t RTE_MARKER64[0]
static uint32_t rte_log2_u32(uint32_t v)
static int rte_fls_u32(uint32_t x)
static int rte_bsf32_safe(uint32_t v, uint32_t *pos)
__extension__ typedef uint8_t RTE_MARKER8[0]
static int rte_bsf64(uint64_t v)
__extension__ typedef uint32_t RTE_MARKER32[0]
__extension__ typedef uint16_t RTE_MARKER16[0]
#define __rte_format_printf(format_index, first_arg)
static uint32_t rte_align32pow2(uint32_t x)
static uint32_t rte_align32prevpow2(uint32_t x)
static int rte_fls_u64(uint64_t x)
static uint64_t rte_align64pow2(uint64_t v)
static uint32_t rte_bsf32(uint32_t v)
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
static uint64_t rte_align64prevpow2(uint64_t v)
__rte_noreturn void rte_exit(int exit_code, const char *format,...) __rte_format_printf(2
#define RTE_PTR_ALIGN(ptr, align)
static int rte_is_power_of_2(uint32_t n)
static uint64_t rte_combine64ms1b(uint64_t v)
static uint32_t rte_combine32ms1b(uint32_t x)
static uint32_t rte_log2_u64(uint64_t v)
static uint64_t rte_str_to_size(const char *str)
__extension__ typedef void * RTE_MARKER[0]