LCOV - code coverage report
Current view: top level - tango/mcache - fd_mcache.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 116 116 100.0 %
Date: 2025-08-05 05:04:49 Functions: 13 13 100.0 %

          Line data    Source code
       1             : #include "fd_mcache_private.h"
       2             : 
       3             : ulong
       4       10242 : fd_mcache_align( void ) {
       5       10242 :   return FD_MCACHE_ALIGN;
       6       10242 : }
       7             : 
       8             : ulong
       9             : fd_mcache_footprint( ulong depth,
      10     3017589 :                      ulong app_sz ) {
      11             : 
      12     3017589 :   if( FD_UNLIKELY( depth<FD_MCACHE_BLOCK                  ) ) return 0UL; /* too small depth */
      13     3014481 :   if( FD_UNLIKELY( depth>ULONG_MAX/sizeof(fd_frag_meta_t) ) ) return 0UL; /* too large depth */
      14     3014478 :   if( FD_UNLIKELY( !fd_ulong_is_pow2( depth )             ) ) return 0UL; /* non-power-of-two depth */
      15       50253 :   ulong meta_footprint = depth*sizeof( fd_frag_meta_t ); /* no overflow */
      16       50253 :   if( FD_UNLIKELY( fd_ulong_align_up( meta_footprint, FD_MCACHE_ALIGN )<meta_footprint ) ) return 0UL; /* too large depth */
      17       50253 :   meta_footprint = fd_ulong_align_up( meta_footprint, FD_MCACHE_ALIGN );
      18             : 
      19       50253 :   ulong app_footprint = fd_ulong_align_up( app_sz, FD_MCACHE_ALIGN );
      20       50253 :   if( FD_UNLIKELY( app_footprint<app_sz ) ) return 0UL; /* overflow */
      21             : 
      22       50250 :   ulong footprint = meta_footprint + app_footprint; /* meta and app */
      23       50250 :   if( footprint<meta_footprint ) return 0UL; /* overflow */
      24             : 
      25       50250 :   footprint += sizeof(fd_mcache_private_hdr_t); /* header and seq */
      26       50250 :   if( FD_UNLIKELY( footprint<sizeof(fd_mcache_private_hdr_t) ) ) return 0UL; /* overflow */
      27             : 
      28       50250 :   return footprint;
      29       50250 : }
      30             : 
      31             : void *
      32             : fd_mcache_new( void * shmem,
      33             :                ulong  depth,
      34             :                ulong  app_sz,
      35        2214 :                ulong  seq0 ) {
      36             : 
      37        2214 :   if( FD_UNLIKELY( !shmem ) ) {
      38           3 :     FD_LOG_WARNING(( "NULL shmem" ));
      39           3 :     return NULL;
      40           3 :   }
      41             : 
      42        2211 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, fd_mcache_align() ) ) ) {
      43           3 :     FD_LOG_WARNING(( "misaligned shmem" ));
      44           3 :     return NULL;
      45           3 :   }
      46             : 
      47        2208 :   ulong footprint = fd_mcache_footprint( depth, app_sz );
      48        2208 :   if( FD_UNLIKELY( !footprint ) ) {
      49           3 :     FD_LOG_WARNING(( "bad depth (%lu) or app_sz (%lu)", depth, app_sz ));
      50           3 :     return NULL;
      51           3 :   }
      52             : 
      53        2205 :   fd_memset( shmem, 0, footprint );
      54             : 
      55        2205 :   fd_mcache_private_hdr_t * hdr = (fd_mcache_private_hdr_t *)shmem;
      56             : 
      57        2205 :   hdr->depth    = depth;
      58        2205 :   hdr->app_sz   = app_sz;
      59        2205 :   hdr->seq0     = seq0;
      60        2205 :   hdr->app_off  = sizeof(fd_mcache_private_hdr_t) + fd_ulong_align_up( depth*sizeof(fd_frag_meta_t), FD_MCACHE_ALIGN );
      61             : 
      62        2205 :   hdr->seq[0] = seq0;
      63             : 
      64        2205 :   fd_frag_meta_t * mcache = fd_mcache_private_mcache( hdr );
      65             : 
      66        2205 :   ulong seq1 = fd_seq_inc( seq0, depth );
      67      117240 :   for( ulong seq=seq0; seq!=seq1; seq=fd_seq_inc(seq,1UL) ) {
      68      115035 :     ulong line = fd_mcache_line_idx( seq, depth );
      69      115035 :     mcache[line].seq = fd_seq_dec( seq, 1UL );
      70      115035 :     mcache[line].ctl = (ushort)fd_frag_meta_ctl( 0UL /*orig*/, 1 /*som*/, 1 /*eom*/, 1 /*err*/ );
      71      115035 :   }
      72             : 
      73        2205 :   FD_COMPILER_MFENCE();
      74        2205 :   FD_VOLATILE( hdr->magic ) = FD_MCACHE_MAGIC;
      75        2205 :   FD_COMPILER_MFENCE();
      76             : 
      77        2205 :   return shmem;
      78        2208 : }
      79             : 
      80             : fd_frag_meta_t *
      81        5700 : fd_mcache_join( void * shmcache ) {
      82             : 
      83        5700 :   if( FD_UNLIKELY( !shmcache ) ) {
      84           3 :     FD_LOG_WARNING(( "NULL shmcache" ));
      85           3 :     return NULL;
      86           3 :   }
      87             : 
      88        5697 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmcache, fd_mcache_align() ) ) ) {
      89           3 :     FD_LOG_WARNING(( "misaligned shmcache" ));
      90           3 :     return NULL;
      91           3 :   }
      92             : 
      93        5694 :   fd_mcache_private_hdr_t * hdr = (fd_mcache_private_hdr_t *)shmcache;
      94        5694 :   if( FD_UNLIKELY( hdr->magic!=FD_MCACHE_MAGIC ) ) {
      95           3 :     FD_LOG_WARNING(( "bad magic" ));
      96           3 :     return NULL;
      97           3 :   }
      98             : 
      99        5691 :   return fd_mcache_private_mcache( hdr );
     100        5694 : }
     101             : 
     102             : void *
     103         165 : fd_mcache_leave( fd_frag_meta_t const * mcache ) {
     104             : 
     105         165 :   if( FD_UNLIKELY( !mcache ) ) {
     106           3 :     FD_LOG_WARNING(( "NULL mcache" ));
     107           3 :     return NULL;
     108           3 :   }
     109             : 
     110         162 :   return (void *)fd_mcache_private_hdr_const( mcache ); /* Kinda ugly const cast */
     111         165 : }
     112             : 
     113             : void *
     114        2205 : fd_mcache_delete( void * shmcache ) {
     115             : 
     116        2205 :   if( FD_UNLIKELY( !shmcache ) ) {
     117           3 :     FD_LOG_WARNING(( "NULL shmcache" ));
     118           3 :     return NULL;
     119           3 :   }
     120             : 
     121        2202 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmcache, fd_mcache_align() ) ) ) {
     122           3 :     FD_LOG_WARNING(( "misaligned shmcache" ));
     123           3 :     return NULL;
     124           3 :   }
     125             : 
     126        2199 :   fd_mcache_private_hdr_t * hdr = (fd_mcache_private_hdr_t *)shmcache;
     127        2199 :   if( FD_UNLIKELY( hdr->magic != FD_MCACHE_MAGIC ) ) {
     128           6 :     FD_LOG_WARNING(( "bad magic" ));
     129           6 :     return NULL;
     130           6 :   }
     131             : 
     132        2193 :   FD_COMPILER_MFENCE();
     133        2193 :   FD_VOLATILE( hdr->magic ) = 0UL;
     134        2193 :   FD_COMPILER_MFENCE();
     135             : 
     136        2193 :   return shmcache;
     137        2199 : }
     138             : 
     139             : ulong
     140        3990 : fd_mcache_depth( fd_frag_meta_t const * mcache ) {
     141        3990 :   return fd_mcache_private_hdr_const( mcache )->depth;
     142        3990 : }
     143             : 
     144             : ulong
     145           6 : fd_mcache_app_sz( fd_frag_meta_t const * mcache ) {
     146           6 :   return fd_mcache_private_hdr_const( mcache )->app_sz;
     147           6 : }
     148             : 
     149             : ulong
     150           9 : fd_mcache_seq0( fd_frag_meta_t const * mcache ) {
     151           9 :   return fd_mcache_private_hdr_const( mcache )->seq0;
     152           9 : }
     153             : 
     154             : ulong const *
     155         108 : fd_mcache_seq_laddr_const( fd_frag_meta_t const * mcache ) {
     156         108 :   return fd_mcache_private_hdr_const( mcache )->seq;
     157         108 : }
     158             : 
     159             : ulong *
     160        5532 : fd_mcache_seq_laddr( fd_frag_meta_t * mcache ) {
     161        5532 :   return fd_mcache_private_hdr( mcache )->seq;
     162        5532 : }
     163             : 
     164             : uchar const *
     165           9 : fd_mcache_app_laddr_const( fd_frag_meta_t const * mcache ) {
     166           9 :   fd_mcache_private_hdr_t const * hdr = fd_mcache_private_hdr_const( mcache );
     167           9 :   return (uchar const *)(((ulong)hdr) + hdr->app_off);
     168           9 : }
     169             : 
     170             : uchar *
     171           6 : fd_mcache_app_laddr( fd_frag_meta_t * mcache ) {
     172           6 :   fd_mcache_private_hdr_t * hdr = fd_mcache_private_hdr( mcache );
     173           6 :   return (uchar *)(((ulong)hdr) + hdr->app_off);
     174           6 : }
     175             : 

Generated by: LCOV version 1.14