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