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 <assert.h> 20 #include <limits.h> 21 #include <stdint.h> 22 #include <stdalign.h> 23 24 #include <rte_config.h> 25 26 /* OS specific include */ 27 #include <rte_os.h> 28 29 #ifndef RTE_TOOLCHAIN_MSVC 30 #ifndef typeof 31 #define typeof __typeof__ 32 #endif 33 #endif 34 35 #ifndef __cplusplus 36 #ifndef asm 37 #define asm __asm__ 38 #endif 39 #endif 40 41 #ifdef RTE_TOOLCHAIN_MSVC 42 #ifdef __cplusplus 43 #define __extension__ 44 #endif 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 type alignment 70 * 71 * This macro should be used when alignment of a struct or union type 72 * is required. For toolchain compatibility it should appear between 73 * the {struct,union} keyword and tag. e.g. 74 * 75 * struct __rte_aligned(8) tag { ... }; 76 * 77 * If alignment of an object/variable is required then this macro should 78 * not be used, instead prefer C11 alignas(a). 79 */ 80 #ifdef RTE_TOOLCHAIN_MSVC 81 #define __rte_aligned(a) __declspec(align(a)) 82 #else 83 #define __rte_aligned(a) __attribute__((__aligned__(a))) 84 #endif 85 86 #ifdef RTE_ARCH_STRICT_ALIGN 87 typedef uint64_t unaligned_uint64_t __rte_aligned(1); 88 typedef uint32_t unaligned_uint32_t __rte_aligned(1); 89 typedef uint16_t unaligned_uint16_t __rte_aligned(1); 90 #else 91 typedef uint64_t unaligned_uint64_t; 92 typedef uint32_t unaligned_uint32_t; 93 typedef uint16_t unaligned_uint16_t; 94 #endif 95 96 /** 97 * Force a structure to be packed 98 */ 99 #ifdef RTE_TOOLCHAIN_MSVC 100 #define __rte_packed 101 #else 102 #define __rte_packed __attribute__((__packed__)) 103 #endif 104 105 /** 106 * Macro to mark a type that is not subject to type-based aliasing rules 107 */ 108 #ifdef RTE_TOOLCHAIN_MSVC 109 #define __rte_may_alias 110 #else 111 #define __rte_may_alias __attribute__((__may_alias__)) 112 #endif 113 114 /******* Macro to mark functions and fields scheduled for removal *****/ 115 #ifdef RTE_TOOLCHAIN_MSVC 116 #define __rte_deprecated 117 #define __rte_deprecated_msg(msg) 118 #else 119 #define __rte_deprecated __attribute__((__deprecated__)) 120 #define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg))) 121 #endif 122 123 /** 124 * Macro to mark macros and defines scheduled for removal 125 */ 126 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG) 127 #define RTE_PRAGMA(x) _Pragma(#x) 128 #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w) 129 #define RTE_DEPRECATED(x) RTE_PRAGMA_WARNING(#x is deprecated) 130 #else 131 #define RTE_DEPRECATED(x) 132 #endif 133 134 /** 135 * Mark a function or variable to a weak reference. 136 */ 137 #define __rte_weak __attribute__((__weak__)) 138 139 /** 140 * Force symbol to be generated even if it appears to be unused. 141 */ 142 #ifdef RTE_TOOLCHAIN_MSVC 143 #define __rte_used 144 #else 145 #define __rte_used __attribute__((used)) 146 #endif 147 148 /*********** Macros to eliminate unused variable warnings ********/ 149 150 /** 151 * short definition to mark a function parameter unused 152 */ 153 #ifdef RTE_TOOLCHAIN_MSVC 154 #define __rte_unused 155 #else 156 #define __rte_unused __attribute__((__unused__)) 157 #endif 158 159 /** 160 * Mark pointer as restricted with regard to pointer aliasing. 161 */ 162 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L 163 #define __rte_restrict __restrict 164 #else 165 #define __rte_restrict restrict 166 #endif 167 168 /** 169 * definition to mark a variable or function parameter as used so 170 * as to avoid a compiler warning 171 */ 172 #define RTE_SET_USED(x) (void)(x) 173 174 /** 175 * Check format string and its arguments at compile-time. 176 * 177 * GCC on Windows assumes MS-specific format string by default, 178 * even if the underlying stdio implementation is ANSI-compliant, 179 * so this must be overridden. 180 */ 181 #ifdef RTE_TOOLCHAIN_MSVC 182 #define __rte_format_printf(format_index, first_arg) 183 #else 184 #if RTE_CC_IS_GNU 185 #define __rte_format_printf(format_index, first_arg) \ 186 __attribute__((format(gnu_printf, format_index, first_arg))) 187 #else 188 #define __rte_format_printf(format_index, first_arg) \ 189 __attribute__((format(printf, format_index, first_arg))) 190 #endif 191 #endif 192 193 /** 194 * Specify data or function section/segment. 195 */ 196 #ifdef RTE_TOOLCHAIN_MSVC 197 #define __rte_section(name) \ 198 __pragma(data_seg(name)) __declspec(allocate(name)) 199 #else 200 #define __rte_section(name) \ 201 __attribute__((section(name))) 202 #endif 203 204 /** 205 * Tells compiler that the function returns a value that points to 206 * memory, where the size is given by the one or two arguments. 207 * Used by compiler to validate object size. 208 */ 209 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG) 210 #define __rte_alloc_size(...) \ 211 __attribute__((alloc_size(__VA_ARGS__))) 212 #else 213 #define __rte_alloc_size(...) 214 #endif 215 216 #define RTE_PRIORITY_LOG 101 217 #define RTE_PRIORITY_BUS 110 218 #define RTE_PRIORITY_CLASS 120 219 #define RTE_PRIORITY_LAST 65535 220 221 #define RTE_PRIO(prio) \ 222 RTE_PRIORITY_ ## prio 223 224 /** 225 * Run function before main() with high priority. 226 * 227 * @param func 228 * Constructor function. 229 * @param prio 230 * Priority number must be above 100. 231 * Lowest number is the first to run. 232 */ 233 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */ 234 #ifndef RTE_TOOLCHAIN_MSVC 235 #define RTE_INIT_PRIO(func, prio) \ 236 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void) 237 #else 238 /* definition from the Microsoft CRT */ 239 typedef int(__cdecl *_PIFV)(void); 240 241 #define CTOR_SECTION_LOG ".CRT$XIB" 242 #define CTOR_SECTION_BUS ".CRT$XIC" 243 #define CTOR_SECTION_CLASS ".CRT$XID" 244 #define CTOR_SECTION_LAST ".CRT$XIY" 245 246 #define CTOR_PRIORITY_TO_SECTION(priority) CTOR_SECTION_ ## priority 247 248 #define RTE_INIT_PRIO(name, priority) \ 249 static void name(void); \ 250 static int __cdecl name ## _thunk(void) { name(); return 0; } \ 251 __pragma(const_seg(CTOR_PRIORITY_TO_SECTION(priority))) \ 252 __declspec(allocate(CTOR_PRIORITY_TO_SECTION(priority))) \ 253 _PIFV name ## _pointer = &name ## _thunk; \ 254 __pragma(const_seg()) \ 255 static void name(void) 256 #endif 257 #endif 258 259 /** 260 * Run function before main() with low priority. 261 * 262 * The constructor will be run after prioritized constructors. 263 * 264 * @param func 265 * Constructor function. 266 */ 267 #define RTE_INIT(func) \ 268 RTE_INIT_PRIO(func, LAST) 269 270 /** 271 * Run after main() with low priority. 272 * 273 * @param func 274 * Destructor function name. 275 * @param prio 276 * Priority number must be above 100. 277 * Lowest number is the last to run. 278 */ 279 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */ 280 #ifndef RTE_TOOLCHAIN_MSVC 281 #define RTE_FINI_PRIO(func, prio) \ 282 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void) 283 #else 284 #define DTOR_SECTION_LOG "mydtor$B" 285 #define DTOR_SECTION_BUS "mydtor$C" 286 #define DTOR_SECTION_CLASS "mydtor$D" 287 #define DTOR_SECTION_LAST "mydtor$Y" 288 289 #define DTOR_PRIORITY_TO_SECTION(priority) DTOR_SECTION_ ## priority 290 291 #define RTE_FINI_PRIO(name, priority) \ 292 static void name(void); \ 293 __pragma(const_seg(DTOR_PRIORITY_TO_SECTION(priority))) \ 294 __declspec(allocate(DTOR_PRIORITY_TO_SECTION(priority))) name ## _pointer = &name; \ 295 __pragma(const_seg()) \ 296 static void name(void) 297 #endif 298 #endif 299 300 /** 301 * Run after main() with high priority. 302 * 303 * The destructor will be run *before* prioritized destructors. 304 * 305 * @param func 306 * Destructor function name. 307 */ 308 #define RTE_FINI(func) \ 309 RTE_FINI_PRIO(func, LAST) 310 311 /** 312 * Hint never returning function 313 */ 314 #ifdef RTE_TOOLCHAIN_MSVC 315 #define __rte_noreturn 316 #else 317 #define __rte_noreturn __attribute__((noreturn)) 318 #endif 319 320 /** 321 * Issue a warning in case the function's return value is ignored. 322 * 323 * The use of this attribute should be restricted to cases where 324 * ignoring the marked function's return value is almost always a 325 * bug. With GCC, some effort is required to make clear that ignoring 326 * the return value is intentional. The usual void-casting method to 327 * mark something unused as used does not suppress the warning with 328 * this compiler. 329 * 330 * @code{.c} 331 * __rte_warn_unused_result int foo(); 332 * 333 * void ignore_foo_result(void) { 334 * foo(); // generates a warning with all compilers 335 * 336 * (void)foo(); // still generates the warning with GCC (but not clang) 337 * 338 * int unused __rte_unused; 339 * unused = foo(); // does the trick with all compilers 340 * } 341 * @endcode 342 */ 343 #ifdef RTE_TOOLCHAIN_MSVC 344 #define __rte_warn_unused_result 345 #else 346 #define __rte_warn_unused_result __attribute__((warn_unused_result)) 347 #endif 348 349 /** 350 * Force a function to be inlined 351 */ 352 #ifdef RTE_TOOLCHAIN_MSVC 353 #define __rte_always_inline 354 #else 355 #define __rte_always_inline inline __attribute__((always_inline)) 356 #endif 357 358 /** 359 * Force a function to be noinlined 360 */ 361 #define __rte_noinline __attribute__((noinline)) 362 363 /** 364 * Hint function in the hot path 365 */ 366 #define __rte_hot __attribute__((hot)) 367 368 /** 369 * Hint function in the cold path 370 */ 371 #ifdef RTE_TOOLCHAIN_MSVC 372 #define __rte_cold 373 #else 374 #define __rte_cold __attribute__((cold)) 375 #endif 376 377 /** 378 * Disable AddressSanitizer on some code 379 */ 380 #ifdef RTE_MALLOC_ASAN 381 #ifdef RTE_CC_CLANG 382 #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress"))) 383 #else 384 #define __rte_no_asan __attribute__((no_sanitize_address)) 385 #endif 386 #else /* ! RTE_MALLOC_ASAN */ 387 #define __rte_no_asan 388 #endif 389 390 /*********** Macros for pointer arithmetic ********/ 391 392 /** 393 * add a byte-value offset to a pointer 394 */ 395 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x))) 396 397 /** 398 * subtract a byte-value offset from a pointer 399 */ 400 #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x))) 401 402 /** 403 * get the difference between two pointer values, i.e. how far apart 404 * in bytes are the locations they point two. It is assumed that 405 * ptr1 is greater than ptr2. 406 */ 407 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2)) 408 409 /** 410 * Workaround to cast a const field of a structure to non-const type. 411 */ 412 #define RTE_CAST_FIELD(var, field, type) \ 413 (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field))) 414 415 /*********** Macros/static functions for doing alignment ********/ 416 417 418 /** 419 * Macro to align a pointer to a given power-of-two. The resultant 420 * pointer will be a pointer of the same type as the first parameter, and 421 * point to an address no higher than the first parameter. Second parameter 422 * must be a power-of-two value. 423 */ 424 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \ 425 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align)) 426 427 /** 428 * Macro to align a value to a given power-of-two. The resultant value 429 * will be of the same type as the first parameter, and will be no 430 * bigger than the first parameter. Second parameter must be a 431 * power-of-two value. 432 */ 433 #define RTE_ALIGN_FLOOR(val, align) \ 434 (typeof(val))((val) & (~((typeof(val))((align) - 1)))) 435 436 /** 437 * Macro to align a pointer to a given power-of-two. The resultant 438 * pointer will be a pointer of the same type as the first parameter, and 439 * point to an address no lower than the first parameter. Second parameter 440 * must be a power-of-two value. 441 */ 442 #define RTE_PTR_ALIGN_CEIL(ptr, align) \ 443 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align) 444 445 /** 446 * Macro to align a value to a given power-of-two. The resultant value 447 * will be of the same type as the first parameter, and will be no lower 448 * than the first parameter. Second parameter must be a power-of-two 449 * value. 450 */ 451 #define RTE_ALIGN_CEIL(val, align) \ 452 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align) 453 454 /** 455 * Macro to align a pointer to a given power-of-two. The resultant 456 * pointer will be a pointer of the same type as the first parameter, and 457 * point to an address no lower than the first parameter. Second parameter 458 * must be a power-of-two value. 459 * This function is the same as RTE_PTR_ALIGN_CEIL 460 */ 461 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align) 462 463 /** 464 * Macro to align a value to a given power-of-two. The resultant 465 * value will be of the same type as the first parameter, and 466 * will be no lower than the first parameter. Second parameter 467 * must be a power-of-two value. 468 * This function is the same as RTE_ALIGN_CEIL 469 */ 470 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align) 471 472 /** 473 * Macro to align a value to the multiple of given value. The resultant 474 * value will be of the same type as the first parameter and will be no lower 475 * than the first parameter. 476 */ 477 #define RTE_ALIGN_MUL_CEIL(v, mul) \ 478 ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul)) 479 480 /** 481 * Macro to align a value to the multiple of given value. The resultant 482 * value will be of the same type as the first parameter and will be no higher 483 * than the first parameter. 484 */ 485 #define RTE_ALIGN_MUL_FLOOR(v, mul) \ 486 (((v) / ((typeof(v))(mul))) * (typeof(v))(mul)) 487 488 /** 489 * Macro to align value to the nearest multiple of the given value. 490 * The resultant value might be greater than or less than the first parameter 491 * whichever difference is the lowest. 492 */ 493 #define RTE_ALIGN_MUL_NEAR(v, mul) \ 494 __extension__ ({ \ 495 typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \ 496 typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \ 497 (ceil - (v)) > ((v) - floor) ? floor : ceil; \ 498 }) 499 500 /** 501 * Checks if a pointer is aligned to a given power-of-two value 502 * 503 * @param ptr 504 * The pointer whose alignment is to be checked 505 * @param align 506 * The power-of-two value to which the ptr should be aligned 507 * 508 * @return 509 * True(1) where the pointer is correctly aligned, false(0) otherwise 510 */ 511 static inline int 512 rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align) 513 { 514 return ((uintptr_t)ptr & (align - 1)) == 0; 515 } 516 517 /*********** Macros for compile type checks ********/ 518 519 /* Workaround for toolchain issues with missing C11 macro in FreeBSD */ 520 #if !defined(static_assert) && !defined(__cplusplus) 521 #define static_assert _Static_assert 522 #endif 523 524 /** 525 * Triggers an error at compilation time if the condition is true. 526 * 527 * The do { } while(0) exists to workaround a bug in clang (#55821) 528 * where it would not handle _Static_assert in a switch case. 529 */ 530 #define RTE_BUILD_BUG_ON(condition) do { static_assert(!(condition), #condition); } while (0) 531 532 /*********** Cache line related macros ********/ 533 534 /** Cache line mask. */ 535 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1) 536 537 /** Return the first cache-aligned value greater or equal to size. */ 538 #define RTE_CACHE_LINE_ROUNDUP(size) RTE_ALIGN_CEIL(size, RTE_CACHE_LINE_SIZE) 539 540 /** Cache line size in terms of log2 */ 541 #if RTE_CACHE_LINE_SIZE == 64 542 #define RTE_CACHE_LINE_SIZE_LOG2 6 543 #elif RTE_CACHE_LINE_SIZE == 128 544 #define RTE_CACHE_LINE_SIZE_LOG2 7 545 #else 546 #error "Unsupported cache line size" 547 #endif 548 549 /** Minimum Cache line size. */ 550 #define RTE_CACHE_LINE_MIN_SIZE 64 551 552 /** Force alignment to cache line. */ 553 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE) 554 555 /** Force minimum cache line alignment. */ 556 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE) 557 558 #define _RTE_CACHE_GUARD_HELPER2(unique) \ 559 alignas(RTE_CACHE_LINE_SIZE) \ 560 char cache_guard_ ## unique[RTE_CACHE_LINE_SIZE * RTE_CACHE_GUARD_LINES] 561 #define _RTE_CACHE_GUARD_HELPER1(unique) _RTE_CACHE_GUARD_HELPER2(unique) 562 /** 563 * Empty cache lines, to guard against false sharing-like effects 564 * on systems with a next-N-lines hardware prefetcher. 565 * 566 * Use as spacing between data accessed by different lcores, 567 * to prevent cache thrashing on hardware with speculative prefetching. 568 */ 569 #define RTE_CACHE_GUARD _RTE_CACHE_GUARD_HELPER1(__COUNTER__) 570 571 /*********** PA/IOVA type definitions ********/ 572 573 /** Physical address */ 574 typedef uint64_t phys_addr_t; 575 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1) 576 577 /** 578 * IO virtual address type. 579 * When the physical addressing mode (IOVA as PA) is in use, 580 * the translation from an IO virtual address (IOVA) to a physical address 581 * is a direct mapping, i.e. the same value. 582 * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation. 583 */ 584 typedef uint64_t rte_iova_t; 585 #define RTE_BAD_IOVA ((rte_iova_t)-1) 586 587 /*********** Structure alignment markers ********/ 588 589 #ifndef RTE_TOOLCHAIN_MSVC 590 591 /** Generic marker for any place in a structure. */ 592 __extension__ typedef void *RTE_MARKER[0]; 593 /** Marker for 1B alignment in a structure. */ 594 __extension__ typedef uint8_t RTE_MARKER8[0]; 595 /** Marker for 2B alignment in a structure. */ 596 __extension__ typedef uint16_t RTE_MARKER16[0]; 597 /** Marker for 4B alignment in a structure. */ 598 __extension__ typedef uint32_t RTE_MARKER32[0]; 599 /** Marker for 8B alignment in a structure. */ 600 __extension__ typedef uint64_t RTE_MARKER64[0]; 601 602 #endif 603 604 /*********** Macros for calculating min and max **********/ 605 606 /** 607 * Macro to return the minimum of two numbers 608 */ 609 #define RTE_MIN(a, b) \ 610 __extension__ ({ \ 611 typeof (a) _a = (a); \ 612 typeof (b) _b = (b); \ 613 _a < _b ? _a : _b; \ 614 }) 615 616 /** 617 * Macro to return the minimum of two numbers 618 * 619 * As opposed to RTE_MIN, it does not use temporary variables so it is not safe 620 * if a or b is an expression. Yet it is guaranteed to be constant for use in 621 * static_assert(). 622 */ 623 #define RTE_MIN_T(a, b, t) \ 624 ((t)(a) < (t)(b) ? (t)(a) : (t)(b)) 625 626 /** 627 * Macro to return the maximum of two numbers 628 */ 629 #define RTE_MAX(a, b) \ 630 __extension__ ({ \ 631 typeof (a) _a = (a); \ 632 typeof (b) _b = (b); \ 633 _a > _b ? _a : _b; \ 634 }) 635 636 /** 637 * Macro to return the maximum of two numbers 638 * 639 * As opposed to RTE_MAX, it does not use temporary variables so it is not safe 640 * if a or b is an expression. Yet it is guaranteed to be constant for use in 641 * static_assert(). 642 */ 643 #define RTE_MAX_T(a, b, t) \ 644 ((t)(a) > (t)(b) ? (t)(a) : (t)(b)) 645 646 /*********** Other general functions / macros ********/ 647 648 #ifndef offsetof 649 /** Return the offset of a field in a structure. */ 650 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER) 651 #endif 652 653 /** 654 * Return pointer to the wrapping struct instance. 655 * 656 * Example: 657 * 658 * struct wrapper { 659 * ... 660 * struct child c; 661 * ... 662 * }; 663 * 664 * struct child *x = obtain(...); 665 * struct wrapper *w = container_of(x, struct wrapper, c); 666 */ 667 #ifndef container_of 668 #ifdef RTE_TOOLCHAIN_MSVC 669 #define container_of(ptr, type, member) \ 670 ((type *)((uintptr_t)(ptr) - offsetof(type, member))) 671 #else 672 #define container_of(ptr, type, member) __extension__ ({ \ 673 const typeof(((type *)0)->member) *_ptr = (ptr); \ 674 __rte_unused type *_target_ptr = \ 675 (type *)(ptr); \ 676 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \ 677 }) 678 #endif 679 #endif 680 681 /** Swap two variables. */ 682 #define RTE_SWAP(a, b) \ 683 __extension__ ({ \ 684 typeof (a) _a = a; \ 685 a = b; \ 686 b = _a; \ 687 }) 688 689 /** 690 * Get the size of a field in a structure. 691 * 692 * @param type 693 * The type of the structure. 694 * @param field 695 * The field in the structure. 696 * @return 697 * The size of the field in the structure, in bytes. 698 */ 699 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field)) 700 701 #define _RTE_STR(x) #x 702 /** Take a macro value and get a string version of it */ 703 #define RTE_STR(x) _RTE_STR(x) 704 705 /** 706 * ISO C helpers to modify format strings using variadic macros. 707 * This is a replacement for the ", ## __VA_ARGS__" GNU extension. 708 * An empty %s argument is appended to avoid a dangling comma. 709 */ 710 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ "" 711 #define RTE_FMT_HEAD(fmt, ...) fmt 712 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__ 713 714 /** Mask value of type "tp" for the first "ln" bit set. */ 715 #define RTE_LEN2MASK(ln, tp) \ 716 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln)))) 717 718 /** Number of elements in the array. */ 719 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0])) 720 721 /** 722 * Converts a numeric string to the equivalent uint64_t value. 723 * As well as straight number conversion, also recognises the suffixes 724 * k, m and g for kilobytes, megabytes and gigabytes respectively. 725 * 726 * If a negative number is passed in i.e. a string with the first non-black 727 * character being "-", zero is returned. Zero is also returned in the case of 728 * an error with the strtoull call in the function. 729 * 730 * @param str 731 * String containing number to convert. 732 * @return 733 * Number. 734 */ 735 uint64_t 736 rte_str_to_size(const char *str); 737 738 /** 739 * Function to terminate the application immediately, printing an error 740 * message and returning the exit_code back to the shell. 741 * 742 * This function never returns 743 * 744 * @param exit_code 745 * The exit code to be returned by the application 746 * @param format 747 * The format string to be used for printing the message. This can include 748 * printf format characters which will be expanded using any further parameters 749 * to the function. 750 */ 751 __rte_noreturn void 752 rte_exit(int exit_code, const char *format, ...) 753 __rte_format_printf(2, 3); 754 755 #ifdef __cplusplus 756 } 757 #endif 758 759 #endif 760