forked from Roen-Ro/CircularBuffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularBuffer.c
executable file
·249 lines (204 loc) · 6.23 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
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
//
// CircularBuffer.c
//
// Created by 罗亮富(Roen [email protected]) on 14-1-14.
// Copyright (c) 2014年 All rights reserved.
//
#include "CircularBuffer.h"
#include <string.h>
struct s_circularBuffer{
size_t size; //capacity bytes size
size_t dataSize; //occupied data size
size_t tailOffset; //head offset, the oldest byte position offset
size_t headOffset; //tail offset, the lastest byte position offset
void *buffer;
};
extern CircularBuffer CircularBufferCreate(size_t size)
{
size_t totalSize = sizeof(struct s_circularBuffer) + size;
void *p = malloc(totalSize);
CircularBuffer buffer = (CircularBuffer)p;
buffer->buffer = p + sizeof(struct s_circularBuffer);
buffer->size = size;
CircularBufferReset(buffer);
return buffer;
}
void CircularBufferFree(CircularBuffer cBuf)
{
CircularBufferReset(cBuf);
cBuf->size = 0;
cBuf->dataSize = 0;
cBuf->buffer = NULL;
free(cBuf);
}
void CircularBufferReset(CircularBuffer cBuf)
{
cBuf->headOffset = -1;
cBuf->tailOffset = -1;
cBuf->dataSize = 0;
}
size_t CircularBufferGetCapacity(CircularBuffer cBuf) {
return cBuf->size;
}
size_t CircularBufferGetSize(CircularBuffer cBuf)
{
return cBuf->size;
}
size_t CircularBufferGetDataSize(CircularBuffer cBuf)
{
return cBuf->dataSize;
}
void CircularBufferPush(CircularBuffer cBuf,void *src, size_t length)
{
if(length == 0)
return;
size_t writableLen = length;
void *pSrc = src;
if(writableLen > cBuf->size)//in case of size overflow
{
size_t overFlowLen = writableLen - cBuf->size;
writableLen = cBuf->size;
pSrc = src + overFlowLen;
}
bool resetHead = false;
//in case the circle buffer won't be full after adding the data
if(cBuf->tailOffset+writableLen < cBuf->size)
{
memcpy(&cBuf->buffer[cBuf->tailOffset + 1], pSrc, writableLen);
if((cBuf->tailOffset < cBuf->headOffset) && (cBuf->tailOffset+writableLen >= cBuf->headOffset) )
resetHead = true;
cBuf->tailOffset += writableLen;
}
else//in case the circle buffer will be overflow after adding the data
{
size_t remainSize = cBuf->size - cBuf->tailOffset - 1; //the remain size
memcpy(&cBuf->buffer[cBuf->tailOffset+1], pSrc, remainSize);
size_t coverSize = writableLen - remainSize; //size of data to be covered from the beginning
memcpy(cBuf->buffer, pSrc+remainSize, coverSize);
if(cBuf->tailOffset < cBuf->headOffset)
resetHead = true;
else
{
if(coverSize>cBuf->headOffset)
resetHead = true;
}
cBuf->tailOffset = coverSize - 1;
}
if(cBuf->headOffset == -1)
cBuf->headOffset = 0;
if(resetHead)
{
if(cBuf->tailOffset+1 < cBuf->size)
cBuf->headOffset = cBuf->tailOffset + 1;
else
cBuf->headOffset = 0;
cBuf->dataSize = cBuf->size;
}
else
{
if(cBuf->tailOffset >= cBuf->headOffset)
cBuf->dataSize = cBuf->tailOffset - cBuf->headOffset + 1;
else
cBuf->dataSize = cBuf->size - (cBuf->headOffset - cBuf->tailOffset - 1);
}
}
size_t inter_circularBuffer_read(CircularBuffer cBuf, size_t length, void *dataOut, bool resetHead)
{
if(cBuf->dataSize == 0 || length == 0)
return 0;
size_t rdLen = length;
if(cBuf->dataSize < rdLen)
rdLen = cBuf->dataSize;
if(cBuf->headOffset <= cBuf->tailOffset)
{
if(dataOut)
memcpy(dataOut, &cBuf->buffer[cBuf->headOffset], rdLen);
if(resetHead)
{
cBuf->headOffset += rdLen;
if(cBuf->headOffset > cBuf->tailOffset)
{
cBuf->headOffset = -1;
cBuf->tailOffset = -1;
}
}
}
else
{
if(cBuf->headOffset+rdLen <= cBuf->size)
{
if(dataOut)
memcpy(dataOut, &cBuf->buffer[cBuf->headOffset], rdLen);
if(resetHead)
{
cBuf->headOffset += rdLen;
if(cBuf->headOffset == cBuf->size)
cBuf->headOffset = 0;
}
}
else
{
size_t frg1Len = cBuf->size - cBuf->headOffset;
if(dataOut)
memcpy(dataOut, &cBuf->buffer[cBuf->headOffset], frg1Len);
size_t frg2len = rdLen - frg1Len;
if(dataOut)
memcpy(dataOut+frg1Len, cBuf->buffer, frg2len);
if(resetHead)
{
cBuf->headOffset = frg2len;
if(cBuf->headOffset > cBuf->tailOffset)
{
cBuf->headOffset = -1;
cBuf->tailOffset = -1;
}
}
}
}
if(resetHead)
cBuf->dataSize -= rdLen;
return rdLen;
}
size_t CircularBufferPop(CircularBuffer cBuf, size_t length, void *dataOut)
{
return inter_circularBuffer_read(cBuf,length,dataOut,true);
}
size_t CircularBufferRead(CircularBuffer cBuf, size_t length, void *dataOut)
{
return inter_circularBuffer_read(cBuf,length,dataOut,false);
}
//print circular buffer's content into str,
void CircularBufferPrint(CircularBuffer cBuf, bool hex)
{
char *b = cBuf->buffer;
size_t cSize = CircularBufferGetSize(cBuf);
char *str = malloc(2*cSize+1);
char c;
for(size_t i=0; i<cSize; i++)
{
if(CircularBufferGetDataSize(cBuf) == 0)
{
c = '_';
}
else if (cBuf->tailOffset < cBuf->headOffset)
{
if(i>cBuf->tailOffset && i<cBuf->headOffset)
c = '_';
else
c = b[i];
}
else
{
if(i>cBuf->tailOffset || i<cBuf->headOffset)
c = '_';
else
c = b[i];
}
if(hex)
sprintf(str+i*2, "%02X|",c);
else
sprintf(str+i*2, "%c|",c);
}
printf("CircularBuffer: %s <size %zu dataSize:%zu>\n",str,CircularBufferGetSize(cBuf),CircularBufferGetDataSize(cBuf));
free(str);
}