LCOV - code coverage report
Current view: top level - util/tmpl - fd_pool.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 119 129 92.2 %
Date: 2025-10-13 04:42:14 Functions: 564 30213 1.9 %

          Line data    Source code
       1             : /* Declare API for object pools of bounded run-time maximum size
       2             :    suitable for non-concurrent high performance persistent IPC usage.
       3             :    Typical usage:
       4             : 
       5             :      struct myele {
       6             :        ulong next; // Technically POOL_IDX_T POOL_NEXT, can go anywhere in struct,
       7             :                    // can be repurposed while acquired
       8             :                    // will be clobbered while in pool though
       9             :        ... user data ...
      10             :        ... structures with power-of-2 sizes have particularly good HPC
      11             :        ... Feng Shui
      12             :      }
      13             : 
      14             :      typedef struct myele myele_t;
      15             : 
      16             :      #define POOL_NAME mypool
      17             :      #define POOL_T    myele_t
      18             :      #include "tmpl/fd_pool.c"
      19             : 
      20             :    This will declare the following static inline APIs as a header only
      21             :    style library in the compilation unit:
      22             : 
      23             :      // align/footprint - Return the alignment/footprint required for a
      24             :      // memory region to be used as mypool that can hold up to max
      25             :      // elements.  footprint returns 0 if max is invalid (e.g. so large
      26             :      // that footprint would overflow ULONG_MAX or zero if mypool is
      27             :      // supposed to have a sentinel).
      28             :      //
      29             :      // new - Format a memory region pointed to by shmem into a mypool.
      30             :      // Assumes shmem points to a region with the required alignment and
      31             :      // footprint not in use by anything else.  Caller is not joined on
      32             :      // return.  Returns shmem on success or NULL on failure (e.g. shmem
      33             :      // or max are obviously bad).
      34             :      //
      35             :      // join - Join a mypool.  Assumes shpool points at a memory region
      36             :      // formatted as a mypool.  Returns a pointer in the caller's
      37             :      // address space to a memory region indexed [0,max) on success and
      38             :      // NULL on failure (e.g. shmem is obviously bad).  THIS IS NOT JUST
      39             :      // A SIMPLE CAST OF SHPOOL.
      40             :      //
      41             :      // leave - Leave a mypool.  Assumes join points to a current local
      42             :      // join.  Returns a pointer to the shared memory region the join on
      43             :      // success and NULL on failure.  THIS IS NOT JUST A SIMPLE CAST OF
      44             :      // JOIN.
      45             :      //
      46             :      // delete - Unformat a memory region used as mypool.  Assumes
      47             :      // shpool points to a formatted region with no current / future
      48             :      // joins.  Returns a pointer to the unformatted memory region.
      49             : 
      50             :      ulong     mypool_align    ( void      );
      51             :      ulong     mypool_footprint( ulong max );
      52             :      void *    mypool_new      ( void *    shmem, ulong max );
      53             :      myele_t * mypool_join     ( void *    shpool );
      54             :      void *    mypool_leave    ( myele_t * join   );
      55             :      void *    mypool_delete   ( void *    shpool );
      56             : 
      57             :      // Special values
      58             : 
      59             :      // All these assume join is a current local join to a mypool.  The
      60             :      // sentinel APIs are only created if POOL_SENTINEL is requested.
      61             : 
      62             :      ulong           mypool_idx_null          ( myele_t const * join ); // Index of the null element (not accessible)
      63             :                                                                         // infinite lifetime, ==(ulong)(POOL_IDX_T)~0UL
      64             :      myele_t *       mypool_ele_null          ( myele_t *       join ); // Location of the null element in the caller's address
      65             :                                                                         // space, infinite lifetime, ==NULL
      66             :      myele_t const * mypool_ele_null_const    ( myele_t const * join ); // Const correct version of above
      67             : 
      68             :      ulong           mypool_idx_sentinel      ( myele_t const * join ); // Index of the sentinel element (==0)
      69             :                                                                         // lifetime is the pool lifetime
      70             :      myele_t *       mypool_ele_sentinel      ( myele_t *       join ); // Location of the sentinel element in the caller's address
      71             :                                                                         // space, lifetime is the join lifetime (==join)
      72             :      myele_t const * mypool_ele_sentinel_const( myele_t const * join ); // Const correct version of above
      73             : 
      74             :      // Address space conversions
      75             : 
      76             :      int             mypool_idx_test ( myele_t const * join, ulong           idx ); // Returns 1 if idx is in [0,max) or is
      77             :                                                                                     // mypool_idx_null.  Returns zero otherwise.
      78             :      int             mypool_ele_test ( myele_t const * join, myele_t const * ele ); // Returns 1 if ele points to a pool ele or is
      79             :                                                                                     // NULL.  Returns zero otherwise.
      80             : 
      81             :      ulong           mypool_idx      ( myele_t const * join, myele_t const * ele ); // Returns idx associated with ele
      82             :                                                                                     // Assumes mypool_ele_test is 1
      83             :      myele_t *       mypool_ele      ( myele_t *       join, ulong           idx ); // Returns ele associated with idx
      84             :                                                                                     // Assumes mypool_idx_test is 1
      85             :                                                                                     // Lifetime is the local join
      86             :      myele_t const * mypool_ele_const( myele_t const * join, ulong           idx ); // Const correct version of above
      87             : 
      88             :      // Accessors
      89             : 
      90             :      ulong mypool_max ( myele_t const * join ); // Max elements in pool in [0,IDX_NULL], [1,IDX_NULL] if POOL_SENTINEL requested
      91             :      ulong mypool_free( myele_t const * join ); // Number of elements free, in [0,max]
      92             :      ulong mypool_used( myele_t const * join ); // Number of elements currently in use / acquired, in [0,max], includes sentinel
      93             :                                                 // if applicable, pool_free + pool_used == pool_max
      94             : 
      95             :      // Operations
      96             : 
      97             :      ulong     mypool_idx_acquire( myele_t * join                ); // Acquire an element from pool, assumes at least 1 free
      98             :                                                                     // Returns index of element, in [0,max) and, if applicable, not
      99             :                                                                     // sentinel index.  Lifetime is lesser of pool lifetime and
     100             :                                                                     // acquired element is released.  Will not return a currently
     101             :                                                                     // acquired element.
     102             :      void      mypool_idx_release( myele_t * join, ulong idx     ); // Release an element to pool by element idx, assumes element
     103             :                                                                     // currently acquired (e.g. not null index and, if applicable,
     104             :                                                                     // not sentinel index).  Element not acquired on return.
     105             : 
     106             :      myele_t * mypool_ele_acquire( myele_t * join                ); // Acquire an element from pool, assumes at least 1 free
     107             :                                                                     // Returns a pointer to element in caller's address space, not
     108             :                                                                     // NULL and, if applicable, not sentinel.  Lifetime is lesser
     109             :                                                                     // of join lifetime and acquired element is released.  Will not
     110             :                                                                     // return a currently acquired element
     111             :      void      mypool_ele_release( myele_t * join, myele_t * ele ); // Release an element to pool by local pointer, assumes element
     112             :                                                                     // currently acquired (e.g. not NULL and, if applicable, not
     113             :                                                                     // sentinel).  Element not acquired on return.
     114             : 
     115             :      You can do this as often as you like in a compilation unit to get
     116             :      different types of pools.  Since it is all static inline, it is
     117             :      fine to do this in a header too.  Additional options to fine tune
     118             :      this are detailed below. */
     119             : 
     120             : #include "../bits/fd_bits.h"
     121             : 
     122             : #ifndef POOL_NAME
     123             : #error "Define POOL_NAME"
     124             : #endif
     125             : 
     126             : /* A POOL_T should be something reasonable to shallow copy
     127             :    with the fields described above. */
     128             : 
     129             : #ifndef POOL_T
     130             : #error "Define POOL_T"
     131             : #endif
     132             : 
     133             : /* POOL_NEXT is the name of the field the pool will clobber for
     134             :    elements currently not allocated. */
     135             : 
     136             : #ifndef POOL_NEXT
     137    30126682 : #define POOL_NEXT next
     138             : #endif
     139             : 
     140             : /* POOL_IDX_T is the type of the POOL_NEXT field.  Should be an unsigned
     141             :    integer type.  The maximum value this type can have is also the
     142             :    maximum number of elements that can be in a pool. */
     143             : 
     144             : #ifndef POOL_IDX_T
     145             : #define POOL_IDX_T ulong
     146             : #endif
     147             : 
     148             : /* If POOL_SENTINEL is non-zero, the pool will reserve element idx
     149             :    0 as a sentinel element (will be considered as always allocated).
     150             :    Setting this also implies that the max for a pool should be at least
     151             :    1. */
     152             : 
     153             : #ifndef POOL_SENTINEL
     154             : #define POOL_SENTINEL 0
     155             : #endif
     156             : 
     157             : /* 0 - local use only
     158             :    1 - library header declaration
     159             :    2 - library implementation */
     160             : 
     161             : #ifndef POOL_IMPL_STYLE
     162             : #define POOL_IMPL_STYLE 0
     163             : #endif
     164             : 
     165             : /* POOL_MAGIC is the magic number that should be used to identify
     166             :    pools of this type in shared memory.  Should be non-zero. */
     167             : 
     168             : #ifndef POOL_MAGIC
     169        7911 : #define POOL_MAGIC (0xF17EDA2CE7900100UL) /* Firedancer pool ver 0 */
     170             : #endif
     171             : 
     172             : #if FD_TMPL_USE_HANDHOLDING
     173             : #include "../log/fd_log.h"
     174             : #endif
     175             : 
     176             : /* Implementation *****************************************************/
     177             : 
     178 26984956469 : #define POOL_(n) FD_EXPAND_THEN_CONCAT3(POOL_NAME,_,n)
     179             : 
     180    83702205 : #define POOL_IDX_NULL ((ulong)((POOL_IDX_T)~0UL))
     181             : 
     182             : #if POOL_IMPL_STYLE==0 || POOL_IMPL_STYLE==1 /* need structures and inlines */
     183             : 
     184             : struct POOL_(private) {
     185             : 
     186             :   /* This point is POOL_ALIGN aligned */
     187             :   ulong magic;    /* ==POOL_MAGIC */
     188             :   ulong max;      /* Max elements in pool, in [POOL_SENTINEL,POOL_IDX_NULL] */
     189             :   ulong free;     /* Num elements in pool available, in [0,max] */
     190             :   ulong free_top; /* Free stack top, POOL_IDX_NULL no elements currently in pool */
     191             : 
     192             :   /* Padding to POOL_ALIGN here */
     193             : 
     194             :   /* max POOL_T elements here, join points to element 0 */
     195             :   /* element 0 will be the sentinel if POOL_SENTINEL is true */
     196             : 
     197             :   /* Padding to POOL_ALIGN here */
     198             : };
     199             : 
     200             : typedef struct POOL_(private) POOL_(private_t);
     201             : 
     202             : FD_PROTOTYPES_BEGIN
     203             : 
     204             : /* Private APIs *******************************************************/
     205             : 
     206             : /* pool_private_meta_footprint returns the number of bytes used by a
     207             :    pool metadata region */
     208             : 
     209             : FD_FN_CONST static inline ulong
     210 11364755079 : POOL_(private_meta_footprint)( void ) {
     211 11364755079 :   return fd_ulong_align_up( sizeof(POOL_(private_t)), fd_ulong_max( alignof(POOL_T), 128UL )  );
     212 11364755079 : }
     213             : 
     214             : /* pool_private_meta returns a pointer in the caller's address space to
     215             :    a pool metadata region.  pool_private_meta_const is a const correct
     216             :    version. */
     217             : 
     218             : FD_FN_CONST static inline POOL_(private_t) *
     219  2687437289 : POOL_(private_meta)( POOL_T * join ) {
     220  2687437289 :   return (POOL_(private_t) *)(((ulong)join) - POOL_(private_meta_footprint)());
     221  2687437289 : }
     222             : 
     223             : FD_FN_CONST static inline POOL_(private_t) const *
     224  8677257457 : POOL_(private_meta_const)( POOL_T const * join ) {
     225  8677257457 :   return (POOL_(private_t) const *)(((ulong)join) - POOL_(private_meta_footprint)());
     226  8677257457 : }
     227             : 
     228             : /* Public APIS ********************************************************/
     229             : 
     230             : FD_FN_CONST static inline ulong
     231           9 : POOL_(max_for_footprint)( ulong footprint ) {
     232           9 :   ulong meta_footprint = POOL_(private_meta_footprint)();
     233           9 :   if( FD_UNLIKELY( footprint <= meta_footprint ) ) return 0UL;
     234           9 :   return fd_ulong_min( (footprint - meta_footprint) / sizeof(POOL_T), POOL_IDX_NULL );
     235           9 : }
     236             : 
     237             : FD_FN_CONST static inline ulong
     238       86097 : POOL_(align)( void ) {
     239       86097 :   return fd_ulong_max( alignof(POOL_T), 128UL );
     240       86097 : }
     241             : 
     242             : FD_FN_CONST static inline ulong
     243       33270 : POOL_(footprint)( ulong max ) {
     244             : # if POOL_SENTINEL
     245         117 :   if( FD_UNLIKELY( !max ) ) return 0UL;
     246         111 : # endif
     247       33264 :   ulong align          = POOL_(align)();
     248       33264 :   ulong meta_footprint = POOL_(private_meta_footprint)(); /* Multiple of align */
     249         111 :   ulong data_footprint = fd_ulong_align_up( sizeof(POOL_T)*max, align );
     250       33264 :   ulong thresh         = fd_ulong_min( (ULONG_MAX - align - meta_footprint + 1UL) / sizeof(POOL_T), POOL_IDX_NULL );
     251         111 :   return fd_ulong_if( max > thresh, 0UL, meta_footprint + data_footprint );
     252       33270 : }
     253             : 
     254             : FD_PROTOTYPES_END
     255             : 
     256             : #endif
     257             : 
     258             : FD_PROTOTYPES_BEGIN
     259             : 
     260             : #if POOL_IMPL_STYLE==1 /* need prototypes */
     261             : 
     262             : FD_FN_UNUSED void * /* Work around -Winline */
     263             : POOL_(new)( void * shmem,
     264             :             ulong  max );
     265             : 
     266             : FD_FN_UNUSED POOL_T *
     267             : POOL_(join)( void * shpool );
     268             : 
     269             : FD_FN_UNUSED void
     270             : POOL_(reset)( POOL_T * join );
     271             : 
     272             : #else /* need implementations */
     273             : 
     274             : #if POOL_IMPL_STYLE==0 /* local only */
     275             : #define POOL_IMPL_STATIC FD_FN_UNUSED static
     276             : #else
     277             : #define POOL_IMPL_STATIC
     278             : #endif
     279             : 
     280             : POOL_IMPL_STATIC void * /* Work around -Winline */
     281             : POOL_(new)( void * shmem,
     282        7929 :             ulong  max ) {
     283             : 
     284        7929 :   if( FD_UNLIKELY( !shmem )                                               ) return NULL;
     285        7926 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, POOL_(align)() ) ) ) return NULL;
     286        7923 :   if( FD_UNLIKELY( !POOL_(footprint)( max ) )                             ) return NULL;
     287             : 
     288        7914 :   POOL_T *           join = (POOL_T *)(((ulong)shmem) + POOL_(private_meta_footprint)());
     289        7914 :   POOL_(private_t) * meta = POOL_(private_meta)( join );
     290             : 
     291        7914 :   meta->max  = max;
     292        7914 :   meta->free = max;
     293             : 
     294        7914 :   if( FD_UNLIKELY( !max ) ) meta->free_top = POOL_IDX_NULL; /* Not reachable if POOL_SENTINEL set (footprint test above fails) */
     295        7908 :   else {
     296        7908 :     meta->free_top = 0UL;
     297    37752894 :     for( ulong idx=1UL; idx<max; idx++ ) join[ idx-1UL ].POOL_NEXT = (POOL_IDX_T)idx;
     298        7908 :     join[ max-1UL ].POOL_NEXT = (POOL_IDX_T)POOL_IDX_NULL;
     299             : 
     300             : #   if POOL_SENTINEL
     301             :     meta->free_top = 1UL;
     302             :     meta->free--;
     303          27 :     join[ 0 ].POOL_NEXT = (POOL_IDX_T)POOL_IDX_NULL;
     304             : #   endif
     305        7908 :   }
     306             : 
     307        7914 :   FD_COMPILER_MFENCE();
     308        7914 :   FD_VOLATILE( meta->magic ) = (POOL_MAGIC);
     309        7914 :   FD_COMPILER_MFENCE();
     310             : 
     311        7914 :   return shmem;
     312        7923 : }
     313             : 
     314             : POOL_IMPL_STATIC void
     315           0 : POOL_(reset)( POOL_T * join ) {
     316           0 :   POOL_(private_t) * meta = POOL_(private_meta)( join );
     317             : 
     318           0 :   meta->free = meta->max;
     319             : 
     320           0 :   if( FD_UNLIKELY( !meta->max ) ) meta->free_top = POOL_IDX_NULL; /* Not reachable if POOL_SENTINEL set (footprint test above fails) */
     321           0 :   else {
     322           0 :     meta->free_top = 0UL;
     323           0 :     for( ulong idx=1UL; idx<meta->max; idx++ ) join[ idx-1UL ].POOL_NEXT = (POOL_IDX_T)idx;
     324           0 :     join[ meta->max-1UL ].POOL_NEXT = (POOL_IDX_T)POOL_IDX_NULL;
     325             : 
     326             : #   if POOL_SENTINEL
     327             :     meta->free_top = 1UL;
     328             :     meta->free--;
     329             :     join[ 0 ].POOL_NEXT = (POOL_IDX_T)POOL_IDX_NULL;
     330             : #   endif
     331           0 :   }
     332           0 : }
     333             : 
     334             : POOL_IMPL_STATIC POOL_T *
     335       10782 : POOL_(join)( void * shpool ) {
     336       10782 :   if( FD_UNLIKELY( !shpool                                               ) ) return NULL;
     337       10779 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shpool, POOL_(align)() ) ) ) return NULL;
     338             : 
     339       10776 :   POOL_T *           join = (POOL_T *)(((ulong)shpool) + POOL_(private_meta_footprint)());
     340       10776 :   POOL_(private_t) * meta = POOL_(private_meta)( join );
     341             : 
     342       10776 :   if( FD_UNLIKELY( FD_VOLATILE_CONST( meta->magic )!=(POOL_MAGIC) ) ) return NULL;
     343             : 
     344       10773 :   return join;
     345       10776 : }
     346             : 
     347             : #endif
     348             : 
     349             : FD_PROTOTYPES_END
     350             : 
     351             : FD_PROTOTYPES_BEGIN
     352             : 
     353             : #if POOL_IMPL_STYLE==0 || POOL_IMPL_STYLE==1 /* need structures and inlines */
     354             : 
     355             : FD_FN_CONST static inline void *
     356        4812 : POOL_(leave)( POOL_T * join ) {
     357        4812 :   if( FD_UNLIKELY( !join ) ) return NULL;
     358        4809 :   return (void *)(((ulong)join) - POOL_(private_meta_footprint)());
     359        4812 : }
     360             : 
     361             : static inline void *
     362        3567 : POOL_(delete)( void * shpool ) {
     363        3567 :   if( FD_UNLIKELY( !shpool ) ) return NULL;
     364        3564 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shpool, POOL_(align)() ) ) ) return NULL;
     365             : 
     366        3561 :   POOL_T *           join = (POOL_T *)(((ulong)shpool) + POOL_(private_meta_footprint)());
     367        3561 :   POOL_(private_t) * meta = POOL_(private_meta)( join );
     368             : 
     369        3561 :   if( FD_UNLIKELY( FD_VOLATILE_CONST( meta->magic )!=(POOL_MAGIC) ) ) return NULL;
     370             : 
     371        3558 :   FD_COMPILER_MFENCE();
     372        3558 :   FD_VOLATILE( meta->magic ) = 0UL;
     373        3558 :   FD_COMPILER_MFENCE();
     374             : 
     375        3558 :   return shpool;
     376        3561 : }
     377             : 
     378             : /* Special values */
     379             : 
     380       15210 : FD_FN_CONST static inline ulong          POOL_(idx_null)          ( POOL_T const * join ) { (void)join; return POOL_IDX_NULL; }
     381           3 : FD_FN_CONST static inline POOL_T *       POOL_(ele_null)          ( POOL_T       * join ) { (void)join; return NULL;          }
     382           3 : FD_FN_CONST static inline POOL_T const * POOL_(ele_null_const)    ( POOL_T const * join ) { (void)join; return NULL;          }
     383             : 
     384             : #if POOL_SENTINEL
     385           3 : FD_FN_CONST static inline ulong          POOL_(idx_sentinel)      ( POOL_T const * join ) { (void)join; return 0UL;  }
     386           9 : FD_FN_CONST static inline POOL_T *       POOL_(ele_sentinel)      ( POOL_T *       join ) { return join; }
     387           3 : FD_FN_CONST static inline POOL_T const * POOL_(ele_sentinel_const)( POOL_T const * join ) { return join; }
     388             : #endif
     389             : 
     390             : /* Address space conversion */
     391             : 
     392             : FD_FN_PURE static inline int
     393             : POOL_(idx_test)( POOL_T const * join,
     394    77652519 :                  ulong          idx ) {
     395    77652519 :   ulong max = POOL_(private_meta_const)( join )->max;
     396    77652519 :   return (idx<max) | (idx==POOL_IDX_NULL);
     397    77652519 : }
     398             : 
     399             : FD_FN_PURE static inline int
     400             : POOL_(ele_test)( POOL_T const * join,
     401  6972185205 :                  POOL_T const * ele ) {
     402  6972185205 :   ulong max = POOL_(private_meta_const)( join )->max;
     403  6972185205 :   ulong idx = (ulong)(ele - join);
     404  6972185205 :   FD_COMPILER_FORGET( idx ); /* prevent compiler from optimizing out alignment test */
     405  6972185205 :   return (!ele) | ((idx<max) & ((ulong)ele==((ulong)join+(idx*sizeof(POOL_T))))); /* last test checks alignment */
     406  6972185205 : }
     407             : 
     408             : FD_FN_CONST static inline ulong
     409             : POOL_(idx)( POOL_T const * join,
     410   152331267 :             POOL_T const * ele ) {
     411             : # if FD_TMPL_USE_HANDHOLDING
     412             :   if( FD_UNLIKELY( !POOL_(ele_test)( join, ele ) ) ) FD_LOG_CRIT(( "no such element" ));
     413             : # endif
     414   152331267 :   return ele ? (ulong)(ele-join) : POOL_IDX_NULL;
     415   152331267 : }
     416             : 
     417             : FD_FN_CONST static inline POOL_T *
     418             : POOL_(ele)( POOL_T *   join,
     419     2991177 :             ulong      idx ) {
     420             : # if FD_TMPL_USE_HANDHOLDING
     421             :   if( FD_UNLIKELY( !POOL_(idx_test)( join, idx ) ) ) FD_LOG_CRIT(( "no such index" ));
     422             : # endif
     423     2991177 :   return (idx==POOL_IDX_NULL) ? NULL : (join + idx);
     424     2991177 : }
     425             : 
     426             : FD_FN_CONST static inline POOL_T const *
     427             : POOL_(ele_const)( POOL_T const *   join,
     428     2988807 :                   ulong            idx ) {
     429             : # if FD_TMPL_USE_HANDHOLDING
     430             :   if( FD_UNLIKELY( !POOL_(idx_test)( join, idx ) ) ) FD_LOG_CRIT(( "no such index" ));
     431             : # endif
     432     2988807 :   return (idx==POOL_IDX_NULL) ? NULL : (join + idx);
     433     2988807 : }
     434             : 
     435             : /* Accessors */
     436             : 
     437     3959148 : FD_FN_PURE static inline ulong POOL_(max) ( POOL_T const * join ) { return POOL_(private_meta_const)( join )->max;  }
     438   704669616 : FD_FN_PURE static inline ulong POOL_(free)( POOL_T const * join ) { return POOL_(private_meta_const)( join )->free; }
     439             : 
     440             : FD_FN_PURE static inline ulong
     441   918790969 : POOL_(used)( POOL_T const * join ) {
     442   918790969 :   POOL_(private_t) const * meta = POOL_(private_meta_const)( join );
     443   918790969 :   return meta->max - meta->free;
     444   918790969 : }
     445             : 
     446             : /* Operations */
     447             : 
     448             : static inline ulong
     449  1343712205 : POOL_(idx_acquire)( POOL_T * join ) {
     450  1343712205 :   POOL_(private_t) * meta = POOL_(private_meta)( join );
     451             : # if FD_TMPL_USE_HANDHOLDING
     452             :   if( FD_UNLIKELY( !meta->free ) ) FD_LOG_CRIT(( "pool is full" ));
     453             : # endif
     454  1343712205 :   ulong idx = meta->free_top;
     455  1343712205 :   meta->free_top = (ulong)join[ idx ].POOL_NEXT;
     456  1343712205 :   meta->free--;
     457  1343712205 :   return idx;
     458  1343712205 : }
     459             : 
     460             : static inline void
     461             : POOL_(idx_release)( POOL_T * join,
     462  1343702833 :                     ulong    idx ) {
     463  1343702833 :   POOL_(private_t) * meta = POOL_(private_meta)( join );
     464             : # if FD_TMPL_USE_HANDHOLDING
     465             :   if( FD_UNLIKELY( (meta->max<=idx) | (idx==POOL_IDX_NULL) ) ) FD_LOG_CRIT(( "invalid index" ));
     466             : # if POOL_SENTINEL
     467             :   if( FD_UNLIKELY( POOL_(idx_sentinel)( join )==idx ) ) FD_LOG_CRIT(( "cannot releaes sentinel" ));
     468             :   if( FD_UNLIKELY( meta->free>=meta->max-1 ) ) FD_LOG_CRIT(( "pool is empty" ));
     469             : # else
     470             :   if( FD_UNLIKELY( meta->free>=meta->max ) ) FD_LOG_CRIT(( "pool is empty" ));
     471             : # endif
     472             : # endif
     473  1343702833 :   join[ idx ].POOL_NEXT = (POOL_IDX_T)meta->free_top;
     474  1343702833 :   meta->free_top = idx;
     475  1343702833 :   meta->free++;
     476  1343702833 : }
     477             : 
     478   331376017 : static inline POOL_T * POOL_(ele_acquire)( POOL_T * join               ) { return join + POOL_(idx_acquire)( join ); }
     479   317869105 : static inline void     POOL_(ele_release)( POOL_T * join, POOL_T * ele ) { POOL_(idx_release)( join, (ulong)(ele - join) );   }
     480             : 
     481             : /* TODO: consider zeroing out pool mem on new? */
     482             : 
     483             : /* TODO: consider providing element size and alignment as metadata? */
     484             : 
     485             : /* TODO: consider lockfree concurrent version with ABA tagged free_top? */
     486             : 
     487             : /* TODO: consider a verify and rebuild that work via most sig bit of
     488             :    POOL_NEXT for element marking (the POOL_NEXT field in the structure
     489             :    would have to become dedicated to the pool though). */
     490             : 
     491             : #endif
     492             : 
     493             : FD_PROTOTYPES_END
     494             : 
     495             : #undef POOL_IDX_NULL
     496             : #undef POOL_
     497             : 
     498             : #undef POOL_MAGIC
     499             : #undef POOL_SENTINEL
     500             : #undef POOL_IDX_T
     501             : #undef POOL_NEXT
     502             : #undef POOL_IMPL_STYLE
     503             : #undef POOL_T
     504             : #undef POOL_NAME

Generated by: LCOV version 1.14