-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffers.c
161 lines (136 loc) · 4.31 KB
/
buffers.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
#include "buffers.h"
volatile Buffer initBuffer(void *data, uint8_t elementSize, uint8_t arraySize) {
return (volatile Buffer) {
.array = data,
.elementSize = elementSize,
.arraySize = arraySize,
.head = 0,
.tail = 0,
.bookmarkIdx = 0,
.isEmpty = true,
.isFull = false,
.dataLoss = false,
.Blocked = false,
.whatIsLife = 42,
};
}
void enq(void *data, volatile Buffer *buffer) {
buffer->dataLoss |= buffer->isFull;
if(BLOCK_WHEN_FULL && buffer->dataLoss){
// doesn't add anymore
return;
}else{
memcpy((uint8_t *)buffer->array + buffer->elementSize * buffer->head, data, buffer->elementSize);
buffer->head = (buffer->head + 1) % buffer->arraySize;
buffer->isEmpty = false;
buffer->isFull = buffer->head == buffer->tail;
if(buffer->Blocked){
buffer->isFull = buffer->head == buffer->bookmarkIdx;
}
return ;
}
}
void deq(void *data, volatile Buffer *buffer) {
if (buffer->isEmpty){
return;
}
memcpy(data, (uint8_t *)buffer->array + buffer->elementSize * buffer->tail, buffer->elementSize);
buffer->tail = (buffer->tail + 1) % buffer->arraySize;
buffer->isFull = false;
buffer->isEmpty = buffer->head == buffer->tail;
return;
}
void nEnq(void *data, volatile Buffer *buffer, uint8_t size) {
//check if there is enough space in the buffer
if(buffer->arraySize - buffer->head + buffer->tail < size){
buffer->dataLoss = true;
return;
}else{
for (uint8_t i = 0; i < size; i++) {
enq((uint8_t *)data + buffer->elementSize * i, buffer);
}
}
}
void nDeq(void *data, volatile Buffer *buffer, uint8_t size) {
if(buffer->arraySize - buffer->head + buffer->tail < size){
size = buffer->arraySize - buffer->head + buffer->tail + 1;
}
for (uint8_t i = 0; i < size; i++) {
deq((uint8_t *)data + buffer->elementSize * i, buffer);
}
}
void reset(volatile Buffer *buffer) {
buffer->head = 0;
buffer->tail = 0;
buffer->isEmpty = true;
buffer->isFull = false;
buffer->dataLoss = false;
buffer->Blocked = false;
}
uint8_t howMuchData(volatile Buffer *buffer) {
if (buffer->isEmpty) {
return 0;
}
if (buffer->head > buffer->tail) {
return buffer->head - buffer->tail;
}
return buffer->arraySize - buffer->tail + buffer->head;
}
void setBookmark(volatile Buffer *buffer){
if(!buffer->Blocked){
buffer->Blocked = true;
buffer->bookmarkIdx = buffer->tail-1;
}
}
void removeBookmark(volatile Buffer *buffer){
buffer->Blocked = false;
if(buffer->isFull && buffer->isEmpty){
buffer->isFull = false; // release the buffer
}
}
bool findNextBookmark(volatile Buffer *buffer){
if(buffer->Blocked){
return(findFlag(buffer, (uint8_t *)buffer->array + buffer->elementSize * buffer->bookmarkIdx));
}else{
return false;
}
}
bool findFlag(volatile Buffer *buffer, void *data){
uint8_t i = buffer->tail; // where to start searching
if(buffer->Blocked){
i = (buffer->bookmarkIdx+1) % buffer->arraySize;
}
for (i ; i != buffer->head; i = (i+1) % buffer->arraySize) {
if(memcmp((uint8_t *)buffer->array + buffer->elementSize * i, data, buffer->elementSize) == 0){
buffer->bookmarkIdx = i;
return true;
}
}
// if the data is not found
return false;
}
void jumpToBookmark(volatile Buffer *buffer){
if(buffer->Blocked){
buffer->tail = (buffer->bookmarkIdx + 1) % buffer->arraySize;
if(buffer->head == buffer->tail){
buffer->isEmpty = true; // no data to read
buffer->isFull = true; // no place to write
// you should unblock the buffer before being able to use it again
}
}
return;
}
void rollback( volatile Buffer *buffer, uint8_t N){
// move back the last N elements written incorrectly
if (buffer->isEmpty || N == 0) {
return;
}
// moves the head backwards by N elements
if(N >= howMuchData(buffer)){
buffer->head = buffer->tail;
buffer->isEmpty = true;
}else{
buffer->head = (buffer->arraySize - N + buffer->head) % buffer->arraySize;
}
return;
}