ring_buf
Loading...
Searching...
No Matches
ring_buf_circ.c
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: MIT
3 * SPDX-FileCopyrightText: 2024, Roy Ratcliffe, Northumberland, United Kingdom
4 */
31#include "ring_buf_circ.h"
32#include "ring_buf.h"
33
34/*
35 * Add a new item to the ring buffer. If the circular buffer is full, remove the
36 * oldest item first.
37 *
38 * Fail if the new data will not fit. This should not happen if the buffer is
39 * sized correctly. It will never happen if the buffer size is a multiple of the
40 * data size.
41 */
42int ring_buf_put_circ(struct ring_buf *buf, void *data, size_t size) {
43 if (ring_buf_is_full(buf))
44 (void)ring_buf_get_ack(buf, ring_buf_get(buf, NULL, size));
45 if (size > ring_buf_free_space(buf))
46 return -EMSGSIZE;
47 return ring_buf_put_ack(buf, ring_buf_put(buf, data, size));
48}
int ring_buf_put_ack(struct ring_buf *buf, ring_buf_size_t size)
Acknowledges space claimed for putting data into a ring buffer.
Definition ring_buf.c:117
int ring_buf_get_ack(struct ring_buf *buf, ring_buf_size_t size)
Acknowledges space claimed for getting data from a ring buffer.
Definition ring_buf.c:143
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
ring_buf_size_t ring_buf_put(struct ring_buf *buf, const void *data, ring_buf_size_t size)
Puts non-contiguous bytes into the ring buffer.
Definition ring_buf.c:153
static bool ring_buf_is_full(const struct ring_buf *buf)
Checks if the ring buffer is full.
Definition ring_buf.h:195
static ring_buf_size_t ring_buf_free_space(const struct ring_buf *buf)
Calculates free space in the ring buffer.
Definition ring_buf.h:183
Ring buffer function prototypes.
#define EMSGSIZE
Message size error code.
Definition ring_buf.h:51
int ring_buf_put_circ(struct ring_buf *buf, void *data, size_t size)
Put data into a circular buffer.
Circular ring buffer function prototypes.
Ring buffer instance.
Definition ring_buf.h:128