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. |
16 | typedef 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. |
25 | typedef struct _ring_buffer |
26 | { |
27 | u8 *data; |
28 | ring_buffer_pos_t pos; |
29 | } ring_buffer_t; |
30 | |
31 | MOSAPI ring_buffer_t *ring_buffer_create(size_t capacity); |
32 | MOSAPI ring_buffer_t *ring_buffer_create_at(void *data, size_t capacity); |
33 | MOSAPI void ring_buffer_pos_init(ring_buffer_pos_t *pos, size_t capacity); |
34 | MOSAPI void ring_buffer_destroy(ring_buffer_t *buffer); |
35 | |
36 | MOSAPI bool ring_buffer_resize(ring_buffer_t *buffer, size_t new_capacity); |
37 | |
38 | MOSAPI size_t ring_buffer_pos_push_back(u8 *buffer, ring_buffer_pos_t *pos, const u8 *data, size_t size); |
39 | MOSAPI size_t ring_buffer_pos_pop_back(u8 *buffer, ring_buffer_pos_t *pos, u8 *buf, size_t size); |
40 | |
41 | MOSAPI size_t ring_buffer_pos_push_front(u8 *buffer, ring_buffer_pos_t *pos, const u8 *data, size_t size); |
42 | MOSAPI size_t ring_buffer_pos_pop_front(u8 *buffer, ring_buffer_pos_t *pos, u8 *buf, size_t size); |
43 | |
44 | should_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 | |
51 | should_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 |
59 | should_inline bool ring_buffer_pos_is_full(ring_buffer_pos_t *pos) { return pos->count == pos->capacity; } |
60 | should_inline bool ring_buffer_pos_is_empty(ring_buffer_pos_t *pos) { return pos->count == 0; } |
61 | |
62 | should_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); } |
63 | should_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 | |
67 | should_inline bool ring_buffer_is_full(ring_buffer_t *buffer) { return ring_buffer_pos_is_full(pos: &buffer->pos); } |
68 | should_inline bool ring_buffer_is_empty(ring_buffer_t *buffer) { return ring_buffer_pos_is_empty(pos: &buffer->pos); } |
69 | |
70 | should_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); } |
71 | should_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 | |
73 | should_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); } |
74 | should_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 | |
76 | should_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 | |
78 | should_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); } |
79 | should_inline u8 ring_buffer_pop_back_byte(ring_buffer_t *buffer) { return ring_buffer_pos_pop_back_byte(buffer: buffer->data, pos: &buffer->pos); } |
80 | should_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 | |