LCOV - code coverage report
Current view: top level - lib/eal/include - rte_common.h (source / functions) Hit Total Coverage
Test: Code coverage Lines: 1 1 100.0 %
Date: 2026-11-01 19:11:22 Functions: 0 0 -
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 2 6 33.3 %

           Branch data     Line data    Source code
       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                 :            : #include <assert.h>
      16                 :            : #include <limits.h>
      17                 :            : #include <stdbool.h>
      18                 :            : #include <stdint.h>
      19                 :            : #include <stdalign.h>
      20                 :            : 
      21                 :            : #include <rte_compat.h>
      22                 :            : #include <rte_config.h>
      23                 :            : 
      24                 :            : /* OS specific include */
      25                 :            : #include <rte_os.h>
      26                 :            : 
      27                 :            : #ifdef __cplusplus
      28                 :            : extern "C" {
      29                 :            : #endif
      30                 :            : 
      31                 :            : #ifndef RTE_TOOLCHAIN_MSVC
      32                 :            : #ifndef typeof
      33                 :            : #define typeof __typeof__
      34                 :            : #endif
      35                 :            : #endif
      36                 :            : 
      37                 :            : #ifndef __cplusplus
      38                 :            : #ifndef asm
      39                 :            : #define asm __asm__
      40                 :            : #endif
      41                 :            : #endif
      42                 :            : 
      43                 :            : #ifdef RTE_TOOLCHAIN_MSVC
      44                 :            : #ifdef __cplusplus
      45                 :            : #define __extension__
      46                 :            : #endif
      47                 :            : #endif
      48                 :            : 
      49                 :            : /*
      50                 :            :  * Macro __rte_constant checks if an expression's value can be determined at
      51                 :            :  * compile time. It takes a single argument, the expression to test, and
      52                 :            :  * returns 1 if the expression is a compile-time constant, and 0 otherwise.
      53                 :            :  * For most compilers it uses built-in function __builtin_constant_p, but for
      54                 :            :  * MSVC it uses a different method because MSVC does not have an equivalent
      55                 :            :  * to __builtin_constant_p.
      56                 :            :  *
      57                 :            :  * The trick used with MSVC relies on the way null pointer constants interact
      58                 :            :  * with the type of a ?: expression:
      59                 :            :  * An integer constant expression with the value 0, or such an expression cast
      60                 :            :  * to type void *, is called a null pointer constant.
      61                 :            :  * If both the second and third operands (of the ?: expression) are pointers or
      62                 :            :  * one is a null pointer constant and the other is a pointer, the result type
      63                 :            :  * is a pointer to a type qualified with all the type qualifiers of the types
      64                 :            :  * referenced by both operands. Furthermore, if both operands are pointers to
      65                 :            :  * compatible types or to differently qualified versions of compatible types,
      66                 :            :  * the result type is a pointer to an appropriately qualified version of the
      67                 :            :  * composite type; if one operand is a null pointer constant, the result has
      68                 :            :  * the type of the other operand; otherwise, one operand is a pointer to void
      69                 :            :  * or a qualified version of void, in which case the result type is a pointer
      70                 :            :  * to an appropriately qualified version of void.
      71                 :            :  *
      72                 :            :  * The _Generic keyword then checks the type of the expression
      73                 :            :  * (void *) ((e) * 0ll). It matches this type against the types listed in the
      74                 :            :  * _Generic construct:
      75                 :            :  *     - If the type is int *, the result is 1.
      76                 :            :  *     - If the type is void *, the result is 0.
      77                 :            :  *
      78                 :            :  * This explanation with some more details can be found at:
      79                 :            :  * https://stackoverflow.com/questions/49480442/detecting-integer-constant-expressions-in-macros
      80                 :            :  */
      81                 :            : #ifdef RTE_TOOLCHAIN_MSVC
      82                 :            : #define __rte_constant(e) _Generic((1 ? (void *) ((e) * 0ll) : (int *) 0), int * : 1, void * : 0)
      83                 :            : #else
      84                 :            : #define __rte_constant(e) __extension__(__builtin_constant_p(e))
      85                 :            : #endif
      86                 :            : 
      87                 :            : /*
      88                 :            :  * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
      89                 :            :  * while a host application (like pmdinfogen) may have another compiler.
      90                 :            :  * RTE_CC_IS_GNU is true if the file is compiled with GCC,
      91                 :            :  * no matter it is a target or host application.
      92                 :            :  */
      93                 :            : #define RTE_CC_IS_GNU 0
      94                 :            : #if defined __clang__
      95                 :            : #define RTE_CC_CLANG
      96                 :            : #elif defined __GNUC__
      97                 :            : #define RTE_CC_GCC
      98                 :            : #undef RTE_CC_IS_GNU
      99                 :            : #define RTE_CC_IS_GNU 1
     100                 :            : #endif
     101                 :            : #if RTE_CC_IS_GNU
     102                 :            : #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 +  \
     103                 :            :                 __GNUC_PATCHLEVEL__)
     104                 :            : #endif
     105                 :            : 
     106                 :            : /*
     107                 :            :  * Type inference for use in macros.
     108                 :            :  */
     109                 :            : #if (defined(__cplusplus) && __cplusplus >= 201103L) || \
     110                 :            :                 (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L)
     111                 :            : #define __rte_auto_type auto
     112                 :            : #elif defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
     113                 :            : #define __rte_auto_type __auto_type
     114                 :            : #endif
     115                 :            : 
     116                 :            : /*
     117                 :            :  * Helper macro for array decay in pointer arithmetic macros.
     118                 :            :  * Example: char arr[10]; RTE_PTR_ADD(arr, 5) needs arr to decay to char*.
     119                 :            :  *
     120                 :            :  * GCC/Clang in C mode need "+ 0" to force arrays to decay to pointers.
     121                 :            :  * Not needed for C++ (automatic decay) or MSVC (ternary checks both branches).
     122                 :            :  *
     123                 :            :  * Note: This must be an object-like macro (not function-like) because it gets
     124                 :            :  * used with nested macro expansion (e.g., RTE_PTR_ALIGN_FLOOR(RTE_PTR_ADD(...))).
     125                 :            :  * A function-like macro would wrap the argument in parentheses, causing _Pragma
     126                 :            :  * directives from nested statement expressions to appear in invalid contexts.
     127                 :            :  */
     128                 :            : #if !defined(RTE_TOOLCHAIN_MSVC) && !defined(__cplusplus)
     129                 :            : #define __rte_ptr_arith_add_zero + 0
     130                 :            : #else
     131                 :            : #define __rte_ptr_arith_add_zero
     132                 :            : #endif
     133                 :            : 
     134                 :            : /**
     135                 :            :  * Force type alignment
     136                 :            :  *
     137                 :            :  * This macro should be used when alignment of a struct or union type
     138                 :            :  * is required. For toolchain compatibility it should appear between
     139                 :            :  * the {struct,union} keyword and tag. e.g.
     140                 :            :  *
     141                 :            :  *   struct __rte_aligned(8) tag { ... };
     142                 :            :  *
     143                 :            :  * If alignment of an object/variable is required then this macro should
     144                 :            :  * not be used, instead prefer C11 alignas(a).
     145                 :            :  */
     146                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     147                 :            : #define __rte_aligned(a) __declspec(align(a))
     148                 :            : #else
     149                 :            : #define __rte_aligned(a) __attribute__((__aligned__(a)))
     150                 :            : #endif
     151                 :            : 
     152                 :            : /**
     153                 :            :  * Integer types with no alignment requirement.
     154                 :            :  */
     155                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     156                 :            : typedef __unaligned uint64_t unaligned_uint64_t;
     157                 :            : typedef __unaligned uint32_t unaligned_uint32_t;
     158                 :            : typedef __unaligned uint16_t unaligned_uint16_t;
     159                 :            : #else
     160                 :            : typedef uint64_t unaligned_uint64_t __rte_aligned(1);
     161                 :            : typedef uint32_t unaligned_uint32_t __rte_aligned(1);
     162                 :            : typedef uint16_t unaligned_uint16_t __rte_aligned(1);
     163                 :            : #endif
     164                 :            : 
     165                 :            : /**
     166                 :            :  * Force a structure to be packed
     167                 :            :  * Usage:
     168                 :            :  *     struct __rte_packed_begin mystruct { ... } __rte_packed_end;
     169                 :            :  *     union __rte_packed_begin myunion { ... } __rte_packed_end;
     170                 :            :  * Note: alignment attributes when present should precede __rte_packed_begin.
     171                 :            :  */
     172                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     173                 :            : #define __rte_packed_begin __pragma(pack(push, 1))
     174                 :            : #define __rte_packed_end __pragma(pack(pop))
     175                 :            : #else
     176                 :            : #define __rte_packed_begin
     177                 :            : #define __rte_packed_end __attribute__((__packed__))
     178                 :            : #endif
     179                 :            : 
     180                 :            : /**
     181                 :            :  * Macro to mark a type that is not subject to type-based aliasing rules
     182                 :            :  */
     183                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     184                 :            : #define __rte_may_alias
     185                 :            : #else
     186                 :            : #define __rte_may_alias __attribute__((__may_alias__))
     187                 :            : #endif
     188                 :            : 
     189                 :            : /******* Macro to mark functions and fields scheduled for removal *****/
     190                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     191                 :            : #define __rte_deprecated
     192                 :            : #define __rte_deprecated_msg(msg)
     193                 :            : #else
     194                 :            : #define __rte_deprecated        __attribute__((__deprecated__))
     195                 :            : #define __rte_deprecated_msg(msg)       __attribute__((__deprecated__(msg)))
     196                 :            : #endif
     197                 :            : 
     198                 :            : /**
     199                 :            :  *  Macro to mark macros and defines scheduled for removal
     200                 :            :  */
     201                 :            : #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
     202                 :            : #define RTE_PRAGMA(x)  _Pragma(#x)
     203                 :            : #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
     204                 :            : #define RTE_DEPRECATED(x)  RTE_PRAGMA_WARNING(#x is deprecated)
     205                 :            : #else
     206                 :            : #define RTE_DEPRECATED(x)
     207                 :            : #endif
     208                 :            : 
     209                 :            : /**
     210                 :            :  * Macros to cause the compiler to remember the state of the diagnostics as of
     211                 :            :  * each push, and restore to that point at each pop.
     212                 :            :  */
     213                 :            : #if !defined(RTE_TOOLCHAIN_MSVC)
     214                 :            : #define __rte_diagnostic_push _Pragma("GCC diagnostic push")
     215                 :            : #define __rte_diagnostic_pop  _Pragma("GCC diagnostic pop")
     216                 :            : #else
     217                 :            : #define __rte_diagnostic_push
     218                 :            : #define __rte_diagnostic_pop
     219                 :            : #endif
     220                 :            : 
     221                 :            : /**
     222                 :            :  * Macro to disable compiler warnings about removing a type
     223                 :            :  * qualifier from the target type.
     224                 :            :  */
     225                 :            : #if !defined(RTE_TOOLCHAIN_MSVC)
     226                 :            : #define __rte_diagnostic_ignored_wcast_qual _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
     227                 :            : #else
     228                 :            : #define __rte_diagnostic_ignored_wcast_qual
     229                 :            : #endif
     230                 :            : 
     231                 :            : /**
     232                 :            :  * Macro to disable compiler warnings about invalid array bounds access.
     233                 :            :  */
     234                 :            : #if !defined(RTE_TOOLCHAIN_MSVC)
     235                 :            : #define __rte_diagnostic_ignored_array_bounds \
     236                 :            :         _Pragma("GCC diagnostic ignored \"-Warray-bounds\"")
     237                 :            : #else
     238                 :            : #define __rte_diagnostic_ignored_array_bounds
     239                 :            : #endif
     240                 :            : 
     241                 :            : /**
     242                 :            :  * Mark a function or variable to a weak reference.
     243                 :            :  */
     244                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     245                 :            : #define __rte_weak RTE_DEPRECATED(__rte_weak)
     246                 :            : #else
     247                 :            : #define __rte_weak RTE_DEPRECATED(__rte_weak) __attribute__((__weak__))
     248                 :            : #endif
     249                 :            : 
     250                 :            : /**
     251                 :            :  * Mark a function to be pure.
     252                 :            :  */
     253                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     254                 :            : #define __rte_pure
     255                 :            : #else
     256                 :            : #define __rte_pure __attribute__((pure))
     257                 :            : #endif
     258                 :            : 
     259                 :            : /**
     260                 :            :  * Force symbol to be generated even if it appears to be unused.
     261                 :            :  */
     262                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     263                 :            : #define __rte_used
     264                 :            : #else
     265                 :            : #define __rte_used __attribute__((used))
     266                 :            : #endif
     267                 :            : 
     268                 :            : /*********** Macros to eliminate unused variable warnings ********/
     269                 :            : 
     270                 :            : /**
     271                 :            :  * short definition to mark a function parameter unused
     272                 :            :  */
     273                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     274                 :            : #define __rte_unused
     275                 :            : #else
     276                 :            : #define __rte_unused __attribute__((__unused__))
     277                 :            : #endif
     278                 :            : 
     279                 :            : /**
     280                 :            :  * Mark pointer as restricted with regard to pointer aliasing.
     281                 :            :  */
     282                 :            : #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
     283                 :            : #define __rte_restrict __restrict
     284                 :            : #else
     285                 :            : #define __rte_restrict restrict
     286                 :            : #endif
     287                 :            : 
     288                 :            : /**
     289                 :            :  * definition to mark a variable or function parameter as used so
     290                 :            :  * as to avoid a compiler warning
     291                 :            :  */
     292                 :            : #define RTE_SET_USED(x) (void)(x)
     293                 :            : 
     294                 :            : /**
     295                 :            :  * Check format string and its arguments at compile-time.
     296                 :            :  *
     297                 :            :  * GCC on Windows assumes MS-specific format string by default,
     298                 :            :  * even if the underlying stdio implementation is ANSI-compliant,
     299                 :            :  * so this must be overridden.
     300                 :            :  */
     301                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     302                 :            : #define __rte_format_printf(format_index, first_arg)
     303                 :            : #else
     304                 :            : #if RTE_CC_IS_GNU
     305                 :            : #define __rte_format_printf(format_index, first_arg) \
     306                 :            :         __attribute__((format(gnu_printf, format_index, first_arg)))
     307                 :            : #else
     308                 :            : #define __rte_format_printf(format_index, first_arg) \
     309                 :            :         __attribute__((format(printf, format_index, first_arg)))
     310                 :            : #endif
     311                 :            : #endif
     312                 :            : 
     313                 :            : /**
     314                 :            :  * Specify data or function section/segment.
     315                 :            :  */
     316                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     317                 :            : #define __rte_section(name) \
     318                 :            :         __pragma(data_seg(name)) __declspec(allocate(name))
     319                 :            : #else
     320                 :            : #define __rte_section(name) \
     321                 :            :         __attribute__((section(name)))
     322                 :            : #endif
     323                 :            : 
     324                 :            : /**
     325                 :            :  * Tells compiler that the function returns a value that points to
     326                 :            :  * memory, where the size is given by the one or two arguments.
     327                 :            :  * Used by compiler to validate object size.
     328                 :            :  */
     329                 :            : #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
     330                 :            : #define __rte_alloc_size(...) \
     331                 :            :         __attribute__((alloc_size(__VA_ARGS__)))
     332                 :            : #else
     333                 :            : #define __rte_alloc_size(...)
     334                 :            : #endif
     335                 :            : 
     336                 :            : /**
     337                 :            :  * Tells the compiler that the function returns a value that points to
     338                 :            :  * memory aligned by a function argument.
     339                 :            :  *
     340                 :            :  * Note: not enabled on Clang because it warns if align argument is zero.
     341                 :            :  */
     342                 :            : #if defined(RTE_CC_GCC)
     343                 :            : #define __rte_alloc_align(argno) \
     344                 :            :         __attribute__((alloc_align(argno)))
     345                 :            : #else
     346                 :            : #define __rte_alloc_align(argno)
     347                 :            : #endif
     348                 :            : 
     349                 :            : /**
     350                 :            :  * Tells the compiler this is a function like malloc and that the pointer
     351                 :            :  * returned cannot alias any other pointer (ie new memory).
     352                 :            :  */
     353                 :            : #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
     354                 :            : #define __rte_malloc __attribute__((__malloc__))
     355                 :            : #else
     356                 :            : #define __rte_malloc
     357                 :            : #endif
     358                 :            : 
     359                 :            : /**
     360                 :            :  * With recent GCC versions also able to track that proper
     361                 :            :  * deallocator function is used for this pointer.
     362                 :            :  */
     363                 :            : #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 110000)
     364                 :            : #define __rte_dealloc(dealloc, argno) \
     365                 :            :         __attribute__((malloc(dealloc, argno)))
     366                 :            : #else
     367                 :            : #define __rte_dealloc(dealloc, argno)
     368                 :            : #endif
     369                 :            : 
     370                 :            : #define RTE_PRIORITY_LOG 101
     371                 :            : #define RTE_PRIORITY_BUS 110
     372                 :            : #define RTE_PRIORITY_CLASS 120
     373                 :            : #define RTE_PRIORITY_LAST 65535
     374                 :            : 
     375                 :            : #define RTE_PRIO(prio) \
     376                 :            :         RTE_PRIORITY_ ## prio
     377                 :            : 
     378                 :            : /**
     379                 :            :  * Run function before main() with high priority.
     380                 :            :  *
     381                 :            :  * @param func
     382                 :            :  *   Constructor function.
     383                 :            :  * @param prio
     384                 :            :  *   Priority number must be above 100.
     385                 :            :  *   Lowest number is the first to run.
     386                 :            :  */
     387                 :            : #ifndef RTE_INIT_PRIO /* Allow overriding from EAL */
     388                 :            : #ifndef RTE_TOOLCHAIN_MSVC
     389                 :            : #define RTE_INIT_PRIO(func, prio) \
     390                 :            : static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
     391                 :            : #else
     392                 :            : /* definition from the Microsoft CRT */
     393                 :            : typedef int(__cdecl *_PIFV)(void);
     394                 :            : 
     395                 :            : #define CTOR_SECTION_LOG ".CRT$XIB"
     396                 :            : #define CTOR_SECTION_BUS ".CRT$XIC"
     397                 :            : #define CTOR_SECTION_CLASS ".CRT$XID"
     398                 :            : #define CTOR_SECTION_LAST ".CRT$XIY"
     399                 :            : 
     400                 :            : #define CTOR_PRIORITY_TO_SECTION(priority) CTOR_SECTION_ ## priority
     401                 :            : 
     402                 :            : #define RTE_INIT_PRIO(name, priority) \
     403                 :            :         static void name(void); \
     404                 :            :         static int __cdecl name ## _thunk(void) { name(); return 0; } \
     405                 :            :         __pragma(const_seg(CTOR_PRIORITY_TO_SECTION(priority))) \
     406                 :            :         __declspec(allocate(CTOR_PRIORITY_TO_SECTION(priority))) \
     407                 :            :             _PIFV name ## _pointer = &name ## _thunk; \
     408                 :            :         __pragma(const_seg()) \
     409                 :            :         static void name(void)
     410                 :            : #endif
     411                 :            : #endif
     412                 :            : 
     413                 :            : /**
     414                 :            :  * Run function before main() with low priority.
     415                 :            :  *
     416                 :            :  * The constructor will be run after prioritized constructors.
     417                 :            :  *
     418                 :            :  * @param func
     419                 :            :  *   Constructor function.
     420                 :            :  */
     421                 :            : #define RTE_INIT(func) \
     422                 :            :         RTE_INIT_PRIO(func, LAST)
     423                 :            : 
     424                 :            : /**
     425                 :            :  * Run after main() with low priority.
     426                 :            :  *
     427                 :            :  * @param func
     428                 :            :  *   Destructor function name.
     429                 :            :  * @param prio
     430                 :            :  *   Priority number must be above 100.
     431                 :            :  *   Lowest number is the last to run.
     432                 :            :  */
     433                 :            : #ifndef RTE_FINI_PRIO /* Allow overriding from EAL */
     434                 :            : #ifndef RTE_TOOLCHAIN_MSVC
     435                 :            : #define RTE_FINI_PRIO(func, prio) \
     436                 :            : static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
     437                 :            : #else
     438                 :            : #define DTOR_SECTION_LOG "mydtor$B"
     439                 :            : #define DTOR_SECTION_BUS "mydtor$C"
     440                 :            : #define DTOR_SECTION_CLASS "mydtor$D"
     441                 :            : #define DTOR_SECTION_LAST "mydtor$Y"
     442                 :            : 
     443                 :            : #define DTOR_PRIORITY_TO_SECTION(priority) DTOR_SECTION_ ## priority
     444                 :            : 
     445                 :            : #define RTE_FINI_PRIO(name, priority) \
     446                 :            :         static void name(void); \
     447                 :            :         __pragma(const_seg(DTOR_PRIORITY_TO_SECTION(priority))) \
     448                 :            :         __declspec(allocate(DTOR_PRIORITY_TO_SECTION(priority))) void *name ## _pointer = &name; \
     449                 :            :         __pragma(const_seg()) \
     450                 :            :         static void name(void)
     451                 :            : #endif
     452                 :            : #endif
     453                 :            : 
     454                 :            : /**
     455                 :            :  * Run after main() with high priority.
     456                 :            :  *
     457                 :            :  * The destructor will be run *before* prioritized destructors.
     458                 :            :  *
     459                 :            :  * @param func
     460                 :            :  *   Destructor function name.
     461                 :            :  */
     462                 :            : #define RTE_FINI(func) \
     463                 :            :         RTE_FINI_PRIO(func, LAST)
     464                 :            : 
     465                 :            : /**
     466                 :            :  * Hint never returning function
     467                 :            :  */
     468                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     469                 :            : #define __rte_noreturn
     470                 :            : #else
     471                 :            : #define __rte_noreturn __attribute__((noreturn))
     472                 :            : #endif
     473                 :            : 
     474                 :            : /**
     475                 :            :  * Hint point in program never reached
     476                 :            :  */
     477                 :            : #if defined(RTE_TOOLCHAIN_GCC) || defined(RTE_TOOLCHAIN_CLANG)
     478                 :            : #define __rte_unreachable() __extension__(__builtin_unreachable())
     479                 :            : #else
     480                 :            : #define __rte_unreachable() __assume(0)
     481                 :            : #endif
     482                 :            : 
     483                 :            : /**
     484                 :            :  * Issue a warning in case the function's return value is ignored.
     485                 :            :  *
     486                 :            :  * The use of this attribute should be restricted to cases where
     487                 :            :  * ignoring the marked function's return value is almost always a
     488                 :            :  * bug. With GCC, some effort is required to make clear that ignoring
     489                 :            :  * the return value is intentional. The usual void-casting method to
     490                 :            :  * mark something unused as used does not suppress the warning with
     491                 :            :  * this compiler.
     492                 :            :  *
     493                 :            :  * @code{.c}
     494                 :            :  * __rte_warn_unused_result int foo();
     495                 :            :  *
     496                 :            :  * void ignore_foo_result(void) {
     497                 :            :  *         foo(); // generates a warning with all compilers
     498                 :            :  *
     499                 :            :  *         (void)foo(); // still generates the warning with GCC (but not clang)
     500                 :            :  *
     501                 :            :  *         int unused __rte_unused;
     502                 :            :  *         unused = foo(); // does the trick with all compilers
     503                 :            :  *  }
     504                 :            :  * @endcode
     505                 :            :  */
     506                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     507                 :            : #define __rte_warn_unused_result
     508                 :            : #else
     509                 :            : #define __rte_warn_unused_result __attribute__((warn_unused_result))
     510                 :            : #endif
     511                 :            : 
     512                 :            : /**
     513                 :            :  * Force a function to be inlined, regardless of the compiler's size
     514                 :            :  * and call-graph heuristics.
     515                 :            :  *
     516                 :            :  * Prefer plain @c inline (or no annotation on a static function)
     517                 :            :  * and let the compiler decide.
     518                 :            :  * Modern compilers at -O2 inline small static functions well,
     519                 :            :  * and forcing it can hurt by inflating call sites, raising register pressure,
     520                 :            :  * and overriding profile-guided optimization.
     521                 :            :  *
     522                 :            :  * Reserve this attribute for cases where inlining is required for correctness,
     523                 :            :  * or for a documented and measured performance reason, e.g.
     524                 :            :  *  - a constant-folded fast path gated by @c __rte_constant()
     525                 :            :  *    that is lost unless the function is inlined into the caller;
     526                 :            :  *  - a wrapper around inline asm or an intrinsic
     527                 :            :  *    that needs a compile-time-constant argument from the caller's context.
     528                 :            :  *
     529                 :            :  * See the DPDK coding style guide for the full policy.
     530                 :            :  */
     531                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     532                 :            : #define __rte_always_inline __forceinline
     533                 :            : #else
     534                 :            : #define __rte_always_inline inline __attribute__((always_inline))
     535                 :            : #endif
     536                 :            : 
     537                 :            : /**
     538                 :            :  * Force a function not to be inlined.
     539                 :            :  *
     540                 :            :  * Useful for explicitly outlining cold paths such as error handling,
     541                 :            :  * initialization, slow-path fallbacks, so they do not bloat the hot path
     542                 :            :  * or add to its instruction-cache footprint.
     543                 :            :  */
     544                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     545                 :            : #define __rte_noinline __declspec(noinline)
     546                 :            : #else
     547                 :            : #define __rte_noinline __attribute__((noinline))
     548                 :            : #endif
     549                 :            : 
     550                 :            : /**
     551                 :            :  * Hint function in the hot path
     552                 :            :  *
     553                 :            :  * The compiler may optimize the function more aggressively,
     554                 :            :  * treat calls to it as likely for branch prediction,
     555                 :            :  * and group it with other hot functions to improve instruction-cache locality.
     556                 :            :  * This affects code placement and prediction, not inlining;
     557                 :            :  * combine with an inline annotation if both are wanted.
     558                 :            :  */
     559                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     560                 :            : #define __rte_hot
     561                 :            : #else
     562                 :            : #define __rte_hot __attribute__((hot))
     563                 :            : #endif
     564                 :            : 
     565                 :            : /**
     566                 :            :  * Hint function in the cold path
     567                 :            :  *
     568                 :            :  * The compiler optimizes the function for size rather than speed,
     569                 :            :  * marks branches that reach it as unlikely,
     570                 :            :  * and may move it to a separate section to keep it off the hot path
     571                 :            :  * and reduce instruction-cache pressure there.
     572                 :            :  *
     573                 :            :  * Suitable for error handling, logging, and setup/teardown code.
     574                 :            :  * Functions marked @c __rte_noreturn are already treated as cold.
     575                 :            :  */
     576                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     577                 :            : #define __rte_cold
     578                 :            : #else
     579                 :            : #define __rte_cold __attribute__((cold))
     580                 :            : #endif
     581                 :            : 
     582                 :            : /**
     583                 :            :  * Hint precondition
     584                 :            :  *
     585                 :            :  * @warning Depending on the compiler, any code in ``condition`` might be executed.
     586                 :            :  * This currently only occurs with GCC prior to version 13.
     587                 :            :  */
     588                 :            : #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 130000)
     589                 :            : #define __rte_assume(condition) __attribute__((assume(condition)))
     590                 :            : #elif defined(RTE_TOOLCHAIN_GCC)
     591                 :            : #define __rte_assume(condition) do { if (!(condition)) __rte_unreachable(); } while (0)
     592                 :            : #elif defined(RTE_TOOLCHAIN_CLANG)
     593                 :            : #define __rte_assume(condition) __extension__(__builtin_assume(condition))
     594                 :            : #else
     595                 :            : #define __rte_assume(condition) __assume(condition)
     596                 :            : #endif
     597                 :            : 
     598                 :            : /**
     599                 :            :  * Disable AddressSanitizer on some code
     600                 :            :  */
     601                 :            : #ifdef RTE_MALLOC_ASAN
     602                 :            : #ifdef RTE_CC_CLANG
     603                 :            : #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
     604                 :            : #else
     605                 :            : #define __rte_no_asan __attribute__((no_sanitize_address))
     606                 :            : #endif
     607                 :            : #else /* ! RTE_MALLOC_ASAN */
     608                 :            : #define __rte_no_asan
     609                 :            : #endif
     610                 :            : 
     611                 :            : /*********** Macros for pointer arithmetic ********/
     612                 :            : 
     613                 :            : /**
     614                 :            :  * Add a byte-value offset to a pointer.
     615                 :            :  *
     616                 :            :  * @param ptr
     617                 :            :  *   The pointer (must be non-NULL).
     618                 :            :  * @param x
     619                 :            :  *   Byte offset to add.
     620                 :            :  * @return
     621                 :            :  *   void* (or const void* / volatile void* / const volatile void* preserving qualifiers).
     622                 :            :  *   Returning void* prevents the compiler from making alignment assumptions
     623                 :            :  *   based on the pointer type,
     624                 :            :  *   which is important when doing byte-offset arithmetic
     625                 :            :  *   that may cross struct boundaries or result in unaligned pointers.
     626                 :            :  */
     627                 :            : #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
     628                 :            : #define RTE_PTR_ADD(ptr, x) \
     629                 :            : (__extension__ ({ \
     630                 :            :         /* (1) Force array decay and ensure single evaluation */ \
     631                 :            :         __rte_auto_type __rte_ptr_add = (ptr) __rte_ptr_arith_add_zero; \
     632                 :            :         __rte_diagnostic_push \
     633                 :            :         __rte_diagnostic_ignored_wcast_qual \
     634                 :            :         /* (2) Calculate result, preserving const/volatile via ternary */ \
     635                 :            :         __rte_auto_type __rte_ptr_add_res = \
     636                 :            :                 (1 ? (void *)((char *)__rte_ptr_add + (x)) : __rte_ptr_add); \
     637                 :            :         __rte_diagnostic_pop \
     638                 :            :         /* (3) Return the result */ \
     639                 :            :         __rte_ptr_add_res; \
     640                 :            : }))
     641                 :            : #else
     642                 :            : /* MSVC fallback (ternary preserves const, no statement exprs) */
     643                 :            : #define RTE_PTR_ADD(ptr, x) \
     644                 :            :         (1 ? (void *)((char *)((ptr) __rte_ptr_arith_add_zero) + (x)) : \
     645                 :            :                 ((ptr) __rte_ptr_arith_add_zero))
     646                 :            : #endif
     647                 :            : 
     648                 :            : /**
     649                 :            :  * Subtract a byte-value offset from a pointer.
     650                 :            :  *
     651                 :            :  * @param ptr
     652                 :            :  *   The pointer (must be non-NULL).
     653                 :            :  * @param x
     654                 :            :  *   Byte offset to subtract.
     655                 :            :  * @return
     656                 :            :  *   void* (or const void* / volatile void* / const volatile void* preserving qualifiers).
     657                 :            :  *   Returning void* prevents the compiler from making alignment assumptions
     658                 :            :  *   based on the pointer type,
     659                 :            :  *   which is important when doing byte-offset arithmetic
     660                 :            :  *   that may cross struct boundaries or result in unaligned pointers.
     661                 :            :  */
     662                 :            : #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
     663                 :            : #define RTE_PTR_SUB(ptr, x) \
     664                 :            : (__extension__ ({ \
     665                 :            :         /* (1) Force array decay and ensure single evaluation */ \
     666                 :            :         __rte_auto_type __rte_ptr_sub = (ptr) __rte_ptr_arith_add_zero; \
     667                 :            :         __rte_diagnostic_push \
     668                 :            :         __rte_diagnostic_ignored_wcast_qual \
     669                 :            :         /* (2) Calculate result, preserving const/volatile via ternary */ \
     670                 :            :         __rte_auto_type __rte_ptr_sub_res = \
     671                 :            :                 (1 ? (void *)((char *)__rte_ptr_sub - (x)) : __rte_ptr_sub); \
     672                 :            :         __rte_diagnostic_pop \
     673                 :            :         /* (3) Return the result */ \
     674                 :            :         __rte_ptr_sub_res; \
     675                 :            : }))
     676                 :            : #else
     677                 :            : /* MSVC fallback (ternary preserves const, no statement exprs) */
     678                 :            : #define RTE_PTR_SUB(ptr, x) \
     679                 :            :         (1 ? (void *)((char *)((ptr) __rte_ptr_arith_add_zero) - (x)) : \
     680                 :            :                 ((ptr) __rte_ptr_arith_add_zero))
     681                 :            : #endif
     682                 :            : 
     683                 :            : /**
     684                 :            :  * get the difference between two pointer values, i.e. how far apart
     685                 :            :  * in bytes are the locations they point two. It is assumed that
     686                 :            :  * ptr1 is greater than ptr2.
     687                 :            :  */
     688                 :            : #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
     689                 :            : 
     690                 :            : /*********** Macros for casting pointers ********/
     691                 :            : 
     692                 :            : /**
     693                 :            :  * Macro to discard qualifiers (such as const, volatile, restrict) from a pointer,
     694                 :            :  * without the compiler emitting a warning.
     695                 :            :  */
     696                 :            : #define RTE_PTR_UNQUAL(X) ((void *)(uintptr_t)(X))
     697                 :            : 
     698                 :            : /**
     699                 :            :  * Macro to cast a pointer to a specific type,
     700                 :            :  * without the compiler emitting a warning about discarding qualifiers.
     701                 :            :  *
     702                 :            :  * @warning
     703                 :            :  * When casting a pointer to point to a larger type, the resulting pointer may
     704                 :            :  * be misaligned, which results in undefined behavior.
     705                 :            :  * E.g.:
     706                 :            :  *
     707                 :            :  * struct s {
     708                 :            :  *       uint16_t a;
     709                 :            :  *       uint8_t  b;
     710                 :            :  *       uint8_t  c;
     711                 :            :  *       uint8_t  d;
     712                 :            :  *   } v;
     713                 :            :  *   uint16_t * p = RTE_CAST_PTR(uint16_t *, &v.c); // "p" is not 16 bit aligned!
     714                 :            :  */
     715                 :            : #define RTE_CAST_PTR(type, ptr) ((type)(uintptr_t)(ptr))
     716                 :            : 
     717                 :            : /**
     718                 :            :  * Workaround to cast a const field of a structure to non-const type.
     719                 :            :  */
     720                 :            : #define RTE_CAST_FIELD(var, field, type) \
     721                 :            :         (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
     722                 :            : 
     723                 :            : /*********** Macros/static functions for doing alignment ********/
     724                 :            : 
     725                 :            : 
     726                 :            : /**
     727                 :            :  * Macro to align a pointer to a given power-of-two.
     728                 :            :  *
     729                 :            :  * Aligns the pointer down to the specified alignment boundary.
     730                 :            :  *
     731                 :            :  * @param ptr
     732                 :            :  *   The pointer (must be non-NULL).
     733                 :            :  * @param align
     734                 :            :  *   Alignment boundary (must be a power-of-two value).
     735                 :            :  * @return
     736                 :            :  *   Aligned pointer of the same type as ptr,
     737                 :            :  *   pointing to an address no higher than ptr.
     738                 :            :  *   Returns pointer of same type as input, preserving const/volatile qualifiers.
     739                 :            :  *   Since alignment operations guarantee proper alignment,
     740                 :            :  *   the return type matches the input type.
     741                 :            :  */
     742                 :            : #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
     743                 :            : #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
     744                 :            : (__extension__ ({ \
     745                 :            :         /* (1) Force array decay and ensure single evaluation */ \
     746                 :            :         __rte_auto_type __rte_ptr_floor = (ptr) __rte_ptr_arith_add_zero; \
     747                 :            :         /* (2) Compute misalignment as integer, but adjust pointer using pointer arithmetic */ \
     748                 :            :         size_t __rte_ptr_floor_misalign = (uintptr_t)__rte_ptr_floor & ((align) - 1); \
     749                 :            :         __rte_diagnostic_push \
     750                 :            :         __rte_diagnostic_ignored_wcast_qual \
     751                 :            :         /* (3) Return the aligned result, cast to preserve input type. We avoid RTE_PTR_SUB */ \
     752                 :            :         /*  to skip the void* cast which may defeat compiler alignment optimizations. */ \
     753                 :            :         __rte_auto_type __rte_ptr_floor_res = \
     754                 :            :                 (typeof(__rte_ptr_floor))((char *)__rte_ptr_floor - __rte_ptr_floor_misalign); \
     755                 :            :         __rte_diagnostic_pop \
     756                 :            :         __rte_ptr_floor_res; \
     757                 :            : }))
     758                 :            : #else
     759                 :            : #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
     760                 :            :         ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t) ((ptr) __rte_ptr_arith_add_zero), align))
     761                 :            : #endif
     762                 :            : 
     763                 :            : /**
     764                 :            :  * Macro to align a value to a given power-of-two. The resultant value
     765                 :            :  * will be of the same type as the first parameter, and will be no
     766                 :            :  * bigger than the first parameter. Second parameter must be a
     767                 :            :  * power-of-two value.
     768                 :            :  */
     769                 :            : #define RTE_ALIGN_FLOOR(val, align) \
     770                 :            :         (typeof(val))((val) & (~((typeof(val))((align) - 1))))
     771                 :            : 
     772                 :            : /**
     773                 :            :  * Macro to align a pointer to a given power-of-two.
     774                 :            :  *
     775                 :            :  * Aligns the pointer up to the specified alignment boundary.
     776                 :            :  *
     777                 :            :  * @param ptr
     778                 :            :  *   The pointer (must be non-NULL).
     779                 :            :  * @param align
     780                 :            :  *   Alignment boundary (must be a power-of-two value).
     781                 :            :  * @return
     782                 :            :  *   Aligned pointer of the same type as ptr,
     783                 :            :  *   pointing to an address no lower than ptr.
     784                 :            :  *   Returns pointer of same type as input, preserving const/volatile qualifiers.
     785                 :            :  *   Since alignment operations guarantee proper alignment,
     786                 :            :  *   the return type matches the input type.
     787                 :            :  */
     788                 :            : #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
     789                 :            : #define RTE_PTR_ALIGN_CEIL(ptr, align) \
     790                 :            : (__extension__ ({ \
     791                 :            :         /* (1) Force array decay and ensure single evaluation */ \
     792                 :            :         __rte_auto_type __rte_ptr_ceil = (ptr) __rte_ptr_arith_add_zero; \
     793                 :            :         /* (2) Compute alignment as integer, but adjust pointer using pointer arithmetic */ \
     794                 :            :         size_t __rte_ptr_ceil_align_m1 = (align) - 1; \
     795                 :            :         uintptr_t __rte_ptr_ceil_aligned = ((uintptr_t)__rte_ptr_ceil + __rte_ptr_ceil_align_m1) \
     796                 :            :                                                 & ~__rte_ptr_ceil_align_m1; \
     797                 :            :         size_t __rte_ptr_ceil_offset = __rte_ptr_ceil_aligned - (uintptr_t)__rte_ptr_ceil; \
     798                 :            :         __rte_diagnostic_push \
     799                 :            :         __rte_diagnostic_ignored_wcast_qual \
     800                 :            :         /* (3) Return the aligned result, cast to preserve input type. We avoid RTE_PTR_SUB */ \
     801                 :            :         /*  to skip the void* cast which may defeat compiler alignment optimizations. */ \
     802                 :            :         __rte_auto_type __rte_ptr_ceil_res = \
     803                 :            :                 (typeof(__rte_ptr_ceil))((char *)__rte_ptr_ceil + __rte_ptr_ceil_offset); \
     804                 :            :         __rte_diagnostic_pop \
     805                 :            :         __rte_ptr_ceil_res; \
     806                 :            : }))
     807                 :            : #else
     808                 :            : #define RTE_PTR_ALIGN_CEIL(ptr, align) \
     809                 :            :         RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
     810                 :            : #endif
     811                 :            : 
     812                 :            : /**
     813                 :            :  * Macro to align a value to a given power-of-two. The resultant value
     814                 :            :  * will be of the same type as the first parameter, and will be no lower
     815                 :            :  * than the first parameter. Second parameter must be a power-of-two
     816                 :            :  * value.
     817                 :            :  */
     818                 :            : #define RTE_ALIGN_CEIL(val, align) \
     819                 :            :         RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
     820                 :            : 
     821                 :            : /**
     822                 :            :  * Macro to align a pointer to a given power-of-two. The resultant
     823                 :            :  * pointer will be a pointer of the same type as the first parameter, and
     824                 :            :  * point to an address no lower than the first parameter. Second parameter
     825                 :            :  * must be a power-of-two value.
     826                 :            :  * This function is the same as RTE_PTR_ALIGN_CEIL
     827                 :            :  */
     828                 :            : #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
     829                 :            : 
     830                 :            : /**
     831                 :            :  * Macro to align a value to a given power-of-two. The resultant
     832                 :            :  * value will be of the same type as the first parameter, and
     833                 :            :  * will be no lower than the first parameter. Second parameter
     834                 :            :  * must be a power-of-two value.
     835                 :            :  * This function is the same as RTE_ALIGN_CEIL
     836                 :            :  */
     837                 :            : #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
     838                 :            : 
     839                 :            : /**
     840                 :            :  * Macro to align a value to the multiple of given value. The resultant
     841                 :            :  * value will be of the same type as the first parameter and will be no lower
     842                 :            :  * than the first parameter.
     843                 :            :  */
     844                 :            : #define RTE_ALIGN_MUL_CEIL(v, mul) \
     845                 :            :         ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
     846                 :            : 
     847                 :            : /**
     848                 :            :  * Macro to align a value to the multiple of given value. The resultant
     849                 :            :  * value will be of the same type as the first parameter and will be no higher
     850                 :            :  * than the first parameter.
     851                 :            :  */
     852                 :            : #define RTE_ALIGN_MUL_FLOOR(v, mul) \
     853                 :            :         (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
     854                 :            : 
     855                 :            : /**
     856                 :            :  * Macro to align value to the nearest multiple of the given value.
     857                 :            :  * The resultant value might be greater than or less than the first parameter
     858                 :            :  * whichever difference is the lowest.
     859                 :            :  */
     860                 :            : #define RTE_ALIGN_MUL_NEAR(v, mul)                              \
     861                 :            :         __extension__ ({                                        \
     862                 :            :                 typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul);    \
     863                 :            :                 typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul);  \
     864                 :            :                 (ceil - (v)) > ((v) - floor) ? floor : ceil; \
     865                 :            :         })
     866                 :            : 
     867                 :            : /**
     868                 :            :  * Macro to divide dividend @p n by divisor @p d,
     869                 :            :  * rounding up if @p d does not divide @p n.
     870                 :            :  *
     871                 :            :  * @param n
     872                 :            :  *   Dividend. Must be a non-negative integer to round correctly.
     873                 :            :  * @param d
     874                 :            :  *   Divisor. Must be a positive integer. Evaluated twice.
     875                 :            :  * @return
     876                 :            :  *   Result of dividing @p n by @p d, rounded up.
     877                 :            :  *
     878                 :            :  * @note The divisor @p d is evaluated twice. Arguments must not have side effects.
     879                 :            :  * @note Both @p n and @p d must be non-negative to round correctly.
     880                 :            :  * @note The sum @c (n) + (d) - 1 can wrap around if @p n is close to the maximum
     881                 :            :  *       representable value of its type (e.g. RTE_DIV_ROUND_UP(UINT32_MAX, 2)
     882                 :            :  *       on uint32_t wraps to 0).
     883                 :            :  */
     884                 :            : #define RTE_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
     885                 :            : 
     886                 :            : /**
     887                 :            :  * Checks if a pointer is aligned to a given power-of-two value
     888                 :            :  *
     889                 :            :  * @param ptr
     890                 :            :  *   The pointer whose alignment is to be checked
     891                 :            :  * @param align
     892                 :            :  *   The power-of-two value to which the ptr should be aligned
     893                 :            :  *
     894                 :            :  * @return
     895                 :            :  *   True(1) where the pointer is correctly aligned, false(0) otherwise
     896                 :            :  */
     897                 :            : static inline int
     898                 :            : rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
     899                 :            : {
     900   [ +  +  #  #  :         18 :         return ((uintptr_t)ptr & (align - 1)) == 0;
                   #  # ]
     901                 :            : }
     902                 :            : 
     903                 :            : /*********** Macros for compile type checks ********/
     904                 :            : 
     905                 :            : /* Workaround for toolchain issues with missing C11 macro in FreeBSD */
     906                 :            : #if !defined(static_assert) && !defined(__cplusplus)
     907                 :            : #define static_assert   _Static_assert
     908                 :            : #endif
     909                 :            : 
     910                 :            : /**
     911                 :            :  * Triggers an error at compilation time if the condition is true.
     912                 :            :  *
     913                 :            :  * The do { } while(0) exists to workaround a bug in clang (#55821)
     914                 :            :  * where it would not handle _Static_assert in a switch case.
     915                 :            :  */
     916                 :            : #define RTE_BUILD_BUG_ON(condition) do { static_assert(!(condition), #condition); } while (0)
     917                 :            : 
     918                 :            : /*********** Cache line related macros ********/
     919                 :            : 
     920                 :            : /** Cache line mask. */
     921                 :            : #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
     922                 :            : 
     923                 :            : /** Return the first cache-aligned value greater or equal to size. */
     924                 :            : #define RTE_CACHE_LINE_ROUNDUP(size) RTE_ALIGN_CEIL(size, RTE_CACHE_LINE_SIZE)
     925                 :            : 
     926                 :            : /** Cache line size in terms of log2 */
     927                 :            : #if RTE_CACHE_LINE_SIZE == 64
     928                 :            : #define RTE_CACHE_LINE_SIZE_LOG2 6
     929                 :            : #elif RTE_CACHE_LINE_SIZE == 128
     930                 :            : #define RTE_CACHE_LINE_SIZE_LOG2 7
     931                 :            : #else
     932                 :            : #error "Unsupported cache line size"
     933                 :            : #endif
     934                 :            : 
     935                 :            : /** Minimum Cache line size. */
     936                 :            : #define RTE_CACHE_LINE_MIN_SIZE 64
     937                 :            : 
     938                 :            : /** Force alignment to cache line. */
     939                 :            : #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
     940                 :            : 
     941                 :            : /** Force minimum cache line alignment. */
     942                 :            : #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
     943                 :            : 
     944                 :            : #define _RTE_CACHE_GUARD_HELPER2(unique) \
     945                 :            :         alignas(RTE_CACHE_LINE_SIZE) \
     946                 :            :         char cache_guard_ ## unique[RTE_CACHE_LINE_SIZE * RTE_CACHE_GUARD_LINES]
     947                 :            : #define _RTE_CACHE_GUARD_HELPER1(unique) _RTE_CACHE_GUARD_HELPER2(unique)
     948                 :            : /**
     949                 :            :  * Empty cache lines, to guard against false sharing-like effects
     950                 :            :  * on systems with a next-N-lines hardware prefetcher.
     951                 :            :  *
     952                 :            :  * Use as spacing between data accessed by different lcores,
     953                 :            :  * to prevent cache thrashing on hardware with speculative prefetching.
     954                 :            :  *
     955                 :            :  * Note: Although __COUNTER__ would be better for uniqueness,
     956                 :            :  * it is not yet (March 2026) part of the C standard,
     957                 :            :  * so compilation would fail when building in pedantic mode.
     958                 :            :  */
     959                 :            : #define RTE_CACHE_GUARD _RTE_CACHE_GUARD_HELPER1(__LINE__)
     960                 :            : 
     961                 :            : /*********** PA/IOVA type definitions ********/
     962                 :            : 
     963                 :            : /** Physical address */
     964                 :            : typedef uint64_t phys_addr_t;
     965                 :            : #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
     966                 :            : 
     967                 :            : /**
     968                 :            :  * IO virtual address type.
     969                 :            :  * When the physical addressing mode (IOVA as PA) is in use,
     970                 :            :  * the translation from an IO virtual address (IOVA) to a physical address
     971                 :            :  * is a direct mapping, i.e. the same value.
     972                 :            :  * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
     973                 :            :  */
     974                 :            : typedef uint64_t rte_iova_t;
     975                 :            : #define RTE_BAD_IOVA ((rte_iova_t)-1)
     976                 :            : 
     977                 :            : /*********** Structure alignment markers ********/
     978                 :            : 
     979                 :            : #ifndef RTE_TOOLCHAIN_MSVC
     980                 :            : 
     981                 :            : /** Generic marker for any place in a structure. */
     982                 :            : __extension__ typedef void    *RTE_MARKER[0];
     983                 :            : /** Marker for 1B alignment in a structure. */
     984                 :            : __extension__ typedef uint8_t  RTE_MARKER8[0];
     985                 :            : /** Marker for 2B alignment in a structure. */
     986                 :            : __extension__ typedef uint16_t RTE_MARKER16[0];
     987                 :            : /** Marker for 4B alignment in a structure. */
     988                 :            : __extension__ typedef uint32_t RTE_MARKER32[0];
     989                 :            : /** Marker for 8B alignment in a structure. */
     990                 :            : __extension__ typedef uint64_t RTE_MARKER64[0];
     991                 :            : 
     992                 :            : #endif
     993                 :            : 
     994                 :            : /*********** Macros for calculating min and max **********/
     995                 :            : 
     996                 :            : /**
     997                 :            :  * Macro to return the minimum of two numbers
     998                 :            :  */
     999                 :            : #define RTE_MIN(a, b) \
    1000                 :            :         __extension__ ({ \
    1001                 :            :                 typeof (a) _a_min = (a); \
    1002                 :            :                 typeof (b) _b_min = (b); \
    1003                 :            :                 _a_min < _b_min ? _a_min : _b_min; \
    1004                 :            :         })
    1005                 :            : 
    1006                 :            : /**
    1007                 :            :  * Macro to return the minimum of three numbers
    1008                 :            :  */
    1009                 :            : #define RTE_MIN3(a, b, c) \
    1010                 :            :         __extension__ ({ \
    1011                 :            :                 typeof (a) _a_min3 = (a); \
    1012                 :            :                 typeof (b) _b_min3 = (b); \
    1013                 :            :                 typeof (c) _c_min3 = (c); \
    1014                 :            :                 _a_min3 < _b_min3 ? \
    1015                 :            :                         (_a_min3 < _c_min3 ? _a_min3 : _c_min3) : \
    1016                 :            :                         (_b_min3 < _c_min3 ? _b_min3 : _c_min3); \
    1017                 :            :         })
    1018                 :            : 
    1019                 :            : /**
    1020                 :            :  * Macro to return the minimum of two numbers
    1021                 :            :  *
    1022                 :            :  * As opposed to RTE_MIN, it does not use temporary variables so it is not safe
    1023                 :            :  * if a or b is an expression. Yet it is guaranteed to be constant for use in
    1024                 :            :  * static_assert().
    1025                 :            :  */
    1026                 :            : #define RTE_MIN_T(a, b, t) \
    1027                 :            :         ((t)(a) < (t)(b) ? (t)(a) : (t)(b))
    1028                 :            : 
    1029                 :            : /**
    1030                 :            :  * Macro to return the maximum of two numbers
    1031                 :            :  */
    1032                 :            : #define RTE_MAX(a, b) \
    1033                 :            :         __extension__ ({ \
    1034                 :            :                 typeof (a) _a_max = (a); \
    1035                 :            :                 typeof (b) _b_max = (b); \
    1036                 :            :                 _a_max > _b_max ? _a_max : _b_max; \
    1037                 :            :         })
    1038                 :            : 
    1039                 :            : /**
    1040                 :            :  * Macro to return the maximum of three numbers
    1041                 :            :  */
    1042                 :            : #define RTE_MAX3(a, b, c) \
    1043                 :            :         __extension__ ({ \
    1044                 :            :                 typeof (a) _a_max3 = (a); \
    1045                 :            :                 typeof (b) _b_max3 = (b); \
    1046                 :            :                 typeof (c) _c_max3 = (c); \
    1047                 :            :                 _a_max3 > _b_max3 ? \
    1048                 :            :                         (_a_max3 > _c_max3 ? _a_max3 : _c_max3) : \
    1049                 :            :                         (_b_max3 > _c_max3 ? _b_max3 : _c_max3); \
    1050                 :            :         })
    1051                 :            : 
    1052                 :            : /**
    1053                 :            :  * Macro to return the maximum of two numbers
    1054                 :            :  *
    1055                 :            :  * As opposed to RTE_MAX, it does not use temporary variables so it is not safe
    1056                 :            :  * if a or b is an expression. Yet it is guaranteed to be constant for use in
    1057                 :            :  * static_assert().
    1058                 :            :  */
    1059                 :            : #define RTE_MAX_T(a, b, t) \
    1060                 :            :         ((t)(a) > (t)(b) ? (t)(a) : (t)(b))
    1061                 :            : 
    1062                 :            : /*********** Other general functions / macros ********/
    1063                 :            : 
    1064                 :            : #ifndef offsetof
    1065                 :            : /** Return the offset of a field in a structure. */
    1066                 :            : #define offsetof(TYPE, MEMBER)  __builtin_offsetof (TYPE, MEMBER)
    1067                 :            : #endif
    1068                 :            : 
    1069                 :            : /**
    1070                 :            :  * Return pointer to the wrapping struct instance.
    1071                 :            :  *
    1072                 :            :  * Example:
    1073                 :            :  *
    1074                 :            :  *  struct wrapper {
    1075                 :            :  *      ...
    1076                 :            :  *      struct child c;
    1077                 :            :  *      ...
    1078                 :            :  *  };
    1079                 :            :  *
    1080                 :            :  *  struct child *x = obtain(...);
    1081                 :            :  *  struct wrapper *w = container_of(x, struct wrapper, c);
    1082                 :            :  */
    1083                 :            : #ifndef container_of
    1084                 :            : #ifdef RTE_TOOLCHAIN_MSVC
    1085                 :            : #define container_of(ptr, type, member) \
    1086                 :            :                         ((type *)((uintptr_t)(ptr) - offsetof(type, member)))
    1087                 :            : #else
    1088                 :            : #define container_of(ptr, type, member) __extension__ ({                \
    1089                 :            :                         const typeof(((type *)0)->member) *_ptr = (ptr); \
    1090                 :            :                         __rte_unused type *_target_ptr =        \
    1091                 :            :                                 (type *)(ptr);                          \
    1092                 :            :                         (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
    1093                 :            :                 })
    1094                 :            : #endif
    1095                 :            : #endif
    1096                 :            : 
    1097                 :            : /** Swap two variables. */
    1098                 :            : #define RTE_SWAP(a, b) \
    1099                 :            :         __extension__ ({ \
    1100                 :            :                 typeof (a) _a = a; \
    1101                 :            :                 a = b; \
    1102                 :            :                 b = _a; \
    1103                 :            :         })
    1104                 :            : 
    1105                 :            : /**
    1106                 :            :  * Get the size of a field in a structure.
    1107                 :            :  *
    1108                 :            :  * @param type
    1109                 :            :  *   The type of the structure.
    1110                 :            :  * @param field
    1111                 :            :  *   The field in the structure.
    1112                 :            :  * @return
    1113                 :            :  *   The size of the field in the structure, in bytes.
    1114                 :            :  */
    1115                 :            : #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
    1116                 :            : 
    1117                 :            : #define _RTE_STR(x) #x
    1118                 :            : /** Take a macro value and get a string version of it */
    1119                 :            : #define RTE_STR(x) _RTE_STR(x)
    1120                 :            : 
    1121                 :            : /** Concatenate two tokens after expanding macros in both. */
    1122                 :            : #define _RTE_CONCAT2(a, b) a ## b
    1123                 :            : #define RTE_CONCAT(a, b) _RTE_CONCAT2(a, b)
    1124                 :            : 
    1125                 :            : /**
    1126                 :            :  * ISO C helpers to modify format strings using variadic macros.
    1127                 :            :  * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
    1128                 :            :  * An empty %s argument is appended to avoid a dangling comma.
    1129                 :            :  */
    1130                 :            : #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
    1131                 :            : #define RTE_FMT_HEAD(fmt, ...) fmt
    1132                 :            : #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
    1133                 :            : 
    1134                 :            : /** Mask value of type "tp" for the first "ln" bit set. */
    1135                 :            : #define RTE_LEN2MASK(ln, tp)    \
    1136                 :            :         ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
    1137                 :            : 
    1138                 :            : /** Number of elements in the array. */
    1139                 :            : #define RTE_DIM(a)      (sizeof (a) / sizeof ((a)[0]))
    1140                 :            : 
    1141                 :            : /**
    1142                 :            :  * Converts a numeric string to the equivalent uint64_t value.
    1143                 :            :  * As well as straight number conversion, also recognises the suffixes
    1144                 :            :  * k, m and g for kilobytes, megabytes and gigabytes respectively.
    1145                 :            :  *
    1146                 :            :  * If a negative number is passed in  i.e. a string with the first non-black
    1147                 :            :  * character being "-", zero is returned. Zero is also returned in the case of
    1148                 :            :  * an error with the strtoull call in the function.
    1149                 :            :  *
    1150                 :            :  * @param str
    1151                 :            :  *     String containing number to convert.
    1152                 :            :  * @return
    1153                 :            :  *     Number.
    1154                 :            :  */
    1155                 :            : uint64_t
    1156                 :            : rte_str_to_size(const char *str);
    1157                 :            : 
    1158                 :            : /**
    1159                 :            :  * @warning
    1160                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
    1161                 :            :  *
    1162                 :            :  * Converts the uint64_t value provided to a human-readable string.
    1163                 :            :  * It null-terminates the string, truncating the data if needed.
    1164                 :            :  * An optional unit (like "B") can be provided as a string. It will be
    1165                 :            :  * appended to the number, and a space will be inserted before the unit if needed.
    1166                 :            :  *
    1167                 :            :  * Sample outputs: (1) "use_iec" disabled, (2) "use_iec" enabled,
    1168                 :            :  *                 (3) "use_iec" enabled and "B" as unit.
    1169                 :            :  * 0 : "0", "0", "0 B"
    1170                 :            :  * 700 : "700", "700", "700 B"
    1171                 :            :  * 1000 : "1.00 k", "1000", "1000 B"
    1172                 :            :  * 1024 : "1.02 k", "1.00 ki", "1.00 kiB"
    1173                 :            :  * 21474836480 : "21.5 G", "20.0 Gi", "20.0 GiB"
    1174                 :            :  * 109951162777600 : "110 T", "100 Ti", "100 TiB"
    1175                 :            :  *
    1176                 :            :  * @param buf
    1177                 :            :  *     Buffer to write the string to.
    1178                 :            :  * @param buf_size
    1179                 :            :  *     Size of the buffer.
    1180                 :            :  * @param count
    1181                 :            :  *     Number to convert.
    1182                 :            :  * @param use_iec
    1183                 :            :  *     If true, use IEC units (1024-based), otherwise use SI units (1000-based).
    1184                 :            :  * @param unit
    1185                 :            :  *     Unit to append to the string (Like "B" for bytes). Can be NULL.
    1186                 :            :  * @return
    1187                 :            :  *     buf on success, NULL if the buffer is too small.
    1188                 :            :  */
    1189                 :            : __rte_experimental
    1190                 :            : char *
    1191                 :            : rte_size_to_str(char *buf, int buf_size, uint64_t count, bool use_iec, const char *unit);
    1192                 :            : 
    1193                 :            : /**
    1194                 :            :  * Function to terminate the application immediately, printing an error
    1195                 :            :  * message and returning the exit_code back to the shell.
    1196                 :            :  *
    1197                 :            :  * This function never returns
    1198                 :            :  *
    1199                 :            :  * @param exit_code
    1200                 :            :  *     The exit code to be returned by the application
    1201                 :            :  * @param format
    1202                 :            :  *     The format string to be used for printing the message. This can include
    1203                 :            :  *     printf format characters which will be expanded using any further parameters
    1204                 :            :  *     to the function.
    1205                 :            :  */
    1206                 :            : __rte_noreturn void
    1207                 :            : rte_exit(int exit_code, const char *format, ...)
    1208                 :            :         __rte_format_printf(2, 3);
    1209                 :            : 
    1210                 :            : #ifdef __cplusplus
    1211                 :            : }
    1212                 :            : #endif
    1213                 :            : 
    1214                 :            : #endif

Generated by: LCOV version 1.14