• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * nghttp3
3  *
4  * Copyright (c) 2019 nghttp3 contributors
5  * Copyright (c) 2017 ngtcp2 contributors
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26 #ifndef NGHTTP3_RINGBUF_H
27 #define NGHTTP3_RINGBUF_H
28 
29 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif /* HAVE_CONFIG_H */
32 
33 #include <nghttp3/nghttp3.h>
34 
35 #include "nghttp3_mem.h"
36 
37 typedef struct nghttp3_ringbuf {
38   /* buf points to the underlying buffer. */
39   uint8_t *buf;
40   const nghttp3_mem *mem;
41   /* nmemb is the number of elements that can be stored in this ring
42      buffer. */
43   size_t nmemb;
44   /* size is the size of each element. */
45   size_t size;
46   /* first is the offset to the first element. */
47   size_t first;
48   /* len is the number of elements actually stored. */
49   size_t len;
50 } nghttp3_ringbuf;
51 
52 /*
53  * nghttp3_ringbuf_init initializes |rb|.  |nmemb| is the number of
54  * elements that can be stored in this buffer.  |size| is the size of
55  * each element.  |size| must be power of 2.
56  *
57  * This function returns 0 if it succeeds, or one of the following
58  * negative error codes:
59  *
60  * NGHTTP3_ERR_NOMEM
61  *     Out of memory.
62  */
63 int nghttp3_ringbuf_init(nghttp3_ringbuf *rb, size_t nmemb, size_t size,
64                          const nghttp3_mem *mem);
65 
66 /*
67  * nghttp3_ringbuf_free frees resources allocated for |rb|.  This
68  * function does not free the memory pointed by |rb|.
69  */
70 void nghttp3_ringbuf_free(nghttp3_ringbuf *rb);
71 
72 /* nghttp3_ringbuf_push_front moves the offset to the first element in
73    the buffer backward, and returns the pointer to the element.
74    Caller can store data to the buffer pointed by the returned
75    pointer.  If this action exceeds the capacity of the ring buffer,
76    the last element is silently overwritten, and rb->len remains
77    unchanged. */
78 void *nghttp3_ringbuf_push_front(nghttp3_ringbuf *rb);
79 
80 /* nghttp3_ringbuf_push_back moves the offset to the last element in
81    the buffer forward, and returns the pointer to the element.  Caller
82    can store data to the buffer pointed by the returned pointer.  If
83    this action exceeds the capacity of the ring buffer, the first
84    element is silently overwritten, and rb->len remains unchanged. */
85 void *nghttp3_ringbuf_push_back(nghttp3_ringbuf *rb);
86 
87 /*
88  * nghttp3_ringbuf_pop_front removes first element in |rb|.
89  */
90 void nghttp3_ringbuf_pop_front(nghttp3_ringbuf *rb);
91 
92 /*
93  * nghttp3_ringbuf_pop_back removes the last element in |rb|.
94  */
95 void nghttp3_ringbuf_pop_back(nghttp3_ringbuf *rb);
96 
97 /* nghttp3_ringbuf_resize changes the number of elements stored.  This
98    does not change the capacity of the underlying buffer. */
99 void nghttp3_ringbuf_resize(nghttp3_ringbuf *rb, size_t len);
100 
101 /* nghttp3_ringbuf_get returns the pointer to the element at
102    |offset|. */
103 void *nghttp3_ringbuf_get(nghttp3_ringbuf *rb, size_t offset);
104 
105 /* nghttp3_ringbuf_len returns the number of elements stored. */
106 #define nghttp3_ringbuf_len(RB) ((RB)->len)
107 
108 /* nghttp3_ringbuf_full returns nonzero if |rb| is full. */
109 int nghttp3_ringbuf_full(nghttp3_ringbuf *rb);
110 
111 int nghttp3_ringbuf_reserve(nghttp3_ringbuf *rb, size_t nmemb);
112 
113 #endif /* NGHTTP3_RINGBUF_H */
114