27
27
#include <stdbool.h>
28
28
#include <stddef.h>
29
29
30
+ /** Ringbuffer type.
31
+ *
32
+ * Stores the capacity of the ring buffer and the positions of
33
+ * reader & writer.
34
+ */
30
35
typedef struct {
31
36
/** buffer capacity.
32
37
*
@@ -49,28 +54,137 @@ typedef struct {
49
54
size_t n_read ;
50
55
} csnip_ringbuf2 ;
51
56
57
+ /** Initialize a ringbuffer.
58
+ *
59
+ * @param min_cap
60
+ * the minimum required capacity of the ring buffer. Since
61
+ * the ringbuffer size must be a power of 2, the actual
62
+ * capacity used will be this value rounded up to the next
63
+ * power of 2.
64
+ *
65
+ * @return the used capacity.
66
+ */
52
67
size_t csnip_ringbuf2_init (csnip_ringbuf2 * rb , size_t min_cap );
68
+
69
+ /** Create a ringbuffer.
70
+ *
71
+ * Returns an initialized ringbuffer of the given minimum
72
+ * capacity. Can be used instead of csnip_ringbuf2_init() if
73
+ * initializer syntax is more convenient.
74
+ *
75
+ * @param min_cap
76
+ * the minimum capacity. The actual value will be rounded
77
+ * up to the next power of 2, @sa csnip_ringbuf2_init()
78
+ *
79
+ * @sa csnip_ringbuf2_init()
80
+ */
53
81
csnip_ringbuf2 csnip_ringbuf2_make (size_t min_cap );
54
82
83
+ /** Return the number of occupied entries.
84
+ *
85
+ * Returns the number of ring buffer entries that are occupied,
86
+ * i.e., written, but not yet consumed with write().
87
+ */
55
88
size_t csnip_ringbuf2_used_size (const csnip_ringbuf2 * rb );
89
+
90
+ /** Return the number of unoccupied entries.
91
+ *
92
+ * Returns the number of ring buffer entries that are unoccupied,
93
+ * i.e., that can still be written with out any reads until the
94
+ * buffer is full.
95
+ */
56
96
size_t csnip_ringbuf2_free_size (const csnip_ringbuf2 * rb );
57
97
98
+ /** Get the write position in the ring buffer.
99
+ *
100
+ * Retrieve the next index in the ring buffer that can be written
101
+ * to, and also, if desired, the amount of contiguous space left at
102
+ * that position.
103
+ *
104
+ * @param rb
105
+ * the ring buffer
106
+ *
107
+ * @param ret_contig_write_max
108
+ * if non-NULL the number of contiguous slots available at
109
+ * the write position will be returned into
110
+ * *ret_contig_write_max.
111
+ *
112
+ * @return next writable index. Note that calling this function on
113
+ * a full buffer is not an error, but writing to that index
114
+ * would then overwrite a previous, not yet read entry.
115
+ */
58
116
size_t csnip_ringbuf2_get_write_idx (const csnip_ringbuf2 * rb ,
59
117
size_t * ret_contig_write_max );
118
+
119
+ /** Return the contiguous writable areas.
120
+ *
121
+ * Return the (at most 2) contiguous areas that are writable, and
122
+ * whose writing in order does fill the ring buffer in that order.
123
+ *
124
+ * @param ret_idx_0
125
+ * if non-NULL, the start index of the first area will be
126
+ * returned here, if applicable.
127
+ *
128
+ * @param ret_len_0
129
+ * if non-NULL, the length of the first area will be
130
+ * returned here, if applicable.
131
+ *
132
+ * @param ret_idx_1, ret_len_1
133
+ * same for the second area, if applicable.
134
+ *
135
+ * @return the number of areas:
136
+ * - 0 if the buffer is full and there are no writable
137
+ * positions.
138
+ * - 1 if the buffer can be filled by writing into a single
139
+ * contiguous area. Only non-NULL ret_idx_0 and ret_len_0
140
+ * will be set in that case.
141
+ * - 2 if two contiguous areas are needed to fill the
142
+ * buffer completely.
143
+ */
60
144
int csnip_ringbuf2_get_write_areas (const csnip_ringbuf2 * rb ,
61
145
size_t * ret_idx_0 ,
62
146
size_t * ret_len_0 ,
63
147
size_t * ret_idx_1 ,
64
148
size_t * ret_len_1 );
149
+
150
+ /** Update ringbuffer after adding entries.
151
+ *
152
+ * @param rb
153
+ * Ringbuffer
154
+ *
155
+ * @param n_written
156
+ * number of items that were written.
157
+ *
158
+ * @return true if the update was successful
159
+ * false if the update resulted in overflowing the
160
+ * buffer.
161
+ */
65
162
bool csnip_ringbuf2_add_written (csnip_ringbuf2 * rb , size_t n_written );
66
163
164
+ /** Get the next read position in the ring buffer.
165
+ *
166
+ * This is the analog to csnip_ringbuf2_get_write_idx() for reads.
167
+ * @sa csnip_ringbuf2_get_write_idx() for details.
168
+ */
67
169
size_t csnip_ringbuf2_get_read_idx (const csnip_ringbuf2 * rb ,
68
170
size_t * ret_contig_read_max );
171
+
172
+ /** Get the contiguous readable areas.
173
+ *
174
+ * This is the analog to csnip_ringbuf2_get_write_areas() for reads.
175
+ * @sa csnip_ringbuf2_get_write_areas() for details.
176
+ */
69
177
int csnip_ringbuf2_get_read_areas (const csnip_ringbuf2 * rb ,
70
178
size_t * ret_idx_0 ,
71
179
size_t * ret_len_0 ,
72
180
size_t * ret_idx_1 ,
73
181
size_t * ret_len_1 );
182
+
183
+ /** Update the ringbuffer after reading entries.
184
+ *
185
+ * Analog to csnip_ringbuf2_add_written(). @sa
186
+ * csnip_ringbuf2_add_written().
187
+ */
74
188
bool csnip_ringbuf2_add_read (csnip_ringbuf2 * rb , size_t n_read );
75
189
76
190
#endif /* CSNIP_RINGBUF2_H */
0 commit comments