1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
5#include <mos/moslib_global.h>
6#include <mos/types.h>
7
8/**
9 * @defgroup ringbuffer libs.RingBuffer
10 * @ingroup libs
11 * @brief A ring buffer.
12 * @{
13 */
14
15// A position-only ring buffer specifier.
16typedef struct _ring_buffer_pos
17{
18 size_t count;
19 size_t capacity;
20 size_t head; // index of the first element
21 size_t next_pos; // index of the next element to be inserted
22} ring_buffer_pos_t;
23
24// A managed ring buffer.
25typedef struct _ring_buffer
26{
27 u8 *data;
28 ring_buffer_pos_t pos;
29} ring_buffer_t;
30
31MOSAPI ring_buffer_t *ring_buffer_create(size_t capacity);
32MOSAPI ring_buffer_t *ring_buffer_create_at(void *data, size_t capacity);
33MOSAPI void ring_buffer_pos_init(ring_buffer_pos_t *pos, size_t capacity);
34MOSAPI void ring_buffer_destroy(ring_buffer_t *buffer);
35
36MOSAPI bool ring_buffer_resize(ring_buffer_t *buffer, size_t new_capacity);
37
38MOSAPI size_t ring_buffer_pos_push_back(u8 *buffer, ring_buffer_pos_t *pos, const u8 *data, size_t size);
39MOSAPI size_t ring_buffer_pos_pop_back(u8 *buffer, ring_buffer_pos_t *pos, u8 *buf, size_t size);
40
41MOSAPI size_t ring_buffer_pos_push_front(u8 *buffer, ring_buffer_pos_t *pos, const u8 *data, size_t size);
42MOSAPI size_t ring_buffer_pos_pop_front(u8 *buffer, ring_buffer_pos_t *pos, u8 *buf, size_t size);
43
44should_inline u8 ring_buffer_pos_pop_back_byte(u8 *buffer, ring_buffer_pos_t *pos)
45{
46 u8 data = '\0';
47 ring_buffer_pos_pop_back(buffer, pos, buf: &data, size: 1);
48 return data;
49}
50
51should_inline u8 ring_buffer_pos_pop_front_byte(u8 *buffer, ring_buffer_pos_t *pos)
52{
53 u8 data = '\0';
54 ring_buffer_pos_pop_front(buffer, pos, buf: &data, size: 1);
55 return data;
56}
57
58// clang-format off
59should_inline bool ring_buffer_pos_is_full(ring_buffer_pos_t *pos) { return pos->count == pos->capacity; }
60should_inline bool ring_buffer_pos_is_empty(ring_buffer_pos_t *pos) { return pos->count == 0; }
61
62should_inline size_t ring_buffer_pos_push_back_byte(u8 *buffer, ring_buffer_pos_t *pos, u8 data) { return ring_buffer_pos_push_back(buffer, pos, data: &data, size: 1); }
63should_inline size_t ring_buffer_pos_push_front_byte(u8 *buffer, ring_buffer_pos_t *pos, u8 data) { return ring_buffer_pos_push_front(buffer, pos, data: &data, size: 1); }
64
65// ring_buffer_t wrapper functions.
66
67should_inline bool ring_buffer_is_full(ring_buffer_t *buffer) { return ring_buffer_pos_is_full(pos: &buffer->pos); }
68should_inline bool ring_buffer_is_empty(ring_buffer_t *buffer) { return ring_buffer_pos_is_empty(pos: &buffer->pos); }
69
70should_inline size_t ring_buffer_push_back(ring_buffer_t *buffer, const u8 *data, size_t size) { return ring_buffer_pos_push_back(buffer: buffer->data, pos: &buffer->pos, data, size); }
71should_inline size_t ring_buffer_pop_back(ring_buffer_t *buffer, u8 *buf, size_t size) { return ring_buffer_pos_pop_back(buffer: buffer->data, pos: &buffer->pos, buf, size); }
72
73should_inline size_t ring_buffer_push_front(ring_buffer_t *buffer, const u8 *data, size_t size) { return ring_buffer_pos_push_front(buffer: buffer->data, pos: &buffer->pos, data, size); }
74should_inline size_t ring_buffer_pop_front(ring_buffer_t *buffer, u8 *buf, size_t size) { return ring_buffer_pos_pop_front(buffer: buffer->data, pos: &buffer->pos, buf, size); }
75
76should_inline size_t ring_buffer_push_front_byte(ring_buffer_t *buffer, u8 byte) { return ring_buffer_pos_push_front_byte(buffer: buffer->data, pos: &buffer->pos, data: byte); }
77
78should_inline size_t ring_buffer_push_back_byte(ring_buffer_t *buffer, u8 byte) { return ring_buffer_pos_push_back_byte(buffer: buffer->data, pos: &buffer->pos, data: byte); }
79should_inline u8 ring_buffer_pop_back_byte(ring_buffer_t *buffer) { return ring_buffer_pos_pop_back_byte(buffer: buffer->data, pos: &buffer->pos); }
80should_inline u8 ring_buffer_pop_front_byte(ring_buffer_t *buffer) { return ring_buffer_pos_pop_front_byte(buffer: buffer->data, pos: &buffer->pos); }
81// clang-format on
82/** @} */
83