LCOV - code coverage report
Current view: top level - waltz/quic - fd_quic_stream_pool.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 37 44 84.1 %
Date: 2025-01-08 12:08:44 Functions: 4 6 66.7 %

          Line data    Source code
       1             : #include "fd_quic_stream_pool.h"
       2             : 
       3             : /* returns the required footprint of fd_quic_stream_pool_t
       4             : 
       5             :    args
       6             :      count        the number of streams the pool will manage
       7             :      tx_buf_sz    the size of the tx buffer
       8             :                   should be 0 for RX only streams */
       9             : FD_FN_CONST
      10             : ulong
      11        2538 : fd_quic_stream_pool_footprint( ulong count, ulong tx_buf_sz ) {
      12        2538 :   ulong foot =  fd_ulong_align_up( sizeof( fd_quic_stream_pool_t ),
      13        2538 :       FD_QUIC_STREAM_POOL_ALIGN );
      14             : 
      15        2538 :   ulong stream_foot = fd_quic_stream_footprint( tx_buf_sz );
      16             : 
      17        2538 :   return foot + stream_foot * count;
      18        2538 : }
      19             : 
      20             : /* returns a newly initialized stream pool
      21             : 
      22             :    args
      23             :      mem          the memory aligned to fd_quic_stream_pool_align, and at least fd_quic_stream_pool_footprint
      24             :                     bytes
      25             :      count        the number of streams the pool will manage
      26             :      type         the stream type used for the streams managed by this pool */
      27             : fd_quic_stream_pool_t *
      28        1230 : fd_quic_stream_pool_new( void * mem, ulong count, ulong tx_buf_sz ) {
      29        1230 :   ulong offs   = 0;
      30        1230 :   ulong ul_mem = (ulong)mem;
      31             : 
      32        1230 :   fd_quic_stream_pool_t * pool = (fd_quic_stream_pool_t*)ul_mem;
      33        1230 :   memset( pool, 0, sizeof( fd_quic_stream_pool_t ) );
      34             : 
      35        1230 :   pool->cap     = count;
      36        1230 :   pool->cur_cnt = 0UL;
      37             : 
      38        1230 :   offs += fd_ulong_align_up( sizeof( fd_quic_stream_pool_t ), FD_QUIC_STREAM_POOL_ALIGN );
      39             : 
      40        1230 :   ulong stream_foot = fd_quic_stream_footprint( tx_buf_sz );
      41             : 
      42        1230 :   FD_QUIC_STREAM_LIST_SENTINEL( pool->head );
      43             : 
      44             :   /* allocate count streams */
      45      781062 :   for( ulong j = 0; j < count; ++j ) {
      46      779832 :     fd_quic_stream_t * stream = fd_quic_stream_new( (void*)( ul_mem + offs ), NULL, tx_buf_sz );
      47             : 
      48      779832 :     FD_QUIC_STREAM_LIST_INIT_STREAM( stream );
      49      779832 :     FD_QUIC_STREAM_LIST_INSERT_AFTER( pool->head, stream );
      50      779832 :     pool->cur_cnt++;
      51             : 
      52      779832 :     offs += stream_foot;
      53             : 
      54      779832 :   }
      55             : 
      56        1230 :   return pool;
      57        1230 : }
      58             : 
      59             : 
      60             : /* delete a stream pool
      61             : 
      62             :    this will also delete all the associated streams
      63             : 
      64             :    All streams should be freed back to the pool before this function is called
      65             : 
      66             :    args
      67             :      stream_pool  the stream pool to free */
      68             : void
      69           0 : fd_quic_stream_pool_delete( fd_quic_stream_pool_t * stream_pool ) {
      70           0 :   (void)stream_pool;
      71           0 : }
      72             : 
      73             : 
      74             : /* allocates a stream from the pool
      75             : 
      76             :    args
      77             :      stream_pool  the pool from which to obtain the stream
      78             : 
      79             :    returns
      80             :      the newly allocated stream, or NULL if no streams are available */
      81             : fd_quic_stream_t *
      82    12764286 : fd_quic_stream_pool_alloc( fd_quic_stream_pool_t * pool ) {
      83    12764286 :   fd_quic_stream_t * stream_sentinel = pool->head;
      84    12764286 :   fd_quic_stream_t * stream          = stream_sentinel->next;
      85             : 
      86    12764286 :   if( FD_UNLIKELY( stream == stream_sentinel ) ) {
      87             :     /* no streams left in free list, return NULL */
      88           0 :     return NULL;
      89           0 :   }
      90             : 
      91             :   /* remove from free list */
      92    12764286 :   FD_QUIC_STREAM_LIST_REMOVE( stream );
      93    12764286 :   pool->cur_cnt--;
      94             : 
      95    12764286 :   return stream;
      96    12764286 : }
      97             : 
      98             : /* free a stream to the specified pool
      99             : 
     100             :    args
     101             :      stream_pool  the pool to return the stream to
     102             :      stream       the stream to return */
     103             : void
     104             : fd_quic_stream_pool_free( fd_quic_stream_pool_t * pool,
     105    12764286 :                           fd_quic_stream_t *      stream ) {
     106    12764286 :   FD_QUIC_STREAM_LIST_INSERT_AFTER( pool->head, stream );
     107    12764286 :   pool->cur_cnt++;
     108    12764286 : }
     109             : 
     110             : void
     111           0 : fd_quic_stream_pool_free_batch( void ) {
     112             :   /* TODO
     113             :    * stream pool and used/send lists are both doubly linked lists
     114             :    * so this can be achieved in O(1) */
     115           0 : }

Generated by: LCOV version 1.14