1 /* 2 * ngtcp2 3 * 4 * Copyright (c) 2017 ngtcp2 contributors 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining 7 * a copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sublicense, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be 15 * included in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 */ 25 #ifndef NGTCP2_BUF_H 26 #define NGTCP2_BUF_H 27 28 #ifdef HAVE_CONFIG_H 29 # include <config.h> 30 #endif /* HAVE_CONFIG_H */ 31 32 #include <ngtcp2/ngtcp2.h> 33 34 typedef struct ngtcp2_buf { 35 /* begin points to the beginning of the buffer. */ 36 uint8_t *begin; 37 /* end points to the one beyond of the last byte of the buffer */ 38 uint8_t *end; 39 /* pos pointers to the start of data. Typically, this points to the 40 point that next data should be read. Initially, it points to 41 |begin|. */ 42 uint8_t *pos; 43 /* last points to the one beyond of the last data of the buffer. 44 Typically, new data is written at this point. Initially, it 45 points to |begin|. */ 46 uint8_t *last; 47 } ngtcp2_buf; 48 49 /* 50 * ngtcp2_buf_init initializes |buf| with the given buffer. 51 */ 52 void ngtcp2_buf_init(ngtcp2_buf *buf, uint8_t *begin, size_t len); 53 54 /* 55 * ngtcp2_buf_reset resets pos and last fields to match begin field to 56 * make ngtcp2_buf_len(buf) return 0. 57 */ 58 void ngtcp2_buf_reset(ngtcp2_buf *buf); 59 60 /* 61 * ngtcp2_buf_left returns the number of additional bytes which can be 62 * written to the underlying buffer. In other words, it returns 63 * buf->end - buf->last. 64 */ 65 #define ngtcp2_buf_left(BUF) (size_t)((BUF)->end - (BUF)->last) 66 67 /* 68 * ngtcp2_buf_len returns the number of bytes left to read. In other 69 * words, it returns buf->last - buf->pos. 70 */ 71 #define ngtcp2_buf_len(BUF) (size_t)((BUF)->last - (BUF)->pos) 72 73 /* 74 * ngtcp2_buf_cap returns the capacity of the buffer. In other words, 75 * it returns buf->end - buf->begin. 76 */ 77 size_t ngtcp2_buf_cap(const ngtcp2_buf *buf); 78 79 /* 80 * ngtcp2_buf_chain is a linked list of ngtcp2_buf. 81 */ 82 typedef struct ngtcp2_buf_chain ngtcp2_buf_chain; 83 84 struct ngtcp2_buf_chain { 85 ngtcp2_buf_chain *next; 86 ngtcp2_buf buf; 87 }; 88 89 /* 90 * ngtcp2_buf_chain_new creates new ngtcp2_buf_chain and initializes 91 * the internal buffer with |len| bytes space. 92 * 93 * This function returns 0 if it succeeds, or one of the following 94 * negative error codes: 95 * 96 * NGTCP2_ERR_NOMEM 97 * Out of memory 98 */ 99 int ngtcp2_buf_chain_new(ngtcp2_buf_chain **pbufchain, size_t len, 100 const ngtcp2_mem *mem); 101 102 /* 103 * ngtcp2_buf_chain_del deletes the resource allocated by |bufchain|. 104 * It also deletes the memory pointed by |bufchain|. 105 */ 106 void ngtcp2_buf_chain_del(ngtcp2_buf_chain *bufchain, const ngtcp2_mem *mem); 107 108 #endif /* NGTCP2_BUF_H */ 109