LCOV - code coverage report
Current view: top level - flamenco/accdb - fd_vinyl_req_pool.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 121 0.0 %
Date: 2026-01-23 05:02:40 Functions: 0 8 0.0 %

          Line data    Source code
       1             : #include "fd_vinyl_req_pool.h"
       2             : 
       3             : #define SET_NAME used
       4             : #include "../../util/tmpl/fd_set_dynamic.c"
       5             : 
       6             : FD_STATIC_ASSERT( alignof(fd_vinyl_req_pool_t)<=FD_VINYL_REQ_POOL_ALIGN, align );
       7             : FD_STATIC_ASSERT( alignof(ulong)              <=FD_VINYL_REQ_POOL_ALIGN, align );
       8             : FD_STATIC_ASSERT( alignof(fd_vinyl_key_t)     <=FD_VINYL_REQ_POOL_ALIGN, align );
       9             : FD_STATIC_ASSERT( alignof(fd_vinyl_comp_t)    <=FD_VINYL_REQ_POOL_ALIGN, align );
      10             : 
      11             : ulong
      12           0 : fd_vinyl_req_pool_align( void ) {
      13           0 :   return FD_VINYL_REQ_POOL_ALIGN;
      14           0 : }
      15             : 
      16             : ulong
      17             : fd_vinyl_req_pool_footprint( ulong batch_max,
      18           0 :                              ulong batch_key_max ) {
      19             :   /* No point in creating empty pools */
      20           0 :   if( FD_UNLIKELY( !batch_max || !batch_key_max ) ) return 0UL;
      21             : 
      22             :   /* Check for integer overflow / oversized params */
      23           0 :   ulong req_max;
      24           0 :   if( FD_UNLIKELY( __builtin_umull_overflow( batch_max, batch_key_max, &req_max ) ) ) return 0UL;
      25           0 :   ulong ignored_;
      26           0 :   if( FD_UNLIKELY( __builtin_umull_overflow( req_max, 1024UL, &ignored_ ) ) ) return 0UL;
      27             : 
      28           0 :   ulong l = FD_LAYOUT_INIT;
      29           0 :   l = FD_LAYOUT_APPEND( l, FD_VINYL_REQ_POOL_ALIGN, sizeof(fd_vinyl_req_pool_t)     );
      30           0 :   l = FD_LAYOUT_APPEND( l, FD_VINYL_REQ_POOL_ALIGN, batch_max*sizeof(ulong)         );
      31           0 :   l = FD_LAYOUT_APPEND( l, FD_VINYL_REQ_POOL_ALIGN, used_footprint( batch_max )     );
      32           0 :   l = FD_LAYOUT_APPEND( l, FD_VINYL_REQ_POOL_ALIGN, req_max*sizeof(fd_vinyl_key_t)  );
      33           0 :   l = FD_LAYOUT_APPEND( l, FD_VINYL_REQ_POOL_ALIGN, req_max*sizeof(ulong)           );
      34           0 :   l = FD_LAYOUT_APPEND( l, FD_VINYL_REQ_POOL_ALIGN, req_max*sizeof(schar)           );
      35           0 :   l = FD_LAYOUT_APPEND( l, FD_VINYL_REQ_POOL_ALIGN,         sizeof(fd_vinyl_comp_t) );
      36           0 :   return FD_LAYOUT_FINI( l, FD_VINYL_REQ_POOL_ALIGN );
      37           0 : }
      38             : 
      39             : void *
      40             : fd_vinyl_req_pool_new( void * shmem,
      41             :                        ulong  batch_max,
      42           0 :                        ulong  batch_key_max ) {
      43           0 :   if( FD_UNLIKELY( !fd_vinyl_req_pool_footprint( batch_max, batch_key_max ) ) ) {
      44           0 :     FD_LOG_WARNING(( "invalid req_pool params: batch_max=%lu batch_key_max=%lu",
      45           0 :                      batch_max, batch_key_max ));
      46           0 :     return NULL;
      47           0 :   }
      48             : 
      49           0 :   if( FD_UNLIKELY( !shmem ) ) {
      50           0 :     FD_LOG_WARNING(( "NULL shmem" ));
      51           0 :     return NULL;
      52           0 :   }
      53           0 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, FD_VINYL_REQ_POOL_ALIGN ) ) ) {
      54           0 :     FD_LOG_WARNING(( "misaligned shmem" ));
      55           0 :     return NULL;
      56           0 :   }
      57           0 :   if( FD_UNLIKELY( !used_footprint( batch_max ) ) ) {
      58           0 :     FD_LOG_WARNING(( "invalid batch_max parameter" ));
      59           0 :     return NULL;
      60           0 :   }
      61             : 
      62           0 :   ulong req_max = batch_max*batch_key_max;
      63             : 
      64           0 :   FD_SCRATCH_ALLOC_INIT( l, shmem );
      65           0 :   fd_vinyl_req_pool_t * pool       = FD_SCRATCH_ALLOC_APPEND( l, FD_VINYL_REQ_POOL_ALIGN, sizeof(fd_vinyl_req_pool_t)     );
      66           0 :   ulong *               free       = FD_SCRATCH_ALLOC_APPEND( l, FD_VINYL_REQ_POOL_ALIGN, batch_max*sizeof(ulong)         );
      67           0 :   void *                used_mem   = FD_SCRATCH_ALLOC_APPEND( l, FD_VINYL_REQ_POOL_ALIGN, used_footprint( batch_max )     );
      68           0 :   fd_vinyl_key_t *      key0       = FD_SCRATCH_ALLOC_APPEND( l, FD_VINYL_REQ_POOL_ALIGN, req_max*sizeof(fd_vinyl_key_t)  );
      69           0 :   ulong *               val_gaddr0 = FD_SCRATCH_ALLOC_APPEND( l, FD_VINYL_REQ_POOL_ALIGN, req_max*sizeof(ulong)           );
      70           0 :   schar *               err0       = FD_SCRATCH_ALLOC_APPEND( l, FD_VINYL_REQ_POOL_ALIGN, req_max*sizeof(schar)           );
      71           0 :   fd_vinyl_comp_t *     comp0      = FD_SCRATCH_ALLOC_APPEND( l, FD_VINYL_REQ_POOL_ALIGN,         sizeof(fd_vinyl_comp_t) );
      72           0 :   ulong obj1 = FD_SCRATCH_ALLOC_FINI( l, FD_VINYL_REQ_POOL_ALIGN );
      73           0 :   FD_TEST( obj1-(ulong)shmem == fd_vinyl_req_pool_footprint( batch_max, batch_key_max ) );
      74             : 
      75           0 :   for( ulong i=0UL; i<batch_max; i++ ) free[i] = i;
      76             : 
      77           0 :   ulong * used_set = used_join( used_new( used_mem, batch_max ) );
      78           0 :   if( FD_UNLIKELY( !used_set ) ) FD_LOG_CRIT(( "set_dynamic_new failed" ));
      79             : 
      80           0 :   *pool = (fd_vinyl_req_pool_t){
      81           0 :     .batch_max     = batch_max,
      82           0 :     .batch_key_max = batch_key_max,
      83           0 :     .free_off      = (ulong)free - (ulong)shmem,
      84           0 :     .free_cnt      = batch_max,
      85           0 :     .used_off      = (ulong)used_set - (ulong)shmem,
      86             : 
      87           0 :     .key_off       = (ulong)key0       - (ulong)shmem,
      88           0 :     .val_gaddr_off = (ulong)val_gaddr0 - (ulong)shmem,
      89           0 :     .err_off       = (ulong)err0       - (ulong)shmem,
      90           0 :     .comp_off      = (ulong)comp0      - (ulong)shmem
      91           0 :   };
      92             : 
      93           0 :   FD_COMPILER_MFENCE();
      94           0 :   pool->magic = FD_VINYL_REQ_POOL_MAGIC;
      95           0 :   FD_COMPILER_MFENCE();
      96             : 
      97           0 :   return pool;
      98           0 : }
      99             : 
     100             : fd_vinyl_req_pool_t *
     101           0 : fd_vinyl_req_pool_join( void * shmem ) {
     102             : 
     103           0 :   if( FD_UNLIKELY( !shmem || !fd_ulong_is_aligned( (ulong)shmem, FD_VINYL_REQ_POOL_ALIGN ) ) ) {
     104           0 :     FD_LOG_WARNING(( "invalid shmem" ));
     105           0 :     return NULL;
     106           0 :   }
     107             : 
     108           0 :   fd_vinyl_req_pool_t * pool = (fd_vinyl_req_pool_t *)shmem;
     109           0 :   if( FD_UNLIKELY( pool->magic!=FD_VINYL_REQ_POOL_MAGIC ) ) {
     110           0 :     FD_LOG_WARNING(( "bad magic" ));
     111           0 :     return NULL;
     112           0 :   }
     113             : 
     114           0 :   return pool;
     115           0 : }
     116             : 
     117             : void *
     118           0 : fd_vinyl_req_pool_leave( fd_vinyl_req_pool_t * pool ) {
     119           0 :   return (void *)pool;
     120           0 : }
     121             : 
     122             : void *
     123           0 : fd_vinyl_req_pool_delete( void * shmem ) {
     124             : 
     125           0 :   if( FD_UNLIKELY( !shmem || !fd_ulong_is_aligned( (ulong)shmem, FD_VINYL_REQ_POOL_ALIGN ) ) ) {
     126           0 :     FD_LOG_WARNING(( "invalid shmem" ));
     127           0 :     return NULL;
     128           0 :   }
     129             : 
     130           0 :   fd_vinyl_req_pool_t * pool = (fd_vinyl_req_pool_t *)shmem;
     131           0 :   pool->magic = 0UL;
     132             : 
     133           0 :   return shmem;
     134           0 : }
     135             : 
     136             : ulong
     137           0 : fd_vinyl_req_pool_acquire( fd_vinyl_req_pool_t * pool ) {
     138             : 
     139           0 :   if( FD_UNLIKELY( !pool->free_cnt ) ) {
     140           0 :     FD_LOG_CRIT(( "Cannot acquire request batch: batch_max %lu exceeded",
     141           0 :                   pool->batch_max ));
     142           0 :   }
     143             : 
     144           0 :   ulong * free = (ulong *)( (ulong)pool + pool->free_off );
     145           0 :   ulong * used = (ulong *)( (ulong)pool + pool->used_off );
     146             : 
     147           0 :   ulong idx = free[ --pool->free_cnt ];
     148           0 :   if( FD_UNLIKELY( used_test( used, idx ) ) ) {
     149           0 :     FD_LOG_CRIT(( "use after free detected" ));
     150           0 :   }
     151           0 :   used_insert( used, idx );
     152             : 
     153           0 :   return idx;
     154           0 : }
     155             : 
     156             : void
     157             : fd_vinyl_req_pool_release( fd_vinyl_req_pool_t * pool,
     158           0 :                            ulong                 idx ) {
     159             : 
     160           0 :   ulong * free = (ulong *)( (ulong)pool + pool->free_off );
     161           0 :   ulong * used = (ulong *)( (ulong)pool + pool->used_off );
     162             : 
     163           0 :   if( FD_UNLIKELY( idx >= pool->batch_max ) ) {
     164           0 :     FD_LOG_CRIT(( "invalid batch_idx %lu (batch_max %lu)",
     165           0 :                   idx, pool->batch_max ));
     166           0 :   }
     167           0 :   if( FD_UNLIKELY( pool->free_cnt>=pool->batch_max ) ) {
     168           0 :     FD_LOG_CRIT(( "double free detected" ));
     169           0 :   }
     170           0 :   if( FD_UNLIKELY( !used_test( used, idx ) ) ) {
     171           0 :     FD_LOG_CRIT(( "double free detected" ));
     172           0 :   }
     173             : 
     174           0 :   used_remove( used, idx );
     175           0 :   free[ pool->free_cnt++ ] = idx;
     176           0 : }

Generated by: LCOV version 1.14