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