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_RINGBUF_H 26 #define NGTCP2_RINGBUF_H 27 28 #ifdef HAVE_CONFIG_H 29 # include <config.h> 30 #endif /* HAVE_CONFIG_H */ 31 32 #include <ngtcp2/ngtcp2.h> 33 34 #include "ngtcp2_mem.h" 35 36 typedef struct ngtcp2_ringbuf { 37 /* buf points to the underlying buffer. */ 38 uint8_t *buf; 39 const ngtcp2_mem *mem; 40 /* nmemb is the number of elements that can be stored in this ring 41 buffer. */ 42 size_t nmemb; 43 /* size is the size of each element. */ 44 size_t size; 45 /* first is the offset to the first element. */ 46 size_t first; 47 /* len is the number of elements actually stored. */ 48 size_t len; 49 } ngtcp2_ringbuf; 50 51 /* 52 * ngtcp2_ringbuf_init initializes |rb|. |nmemb| is the number of 53 * elements that can be stored in this buffer. |size| is the size of 54 * each element. |size| must be power of 2. 55 * 56 * This function returns 0 if it succeeds, or one of the following 57 * negative error codes: 58 * 59 * NGTCP2_ERR_NOMEM 60 * Out of memory. 61 */ 62 int ngtcp2_ringbuf_init(ngtcp2_ringbuf *rb, size_t nmemb, size_t size, 63 const ngtcp2_mem *mem); 64 65 /* 66 * ngtcp2_ringbuf_buf_init initializes |rb| with given buffer and 67 * size. 68 */ 69 void ngtcp2_ringbuf_buf_init(ngtcp2_ringbuf *rb, size_t nmemb, size_t size, 70 uint8_t *buf, const ngtcp2_mem *mem); 71 72 /* 73 * ngtcp2_ringbuf_free frees resources allocated for |rb|. This 74 * function does not free the memory pointed by |rb|. 75 */ 76 void ngtcp2_ringbuf_free(ngtcp2_ringbuf *rb); 77 78 /* ngtcp2_ringbuf_push_front moves the offset to the first element in 79 the buffer backward, and returns the pointer to the element. 80 Caller can store data to the buffer pointed by the returned 81 pointer. If this action exceeds the capacity of the ring buffer, 82 the last element is silently overwritten, and rb->len remains 83 unchanged. */ 84 void *ngtcp2_ringbuf_push_front(ngtcp2_ringbuf *rb); 85 86 /* ngtcp2_ringbuf_push_back moves the offset to the last element in 87 the buffer forward, and returns the pointer to the element. Caller 88 can store data to the buffer pointed by the returned pointer. If 89 this action exceeds the capacity of the ring buffer, the first 90 element is silently overwritten, and rb->len remains unchanged. */ 91 void *ngtcp2_ringbuf_push_back(ngtcp2_ringbuf *rb); 92 93 /* 94 * ngtcp2_ringbuf_pop_front removes first element in |rb|. 95 */ 96 void ngtcp2_ringbuf_pop_front(ngtcp2_ringbuf *rb); 97 98 /* 99 * ngtcp2_ringbuf_pop_back removes the last element in |rb|. 100 */ 101 void ngtcp2_ringbuf_pop_back(ngtcp2_ringbuf *rb); 102 103 /* ngtcp2_ringbuf_resize changes the number of elements stored. This 104 does not change the capacity of the underlying buffer. */ 105 void ngtcp2_ringbuf_resize(ngtcp2_ringbuf *rb, size_t len); 106 107 /* ngtcp2_ringbuf_get returns the pointer to the element at 108 |offset|. */ 109 void *ngtcp2_ringbuf_get(ngtcp2_ringbuf *rb, size_t offset); 110 111 /* ngtcp2_ringbuf_len returns the number of elements stored. */ 112 #define ngtcp2_ringbuf_len(RB) ((RB)->len) 113 114 /* ngtcp2_ringbuf_full returns nonzero if |rb| is full. */ 115 int ngtcp2_ringbuf_full(ngtcp2_ringbuf *rb); 116 117 /* ngtcp2_static_ringbuf_def defines ngtcp2_ringbuf struct wrapper 118 which uses a statically allocated buffer that is suitable for a 119 usage that does not change buffer size with ngtcp2_ringbuf_resize. 120 ngtcp2_ringbuf_free should never be called for rb field. */ 121 #define ngtcp2_static_ringbuf_def(NAME, NMEMB, SIZE) \ 122 typedef struct ngtcp2_static_ringbuf_##NAME { \ 123 ngtcp2_ringbuf rb; \ 124 uint8_t buf[(NMEMB) * (SIZE)]; \ 125 } ngtcp2_static_ringbuf_##NAME; \ 126 \ 127 static inline void ngtcp2_static_ringbuf_##NAME##_init( \ 128 ngtcp2_static_ringbuf_##NAME *srb) { \ 129 ngtcp2_ringbuf_buf_init(&srb->rb, (NMEMB), (SIZE), srb->buf, NULL); \ 130 } 131 132 #endif /* NGTCP2_RINGBUF_H */ 133