-
Notifications
You must be signed in to change notification settings - Fork 11
/
ringbuffer.c
318 lines (284 loc) · 6.63 KB
/
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
/// Copyright (C) 2009, 2011, 2014 by Johns. All Rights Reserved.
/// Copyright (C) 2018 by pesintta, rofafor.
///
/// SPDX-License-Identifier: AGPL-3.0-only
///
/// Lock free ring buffer with only one writer and one reader.
///
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "iatomic.h"
#include "ringbuffer.h"
/// ring buffer structure
struct _ring_buffer_
{
char *Buffer; ///< ring buffer data
const char *BufferEnd; ///< end of buffer
size_t Size; ///< bytes in buffer (for faster calc)
const char *ReadPointer; ///< only used by reader
char *WritePointer; ///< only used by writer
/// The only thing modified by both
atomic_t Filled; ///< how many of the buffer is used
};
/**
** Reset ring buffer pointers.
**
** @param rb Ring buffer to reset read/write pointers.
*/
void RingBufferReset(RingBuffer * rb)
{
rb->ReadPointer = rb->Buffer;
rb->WritePointer = rb->Buffer;
atomic_set(&rb->Filled, 0);
}
/**
** Allocate a new ring buffer.
**
** @param size Size of the ring buffer.
**
** @returns Allocated ring buffer, must be freed with
** RingBufferDel(), NULL for out of memory.
*/
RingBuffer *RingBufferNew(size_t size)
{
RingBuffer *rb;
if (!(rb = malloc(sizeof(*rb)))) { // allocate structure
return rb;
}
if (!(rb->Buffer = malloc(size))) { // allocate buffer
free(rb);
return NULL;
}
rb->Size = size;
rb->BufferEnd = rb->Buffer + size;
RingBufferReset(rb);
return rb;
}
/**
** Free an allocated ring buffer.
*/
void RingBufferDel(RingBuffer * rb)
{
free(rb->Buffer);
free(rb);
}
/**
** Advance write pointer in ring buffer.
**
** @param rb Ring buffer to advance write pointer.
** @param cnt Number of bytes to be adavanced.
**
** @returns Number of bytes that could be advanced in ring buffer.
*/
size_t RingBufferWriteAdvance(RingBuffer * rb, size_t cnt)
{
size_t n;
n = rb->Size - atomic_read(&rb->Filled);
if (cnt > n) { // not enough space
cnt = n;
}
//
// Hitting end of buffer?
//
n = rb->BufferEnd - rb->WritePointer;
if (n > cnt) { // don't cross the end
rb->WritePointer += cnt;
} else { // reached or cross the end
rb->WritePointer = rb->Buffer;
if (n < cnt) {
n = cnt - n;
rb->WritePointer += n;
}
}
//
// Only atomic modification!
//
atomic_add(cnt, &rb->Filled);
return cnt;
}
/**
** Write to a ring buffer.
**
** @param rb Ring buffer to write to.
** @param buf Buffer of @p cnt bytes.
** @param cnt Number of bytes in buffer.
**
** @returns The number of bytes that could be placed in the ring
** buffer.
*/
size_t RingBufferWrite(RingBuffer * rb, const void *buf, size_t cnt)
{
size_t n;
n = rb->Size - atomic_read(&rb->Filled);
if (cnt > n) { // not enough space
cnt = n;
}
//
// Hitting end of buffer?
//
n = rb->BufferEnd - rb->WritePointer;
if (n > cnt) { // don't cross the end
memcpy(rb->WritePointer, buf, cnt);
rb->WritePointer += cnt;
} else { // reached or cross the end
memcpy(rb->WritePointer, buf, n);
rb->WritePointer = rb->Buffer;
if (n < cnt) {
buf += n;
n = cnt - n;
memcpy(rb->WritePointer, buf, n);
rb->WritePointer += n;
}
}
//
// Only atomic modification!
//
atomic_add(cnt, &rb->Filled);
return cnt;
}
/**
** Get write pointer and free bytes at this position of ring buffer.
**
** @param rb Ring buffer to write to.
** @param[out] wp Write pointer is placed here
**
** @returns The number of bytes that could be placed in the ring
** buffer at the write pointer.
*/
size_t RingBufferGetWritePointer(RingBuffer * rb, void **wp)
{
size_t n;
size_t cnt;
// Total free bytes available in ring buffer
cnt = rb->Size - atomic_read(&rb->Filled);
*wp = rb->WritePointer;
//
// Hitting end of buffer?
//
n = rb->BufferEnd - rb->WritePointer;
if (n <= cnt) { // reached or cross the end
return n;
}
return cnt;
}
/**
** Advance read pointer in ring buffer.
**
** @param rb Ring buffer to advance read pointer.
** @param cnt Number of bytes to be advanced.
**
** @returns Number of bytes that could be advanced in ring buffer.
*/
size_t RingBufferReadAdvance(RingBuffer * rb, size_t cnt)
{
size_t n;
n = atomic_read(&rb->Filled);
if (cnt > n) { // not enough filled
cnt = n;
}
//
// Hitting end of buffer?
//
n = rb->BufferEnd - rb->ReadPointer;
if (n > cnt) { // don't cross the end
rb->ReadPointer += cnt;
} else { // reached or cross the end
rb->ReadPointer = rb->Buffer;
if (n < cnt) {
n = cnt - n;
rb->ReadPointer += n;
}
}
//
// Only atomic modification!
//
atomic_sub(cnt, &rb->Filled);
return cnt;
}
/**
** Read from a ring buffer.
**
** @param rb Ring buffer to read from.
** @param buf Buffer of @p cnt bytes.
** @param cnt Number of bytes to be read.
**
** @returns Number of bytes that could be read from ring buffer.
*/
size_t RingBufferRead(RingBuffer * rb, void *buf, size_t cnt)
{
size_t n;
n = atomic_read(&rb->Filled);
if (cnt > n) { // not enough filled
cnt = n;
}
//
// Hitting end of buffer?
//
n = rb->BufferEnd - rb->ReadPointer;
if (n > cnt) { // don't cross the end
memcpy(buf, rb->ReadPointer, cnt);
rb->ReadPointer += cnt;
} else { // reached or cross the end
memcpy(buf, rb->ReadPointer, n);
rb->ReadPointer = rb->Buffer;
if (n < cnt) {
buf += n;
n = cnt - n;
memcpy(buf, rb->ReadPointer, n);
rb->ReadPointer += n;
}
}
//
// Only atomic modification!
//
atomic_sub(cnt, &rb->Filled);
return cnt;
}
/**
** Get read pointer and used bytes at this position of ring buffer.
**
** @param rb Ring buffer to read from.
** @param[out] rp Read pointer is placed here
**
** @returns The number of bytes that could be read from the ring
** buffer at the read pointer.
*/
size_t RingBufferGetReadPointer(RingBuffer * rb, const void **rp)
{
size_t n;
size_t cnt;
// Total used bytes in ring buffer
cnt = atomic_read(&rb->Filled);
*rp = rb->ReadPointer;
//
// Hitting end of buffer?
//
n = rb->BufferEnd - rb->ReadPointer;
if (n <= cnt) { // reached or cross the end
return n;
}
return cnt;
}
/**
** Get free bytes in ring buffer.
**
** @param rb Ring buffer.
**
** @returns Number of bytes free in buffer.
*/
size_t RingBufferFreeBytes(RingBuffer * rb)
{
return rb->Size - atomic_read(&rb->Filled);
}
/**
** Get used bytes in ring buffer.
**
** @param rb Ring buffer.
**
** @returns Number of bytes used in buffer.
*/
size_t RingBufferUsedBytes(RingBuffer * rb)
{
return atomic_read(&rb->Filled);
}