Line data Source code
1 : #include "fd_quic_pkt_meta.h" 2 : 3 : /* initialize pool with existing array of pkt_meta */ 4 : void 5 : fd_quic_pkt_meta_pool_init( fd_quic_pkt_meta_pool_t * pool, 6 : fd_quic_pkt_meta_t * pkt_meta_array, 7 314478 : ulong pkt_meta_array_sz ) { 8 : /* initialize all to zeros */ 9 314478 : fd_memset( pool, 0, sizeof( *pool ) ); 10 : 11 : /* free list */ 12 314478 : fd_quic_pkt_meta_list_t * free = &pool->free; 13 : 14 : /* initialize free list of packet metadata */ 15 13838454 : for( ulong j = 0; j < pkt_meta_array_sz; ++j ) { 16 13523976 : fd_quic_pkt_meta_push_back( free, &pkt_meta_array[j] ); 17 13523976 : } 18 : 19 314478 : } 20 : 21 : 22 : /* pop from front of list */ 23 : fd_quic_pkt_meta_t * 24 19625864 : fd_quic_pkt_meta_pop_front( fd_quic_pkt_meta_list_t * list ) { 25 19625864 : fd_quic_pkt_meta_t * front = list->head; 26 19625864 : if( front ) { 27 19418909 : list->head = front->next; 28 19418909 : } 29 19625864 : return front; 30 19625864 : } 31 : 32 : 33 : /* push onto front of list */ 34 : void 35 : fd_quic_pkt_meta_push_front( fd_quic_pkt_meta_list_t * list, 36 18518836 : fd_quic_pkt_meta_t * pkt_meta ) { 37 18518836 : pkt_meta->next = list->head; 38 18518836 : list->head = pkt_meta; 39 18518836 : } 40 : 41 : 42 : /* push onto back of list */ 43 : void 44 : fd_quic_pkt_meta_push_back( fd_quic_pkt_meta_list_t * list, 45 32930474 : fd_quic_pkt_meta_t * pkt_meta ) { 46 32930474 : fd_quic_pkt_meta_t * tail = list->tail; 47 32930474 : if( tail ) { 48 30128760 : tail->next = pkt_meta; 49 30128760 : list->tail = pkt_meta; 50 30128760 : } else { 51 2801714 : list->head = list->tail = pkt_meta; 52 2801714 : } 53 : 54 32930474 : pkt_meta->next = NULL; 55 32930474 : } 56 : 57 : /* remove from list 58 : requires the prior element */ 59 : void 60 : fd_quic_pkt_meta_remove( fd_quic_pkt_meta_list_t * list, 61 : fd_quic_pkt_meta_t * pkt_meta_prior, 62 18506425 : fd_quic_pkt_meta_t * pkt_meta ) { 63 18506425 : fd_quic_pkt_meta_t * pkt_meta_next = pkt_meta->next; 64 : 65 18506425 : if( pkt_meta_prior == NULL ) { 66 18506422 : if( pkt_meta_next == NULL ) { 67 : /* at tail... then head = tail = NULL */ 68 2181532 : list->head = list->tail = NULL; 69 16324890 : } else { 70 : /* at head... move it to next */ 71 16324890 : list->head = pkt_meta_next; 72 16324890 : } 73 18506422 : } else { 74 3 : if( pkt_meta_next == NULL ) { 75 : /* we're removing the last, so move tail */ 76 3 : list->tail = pkt_meta_prior; 77 3 : } 78 : 79 : /* not head, make pkt_meta_prior point to next */ 80 3 : pkt_meta_prior->next = pkt_meta_next; 81 3 : } 82 : 83 18506425 : pkt_meta->next = NULL; 84 18506425 : } 85 : 86 : 87 : /* allocate a pkt_meta 88 : obtains a free pkt_meta from the free list, and returns it 89 : returns NULL if none is available */ 90 : fd_quic_pkt_meta_t * 91 19625864 : fd_quic_pkt_meta_allocate( fd_quic_pkt_meta_pool_t * pool ) { 92 19625864 : fd_quic_pkt_meta_t * pkt_meta = fd_quic_pkt_meta_pop_front( &pool->free ); 93 19625864 : if( FD_LIKELY( pkt_meta ) ) { 94 19418909 : fd_memset( pkt_meta, 0, sizeof( *pkt_meta ) ); 95 19418909 : } 96 19625864 : return pkt_meta; 97 19625864 : } 98 : 99 : 100 : /* free a pkt_meta 101 : returns a pkt_meta to the free list, ready to be allocated again */ 102 : void 103 18518836 : fd_quic_pkt_meta_deallocate( fd_quic_pkt_meta_pool_t * pool, fd_quic_pkt_meta_t * pkt_meta ) { 104 : /* pushing to the front should help cache usage */ 105 18518836 : fd_quic_pkt_meta_push_front( &pool->free, pkt_meta ); 106 18518836 : } 107 : 108 :