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