1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2019 Intel Corporation 3 */ 4 5 #ifndef _RTE_COMMON_H_ 6 #define _RTE_COMMON_H_ 7 8 /** 9 * @file 10 * 11 * Generic, commonly-used macro and inline function definitions 12 * for DPDK. 13 */ 14 15 #ifdef __cplusplus 16 extern "C" { 17 #endif 18 19 #include <stdint.h> 20 #include <limits.h> 21 22 #include <rte_config.h> 23 24 /* OS specific include */ 25 #include <rte_os.h> 26 27 #ifndef typeof 28 #define typeof __typeof__ 29 #endif 30 31 #ifndef __cplusplus 32 #ifndef asm 33 #define asm __asm__ 34 #endif 35 #endif 36 37 /** C extension macro for environments lacking C11 features. */ 38 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L 39 #define RTE_STD_C11 __extension__ 40 #else 41 #define RTE_STD_C11 42 #endif 43 44 /* 45 * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC, 46 * while a host application (like pmdinfogen) may have another compiler. 47 * RTE_CC_IS_GNU is true if the file is compiled with GCC, 48 * no matter it is a target or host application. 49 */ 50 #define RTE_CC_IS_GNU 0 51 #if defined __clang__ 52 #define RTE_CC_CLANG 53 #elif defined __INTEL_COMPILER 54 #define RTE_CC_ICC 55 #elif defined __GNUC__ 56 #define RTE_CC_GCC 57 #undef RTE_CC_IS_GNU 58 #define RTE_CC_IS_GNU 1 59 #endif 60 #if RTE_CC_IS_GNU 61 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \ 62 __GNUC_PATCHLEVEL__) 63 #endif 64 65 /** 66 * Force alignment 67 */ 68 #define __rte_aligned(a) __attribute__((__aligned__(a))) 69 70 #ifdef RTE_ARCH_STRICT_ALIGN 71 typedef uint64_t unaligned_uint64_t __rte_aligned(1); 72 typedef uint32_t unaligned_uint32_t __rte_aligned(1); 73 typedef uint16_t unaligned_uint16_t __rte_aligned(1); 74 #else 75 typedef uint64_t unaligned_uint64_t; 76 typedef uint32_t unaligned_uint32_t; 77 typedef uint16_t unaligned_uint16_t; 78 #endif 79 80 /** 81 * Force a structure to be packed 82 */ 83 #define __rte_packed __attribute__((__packed__)) 84 85 /** 86 * Macro to mark a type that is not subject to type-based aliasing rules 87 */ 88 #define __rte_may_alias __attribute__((__may_alias__)) 89 90 /******* Macro to mark functions and fields scheduled for removal *****/ 91 #define __rte_deprecated __attribute__((__deprecated__)) 92 #define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg))) 93 94 /** 95 * Macro to mark macros and defines scheduled for removal 96 */ 97 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG) 98 #define RTE_PRAGMA(x) _Pragma(#x) 99 #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w) 100 #define RTE_DEPRECATED(x) RTE_PRAGMA_WARNING(#x is deprecated) 101 #else 102 #define RTE_DEPRECATED(x) 103 #endif 104 105 /** 106 * Mark a function or variable to a weak reference. 107 */ 108 #define __rte_weak __attribute__((__weak__)) 109 110 /** 111 * Force symbol to be generated even if it appears to be unused. 112 */ 113 #define __rte_used __attribute__((used)) 114 115 /*********** Macros to eliminate unused variable warnings ********/ 116 117 /** 118 * short definition to mark a function parameter unused 119 */ 120 #define __rte_unused __attribute__((__unused__)) 121 122 /** 123 * Mark pointer as restricted with regard to pointer aliasing. 124 */ 125 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L 126 #define __rte_restrict __restrict 127 #else 128 #define __rte_restrict restrict 129 #endif 130 131 /** 132 * definition to mark a variable or function parameter as used so 133 * as to avoid a compiler warning 134 */ 135 #define RTE_SET_USED(x) (void)(x) 136 137 /** 138 * Check format string and its arguments at compile-time. 139 * 140 * GCC on Windows assumes MS-specific format string by default, 141 * even if the underlying stdio implementation is ANSI-compliant, 142 * so this must be overridden. 143 */ 144 #if RTE_CC_IS_GNU 145 #define __rte_format_printf(format_index, first_arg) \ 146 __attribute__((format(gnu_printf, format_index, first_arg))) 147 #else 148 #define __rte_format_printf(format_index, first_arg) \ 149 __attribute__((format(printf, format_index, first_arg))) 150 #endif 151 152 /** 153 * Tells compiler that the function returns a value that points to 154 * memory, where the size is given by the one or two arguments. 155 * Used by compiler to validate object size. 156 */ 157 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG) 158 #define __rte_alloc_size(...) \ 159 __attribute__((alloc_size(__VA_ARGS__))) 160 #else 161 #define __rte_alloc_size(...) 162 #endif 163 164 #define RTE_PRIORITY_LOG 101 165 #define RTE_PRIORITY_BUS 110 166 #define RTE_PRIORITY_CLASS 120 167 #define RTE_PRIORITY_LAST 65535 168 169 #define RTE_PRIO(prio) \ 170 RTE_PRIORITY_ ## prio 171 172 /** 173 * Run function before main() with high priority. 174 * 175 * @param func 176 * Constructor function. 177 * @param prio 178 * Priority number must be above 100. 179 * Lowest number is the first to run. 180 */ 181 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */ 182 #define RTE_INIT_PRIO(func, prio) \ 183 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void) 184 #endif 185 186 /** 187 * Run function before main() with low priority. 188 * 189 * The constructor will be run after prioritized constructors. 190 * 191 * @param func 192 * Constructor function. 193 */ 194 #define RTE_INIT(func) \ 195 RTE_INIT_PRIO(func, LAST) 196 197 /** 198 * Run after main() with low priority. 199 * 200 * @param func 201 * Destructor function name. 202 * @param prio 203 * Priority number must be above 100. 204 * Lowest number is the last to run. 205 */ 206 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */ 207 #define RTE_FINI_PRIO(func, prio) \ 208 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void) 209 #endif 210 211 /** 212 * Run after main() with high priority. 213 * 214 * The destructor will be run *before* prioritized destructors. 215 * 216 * @param func 217 * Destructor function name. 218 */ 219 #define RTE_FINI(func) \ 220 RTE_FINI_PRIO(func, LAST) 221 222 /** 223 * Hint never returning function 224 */ 225 #define __rte_noreturn __attribute__((noreturn)) 226 227 /** 228 * Issue a warning in case the function's return value is ignored. 229 * 230 * The use of this attribute should be restricted to cases where 231 * ignoring the marked function's return value is almost always a 232 * bug. With GCC, some effort is required to make clear that ignoring 233 * the return value is intentional. The usual void-casting method to 234 * mark something unused as used does not suppress the warning with 235 * this compiler. 236 * 237 * @code{.c} 238 * __rte_warn_unused_result int foo(); 239 * 240 * void ignore_foo_result(void) { 241 * foo(); // generates a warning with all compilers 242 * 243 * (void)foo(); // still generates the warning with GCC (but not clang) 244 * 245 * int unused __rte_unused; 246 * unused = foo(); // does the trick with all compilers 247 * } 248 * @endcode 249 */ 250 #define __rte_warn_unused_result __attribute__((warn_unused_result)) 251 252 /** 253 * Force a function to be inlined 254 */ 255 #define __rte_always_inline inline __attribute__((always_inline)) 256 257 /** 258 * Force a function to be noinlined 259 */ 260 #define __rte_noinline __attribute__((noinline)) 261 262 /** 263 * Hint function in the hot path 264 */ 265 #define __rte_hot __attribute__((hot)) 266 267 /** 268 * Hint function in the cold path 269 */ 270 #define __rte_cold __attribute__((cold)) 271 272 /** 273 * Disable AddressSanitizer on some code 274 */ 275 #ifdef RTE_MALLOC_ASAN 276 #ifdef RTE_CC_CLANG 277 #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress"))) 278 #else 279 #define __rte_no_asan __attribute__((no_sanitize_address)) 280 #endif 281 #else /* ! RTE_MALLOC_ASAN */ 282 #define __rte_no_asan 283 #endif 284 285 /*********** Macros for pointer arithmetic ********/ 286 287 /** 288 * add a byte-value offset to a pointer 289 */ 290 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x))) 291 292 /** 293 * subtract a byte-value offset from a pointer 294 */ 295 #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x))) 296 297 /** 298 * get the difference between two pointer values, i.e. how far apart 299 * in bytes are the locations they point two. It is assumed that 300 * ptr1 is greater than ptr2. 301 */ 302 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2)) 303 304 /** 305 * Workaround to cast a const field of a structure to non-const type. 306 */ 307 #define RTE_CAST_FIELD(var, field, type) \ 308 (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field))) 309 310 /*********** Macros/static functions for doing alignment ********/ 311 312 313 /** 314 * Macro to align a pointer to a given power-of-two. The resultant 315 * pointer will be a pointer of the same type as the first parameter, and 316 * point to an address no higher than the first parameter. Second parameter 317 * must be a power-of-two value. 318 */ 319 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \ 320 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align)) 321 322 /** 323 * Macro to align a value to a given power-of-two. The resultant value 324 * will be of the same type as the first parameter, and will be no 325 * bigger than the first parameter. Second parameter must be a 326 * power-of-two value. 327 */ 328 #define RTE_ALIGN_FLOOR(val, align) \ 329 (typeof(val))((val) & (~((typeof(val))((align) - 1)))) 330 331 /** 332 * Macro to align a pointer to a given power-of-two. The resultant 333 * pointer will be a pointer of the same type as the first parameter, and 334 * point to an address no lower than the first parameter. Second parameter 335 * must be a power-of-two value. 336 */ 337 #define RTE_PTR_ALIGN_CEIL(ptr, align) \ 338 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align) 339 340 /** 341 * Macro to align a value to a given power-of-two. The resultant value 342 * will be of the same type as the first parameter, and will be no lower 343 * than the first parameter. Second parameter must be a power-of-two 344 * value. 345 */ 346 #define RTE_ALIGN_CEIL(val, align) \ 347 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align) 348 349 /** 350 * Macro to align a pointer to a given power-of-two. The resultant 351 * pointer will be a pointer of the same type as the first parameter, and 352 * point to an address no lower than the first parameter. Second parameter 353 * must be a power-of-two value. 354 * This function is the same as RTE_PTR_ALIGN_CEIL 355 */ 356 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align) 357 358 /** 359 * Macro to align a value to a given power-of-two. The resultant 360 * value will be of the same type as the first parameter, and 361 * will be no lower than the first parameter. Second parameter 362 * must be a power-of-two value. 363 * This function is the same as RTE_ALIGN_CEIL 364 */ 365 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align) 366 367 /** 368 * Macro to align a value to the multiple of given value. The resultant 369 * value will be of the same type as the first parameter and will be no lower 370 * than the first parameter. 371 */ 372 #define RTE_ALIGN_MUL_CEIL(v, mul) \ 373 ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul)) 374 375 /** 376 * Macro to align a value to the multiple of given value. The resultant 377 * value will be of the same type as the first parameter and will be no higher 378 * than the first parameter. 379 */ 380 #define RTE_ALIGN_MUL_FLOOR(v, mul) \ 381 (((v) / ((typeof(v))(mul))) * (typeof(v))(mul)) 382 383 /** 384 * Macro to align value to the nearest multiple of the given value. 385 * The resultant value might be greater than or less than the first parameter 386 * whichever difference is the lowest. 387 */ 388 #define RTE_ALIGN_MUL_NEAR(v, mul) \ 389 ({ \ 390 typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \ 391 typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \ 392 (ceil - (v)) > ((v) - floor) ? floor : ceil; \ 393 }) 394 395 /** 396 * Checks if a pointer is aligned to a given power-of-two value 397 * 398 * @param ptr 399 * The pointer whose alignment is to be checked 400 * @param align 401 * The power-of-two value to which the ptr should be aligned 402 * 403 * @return 404 * True(1) where the pointer is correctly aligned, false(0) otherwise 405 */ 406 static inline int 407 rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align) 408 { 409 return ((uintptr_t)ptr & (align - 1)) == 0; 410 } 411 412 /*********** Macros for compile type checks ********/ 413 414 /** 415 * Triggers an error at compilation time if the condition is true. 416 */ 417 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 418 419 /*********** Cache line related macros ********/ 420 421 /** Cache line mask. */ 422 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1) 423 424 /** Return the first cache-aligned value greater or equal to size. */ 425 #define RTE_CACHE_LINE_ROUNDUP(size) RTE_ALIGN_CEIL(size, RTE_CACHE_LINE_SIZE) 426 427 /** Cache line size in terms of log2 */ 428 #if RTE_CACHE_LINE_SIZE == 64 429 #define RTE_CACHE_LINE_SIZE_LOG2 6 430 #elif RTE_CACHE_LINE_SIZE == 128 431 #define RTE_CACHE_LINE_SIZE_LOG2 7 432 #else 433 #error "Unsupported cache line size" 434 #endif 435 436 /** Minimum Cache line size. */ 437 #define RTE_CACHE_LINE_MIN_SIZE 64 438 439 /** Force alignment to cache line. */ 440 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE) 441 442 /** Force minimum cache line alignment. */ 443 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE) 444 445 /*********** PA/IOVA type definitions ********/ 446 447 /** Physical address */ 448 typedef uint64_t phys_addr_t; 449 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1) 450 451 /** 452 * IO virtual address type. 453 * When the physical addressing mode (IOVA as PA) is in use, 454 * the translation from an IO virtual address (IOVA) to a physical address 455 * is a direct mapping, i.e. the same value. 456 * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation. 457 */ 458 typedef uint64_t rte_iova_t; 459 #define RTE_BAD_IOVA ((rte_iova_t)-1) 460 461 /*********** Structure alignment markers ********/ 462 463 /** Generic marker for any place in a structure. */ 464 __extension__ typedef void *RTE_MARKER[0]; 465 /** Marker for 1B alignment in a structure. */ 466 __extension__ typedef uint8_t RTE_MARKER8[0]; 467 /** Marker for 2B alignment in a structure. */ 468 __extension__ typedef uint16_t RTE_MARKER16[0]; 469 /** Marker for 4B alignment in a structure. */ 470 __extension__ typedef uint32_t RTE_MARKER32[0]; 471 /** Marker for 8B alignment in a structure. */ 472 __extension__ typedef uint64_t RTE_MARKER64[0]; 473 474 /** 475 * Combines 32b inputs most significant set bits into the least 476 * significant bits to construct a value with the same MSBs as x 477 * but all 1's under it. 478 * 479 * @param x 480 * The integer whose MSBs need to be combined with its LSBs 481 * @return 482 * The combined value. 483 */ 484 static inline uint32_t 485 rte_combine32ms1b(uint32_t x) 486 { 487 x |= x >> 1; 488 x |= x >> 2; 489 x |= x >> 4; 490 x |= x >> 8; 491 x |= x >> 16; 492 493 return x; 494 } 495 496 /** 497 * Combines 64b inputs most significant set bits into the least 498 * significant bits to construct a value with the same MSBs as x 499 * but all 1's under it. 500 * 501 * @param v 502 * The integer whose MSBs need to be combined with its LSBs 503 * @return 504 * The combined value. 505 */ 506 static inline uint64_t 507 rte_combine64ms1b(uint64_t v) 508 { 509 v |= v >> 1; 510 v |= v >> 2; 511 v |= v >> 4; 512 v |= v >> 8; 513 v |= v >> 16; 514 v |= v >> 32; 515 516 return v; 517 } 518 519 /*********** Macros to work with powers of 2 ********/ 520 521 /** 522 * Macro to return 1 if n is a power of 2, 0 otherwise 523 */ 524 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n))) 525 526 /** 527 * Returns true if n is a power of 2 528 * @param n 529 * Number to check 530 * @return 1 if true, 0 otherwise 531 */ 532 static inline int 533 rte_is_power_of_2(uint32_t n) 534 { 535 return n && !(n & (n - 1)); 536 } 537 538 /** 539 * Aligns input parameter to the next power of 2 540 * 541 * @param x 542 * The integer value to align 543 * 544 * @return 545 * Input parameter aligned to the next power of 2 546 */ 547 static inline uint32_t 548 rte_align32pow2(uint32_t x) 549 { 550 x--; 551 x = rte_combine32ms1b(x); 552 553 return x + 1; 554 } 555 556 /** 557 * Aligns input parameter to the previous power of 2 558 * 559 * @param x 560 * The integer value to align 561 * 562 * @return 563 * Input parameter aligned to the previous power of 2 564 */ 565 static inline uint32_t 566 rte_align32prevpow2(uint32_t x) 567 { 568 x = rte_combine32ms1b(x); 569 570 return x - (x >> 1); 571 } 572 573 /** 574 * Aligns 64b input parameter to the next power of 2 575 * 576 * @param v 577 * The 64b value to align 578 * 579 * @return 580 * Input parameter aligned to the next power of 2 581 */ 582 static inline uint64_t 583 rte_align64pow2(uint64_t v) 584 { 585 v--; 586 v = rte_combine64ms1b(v); 587 588 return v + 1; 589 } 590 591 /** 592 * Aligns 64b input parameter to the previous power of 2 593 * 594 * @param v 595 * The 64b value to align 596 * 597 * @return 598 * Input parameter aligned to the previous power of 2 599 */ 600 static inline uint64_t 601 rte_align64prevpow2(uint64_t v) 602 { 603 v = rte_combine64ms1b(v); 604 605 return v - (v >> 1); 606 } 607 608 /*********** Macros for calculating min and max **********/ 609 610 /** 611 * Macro to return the minimum of two numbers 612 */ 613 #define RTE_MIN(a, b) \ 614 __extension__ ({ \ 615 typeof (a) _a = (a); \ 616 typeof (b) _b = (b); \ 617 _a < _b ? _a : _b; \ 618 }) 619 620 /** 621 * Macro to return the maximum of two numbers 622 */ 623 #define RTE_MAX(a, b) \ 624 __extension__ ({ \ 625 typeof (a) _a = (a); \ 626 typeof (b) _b = (b); \ 627 _a > _b ? _a : _b; \ 628 }) 629 630 /*********** Other general functions / macros ********/ 631 632 /** 633 * Searches the input parameter for the least significant set bit 634 * (starting from zero). 635 * If a least significant 1 bit is found, its bit index is returned. 636 * If the content of the input parameter is zero, then the content of the return 637 * value is undefined. 638 * @param v 639 * input parameter, should not be zero. 640 * @return 641 * least significant set bit in the input parameter. 642 */ 643 static inline uint32_t 644 rte_bsf32(uint32_t v) 645 { 646 return (uint32_t)__builtin_ctz(v); 647 } 648 649 /** 650 * Searches the input parameter for the least significant set bit 651 * (starting from zero). Safe version (checks for input parameter being zero). 652 * 653 * @warning ``pos`` must be a valid pointer. It is not checked! 654 * 655 * @param v 656 * The input parameter. 657 * @param pos 658 * If ``v`` was not 0, this value will contain position of least significant 659 * bit within the input parameter. 660 * @return 661 * Returns 0 if ``v`` was 0, otherwise returns 1. 662 */ 663 static inline int 664 rte_bsf32_safe(uint32_t v, uint32_t *pos) 665 { 666 if (v == 0) 667 return 0; 668 669 *pos = rte_bsf32(v); 670 return 1; 671 } 672 673 /** 674 * Return the rounded-up log2 of a integer. 675 * 676 * @note Contrary to the logarithm mathematical operation, 677 * rte_log2_u32(0) == 0 and not -inf. 678 * 679 * @param v 680 * The input parameter. 681 * @return 682 * The rounded-up log2 of the input, or 0 if the input is 0. 683 */ 684 static inline uint32_t 685 rte_log2_u32(uint32_t v) 686 { 687 if (v == 0) 688 return 0; 689 v = rte_align32pow2(v); 690 return rte_bsf32(v); 691 } 692 693 694 /** 695 * Return the last (most-significant) bit set. 696 * 697 * @note The last (most significant) bit is at position 32. 698 * @note rte_fls_u32(0) = 0, rte_fls_u32(1) = 1, rte_fls_u32(0x80000000) = 32 699 * 700 * @param x 701 * The input parameter. 702 * @return 703 * The last (most-significant) bit set, or 0 if the input is 0. 704 */ 705 static inline int 706 rte_fls_u32(uint32_t x) 707 { 708 return (x == 0) ? 0 : 32 - __builtin_clz(x); 709 } 710 711 /** 712 * Searches the input parameter for the least significant set bit 713 * (starting from zero). 714 * If a least significant 1 bit is found, its bit index is returned. 715 * If the content of the input parameter is zero, then the content of the return 716 * value is undefined. 717 * @param v 718 * input parameter, should not be zero. 719 * @return 720 * least significant set bit in the input parameter. 721 */ 722 static inline int 723 rte_bsf64(uint64_t v) 724 { 725 return (uint32_t)__builtin_ctzll(v); 726 } 727 728 /** 729 * Searches the input parameter for the least significant set bit 730 * (starting from zero). Safe version (checks for input parameter being zero). 731 * 732 * @warning ``pos`` must be a valid pointer. It is not checked! 733 * 734 * @param v 735 * The input parameter. 736 * @param pos 737 * If ``v`` was not 0, this value will contain position of least significant 738 * bit within the input parameter. 739 * @return 740 * Returns 0 if ``v`` was 0, otherwise returns 1. 741 */ 742 static inline int 743 rte_bsf64_safe(uint64_t v, uint32_t *pos) 744 { 745 if (v == 0) 746 return 0; 747 748 *pos = rte_bsf64(v); 749 return 1; 750 } 751 752 /** 753 * Return the last (most-significant) bit set. 754 * 755 * @note The last (most significant) bit is at position 64. 756 * @note rte_fls_u64(0) = 0, rte_fls_u64(1) = 1, 757 * rte_fls_u64(0x8000000000000000) = 64 758 * 759 * @param x 760 * The input parameter. 761 * @return 762 * The last (most-significant) bit set, or 0 if the input is 0. 763 */ 764 static inline int 765 rte_fls_u64(uint64_t x) 766 { 767 return (x == 0) ? 0 : 64 - __builtin_clzll(x); 768 } 769 770 /** 771 * Return the rounded-up log2 of a 64-bit integer. 772 * 773 * @note Contrary to the logarithm mathematical operation, 774 * rte_log2_u64(0) == 0 and not -inf. 775 * 776 * @param v 777 * The input parameter. 778 * @return 779 * The rounded-up log2 of the input, or 0 if the input is 0. 780 */ 781 static inline uint32_t 782 rte_log2_u64(uint64_t v) 783 { 784 if (v == 0) 785 return 0; 786 v = rte_align64pow2(v); 787 /* we checked for v being 0 already, so no undefined behavior */ 788 return rte_bsf64(v); 789 } 790 791 #ifndef offsetof 792 /** Return the offset of a field in a structure. */ 793 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER) 794 #endif 795 796 /** 797 * Return pointer to the wrapping struct instance. 798 * 799 * Example: 800 * 801 * struct wrapper { 802 * ... 803 * struct child c; 804 * ... 805 * }; 806 * 807 * struct child *x = obtain(...); 808 * struct wrapper *w = container_of(x, struct wrapper, c); 809 */ 810 #ifndef container_of 811 #define container_of(ptr, type, member) __extension__ ({ \ 812 const typeof(((type *)0)->member) *_ptr = (ptr); \ 813 __rte_unused type *_target_ptr = \ 814 (type *)(ptr); \ 815 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \ 816 }) 817 #endif 818 819 /** Swap two variables. */ 820 #define RTE_SWAP(a, b) \ 821 __extension__ ({ \ 822 typeof (a) _a = a; \ 823 a = b; \ 824 b = _a; \ 825 }) 826 827 /** 828 * Get the size of a field in a structure. 829 * 830 * @param type 831 * The type of the structure. 832 * @param field 833 * The field in the structure. 834 * @return 835 * The size of the field in the structure, in bytes. 836 */ 837 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field)) 838 839 #define _RTE_STR(x) #x 840 /** Take a macro value and get a string version of it */ 841 #define RTE_STR(x) _RTE_STR(x) 842 843 /** 844 * ISO C helpers to modify format strings using variadic macros. 845 * This is a replacement for the ", ## __VA_ARGS__" GNU extension. 846 * An empty %s argument is appended to avoid a dangling comma. 847 */ 848 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ "" 849 #define RTE_FMT_HEAD(fmt, ...) fmt 850 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__ 851 852 /** Mask value of type "tp" for the first "ln" bit set. */ 853 #define RTE_LEN2MASK(ln, tp) \ 854 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln)))) 855 856 /** Number of elements in the array. */ 857 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0])) 858 859 /** 860 * Converts a numeric string to the equivalent uint64_t value. 861 * As well as straight number conversion, also recognises the suffixes 862 * k, m and g for kilobytes, megabytes and gigabytes respectively. 863 * 864 * If a negative number is passed in i.e. a string with the first non-black 865 * character being "-", zero is returned. Zero is also returned in the case of 866 * an error with the strtoull call in the function. 867 * 868 * @param str 869 * String containing number to convert. 870 * @return 871 * Number. 872 */ 873 uint64_t 874 rte_str_to_size(const char *str); 875 876 /** 877 * Function to terminate the application immediately, printing an error 878 * message and returning the exit_code back to the shell. 879 * 880 * This function never returns 881 * 882 * @param exit_code 883 * The exit code to be returned by the application 884 * @param format 885 * The format string to be used for printing the message. This can include 886 * printf format characters which will be expanded using any further parameters 887 * to the function. 888 */ 889 __rte_noreturn void 890 rte_exit(int exit_code, const char *format, ...) 891 __rte_format_printf(2, 3); 892 893 #ifdef __cplusplus 894 } 895 #endif 896 897 #endif 898