MOS Source Code
Loading...
Searching...
No Matches
ring_buffer.c
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
4#include <mos/moslib_global.h>
5#include <mos_stdlib.h>
6#include <mos_string.h>
7
8static u8 ring_buffer_get(u8 *data, ring_buffer_pos_t *pos, size_t index)
9{
10 return data[(pos->head + index) % pos->count];
11}
12
14{
15 if (capacity == 0)
16 return NULL; // forget about it
17
18 ring_buffer_t *rb = kmalloc(sizeof(ring_buffer_t));
19 if (!rb)
20 return NULL;
21 rb->data = kmalloc(capacity);
22 if (!rb->data)
23 {
24 kfree(rb);
25 return NULL;
26 }
27 ring_buffer_pos_init(&rb->pos, capacity);
28 return rb;
29}
30
31ring_buffer_t *ring_buffer_create_at(void *data, size_t capacity)
32{
33 if (capacity == 0)
34 return NULL; // forget about it
35
36 ring_buffer_t *rb = kmalloc(sizeof(ring_buffer_t));
37 if (!rb)
38 return NULL;
39 rb->data = data;
40 ring_buffer_pos_init(&rb->pos, capacity);
41 return rb;
42}
43
44void ring_buffer_pos_init(ring_buffer_pos_t *pos, size_t capacity)
45{
46 pos->capacity = capacity;
47 pos->count = 0;
48 pos->head = 0;
49 pos->next_pos = 0;
50}
51
53{
54 kfree(buffer->data);
55 kfree(buffer);
56}
57
58bool ring_buffer_resize(ring_buffer_t *buffer, size_t new_capacity)
59{
60 if (new_capacity < buffer->pos.count)
61 return false;
62 void *new_data = kmalloc(new_capacity);
63 if (!new_data)
64 return false;
65 size_t i = 0;
66 while (i < buffer->pos.count)
67 {
68 ((char *) new_data)[i] = ring_buffer_get(buffer->data, &buffer->pos, i);
69 i++;
70 }
71
72 kfree(buffer->data);
73 buffer->data = new_data;
74 buffer->pos.capacity = new_capacity;
75 buffer->pos.head = 0;
76 buffer->pos.next_pos = buffer->pos.count;
77 return true;
78}
79
80size_t ring_buffer_pos_push_back(u8 *data, ring_buffer_pos_t *pos, const u8 *target, size_t size)
81{
82 if (pos->count + size > pos->capacity)
83 size = pos->capacity - pos->count;
84
85 size_t first_part_i = pos->next_pos;
86 size_t first_part_size = MIN(size, pos->capacity - pos->next_pos);
87 size_t second_part = size - first_part_size;
88 memcpy(data + first_part_i, target, first_part_size);
89 memcpy(data, target + first_part_size, second_part);
90 pos->next_pos = (pos->next_pos + size) % pos->capacity;
91 pos->count += size;
92 return size;
93}
94
95size_t ring_buffer_pos_pop_back(u8 *data, ring_buffer_pos_t *pos, u8 *target, size_t size)
96{
97 if (size > pos->count)
98 size = pos->count;
99
100 size_t first_part_i = (pos->capacity + pos->next_pos - size) % pos->capacity;
101 size_t first_part_size = MIN(size, pos->capacity - first_part_i);
102
103 size_t second_part_i = 0;
104 size_t second_part_size = size - first_part_size;
105
106 memcpy(target, data + first_part_i, first_part_size);
107 memcpy(target + first_part_size, data + second_part_i, second_part_size);
108
109 pos->next_pos = (pos->capacity + pos->next_pos - size) % pos->capacity;
110 pos->count -= size;
111
112 return size;
113}
114
115size_t ring_buffer_pos_push_front(u8 *data, ring_buffer_pos_t *pos, const u8 *target, size_t size)
116{
117 if (pos->count + size > pos->capacity)
118 return 0;
119
120 size_t first_part_i = (pos->capacity + pos->head - size) % pos->capacity;
121 size_t first_part_size = MIN(size, pos->capacity - first_part_i);
122
123 size_t second_part_i = 0;
124 size_t second_part_size = size - first_part_size;
125
126 memcpy(data + first_part_i, target, first_part_size);
127 memcpy(data + second_part_i, target + first_part_size, second_part_size);
128
129 pos->head = (pos->capacity + pos->head - size) % pos->capacity;
130 pos->count += size;
131 return size;
132}
133
134size_t ring_buffer_pos_pop_front(u8 *data, ring_buffer_pos_t *pos, u8 *target, size_t size)
135{
136 if (size > pos->count)
137 size = pos->count;
138
139 size_t first_part_i = pos->head;
140 size_t first_part_size = MIN(size, pos->capacity - first_part_i);
141
142 size_t second_part_i = 0;
143 size_t second_part_size = size - first_part_size;
144
145 memcpy(target, data + first_part_i, first_part_size);
146 memcpy(target + first_part_size, data + second_part_i, second_part_size);
147
148 pos->head = (pos->head + size) % pos->capacity;
149 pos->count -= size;
150 return size;
151}
#define MIN(a, b)
Definition mos_stdlib.h:30
void ring_buffer_destroy(ring_buffer_t *buffer)
Definition ring_buffer.c:52
ring_buffer_t * ring_buffer_create_at(void *data, size_t capacity)
Definition ring_buffer.c:31
size_t ring_buffer_pos_pop_back(u8 *data, ring_buffer_pos_t *pos, u8 *target, size_t size)
Definition ring_buffer.c:95
size_t ring_buffer_pos_pop_front(u8 *data, ring_buffer_pos_t *pos, u8 *target, size_t size)
size_t ring_buffer_pos_push_front(u8 *data, ring_buffer_pos_t *pos, const u8 *target, size_t size)
bool ring_buffer_resize(ring_buffer_t *buffer, size_t new_capacity)
Definition ring_buffer.c:58
void ring_buffer_pos_init(ring_buffer_pos_t *pos, size_t capacity)
Definition ring_buffer.c:44
ring_buffer_t * ring_buffer_create(size_t capacity)
Definition ring_buffer.c:13
size_t ring_buffer_pos_push_back(u8 *data, ring_buffer_pos_t *pos, const u8 *target, size_t size)
Definition ring_buffer.c:80
static void * memcpy(void *s1, const void *s2, size_t n)
Definition pb_syshdr.h:90
#define NULL
Definition pb_syshdr.h:46
static u8 ring_buffer_get(u8 *data, ring_buffer_pos_t *pos, size_t index)
Definition ring_buffer.c:8
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