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

Generated by: LCOV version 1.14