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

Generated by: LCOV version 1.14