Line data Source code
1 : #include "fd_quic_stream.h" 2 : #include "fd_quic_enum.h" 3 : 4 : /* buffer helper functions */ 5 : /* fd_quic_buffer_store 6 : store data into circular buffer */ 7 : void 8 : fd_quic_buffer_store( fd_quic_buffer_t * buf, 9 : uchar const * data, 10 16624993 : ulong data_sz ) { 11 : /* do we have space to buffer data? */ 12 : /* see fd_quic_stream.h for invariants */ 13 16624993 : uchar * raw = buf->buf; 14 16624993 : ulong cap = buf->cap; 15 16624993 : ulong head = buf->head; 16 16624993 : ulong free = cap - head; 17 : 18 : /* not enough room - caller responsible for checking available space */ 19 16624993 : if( data_sz > free ) { 20 0 : return; 21 0 : } 22 : 23 16624993 : fd_memcpy( raw + head, data, data_sz ); 24 : 25 16624993 : buf->head += data_sz; 26 16624993 : } 27 : 28 : /* fd_quic_buffer_load 29 : load data from circular buffer */ 30 : void 31 : fd_quic_buffer_load( fd_quic_buffer_t * buf, 32 : ulong offs, 33 : uchar * data, 34 16635832 : ulong data_sz ) { 35 16635832 : uchar * raw = buf->buf; 36 16635832 : ulong head = buf->head; 37 : 38 : /* caller responsible for checking operation valid */ 39 16635832 : if( FD_UNLIKELY( offs+data_sz > head ) ) return; 40 : 41 : /* two cases: 42 : 1. data fits within free contiguous space at m_tail 43 : 2. data is split 44 : 45 : used is in [offs,head) */ 46 : 47 16635832 : fd_memcpy( data, raw + offs, data_sz ); 48 16635832 : } 49 : 50 : 51 : extern 52 : ulong 53 : fd_quic_stream_align( void ); 54 : 55 : ulong 56 826791 : fd_quic_stream_footprint( ulong tx_buf_sz ) { 57 826791 : ulong align = fd_quic_stream_align(); 58 826791 : ulong offs = 0ul; 59 : 60 826791 : ulong tx_ack_sz = (tx_buf_sz >> 3ul)+1UL; 61 826791 : ulong align_stream_sz = fd_ulong_align_up( sizeof( fd_quic_stream_t ), align ); 62 826791 : ulong align_tx_ack_sz = fd_ulong_align_up( tx_ack_sz, align ); 63 826791 : ulong align_tx_buf_sz = fd_ulong_align_up( tx_buf_sz, align ); 64 : 65 826791 : offs += align_stream_sz; /* space for stream instance */ 66 826791 : offs += align_tx_buf_sz; /* space for tx_buf */ 67 826791 : offs += align_tx_ack_sz; /* space for tx_ack */ 68 : 69 826791 : return offs; 70 826791 : } 71 : 72 : /* returns a newly initialized stream 73 : 74 : args 75 : mem the memory aligned to fd_quic_stream_align, and at least fd_quic_stream_footprint 76 : bytes 77 : tx_buf_sz the size of the tx buffer */ 78 : fd_quic_stream_t * 79 812370 : fd_quic_stream_new( void * mem, fd_quic_conn_t * conn, ulong tx_buf_sz ) { 80 812370 : ulong align = fd_quic_stream_align(); 81 : 82 812370 : ulong tx_ack_sz = (tx_buf_sz >> 3ul)+1UL; 83 812370 : ulong align_stream_sz = fd_ulong_align_up( sizeof( fd_quic_stream_t ), align ); 84 812370 : ulong align_tx_buf_sz = fd_ulong_align_up( tx_buf_sz, align ); 85 812370 : ulong align_tx_ack_sz = fd_ulong_align_up( tx_ack_sz, align ); 86 : 87 812370 : ulong offs = 0; 88 812370 : ulong base = (ulong)mem; 89 : 90 : /* allocate memory for the stream */ 91 812370 : fd_quic_stream_t * stream = (fd_quic_stream_t*)mem; 92 : 93 812370 : offs += align_stream_sz; 94 : 95 : /* allocate memory for the tx buffer */ 96 812370 : stream->tx_buf.buf = (uchar*)( base + offs ); 97 812370 : stream->tx_buf.cap = tx_buf_sz; 98 : 99 812370 : offs += align_tx_buf_sz; 100 : 101 : /* allocate memory for the tx ack buffer */ 102 812370 : stream->tx_ack = (uchar*)( base + offs ); 103 : 104 812370 : offs += align_tx_ack_sz; 105 : 106 812370 : if( offs != fd_quic_stream_footprint( tx_buf_sz ) ) { 107 0 : FD_LOG_ERR(( "fd_quic_stream_new : allocated size of stream does not match footprint" )); 108 0 : } 109 : 110 812370 : stream->conn = conn; 111 812370 : stream->stream_id = FD_QUIC_STREAM_ID_UNUSED; 112 : 113 : /* stream pointing to itself is not a member of any list */ 114 812370 : stream->next = stream->prev = stream; 115 : 116 812370 : return stream; 117 812370 : } 118 : 119 : /* delete a stream 120 : 121 : args 122 : stream the stream to free */ 123 : void 124 0 : fd_quic_stream_delete( fd_quic_stream_t * stream ) { 125 : /* stream pointing to itself is not a member of any list */ 126 0 : stream->next = stream->prev = stream; 127 0 : stream->list_memb = FD_QUIC_STREAM_LIST_MEMB_NONE; 128 0 : } 129 : 130 : 131 : /* set stream context 132 : 133 : args 134 : stream the stream with which to associate the context 135 : context the user-defined context associated with the stream */ 136 : void 137 0 : fd_quic_stream_set_context( fd_quic_stream_t * stream, void * context ) { 138 0 : stream->context = context; 139 0 : } 140 : 141 : 142 : /* get stream context 143 : 144 : args 145 : stream the stream from which to obtain the context 146 : 147 : returns 148 : context the user defined context associated with the stream */ 149 : void * 150 0 : fd_quic_stream_get_context( fd_quic_stream_t * stream ) { 151 0 : return stream->context; 152 0 : }