LCOV - code coverage report
Current view: top level - util/valloc - fd_valloc.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 6 53 11.3 %
Date: 2024-11-13 11:58:15 Functions: 2 4 50.0 %

          Line data    Source code
       1             : #include "fd_valloc.h"
       2             : #include "../bits/fd_bits.h"
       3             : #include "../log/fd_log.h"
       4             : #include <stdlib.h>
       5             : #include <stdio.h>
       6             : #include <execinfo.h>
       7             : #include <unistd.h>
       8             : 
       9             : static void *
      10             : fd_libc_malloc_virtual( void * _self FD_PARAM_UNUSED,
      11             :                         ulong  align,
      12       17628 :                         ulong  sz ) {
      13       17628 :   return aligned_alloc( align, fd_ulong_align_up( sz, align ) );
      14       17628 : }
      15             : 
      16             : static void
      17             : fd_libc_free_virtual( void * _self FD_PARAM_UNUSED,
      18       17628 :                       void * _addr ) {
      19       17628 :   free( _addr );
      20       17628 : }
      21             : 
      22             : #if FD_HAS_HOSTED
      23             : static volatile ushort backtracing_lock = 0;
      24             : 
      25             : static void *
      26             : fd_backtracing_malloc_virtual( void * self,
      27             :                                ulong  align,
      28           0 :                                ulong  sz ) {
      29           0 :   for(;;) {
      30           0 :     if( FD_LIKELY( !backtracing_lock ) ) {
      31           0 :       if( FD_LIKELY( !FD_ATOMIC_CAS( &backtracing_lock, 0, 0xFFFF ) ) ) break;
      32           0 :     }
      33           0 :     FD_SPIN_PAUSE();
      34           0 :   }
      35           0 :   FD_COMPILER_MFENCE();
      36             : 
      37           0 :   void * addr = fd_valloc_malloc( *(fd_valloc_t *)self, align, sz );
      38           0 :   void * btrace[128];
      39           0 :   int btrace_cnt = backtrace( btrace, 128 );
      40             : 
      41           0 :   char buf[1024];
      42           0 :   int len = sprintf(buf,"+++ ALLOC:%d:0x%016lx:%lu:%lu\n", btrace_cnt, (ulong)addr, align, sz );
      43           0 :   if( write( STDOUT_FILENO, buf, (ulong)len )<0 ) {
      44           0 :     FD_LOG_ERR(( "cannot write to stdout" ));
      45           0 :   }
      46           0 :   backtrace_symbols_fd( btrace, btrace_cnt, STDOUT_FILENO );
      47           0 :   if( write( STDOUT_FILENO, "---\n", 4UL )<0 ) {
      48           0 :     FD_LOG_ERR(( "cannot write to stdout" ));
      49           0 :   }
      50           0 :   FD_COMPILER_MFENCE();
      51           0 :   backtracing_lock = 0;
      52           0 :   return addr;
      53           0 : }
      54             : 
      55             : static void
      56             : fd_backtracing_free_virtual( void * self,
      57           0 :                              void * addr ) {
      58           0 :   for(;;) {
      59           0 :   if( FD_LIKELY( !backtracing_lock ) ) {
      60           0 :       if( FD_LIKELY( !FD_ATOMIC_CAS( &backtracing_lock, 0, 0xFFFF ) ) ) break;
      61           0 :     }
      62           0 :     FD_SPIN_PAUSE();
      63           0 :   }
      64           0 :   FD_COMPILER_MFENCE();
      65           0 :   fd_valloc_free( *(fd_valloc_t *)self, addr );
      66           0 :   void * btrace[128];
      67           0 :   int btrace_cnt = backtrace( btrace, 128 );
      68           0 :   char buf[1024];
      69           0 :   int len = sprintf(buf, "+++ FREE:%d:0x%016lx\n", btrace_cnt, (ulong)addr );
      70           0 :   if( write( STDOUT_FILENO, buf, (ulong)len )<0 ) {
      71           0 :     FD_LOG_ERR(( "cannot write to stdout" ));
      72           0 :   }
      73           0 :   backtrace_symbols_fd( btrace, btrace_cnt, STDOUT_FILENO );
      74             : 
      75           0 :   if( write( STDOUT_FILENO, "---\n", 4UL )<0 ) {
      76           0 :     FD_LOG_ERR(( "cannot write to stdout" ));
      77           0 :   }
      78           0 :   FD_COMPILER_MFENCE();
      79           0 :   backtracing_lock = 0;
      80           0 : }
      81             : #endif
      82             : 
      83             : const fd_valloc_vtable_t
      84             : fd_libc_vtable = {
      85             :   .malloc = fd_libc_malloc_virtual,
      86             :   .free   = fd_libc_free_virtual
      87             : };
      88             : 
      89             : #if FD_HAS_HOSTED
      90             : const fd_valloc_vtable_t
      91             : fd_backtracing_vtable = {
      92             :   .malloc = fd_backtracing_malloc_virtual,
      93             :   .free   = fd_backtracing_free_virtual
      94             : };
      95             : #endif

Generated by: LCOV version 1.14