ring_buf
Loading...
Searching...
No Matches
ring_buf_item.c
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: MIT
3 * SPDX-FileCopyrightText: 2024, Roy Ratcliffe, Northumberland, United Kingdom
4 */
33#include "ring_buf_item.h"
34
35int ring_buf_item_put(struct ring_buf *buf, const void *item,
37 if (sizeof(length) + length > ring_buf_free_space(buf))
38 return -EMSGSIZE;
39 const ring_buf_size_t claim = ring_buf_put(buf, &length, sizeof(length));
40 return claim + ring_buf_put(buf, item, length);
41}
42
43int ring_buf_item_get(struct ring_buf *buf, void *item,
44 ring_buf_item_length_t *length) {
45 if (ring_buf_is_empty(buf))
46 return -EAGAIN;
47 const ring_buf_size_t claim = ring_buf_get(buf, length, sizeof(*length));
48 return claim + ring_buf_get(buf, item, *length);
49}
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_empty(const struct ring_buf *buf)
Checks if the ring buffer is empty.
Definition ring_buf.h:172
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
size_t ring_buf_size_t
Ring buffer size type.
Definition ring_buf.h:64
#define EMSGSIZE
Message size error code.
Definition ring_buf.h:51
int ring_buf_item_put(struct ring_buf *buf, const void *item, ring_buf_item_length_t length)
Puts an item's length and content.
int ring_buf_item_get(struct ring_buf *buf, void *item, ring_buf_item_length_t *length)
Gets an item from a ring buffer.
Header file for ring buffer item functions.
uint16_t ring_buf_item_length_t
Size of the ring buffer's item length.
Ring buffer instance.
Definition ring_buf.h:128