@@ -35,7 +35,6 @@ void ringbuffer_init(ringbufferctrl_t *ringbuffer, unsigned char *buffer, unsign
35
35
ringbuffer -> length = bufferlength ;
36
36
ringbuffer -> readpos = 0 ;
37
37
ringbuffer -> writepos = 0 ;
38
- ringbuffer -> hasdata = 0 ;
39
38
}
40
39
41
40
size_t ringbuffer_write (ringbufferctrl_t * ringbuffer , const void * pointer , const size_t size , const size_t length )
@@ -73,9 +72,6 @@ size_t ringbuffer_write(ringbufferctrl_t *ringbuffer, const void *pointer, const
73
72
}
74
73
}
75
74
76
- /* There's data in the buffer */
77
- ringbuffer -> hasdata = 1 ;
78
-
79
75
return written / size ;
80
76
}
81
77
@@ -92,16 +88,13 @@ signed short ringbuffer_peek(ringbufferctrl_t *ringbuffer, const size_t offset)
92
88
dprint ("ringbuffer_peek(): offset = %i, readpos = %i, writepos = %i, free = %i\n\r" , offset , ringbuffer -> readpos , ringbuffer -> writepos , ringbuffer_getfreebytes (ringbuffer ));
93
89
#endif
94
90
95
- if (offset < 0 || offset >= ringbuffer -> length ) {
96
- /* This will never fit */
91
+ if (offset < 0 || offset >= ringbuffer -> length || ringbuffer_isempty ( ringbuffer ) ) {
92
+ /* Invalid offset, or no data at all */
97
93
return -1 ;
98
94
}
99
95
100
96
/* Figure out what position to read from */
101
- peekpos = ringbuffer -> readpos + offset ;
102
- if (peekpos >= ringbuffer -> length ) {
103
- peekpos -= ringbuffer -> length ;
104
- }
97
+ peekpos = (ringbuffer -> readpos + offset ) % ringbuffer -> length ;
105
98
106
99
if (ringbuffer -> writepos > ringbuffer -> readpos && peekpos >= ringbuffer -> writepos ) {
107
100
/* Readpointer is before writepointer, thus peekpos must also be before writepointer */
@@ -135,31 +128,17 @@ size_t ringbuffer_read(ringbufferctrl_t *ringbuffer, void *pointer, const size_t
135
128
dprint ("ringbuffer_read(): toread = %i, readpos = %i, writepos = %i, free = %i\n\r" , toread , ringbuffer -> readpos , ringbuffer -> writepos , ringbuffer_getfreebytes (ringbuffer ));
136
129
#endif
137
130
138
- if (toread > ringbuffer -> length - ringbuffer_getfreebytes (ringbuffer )) {
139
- #if RINGBUFFER_WHENREADOVERFLOW_SHRINK
140
- /* Oops.. trying to read more than available */
141
- #ifdef DEBUG_RINGBUFFER
142
- dprint ("ringbuffer_read(): Too much data, shrink! (was %i, is now %i)\n\r" ,toread ,ringbuffer -> length - ringbuffer_getfreebytes ());
143
- #endif
144
- toread = ringbuffer -> length - ringbuffer_getfreebytes (ringbuffer );
145
- /* Ringbuffer will be empty when we're done here */
146
- ringbuffer -> hasdata = 0 ;
147
- #else
131
+ if (toread > ringbuffer_getusedbytes (ringbuffer )) {
148
132
/* Do nothing */
149
133
#ifdef ringbufferdebug
150
134
DEBUG ("ringbuffer_read(): too much data, leave\n\r" );
151
135
#endif
152
136
return 0 ;
153
- #endif
154
- }
155
- else if (toread == ringbuffer -> length - ringbuffer_getfreebytes (ringbuffer )) {
156
- /* Ringbuffer will be empty when we're done here */
157
- ringbuffer -> hasdata = 0 ;
158
137
}
159
138
160
139
while (read < toread ) {
161
140
/* Read byte.. */
162
- buffer [read ]= ringbuffer -> data [ringbuffer -> readpos ];
141
+ buffer [read ] = ringbuffer -> data [ringbuffer -> readpos ];
163
142
read ++ ;
164
143
/* And advance ringbuffer pointer */
165
144
ringbuffer -> readpos ++ ;
@@ -171,34 +150,6 @@ size_t ringbuffer_read(ringbufferctrl_t *ringbuffer, void *pointer, const size_t
171
150
return read / size ;
172
151
}
173
152
174
- static unsigned int ringbuffer_toskip (ringbufferctrl_t * ringbuffer , const size_t size , const size_t length )
175
- {
176
- unsigned int toskip ;
177
-
178
- /* Figure out how much bytes we have to skip */
179
- toskip = length * size ;
180
-
181
- #ifdef DEBUG_RINGBUFFER
182
- dprint ("ringbuffer_toskip(): toskip=%i, readpos = %i, writepos = %i, free = %i\n\r" ,toskip , ringbuffer -> readpos , ringbuffer -> writepos , ringbuffer_getfreebytes (ringbuffer ));
183
- #endif
184
-
185
- if (toskip > ringbuffer -> length - ringbuffer_getfreebytes (ringbuffer )) {
186
- /* Oops.. trying to skip more than available */
187
- #ifdef DEBUG_RINGBUFFER
188
- dprint ("ringbuffer_toskip(): too much data, shrink! (was %i, is now %i)\n\r" ,toskip ,ringbuffer -> length - ringbuffer_getfreebytes (ringbuffer ));
189
- #endif
190
- toskip = ringbuffer -> length - ringbuffer_getfreebytes (ringbuffer );
191
- /* Ringbuffer will be empty when we're done here */
192
- ringbuffer -> hasdata = 0 ;
193
- }
194
- else if (toskip == ringbuffer -> length - ringbuffer_getfreebytes (ringbuffer )) {
195
- /* Ringbuffer will be empty when we're done here */
196
- ringbuffer -> hasdata = 0 ;
197
- }
198
-
199
- return toskip ;
200
- }
201
-
202
153
size_t ringbuffer_skip (ringbufferctrl_t * ringbuffer , const size_t size , const size_t length )
203
154
/**
204
155
Skip 'length' entries of 'size' size (iow, advance the read-pointer).
@@ -207,9 +158,15 @@ size_t ringbuffer_skip(ringbufferctrl_t *ringbuffer, const size_t size, const si
207
158
{
208
159
unsigned int toskip ;
209
160
210
- toskip = ringbuffer_toskip (ringbuffer , size , length );
161
+ /* Figure out how much bytes we have to skip */
162
+ toskip = length * size ;
211
163
212
- if (ringbuffer -> readpos + toskip >= ringbuffer -> length ) {
164
+ if (toskip > ringbuffer_getusedbytes (ringbuffer )) {
165
+ /* Oops.. trying to skip more than available. We skip as much as possible, or iow empty the ringbuffer */
166
+ toskip = ringbuffer_getusedbytes (ringbuffer );
167
+ ringbuffer -> readpos = ringbuffer -> writepos ;
168
+ }
169
+ else if (ringbuffer -> readpos + toskip >= ringbuffer -> length ) {
213
170
ringbuffer -> readpos = toskip - (ringbuffer -> length - ringbuffer -> readpos );
214
171
}
215
172
else {
@@ -227,9 +184,15 @@ size_t ringbuffer_revert(ringbufferctrl_t *ringbuffer, const size_t size, const
227
184
{
228
185
unsigned int toskip ;
229
186
230
- toskip = ringbuffer_toskip (ringbuffer , size , length );
187
+ /* Figure out how much bytes we have to skip */
188
+ toskip = length * size ;
231
189
232
- if (toskip > ringbuffer -> writepos ) {
190
+ if (toskip > ringbuffer_getusedbytes (ringbuffer )) {
191
+ /* Oops.. trying to skip more than available. We skip as much as possible, or iow empty the ringbuffer */
192
+ toskip = ringbuffer_getusedbytes (ringbuffer );
193
+ ringbuffer -> writepos = ringbuffer -> readpos ;
194
+ }
195
+ else if (toskip > ringbuffer -> writepos ) {
233
196
ringbuffer -> writepos = ringbuffer -> length - (toskip - ringbuffer -> writepos );
234
197
}
235
198
else {
@@ -244,9 +207,15 @@ unsigned int ringbuffer_getfreebytes(const ringbufferctrl_t *ringbuffer)
244
207
Return amount of free bytes in ringbuffer
245
208
*/
246
209
{
247
- /* Data from readpos to writepos is unhandled. No data should be written there.
248
- So there are two cases possible; readpos is before or after writepos: */
249
- if (ringbuffer -> writepos > ringbuffer -> readpos ) {
210
+ if (ringbuffer -> readpos == ringbuffer -> writepos ) {
211
+ /* Ringbuffer is empty */
212
+ return ringbuffer -> length - 1 ;
213
+ }
214
+ else if ((ringbuffer -> writepos + 1 )%ringbuffer -> length == ringbuffer -> readpos ) {
215
+ /* One byte remaining between readpos and writepos: ringbuffer is full */
216
+ return 0 ;
217
+ }
218
+ else if (ringbuffer -> writepos > ringbuffer -> readpos ) {
250
219
/* [ start ]
251
220
[ ]
252
221
[ readpos ] -\
@@ -255,22 +224,7 @@ unsigned int ringbuffer_getfreebytes(const ringbufferctrl_t *ringbuffer)
255
224
[ ]
256
225
[ length ]
257
226
*/
258
- return ringbuffer -> readpos + (ringbuffer -> length - ringbuffer -> writepos );
259
- }
260
- else if (ringbuffer -> writepos == ringbuffer -> readpos ) {
261
- /* This is a tricky one; is the buffer completely empty or completely full? */
262
- if (ringbuffer -> hasdata ) {
263
- #ifdef DEBUG_RINGBUFFER
264
- dprint ("ringbuffer_getfreebytes(): Pointer clash, hasdata is SET: buffer full\n\r" );
265
- #endif
266
- return 0 ;
267
- }
268
- else {
269
- #ifdef DEBUG_RINGBUFFER
270
- dprint ("ringbuffer_getfreebytes(): Pointer clash, hasdata is NOT SET: buffer empty\n\r" );
271
- #endif
272
- return ringbuffer -> length ;
273
- }
227
+ return (ringbuffer -> readpos + (ringbuffer -> length - ringbuffer -> writepos )) - 1 ;
274
228
}
275
229
else {
276
230
/* [ start ] |
@@ -281,7 +235,7 @@ unsigned int ringbuffer_getfreebytes(const ringbufferctrl_t *ringbuffer)
281
235
[ ] | -> used, unread data
282
236
[ length ] |
283
237
*/
284
- return ringbuffer -> readpos - ringbuffer -> writepos ;
238
+ return ( ringbuffer -> readpos - ringbuffer -> writepos ) - 1 ;
285
239
}
286
240
}
287
241
@@ -290,7 +244,20 @@ bool ringbuffer_isfull(const ringbufferctrl_t *ringbuffer)
290
244
Return TRUE is ringbuffer is full, FALSE otherwise
291
245
*/
292
246
{
293
- if (ringbuffer -> hasdata && ringbuffer -> writepos == ringbuffer -> readpos ) {
247
+ if ((ringbuffer -> writepos + 1 )%ringbuffer -> length == ringbuffer -> readpos ) {
248
+ /* One byte remaining between readpos and writepos: ringbuffer is full */
249
+ return TRUE;
250
+ }
251
+ return FALSE;
252
+ }
253
+
254
+ bool ringbuffer_isempty (const ringbufferctrl_t * ringbuffer )
255
+ /**
256
+ Return TRUE is ringbuffer is empty, FALSE otherwise
257
+ */
258
+ {
259
+ if (ringbuffer -> readpos == ringbuffer -> writepos ) {
260
+ /* Ringbuffer is empty */
294
261
return TRUE;
295
262
}
296
263
return FALSE;
0 commit comments