Skip to content

Commit ef0561c

Browse files
committed
ringbuf2: Add documentation.
1 parent 289e2a7 commit ef0561c

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

src/csnip/ringbuf2.h

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
#include <stdbool.h>
2828
#include <stddef.h>
2929

30+
/** Ringbuffer type.
31+
*
32+
* Stores the capacity of the ring buffer and the positions of
33+
* reader & writer.
34+
*/
3035
typedef struct {
3136
/** buffer capacity.
3237
*
@@ -49,28 +54,137 @@ typedef struct {
4954
size_t n_read;
5055
} csnip_ringbuf2;
5156

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+
*/
5267
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+
*/
5381
csnip_ringbuf2 csnip_ringbuf2_make(size_t min_cap);
5482

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+
*/
5588
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+
*/
5696
size_t csnip_ringbuf2_free_size(const csnip_ringbuf2* rb);
5797

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+
*/
58116
size_t csnip_ringbuf2_get_write_idx(const csnip_ringbuf2* rb,
59117
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+
*/
60144
int csnip_ringbuf2_get_write_areas(const csnip_ringbuf2* rb,
61145
size_t* ret_idx_0,
62146
size_t* ret_len_0,
63147
size_t* ret_idx_1,
64148
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+
*/
65162
bool csnip_ringbuf2_add_written(csnip_ringbuf2* rb, size_t n_written);
66163

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+
*/
67169
size_t csnip_ringbuf2_get_read_idx(const csnip_ringbuf2* rb,
68170
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+
*/
69177
int csnip_ringbuf2_get_read_areas(const csnip_ringbuf2* rb,
70178
size_t* ret_idx_0,
71179
size_t* ret_len_0,
72180
size_t* ret_idx_1,
73181
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+
*/
74188
bool csnip_ringbuf2_add_read(csnip_ringbuf2* rb, size_t n_read);
75189

76190
#endif /* CSNIP_RINGBUF2_H */

0 commit comments

Comments
 (0)