MOS Source Code
Loading...
Searching...
No Matches
ring_buffer.h
Go to the documentation of this file.
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
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
23
24// A managed ring buffer.
25typedef struct _ring_buffer
26{
30
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);
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);
40
41MOSAPI size_t ring_buffer_pos_push_front(u8 *buffer, ring_buffer_pos_t *pos, const u8 *data, size_t size);
43
45{
46 u8 data = '\0';
47 ring_buffer_pos_pop_back(buffer, pos, &data, 1);
48 return data;
49}
50
52{
53 u8 data = '\0';
54 ring_buffer_pos_pop_front(buffer, pos, &data, 1);
55 return data;
56}
57
58// clang-format off
61
64
65// ring_buffer_t wrapper functions.
66
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->data, &buffer->pos, data, 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->data, &buffer->pos, data, size); }
75
77
81// clang-format on
should_inline u8 ring_buffer_pos_pop_front_byte(u8 *buffer, ring_buffer_pos_t *pos)
Definition ring_buffer.h:51
MOSAPI void ring_buffer_destroy(ring_buffer_t *buffer)
Definition ring_buffer.c:52
MOSAPI ring_buffer_t * ring_buffer_create_at(void *data, size_t capacity)
Definition ring_buffer.c:31
should_inline size_t ring_buffer_pos_push_front_byte(u8 *buffer, ring_buffer_pos_t *pos, u8 data)
Definition ring_buffer.h:63
should_inline size_t ring_buffer_pop_front(ring_buffer_t *buffer, u8 *buf, size_t size)
Definition ring_buffer.h:74
should_inline size_t ring_buffer_push_back_byte(ring_buffer_t *buffer, u8 byte)
Definition ring_buffer.h:78
should_inline bool ring_buffer_is_empty(ring_buffer_t *buffer)
Definition ring_buffer.h:68
should_inline bool ring_buffer_is_full(ring_buffer_t *buffer)
Definition ring_buffer.h:67
MOSAPI size_t ring_buffer_pos_pop_back(u8 *buffer, ring_buffer_pos_t *pos, u8 *buf, size_t size)
Definition ring_buffer.c:95
should_inline size_t ring_buffer_push_front_byte(ring_buffer_t *buffer, u8 byte)
Definition ring_buffer.h:76
should_inline bool ring_buffer_pos_is_full(ring_buffer_pos_t *pos)
Definition ring_buffer.h:59
MOSAPI size_t ring_buffer_pos_pop_front(u8 *buffer, ring_buffer_pos_t *pos, u8 *buf, size_t size)
should_inline size_t ring_buffer_push_back(ring_buffer_t *buffer, const u8 *data, size_t size)
Definition ring_buffer.h:70
should_inline size_t ring_buffer_pop_back(ring_buffer_t *buffer, u8 *buf, size_t size)
Definition ring_buffer.h:71
MOSAPI size_t ring_buffer_pos_push_front(u8 *buffer, ring_buffer_pos_t *pos, const u8 *data, size_t size)
MOSAPI bool ring_buffer_resize(ring_buffer_t *buffer, size_t new_capacity)
Definition ring_buffer.c:58
should_inline u8 ring_buffer_pop_front_byte(ring_buffer_t *buffer)
Definition ring_buffer.h:80
should_inline bool ring_buffer_pos_is_empty(ring_buffer_pos_t *pos)
Definition ring_buffer.h:60
MOSAPI void ring_buffer_pos_init(ring_buffer_pos_t *pos, size_t capacity)
Definition ring_buffer.c:44
should_inline u8 ring_buffer_pop_back_byte(ring_buffer_t *buffer)
Definition ring_buffer.h:79
should_inline u8 ring_buffer_pos_pop_back_byte(u8 *buffer, ring_buffer_pos_t *pos)
Definition ring_buffer.h:44
MOSAPI ring_buffer_t * ring_buffer_create(size_t capacity)
Definition ring_buffer.c:13
MOSAPI size_t ring_buffer_pos_push_back(u8 *buffer, ring_buffer_pos_t *pos, const u8 *data, size_t size)
Definition ring_buffer.c:80
should_inline size_t ring_buffer_push_front(ring_buffer_t *buffer, const u8 *data, size_t size)
Definition ring_buffer.h:73
should_inline size_t ring_buffer_pos_push_back_byte(u8 *buffer, ring_buffer_pos_t *pos, u8 data)
Definition ring_buffer.h:62
#define MOSAPI
Definition mos_global.h:102
#define should_inline
Definition mos_global.h:37
size_t size
Definition slab.c:30
ring_buffer_pos_t pos
Definition ring_buffer.h:28
static char buffer[2048]
Definition test_printf.c:7
unsigned char u8
Definition types.h:19