-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCircularBuffer.c
73 lines (58 loc) · 1.65 KB
/
CircularBuffer.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
/* CircularBuffer.cpp
modified from example code from Wikipedia:
http://en.wikipedia.org/wiki/Circular_queue#Use_a_Fill_Count
modified to only contain indexes and not any data elements, and allow for peeking at the next read/write index
*/
#include "CircularBuffer.h"
void cbInit(CircularBuffer *cb, int size) {
cb->size = size;
cb->start = 0;
cb->count = 0;
cb->min_free = size;
cb->overflow = 0;
}
/* below from fill count mods */
int cbIsFull(CircularBuffer *cb) {
return cb->count == cb->size;
}
int cbIsEmpty(CircularBuffer *cb) {
return cb->count == 0;
}
// returns index of next free element
int cbGetNextWrite(CircularBuffer *cb) {
return (cb->start + cb->count) % cb->size;
}
void cbRead(CircularBuffer *cb) {
cb->start = (cb->start + 1) % cb->size;
-- cb->count;
}
void cbWrite(CircularBuffer *cb) {
if (cb->count == cb->size) {
cb->start = (cb->start + 1) % cb->size; /* full, overwrite */
// set overflow flag
cb->overflow = 1;
} else {
++ cb->count;
}
if((cb->size - cb->count) < cb->min_free)
cb->min_free = cb->size - cb->count;
}
int cbGetNextRead(CircularBuffer *cb) {
return cb->start;
}
// get minimum number of free elements
int cbGetMinFree(CircularBuffer *cb) {
return cb->min_free;
}
// clear minimum number of free elements
void cbClearMinFree(CircularBuffer *cb) {
cb->min_free = cb->size;
}
// return non-zero if there was an overflow since the last time the overflow flag was cleared
int cbIsOverflow(CircularBuffer *cb) {
return cb->overflow;
}
// clear overflow flag
void cbClearOverflow(CircularBuffer *cb) {
cb->overflow = 0;
}