ring_buf
Loading...
Searching...
No Matches
ring_buf_yield.c
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: MIT
3 * SPDX-FileCopyrightText: 2024, Roy Ratcliffe, Northumberland, United Kingdom
4 */
32#include "ring_buf_yield.h"
33
35 int yield(void *space, int index, void *extra),
36 void *extra) {
37 int index = 0;
38 void *space;
39 while (ring_buf_get_claim(buf, &space, size) == size) {
40 /*
41 * Call the yield function with the claimed buffer span.
42 * If the yield function returns anything other than -EAGAIN,
43 * terminate the yielding process and return that value.
44 */
45 int yielded = yield(space, index, extra);
46 if (yielded != -EAGAIN)
47 return yielded;
48 index++;
49 }
50 return index;
51}
52
53int ring_buf_get_yield(struct ring_buf *buf, void *data, ring_buf_size_t size,
54 int yield(void *data, int index, void *extra),
55 void *extra) {
56 int index = 0;
57 while (ring_buf_get(buf, data, size) == size) {
58 int yielded = yield(data, index, extra);
59 if (yielded != -EAGAIN)
60 return yielded;
61 index++;
62 }
63 return index;
64}
ring_buf_size_t ring_buf_get_claim(struct ring_buf *buf, void **space, ring_buf_size_t size)
Claims contiguous space for getting.
Definition ring_buf.c:127
ring_buf_size_t ring_buf_get(struct ring_buf *buf, void *data, ring_buf_size_t size)
Gets data from a ring buffer.
Definition ring_buf.c:166
size_t ring_buf_size_t
Ring buffer size type.
Definition ring_buf.h:64
int ring_buf_get_yield(struct ring_buf *buf, void *data, ring_buf_size_t size, int yield(void *data, int index, void *extra), void *extra)
Copies buffer space and yields it to a callback function.
int ring_buf_get_claim_yield(struct ring_buf *buf, ring_buf_size_t size, int yield(void *space, int index, void *extra), void *extra)
Claims ring buffer space and yields it to a callback function.
Header file for ring buffer yielding functions.
Ring buffer instance.
Definition ring_buf.h:128