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