LCOV - code coverage report
Current view: top level - util/spad - fd_spad.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 72 74 97.3 %
Date: 2025-01-08 12:08:44 Functions: 13 13 100.0 %

          Line data    Source code
       1             : #include "fd_spad.h"
       2             : #include "../log/fd_log.h"
       3             : 
       4             : int
       5      595932 : fd_spad_verify( fd_spad_t const * spad ) {
       6             : 
       7     2383728 : # define TEST(c) do { if( FD_UNLIKELY( !(c) ) ) { FD_LOG_WARNING(( "FAIL: %s", #c )); return -1; } } while(0)
       8             : 
       9             :   /* Test spad is a current local join */
      10             : 
      11      595932 :   TEST( spad!=NULL );
      12      595932 :   TEST( spad->magic==FD_SPAD_MAGIC );
      13             : 
      14             :   /* Extract the metadata */
      15             : 
      16      595932 :   ulong frame_free = spad->frame_free; TEST( frame_free<=FD_SPAD_FRAME_MAX );
      17      595932 :   ulong mem_used   = spad->mem_used;   TEST( mem_used  <=spad->mem_max     );
      18             : 
      19             :   /* If there are no frames, there should be no memory used.  Otherwise,
      20             :      make sure the mem_used and frames are properly ordered starting
      21             :      from 0. */
      22             : 
      23      595932 :   if( frame_free==FD_SPAD_FRAME_MAX ) FD_TEST( !mem_used );
      24      576561 :   else {
      25      576561 :     FD_TEST( mem_used >= spad->off[ frame_free ] );
      26     1997526 :     for( ulong idx=frame_free; idx<FD_SPAD_FRAME_MAX-1UL; idx++ ) FD_TEST( spad->off[ idx ]>=spad->off[ idx+1UL ] );
      27      576561 :     FD_TEST( !spad->off[ FD_SPAD_FRAME_MAX-1UL] );
      28      576561 :   }
      29             : 
      30      595932 : # undef TEST
      31             : 
      32      595932 :   return 0;
      33      595932 : }
      34             : 
      35             : ulong
      36             : fd_spad_alloc_max_debug( fd_spad_t const * spad,
      37     2606853 :                          ulong             align ) {
      38     2606853 :   if( FD_UNLIKELY( !fd_spad_frame_used( spad )               ) ) FD_LOG_CRIT(( "not in a frame" ));
      39     2606853 :   if( FD_UNLIKELY( (!!align) & (!fd_ulong_is_pow2( align ) ) ) ) FD_LOG_CRIT(( "bad align"      ));
      40     2606853 :   return fd_spad_alloc_max( spad, align );
      41     2606853 : }
      42             : 
      43             : void *
      44           6 : fd_spad_frame_lo_debug( fd_spad_t * spad ) {
      45           6 :   if( FD_UNLIKELY( !fd_spad_frame_used( spad ) ) ) FD_LOG_CRIT(( "not in a frame" ));
      46           6 :   return fd_spad_frame_lo( spad );
      47           6 : }
      48             : 
      49             : void *
      50           3 : fd_spad_frame_hi_debug( fd_spad_t * spad ) {
      51           3 :   if( FD_UNLIKELY( !fd_spad_frame_used( spad ) ) ) FD_LOG_CRIT(( "not in a frame" ));
      52           3 :   return fd_spad_frame_hi( spad );
      53           3 : }
      54             : 
      55             : void
      56     2037663 : fd_spad_push_debug( fd_spad_t * spad ) {
      57     2037663 :   if( FD_UNLIKELY( !fd_spad_frame_free( spad ) ) ) FD_LOG_CRIT(( "too many frames" ));
      58     2037663 :   fd_spad_push( spad );
      59     2037663 : }
      60             : 
      61             : void
      62     2031666 : fd_spad_pop_debug( fd_spad_t * spad ) {
      63     2031666 :   if( FD_UNLIKELY( !fd_spad_frame_used( spad ) ) ) FD_LOG_CRIT(( "not in a frame" ));
      64     2031666 :   fd_spad_pop( spad );
      65     2031666 : }
      66             : 
      67             : void *
      68             : fd_spad_alloc_debug( fd_spad_t * spad,
      69             :                      ulong       align,
      70     2365878 :                      ulong       sz ) {
      71     2365878 :   if( FD_UNLIKELY( !fd_spad_frame_used( spad )               ) ) FD_LOG_CRIT(( "not in a frame"  ));
      72     2365878 :   if( FD_UNLIKELY( (!!align) & (!fd_ulong_is_pow2( align ) ) ) ) FD_LOG_CRIT(( "bad align"       ));
      73     2365878 :   if( FD_UNLIKELY( fd_spad_alloc_max( spad, align )<sz       ) ) FD_LOG_CRIT(( "bad sz"          ));
      74     2365878 :   return fd_spad_alloc( spad, align, sz );
      75     2365878 : }
      76             : 
      77             : void
      78             : fd_spad_trim_debug( fd_spad_t * spad,
      79     2237808 :                     void *      hi ) {
      80     2237808 :   if( FD_UNLIKELY( !fd_spad_frame_used( spad )                 ) ) FD_LOG_CRIT(( "not in a frame"    ));
      81     2237808 :   if( FD_UNLIKELY( ((ulong)fd_spad_frame_lo( spad ))>(ulong)hi ) ) FD_LOG_CRIT(( "hi below frame_lo" ));
      82     2237808 :   if( FD_UNLIKELY( ((ulong)fd_spad_frame_hi( spad ))<(ulong)hi ) ) FD_LOG_CRIT(( "hi above frame_hi" ));
      83     2237808 :   fd_spad_trim( spad, hi );
      84     2237808 : }
      85             : 
      86             : void *
      87             : fd_spad_prepare_debug( fd_spad_t * spad,
      88             :                        ulong       align,
      89     1676562 :                        ulong       max ) {
      90     1676562 :   if( FD_UNLIKELY( !fd_spad_frame_used( spad )               ) ) FD_LOG_CRIT(( "not in a frame" ));
      91     1676562 :   if( FD_UNLIKELY( (!!align) & (!fd_ulong_is_pow2( align ) ) ) ) FD_LOG_CRIT(( "bad align"      ));
      92     1676562 :   if( FD_UNLIKELY( fd_spad_alloc_max( spad, align )<max      ) ) FD_LOG_CRIT(( "bad max"        ));
      93     1676562 :   return fd_spad_prepare( spad, align, max );
      94     1676562 : }
      95             : 
      96             : void
      97      558702 : fd_spad_cancel_debug( fd_spad_t * spad ) {
      98      558702 :   if( FD_UNLIKELY( !fd_spad_frame_used( spad ) ) ) FD_LOG_CRIT(( "not in a frame" ));
      99             :   /* FIXME: check if in prepare?  needs extra state and a lot of extra
     100             :      tracking that state */
     101      558702 :   fd_spad_cancel( spad );
     102      558702 : }
     103             : 
     104             : void
     105             : fd_spad_publish_debug( fd_spad_t * spad,
     106     1117860 :                        ulong       sz ) {
     107     1117860 :   if( FD_UNLIKELY( !fd_spad_frame_used( spad )       ) ) FD_LOG_CRIT(( "not in a frame" ));
     108     1117860 :   if( FD_UNLIKELY( fd_spad_alloc_max( spad, 1UL )<sz ) ) FD_LOG_CRIT(( "bad sz"         ));
     109             :   /* FIXME: check if in prepare?  needs extra state and a lot of extra
     110             :      tracking that state */
     111     1117860 :   fd_spad_publish( spad, sz );
     112     1117860 : }
     113             : 
     114             : /* fd_valloc virtual function table for spad */
     115             : static void *
     116             : fd_spad_valloc_malloc( void * _self,
     117             :                                ulong  align,
     118     1333815 :                                ulong  sz ) {
     119     1333815 :   fd_spad_t * spad = _self;
     120     1333815 :   void * rv = fd_spad_alloc( spad, align, sz );
     121     1333815 :   if( FD_UNLIKELY( fd_spad_mem_used( spad )>fd_spad_mem_max( spad ) ) ) {
     122           0 :     FD_LOG_ERR(( "spad overflow mem_used=%lu mem_max=%lu", fd_spad_mem_used( spad ), fd_spad_mem_max( spad ) ));
     123           0 :   }
     124     1333815 :   return rv;
     125     1333815 : }
     126             : 
     127             : static void
     128             : fd_spad_valloc_free( void * _self,
     129     5364612 :                              void * _addr ) {
     130     5364612 :   (void)_self; (void)_addr;
     131     5364612 : }
     132             : 
     133             : const fd_valloc_vtable_t
     134             : fd_spad_vtable = {
     135             :   .malloc = fd_spad_valloc_malloc,
     136             :   .free   = fd_spad_valloc_free
     137             : };

Generated by: LCOV version 1.14