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_free frees resources allocated for |rb|. This 67 * function does not free the memory pointed by |rb|. 68 */ 69 void ngtcp2_ringbuf_free(ngtcp2_ringbuf *rb); 70 71 /* ngtcp2_ringbuf_push_front moves the offset to the first element in 72 the buffer backward, and returns the pointer to the element. 73 Caller can store data to the buffer pointed by the returned 74 pointer. If this action exceeds the capacity of the ring buffer, 75 the last element is silently overwritten, and rb->len remains 76 unchanged. */ 77 void *ngtcp2_ringbuf_push_front(ngtcp2_ringbuf *rb); 78 79 /* ngtcp2_ringbuf_push_back moves the offset to the last element in 80 the buffer forward, and returns the pointer to the element. Caller 81 can store data to the buffer pointed by the returned pointer. If 82 this action exceeds the capacity of the ring buffer, the first 83 element is silently overwritten, and rb->len remains unchanged. */ 84 void *ngtcp2_ringbuf_push_back(ngtcp2_ringbuf *rb); 85 86 /* 87 * ngtcp2_ringbuf_pop_front removes first element in |rb|. 88 */ 89 void ngtcp2_ringbuf_pop_front(ngtcp2_ringbuf *rb); 90 91 /* 92 * ngtcp2_ringbuf_pop_back removes the last element in |rb|. 93 */ 94 void ngtcp2_ringbuf_pop_back(ngtcp2_ringbuf *rb); 95 96 /* ngtcp2_ringbuf_resize changes the number of elements stored. This 97 does not change the capacity of the underlying buffer. */ 98 void ngtcp2_ringbuf_resize(ngtcp2_ringbuf *rb, size_t len); 99 100 /* ngtcp2_ringbuf_get returns the pointer to the element at 101 |offset|. */ 102 void *ngtcp2_ringbuf_get(ngtcp2_ringbuf *rb, size_t offset); 103 104 /* ngtcp2_ringbuf_len returns the number of elements stored. */ 105 #define ngtcp2_ringbuf_len(RB) ((RB)->len) 106 107 /* ngtcp2_ringbuf_full returns nonzero if |rb| is full. */ 108 int ngtcp2_ringbuf_full(ngtcp2_ringbuf *rb); 109 110 #endif /* NGTCP2_RINGBUF_H */ 111