-
Notifications
You must be signed in to change notification settings - Fork 15
/
chry_ringbuffer.c
474 lines (411 loc) · 14.1 KB
/
chry_ringbuffer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
* Copyright (c) 2022, Egahp
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include "chry_ringbuffer.h"
/*****************************************************************************
* @brief init ringbuffer
*
* @param[in] rb ringbuffer instance
* @param[in] pool memory pool address
* @param[in] size memory size in byte,
* must be power of 2 !!!
*
* @retval int 0:Success -1:Error
*****************************************************************************/
int chry_ringbuffer_init(chry_ringbuffer_t *rb, void *pool, uint32_t size)
{
if (NULL == rb) {
return -1;
}
if (NULL == pool) {
return -1;
}
if ((size < 2) || (size & (size - 1))) {
return -1;
}
rb->in = 0;
rb->out = 0;
rb->mask = size - 1;
rb->pool = pool;
return 0;
}
/*****************************************************************************
* @brief reset ringbuffer, clean all data,
* should be add lock in multithread
*
* @param[in] rb ringbuffer instance
*
*****************************************************************************/
void chry_ringbuffer_reset(chry_ringbuffer_t *rb)
{
rb->in = 0;
rb->out = 0;
}
/*****************************************************************************
* @brief reset ringbuffer, clean all data,
* should be add lock in multithread,
* in single read thread not need lock
*
* @param[in] rb ringbuffer instance
*
*****************************************************************************/
void chry_ringbuffer_reset_read(chry_ringbuffer_t *rb)
{
rb->out = rb->in;
}
/*****************************************************************************
* @brief get ringbuffer total size in byte
*
* @param[in] rb ringbuffer instance
*
* @retval uint32_t total size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_get_size(chry_ringbuffer_t *rb)
{
return rb->mask + 1;
}
/*****************************************************************************
* @brief get ringbuffer used size in byte
*
* @param[in] rb ringbuffer instance
*
* @retval uint32_t used size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_get_used(chry_ringbuffer_t *rb)
{
return rb->in - rb->out;
}
/*****************************************************************************
* @brief get ringbuffer free size in byte
*
* @param[in] rb ringbuffer instance
*
* @retval uint32_t free size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_get_free(chry_ringbuffer_t *rb)
{
return (rb->mask + 1) - (rb->in - rb->out);
}
/*****************************************************************************
* @brief check if ringbuffer is full
*
* @param[in] rb ringbuffer instance
*
* @retval true full
* @retval false not full
*****************************************************************************/
bool chry_ringbuffer_check_full(chry_ringbuffer_t *rb)
{
return chry_ringbuffer_get_used(rb) > rb->mask;
}
/*****************************************************************************
* @brief check if ringbuffer is empty
*
* @param[in] rb ringbuffer instance
*
* @retval true empty
* @retval false not empty
*****************************************************************************/
bool chry_ringbuffer_check_empty(chry_ringbuffer_t *rb)
{
return rb->in == rb->out;
}
/*****************************************************************************
* @brief write one byte to ringbuffer,
* should be add lock in multithread,
* in single write thread not need lock
*
* @param[in] rb ringbuffer instance
* @param[in] byte data
*
* @retval true Success
* @retval false ringbuffer is full
*****************************************************************************/
bool chry_ringbuffer_write_byte(chry_ringbuffer_t *rb, uint8_t byte)
{
if (chry_ringbuffer_check_full(rb)) {
return false;
}
((uint8_t *)(rb->pool))[rb->in & rb->mask] = byte;
rb->in++;
return true;
}
/*****************************************************************************
* @brief overwrite one byte to ringbuffer, drop oldest data,
* should be add lock always
*
* @param[in] rb ringbuffer instance
* @param[in] byte data
*
* @retval true Success
* @retval false always return true
*****************************************************************************/
bool chry_ringbuffer_overwrite_byte(chry_ringbuffer_t *rb, uint8_t byte)
{
if (chry_ringbuffer_check_full(rb)) {
rb->out++;
}
((uint8_t *)(rb->pool))[rb->in & rb->mask] = byte;
rb->in++;
return true;
}
/*****************************************************************************
* @brief peek one byte from ringbuffer,
* should be add lock in multithread,
* in single read thread not need lock
*
* @param[in] rb ringbuffer instance
* @param[in] byte pointer to save data
*
* @retval true Success
* @retval false ringbuffer is empty
*****************************************************************************/
bool chry_ringbuffer_peek_byte(chry_ringbuffer_t *rb, uint8_t *byte)
{
if (chry_ringbuffer_check_empty(rb)) {
return false;
}
*byte = ((uint8_t *)(rb->pool))[rb->out & rb->mask];
return true;
}
/*****************************************************************************
* @brief read one byte from ringbuffer,
* should be add lock in multithread,
* in single read thread not need lock
*
* @param[in] rb ringbuffer instance
* @param[in] byte pointer to save data
*
* @retval true Success
* @retval false ringbuffer is empty
*****************************************************************************/
bool chry_ringbuffer_read_byte(chry_ringbuffer_t *rb, uint8_t *byte)
{
bool ret;
ret = chry_ringbuffer_peek_byte(rb, byte);
rb->out += ret;
return ret;
}
/*****************************************************************************
* @brief drop one byte from ringbuffer,
* should be add lock in multithread,
* in single read thread not need lock
*
* @param[in] rb ringbuffer instance
*
* @retval true Success
* @retval false ringbuffer is empty
*****************************************************************************/
bool chry_ringbuffer_drop_byte(chry_ringbuffer_t *rb)
{
if (chry_ringbuffer_check_empty(rb)) {
return false;
}
rb->out += 1;
return true;
}
/*****************************************************************************
* @brief write data to ringbuffer,
* should be add lock in multithread,
* in single write thread not need lock
*
* @param[in] rb ringbuffer instance
* @param[in] data data pointer
* @param[in] size size in byte
*
* @retval uint32_t actual write size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_write(chry_ringbuffer_t *rb, void *data, uint32_t size)
{
uint32_t unused;
uint32_t offset;
uint32_t remain;
unused = (rb->mask + 1) - (rb->in - rb->out);
if (size > unused) {
size = unused;
}
offset = rb->in & rb->mask;
remain = rb->mask + 1 - offset;
remain = remain > size ? size : remain;
memcpy(((uint8_t *)(rb->pool)) + offset, data, remain);
memcpy(rb->pool, (uint8_t *)data + remain, size - remain);
rb->in += size;
return size;
}
/*****************************************************************************
* @brief write data to ringbuffer,
* should be add lock always
*
* @param[in] rb ringbuffer instance
* @param[in] data data pointer
* @param[in] size size in byte
*
* @retval uint32_t actual write size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_overwrite(chry_ringbuffer_t *rb, void *data, uint32_t size)
{
uint32_t unused;
uint32_t offset;
uint32_t remain;
unused = (rb->mask + 1) - (rb->in - rb->out);
if (size > unused) {
if (size > (rb->mask + 1)) {
size = rb->mask + 1;
}
rb->out += size - unused;
}
offset = rb->in & rb->mask;
remain = rb->mask + 1 - offset;
remain = remain > size ? size : remain;
memcpy(((uint8_t *)(rb->pool)) + offset, data, remain);
memcpy(rb->pool, (uint8_t *)data + remain, size - remain);
rb->in += size;
return size;
}
/*****************************************************************************
* @brief peek data from ringbuffer
* should be add lock in multithread,
* in single read thread not need lock
*
* @param[in] rb ringbuffer instance
* @param[in] data data pointer
* @param[in] size size in byte
*
* @retval uint32_t actual peek size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_peek(chry_ringbuffer_t *rb, void *data, uint32_t size)
{
uint32_t used;
uint32_t offset;
uint32_t remain;
used = rb->in - rb->out;
if (size > used) {
size = used;
}
offset = rb->out & rb->mask;
remain = rb->mask + 1 - offset;
remain = remain > size ? size : remain;
memcpy(data, ((uint8_t *)(rb->pool)) + offset, remain);
memcpy((uint8_t *)data + remain, rb->pool, size - remain);
return size;
}
/*****************************************************************************
* @brief read data from ringbuffer
* should be add lock in multithread,
* in single read thread not need lock
*
* @param[in] rb ringbuffer instance
* @param[in] data data pointer
* @param[in] size size in byte
*
* @retval uint32_t actual read size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_read(chry_ringbuffer_t *rb, void *data, uint32_t size)
{
size = chry_ringbuffer_peek(rb, data, size);
rb->out += size;
return size;
}
/*****************************************************************************
* @brief drop data from ringbuffer
* should be add lock in multithread,
* in single read thread not need lock
*
* @param[in] rb ringbuffer instance
* @param[in] size size in byte
*
* @retval uint32_t actual drop size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_drop(chry_ringbuffer_t *rb, uint32_t size)
{
uint32_t used;
used = rb->in - rb->out;
if (size > used) {
size = used;
}
rb->out += size;
return size;
}
/*****************************************************************************
* @brief linear write setup, get write pointer and max linear size.
*
* @param[in] rb ringbuffer instance
* @param[in] size pointer to store max linear size in byte
*
* @retval void* write memory pointer
*****************************************************************************/
void *chry_ringbuffer_linear_write_setup(chry_ringbuffer_t *rb, uint32_t *size)
{
uint32_t unused;
uint32_t offset;
uint32_t remain;
unused = (rb->mask + 1) - (rb->in - rb->out);
offset = rb->in & rb->mask;
remain = rb->mask + 1 - offset;
remain = remain > unused ? unused : remain;
if (remain) {
*size = remain;
return ((uint8_t *)(rb->pool)) + offset;
} else {
*size = unused - remain;
return rb->pool;
}
}
/*****************************************************************************
* @brief linear read setup, get read pointer and max linear size.
*
* @param[in] rb ringbuffer instance
* @param[in] size pointer to store max linear size in byte
*
* @retval void*
*****************************************************************************/
void *chry_ringbuffer_linear_read_setup(chry_ringbuffer_t *rb, uint32_t *size)
{
uint32_t used;
uint32_t offset;
uint32_t remain;
used = rb->in - rb->out;
offset = rb->out & rb->mask;
remain = rb->mask + 1 - offset;
remain = remain > used ? used : remain;
if (remain) {
*size = remain;
return ((uint8_t *)(rb->pool)) + offset;
} else {
*size = used - remain;
return rb->pool;
}
}
/*****************************************************************************
* @brief linear write done, add write pointer only
*
* @param[in] rb ringbuffer instance
* @param[in] size write size in byte
*
* @retval uint32_t actual write size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_linear_write_done(chry_ringbuffer_t *rb, uint32_t size)
{
uint32_t unused;
unused = (rb->mask + 1) - (rb->in - rb->out);
if (size > unused) {
size = unused;
}
rb->in += size;
return size;
}
/*****************************************************************************
* @brief linear read done, add read pointer only
*
* @param[in] rb ringbuffer instance
* @param[in] size read size in byte
*
* @retval uint32_t actual read size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_linear_read_done(chry_ringbuffer_t *rb, uint32_t size)
{
return chry_ringbuffer_drop(rb, size);
}