34 #ifndef _RTE_COMMON_H_
35 #define _RTE_COMMON_H_
54 #include <rte_config.h>
57 #define typeof __typeof__
65 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
66 #define RTE_STD_C11 __extension__
72 #ifdef RTE_TOOLCHAIN_GCC
73 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
77 #ifdef RTE_ARCH_STRICT_ALIGN
79 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
80 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
83 typedef uint32_t unaligned_uint32_t;
84 typedef uint16_t unaligned_uint16_t;
90 #define __rte_aligned(a) __attribute__((__aligned__(a)))
95 #define __rte_packed __attribute__((__packed__))
98 #define __rte_deprecated __attribute__((__deprecated__))
105 #define __rte_unused __attribute__((__unused__))
111 #define RTE_SET_USED(x) (void)(x)
122 #if defined(__x86_64__) || defined(__i386__)
123 #define RTE_INIT_PRIO(func, prio) \
125 __attribute__((constructor(prio), used)) \
126 __attribute__((target ("sse2"))) \
127 __attribute__((target ("no-sse3"))) \
128 __attribute__((target ("no-sse4"))) \
131 #define RTE_INIT_PRIO(func, prio) \
133 __attribute__((constructor(prio), used)) \
144 #define RTE_INIT(func) \
145 RTE_INIT_PRIO(func, 65535)
150 #define __rte_always_inline inline __attribute__((always_inline))
155 #define __rte_noinline __attribute__((noinline))
162 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
167 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
174 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
185 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
186 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
194 #define RTE_ALIGN_FLOOR(val, align) \
195 (typeof(val))((val) & (~((typeof(val))((align) - 1))))
203 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
204 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
212 #define RTE_ALIGN_CEIL(val, align) \
213 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
222 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
231 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
256 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
258 extern int RTE_BUILD_BUG_ON_detected_error;
259 #define RTE_BUILD_BUG_ON(condition) do { \
260 ((void)sizeof(char[1 - 2*!!(condition)])); \
262 RTE_BUILD_BUG_ON_detected_error = 1; \
277 return n && !(n & (n - 1));
289 static inline uint32_t
311 static inline uint64_t
330 #define RTE_MIN(a, b) \
332 typeof (a) _a = (a); \
333 typeof (b) _b = (b); \
340 #define RTE_MAX(a, b) \
342 typeof (a) _a = (a); \
343 typeof (b) _b = (b); \
360 static inline uint32_t
363 return __builtin_ctz(v);
374 static inline uint32_t
385 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
403 #define container_of(ptr, type, member) __extension__ ({ \
404 const typeof(((type *)0)->member) *_ptr = (ptr); \
405 __attribute__((unused)) type *_target_ptr = \
407 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
411 #define _RTE_STR(x) #x
413 #define RTE_STR(x) _RTE_STR(x)
420 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
421 #define RTE_FMT_HEAD(fmt, ...) fmt
422 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
425 #define RTE_LEN2MASK(ln, tp) \
426 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
429 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
445 static inline uint64_t
449 unsigned long long size;
451 while (isspace((
int)*str))
457 size = strtoull(str, &endptr, 0);
465 case 'G':
case 'g': size *= 1024;
466 case 'M':
case 'm': size *= 1024;
467 case 'K':
case 'k': size *= 1024;
488 rte_exit(
int exit_code,
const char *format, ...)
489 __attribute__((noreturn))
490 __attribute__((format(printf, 2, 3)));
static int rte_is_aligned(void *ptr, unsigned align)
static uint64_t rte_align64pow2(uint64_t v)
static uint32_t rte_log2_u32(uint32_t v)
static uint32_t rte_bsf32(uint32_t v)
static uint32_t rte_align32pow2(uint32_t x)
uint64_t unaligned_uint64_t
#define RTE_PTR_ALIGN(ptr, align)
static int rte_is_power_of_2(uint32_t n)
void rte_exit(int exit_code, const char *format,...)
static uint64_t rte_str_to_size(const char *str)