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 19533697 : ulong data_sz ) { 11 : /* do we have space to buffer data? */ 12 : /* see fd_quic_stream.h for invariants */ 13 19533697 : uchar * raw = buf->buf; 14 19533697 : ulong cap = buf->cap; 15 19533697 : ulong head = buf->head; 16 19533697 : ulong free = cap - head; 17 : 18 : /* not enough room - caller responsible for checking available space */ 19 19533697 : if( data_sz > free ) { 20 0 : return; 21 0 : } 22 : 23 19533697 : fd_memcpy( raw + head, data, data_sz ); 24 : 25 19533697 : buf->head += data_sz; 26 19533697 : } 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 19544563 : ulong data_sz ) { 35 19544563 : uchar * raw = buf->buf; 36 19544563 : ulong head = buf->head; 37 : 38 : /* caller responsible for checking operation valid */ 39 19544563 : 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 19544563 : fd_memcpy( data, raw + offs, data_sz ); 48 19544563 : } 49 : 50 : 51 : extern 52 : ulong 53 : fd_quic_stream_align( void ); 54 : 55 : ulong 56 829926 : fd_quic_stream_footprint( ulong tx_buf_sz ) { 57 829926 : ulong align = fd_quic_stream_align(); 58 829926 : ulong offs = 0ul; 59 : 60 829926 : ulong tx_ack_sz = fd_quic_tx_ack_bufsz( tx_buf_sz ); 61 829926 : ulong align_stream_sz = fd_ulong_align_up( sizeof( fd_quic_stream_t ), align ); 62 829926 : ulong align_tx_ack_sz = fd_ulong_align_up( tx_ack_sz, align ); 63 829926 : ulong align_tx_buf_sz = fd_ulong_align_up( tx_buf_sz, align ); 64 : 65 829926 : offs += align_stream_sz; /* space for stream instance */ 66 829926 : offs += align_tx_buf_sz; /* space for tx_buf */ 67 829926 : offs += align_tx_ack_sz; /* space for tx_ack */ 68 : 69 829926 : return offs; 70 829926 : } 71 : 72 : fd_quic_stream_t * 73 815370 : fd_quic_stream_new( void * mem, fd_quic_conn_t * conn, ulong tx_buf_sz ) { 74 815370 : ulong align = fd_quic_stream_align(); 75 : 76 815370 : ulong tx_ack_sz = fd_quic_tx_ack_bufsz( tx_buf_sz ); 77 815370 : ulong align_stream_sz = fd_ulong_align_up( sizeof( fd_quic_stream_t ), align ); 78 815370 : ulong align_tx_buf_sz = fd_ulong_align_up( tx_buf_sz, align ); 79 815370 : ulong align_tx_ack_sz = fd_ulong_align_up( tx_ack_sz, align ); 80 : 81 815370 : ulong offs = 0; 82 815370 : ulong base = (ulong)mem; 83 : 84 : /* allocate memory for the stream */ 85 815370 : fd_quic_stream_t * stream = (fd_quic_stream_t*)mem; 86 : 87 815370 : offs += align_stream_sz; 88 : 89 : /* allocate memory for the tx buffer */ 90 815370 : stream->tx_buf.buf = (uchar*)( base + offs ); 91 815370 : stream->tx_buf.cap = tx_buf_sz; 92 : 93 815370 : offs += align_tx_buf_sz; 94 : 95 : /* allocate memory for the tx ack buffer */ 96 815370 : stream->tx_ack = (uchar*)( base + offs ); 97 : 98 815370 : offs += align_tx_ack_sz; 99 : 100 815370 : if( offs != fd_quic_stream_footprint( tx_buf_sz ) ) { 101 0 : FD_LOG_ERR(( "fd_quic_stream_new : allocated size of stream does not match footprint" )); 102 0 : } 103 : 104 815370 : stream->conn = conn; 105 815370 : stream->stream_id = FD_QUIC_STREAM_ID_UNUSED; 106 : 107 : /* stream pointing to itself is not a member of any list */ 108 815370 : stream->next = stream->prev = stream; 109 : 110 815370 : return stream; 111 815370 : }