The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
ring_buffer.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: e7a07e1c228a9fceb718c6c831d62a6037fc4ed5 $
19 *
20 * @brief Simple ring buffers for packet contents
21 * @file io/ring_buffer.c
22 *
23 * @copyright 2016 Alan DeKok (aland@freeradius.org)
24 */
25RCSID("$Id: e7a07e1c228a9fceb718c6c831d62a6037fc4ed5 $")
26
27#include <freeradius-devel/io/ring_buffer.h>
28#include <freeradius-devel/util/strerror.h>
29#include <freeradius-devel/util/debug.h>
30
31/*
32 * Ring buffers are allocated in a block.
33 */
35 uint8_t *buffer; //!< actual start of the ring buffer
36 size_t size; //!< Size of this ring buffer
37
38 size_t data_start; //!< start of used portion of the buffer
39 size_t data_end; //!< end of used portion of the buffer
40
41 size_t write_offset; //!< where writes are done
42 size_t reserved_offset; //!< where the reservation starts.
43 size_t reserved; //!< amount of reserved data at reserved_offset
44
45 bool closed; //!< whether allocations are closed
46};
47
48/** Create a ring buffer.
49 *
50 * The size provided will be rounded up to the next highest power of
51 * 2, if it's not already a power of 2.
52 *
53 * The ring buffer manages how much room is reserved (i.e. available
54 * to write to), and used. The application is responsible for
55 * tracking the start of the reservation, *and* it's write offset
56 * within that reservation.
57 *
58 * @param[in] ctx a talloc context
59 * @param[in] size of the raw ring buffer array to allocate.
60 * @return
61 * - A new ring buffer on success.
62 * - NULL on failure.
63 */
64fr_ring_buffer_t *fr_ring_buffer_create(TALLOC_CTX *ctx, size_t size)
65{
67
68 rb = talloc_zero(ctx, fr_ring_buffer_t);
69 if (!rb) {
70 fail:
71 fr_strerror_const("Failed allocating memory.");
72 return NULL;
73 }
74
75 if (size < 1024) size = 1024;
76
77 if (size > (1 << 30)) {
78 fr_strerror_const("Ring buffer size must be no more than (1 << 30)");
79 talloc_free(rb);
80 return NULL;
81 }
82
83 /*
84 * Round up to the nearest power of 2.
85 */
86 size--;
87 size |= size >> 1;
88 size |= size >> 2;
89 size |= size >> 4;
90 size |= size >> 8;
91 size |= size >> 16;
92 size++;
93
94 rb->buffer = talloc_array(rb, uint8_t, size);
95 if (!rb->buffer) {
96 talloc_free(rb);
97 goto fail;
98 }
99 rb->size = size;
100
101 return rb;
102}
103
104
105/** Reserve room in the ring buffer.
106 *
107 * The size does not need to be a power of two. The application is
108 * responsible for doing cache alignment, if required.
109 *
110 * If the reservation fails, the application should create a new ring
111 * buffer, and start reserving data there.
112 *
113 * @param[in] rb a ring buffer
114 * @param[in] size to see if we can reserve
115 * @return
116 * - NULL on error. Which can only be "ring buffer is full".
117 * - pointer to data on success
118 */
120{
121 (void) talloc_get_type_abort(rb, fr_ring_buffer_t);
122
123 if (rb->closed) {
124 fr_strerror_const("Allocation request after ring buffer is closed");
125 return NULL;
126 }
127
128 /*
129 * We're writing to the start of the buffer, and there is
130 * already data in it. See if the data fits.
131 *
132 * |***W....S****E....|
133 */
134 if (rb->write_offset < rb->data_start) {
135 if ((rb->write_offset + size) < rb->data_start) {
136 rb->reserved = size;
138 return rb->buffer + rb->write_offset;
139 }
140
141 fr_strerror_const("No memory available in ring buffer");
142 return NULL;
143 }
144
145 fr_assert(rb->write_offset == rb->data_end);
146
147 /*
148 * Data fits at the end of the ring buffer.
149 *
150 * |....S****WE....|
151 */
152 if ((rb->write_offset + size) <= rb->size) {
153 rb->reserved = size;
155 return rb->buffer + rb->write_offset;
156 }
157
158 /*
159 * Data fits at the start of the ring buffer, ensure that
160 * we write it there. This also catches the case where
161 * data_start==0.
162 *
163 * |W....S****E....|
164 */
165 if (size < rb->data_start) {
166 rb->write_offset = 0;
167 rb->reserved = size;
169 return rb->buffer;
170 }
171
172 /*
173 * Not enough room for the new data, fail the allocation.
174 *
175 * |....S****WE....|
176 */
177 fr_strerror_const("No memory available in ring buffer");
178 return NULL;
179}
180
181
182/** Mark data as allocated.
183 *
184 * The size does not need to be a power of two. The application is
185 * responsible for doing cache-line alignment, if required.
186 *
187 * The application does NOT need to call fr_ring_buffer_reserve() before
188 * calling this function.
189 *
190 * If the allocation fails, the application should create a new ring
191 * buffer, and start allocating data there.
192 *
193 * @param[in] rb a ring buffer
194 * @param[in] size to mark as "used" at the tail end of the buffer.
195 * @return
196 * - NULL on error. Which can only be "ring buffer is full".
197 * - pointer to data on success
198 */
200{
201 uint8_t *p;
202
203 (void) talloc_get_type_abort(rb, fr_ring_buffer_t);
204
205 if (rb->closed) {
206#ifndef NDEBUG
207 fr_strerror_const("Allocation request after ring buffer is closed");
208#endif
209 return NULL;
210 }
211
212 if (rb->reserved > 0) {
213 if (!rb->data_start && !rb->data_end) {
214 /*
215 * If, between reservation and allocation all entries are
216 * freed, then data_start and data_end will be zero.
217 * Set all the offsets to reserved_offset, so we
218 * return the previously reserved chunk.
219 */
220 rb->write_offset = rb->data_start = rb->data_end = rb->reserved_offset;
221 }
222 rb->reserved = 0;
223 }
224
225 /*
226 * We're writing to the start of the buffer, and there is
227 * already data in it. See if the data fits.
228 *
229 * |***W....S****E....|
230 */
231 if (rb->write_offset < rb->data_start) {
232 if ((rb->write_offset + size) < rb->data_start) {
233 p = rb->buffer + rb->write_offset;
234 rb->write_offset += size;
235 return p;
236 }
237
238#ifndef NDEBUG
239 fr_strerror_const("No memory available in ring buffer");
240#endif
241 return NULL;
242 }
243
244 fr_assert(rb->write_offset == rb->data_end);
245
246 /*
247 * Data fits at the end of the ring buffer.
248 *
249 * |....S****WE....|
250 */
251 if ((rb->write_offset + size) <= rb->size) {
252 p = rb->buffer + rb->write_offset;
253
254 rb->write_offset += size;
255 rb->data_end = rb->write_offset;
256
257 /*
258 * Don't update write_offset if it's fallen off
259 * of the end of the buffer. The data_start may
260 * be zero, and we don't want to over-write
261 * that.
262 */
263 return p;
264 }
265
266 /*
267 * Data fits at the start of the ring buffer, ensure that
268 * we write it there. This also catches the case where
269 * data_start==0.
270 *
271 * |W....S****E....|
272 */
273 if (size < rb->data_start) {
274 rb->write_offset = size;
275
276 /*
277 * Don't update data_end. It points to the tail
278 * end of the ring buffer.
279 */
280 return rb->buffer;
281 }
282
283 /*
284 * Not enough room for the new data, fail the allocation.
285 *
286 * |....S****WE....|
287 */
288#ifndef NDEBUG
289 fr_strerror_const("No memory available in ring buffer");
290#endif
291 return NULL;
292}
293
294/** Mark data as free,
295 *
296 * The size does not need to be a power of two. The application is
297 * responsible for doing cache alignment, if required. The
298 * application is responsible for tracking sizes of packets in the
299 * ring buffer.
300 *
301 * If "unused" bytes are more than what's in the buffer, the used
302 * bytes are reset to zero.
303 *
304 * @param[in] rb a ring buffer
305 * @param[in] size_to_free bytes to mark as "unused" in the buffer.
306 * @return
307 * - <0 on error. Which can only be "ring buffer is full".
308 * - 0 on success
309 */
310int fr_ring_buffer_free(fr_ring_buffer_t *rb, size_t size_to_free)
311{
312 size_t block_size;
313
314 (void) talloc_get_type_abort(rb, fr_ring_buffer_t);
315
316 /*
317 * Nothing to free, do nothing.
318 */
319 if (!size_to_free) return 0;
320
321 /*
322 * Freeing data from the middle of the buffer.
323 *
324 * |***W....S****E....|
325 */
326 if (rb->write_offset < rb->data_start) {
327 block_size = rb->data_end - rb->data_start;
328
329 /*
330 * |***W....S****E....|, free 3
331 *
332 * |***W.......S*E....|
333 */
334 if (size_to_free < block_size) {
335 rb->data_start += size_to_free;
336 return 0;
337 }
338
339 /*
340 * Free all (or more than) the block.
341 */
342 rb->data_start = 0;
343 rb->data_end = rb->write_offset;
344 size_to_free -= block_size;
345
346 /*
347 * Free everything left: empty the buffer
348 * entirely. This also handles the case of
349 * size_to_free==0 and write_offset==0.
350 */
351 if (size_to_free == rb->write_offset) {
352 goto empty_buffer;
353 }
354
355 /*
356 * The buffer has data but we're not freeing
357 * any more of it, return.
358 */
359 if (!size_to_free) return 0;
360 }
361
362 fr_assert(rb->write_offset == rb->data_end);
363
364 block_size = rb->data_end - rb->data_start;
365
366 /*
367 * Freeing too much, return an error.
368 */
369 if (size_to_free > block_size) {
370 fr_strerror_const("Cannot free more memory than exists.");
371 return -1;
372 }
373
374 /*
375 * Free some data from the start.
376 */
377 if (size_to_free < block_size) {
378 rb->data_start += size_to_free;
379 return 0;
380 }
381
382 /*
383 * Free all data in the buffer.
384 */
385empty_buffer:
386 rb->data_start = 0;
387 rb->data_end = 0;
388 rb->write_offset = 0;
389
390 /*
391 * If the ring buffer is closed to all allocations, and
392 * it's now empty, we automatically free it.
393 */
394 if (rb->closed) talloc_free(rb);
395
396 return 0;
397}
398
399/** Close a ring buffer so that no further allocations can take place.
400 *
401 * Once the ring buffer is empty, it will be automatically freed.
402 * It's called "close" and not "delete", because the ring buffer will
403 * still be active until all data has been removed.
404 *
405 * If you don't care about the data in the ring buffer, you can just
406 * call talloc_free() on it.
407 *
408 * @param[in] rb a ring buffer
409 * @return
410 * - <0 on error.
411 * - 0 on success
412 */
414{
415 (void) talloc_get_type_abort(rb, fr_ring_buffer_t);
416
417 rb->closed = true;
418 return 0;
419}
420
421
422/** Get the size of the ring buffer
423 *
424 * @param[in] rb a ring buffer
425 * @return size of the ring buffer.
426 * - <0 on error.
427 * - 0 on success
428 */
430{
431 (void) talloc_get_type_abort(rb, fr_ring_buffer_t);
432
433 return rb->size;
434}
435
436/** Get the amount of data used in a ring buffer.
437 *
438 * @param[in] rb a ring buffer
439 * @return size of the used data in the ring buffer.
440 * - <0 on error.
441 * - 0 on success
442 */
444{
445 size_t size;
446
447 (void) talloc_get_type_abort(rb, fr_ring_buffer_t);
448
449 if (rb->write_offset < rb->data_start) {
450 size = rb->write_offset;
451 } else {
452 fr_assert(rb->write_offset == rb->data_end);
453 size = 0;
454 }
455
456 size += (rb->data_end - rb->data_start);
457
458 return size;
459}
460
461/** Get a pointer to the data at the start of the ring buffer.
462 *
463 * @param[in] rb a ring buffer
464 * @param[out] p_start pointer to data at the start of the ring buffer
465 * @param[in] p_size size of the allocated block at the start of the ring buffer.
466 * @return size of the used data in the ring buffer.
467 * - <0 on error.
468 * - 0 on success
469 */
470int fr_ring_buffer_start(fr_ring_buffer_t *rb, uint8_t **p_start, size_t *p_size)
471{
472 (void) talloc_get_type_abort(rb, fr_ring_buffer_t);
473
474 *p_start = rb->buffer + rb->data_start;
475
476 if (rb->write_offset < rb->data_start) {
477 *p_size = rb->data_end - rb->data_start;
478 return 0;
479 }
480
481 *p_size = (rb->data_end - rb->data_start);
482
483 return 0;
484}
485
486/** Print debug information about the ring buffer
487 *
488 * @param[in] rb the ring buffer
489 * @param[in] fp the FILE where the messages are printed.
490 */
492{
493 fprintf(fp, "Buffer %p, write_offset %zu, data_start %zu, data_end %zu\n",
494 rb->buffer, rb->write_offset, rb->data_start, rb->data_end);
495}
#define RCSID(id)
Definition build.h:512
talloc_free(hp)
unsigned char uint8_t
#define fr_assert(_expr)
Definition rad_assert.h:37
size_t data_start
start of used portion of the buffer
Definition ring_buffer.c:38
fr_ring_buffer_t * fr_ring_buffer_create(TALLOC_CTX *ctx, size_t size)
Create a ring buffer.
Definition ring_buffer.c:64
uint8_t * fr_ring_buffer_alloc(fr_ring_buffer_t *rb, size_t size)
Mark data as allocated.
size_t reserved
amount of reserved data at reserved_offset
Definition ring_buffer.c:43
uint8_t * fr_ring_buffer_reserve(fr_ring_buffer_t *rb, size_t size)
Reserve room in the ring buffer.
size_t size
Size of this ring buffer.
Definition ring_buffer.c:36
size_t reserved_offset
where the reservation starts.
Definition ring_buffer.c:42
int fr_ring_buffer_free(fr_ring_buffer_t *rb, size_t size_to_free)
Mark data as free,.
size_t write_offset
where writes are done
Definition ring_buffer.c:41
bool closed
whether allocations are closed
Definition ring_buffer.c:45
size_t data_end
end of used portion of the buffer
Definition ring_buffer.c:39
int fr_ring_buffer_close(fr_ring_buffer_t *rb)
Close a ring buffer so that no further allocations can take place.
size_t fr_ring_buffer_used(fr_ring_buffer_t *rb)
Get the amount of data used in a ring buffer.
void fr_ring_buffer_debug(FILE *fp, fr_ring_buffer_t *rb)
Print debug information about the ring buffer.
size_t fr_ring_buffer_size(fr_ring_buffer_t *rb)
Get the size of the ring buffer.
int fr_ring_buffer_start(fr_ring_buffer_t *rb, uint8_t **p_start, size_t *p_size)
Get a pointer to the data at the start of the ring buffer.
uint8_t * buffer
actual start of the ring buffer
Definition ring_buffer.c:35
#define fr_strerror_const(_msg)
Definition strerror.h:223