forked from PDXostc/reliable_multicast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circular_buffer.c
309 lines (247 loc) · 8.11 KB
/
circular_buffer.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
// Copyright (C) 2018, Jaguar Land Rover
// This program is licensed under the terms and conditions of the
// Mozilla Public License, version 2.0. The full text of the
// Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
//
// Author: Magnus Feuer ([email protected])
#include "circular_buffer.h"
#include <errno.h>
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#define RMC_MIN(x,y) (((x)<(y))?(x):(y))
// circ_buf_t::start_ind = First byte of data in use.
// circ_buf_t::stop_ind = First byte *AFTER* last data byte in use
//
// if circ_buf_t::start_ind == circ_buf_t::stop_ind then no data is stored.
//
// The last available byte in the buffer is resreved so that we can
// distinguish between empty and full buffer, which in both cases
// have start_ind and stop_ind point at the same index.
//
void circ_buf_init(circ_buf_t* circ_buf, uint8_t* buffer, uint32_t buffer_size)
{
circ_buf->start_ind = 0;
circ_buf->stop_ind = 0;
circ_buf->buffer = buffer;
circ_buf->size = buffer_size;
}
inline uint32_t circ_buf_in_use(circ_buf_t* circ_buf)
{
int buf_size = circ_buf->size;
if (circ_buf->stop_ind >= circ_buf->start_ind)
return circ_buf->stop_ind - circ_buf->start_ind;
return buf_size - circ_buf->start_ind + circ_buf->stop_ind;
}
inline uint32_t circ_buf_available(circ_buf_t* circ_buf)
{
// Keep one byte reserved to distinguish between full and empty buffer.
return circ_buf->size - circ_buf_in_use(circ_buf) - 1;
}
int circ_buf_alloc(circ_buf_t* circ_buf,
uint32_t len,
uint8_t **segment1,
uint32_t* segment1_len,
uint8_t **segment2,
uint32_t* segment2_len)
{
uint32_t buf_size = 0;
uint32_t available_size = 0;
if (!circ_buf ||
!segment1 || !segment1_len ||
!segment2 || !segment2_len)
return EINVAL;
buf_size = circ_buf->size;
available_size = circ_buf_available(circ_buf);
// Do we have enough memory?
if (available_size < len)
return ENOMEM;
// Can we fit the requested number of bytes into a single segmetn?
if (buf_size - circ_buf->stop_ind >= len) {
*segment1 = &circ_buf->buffer[circ_buf->stop_ind];
*segment1_len = len;
*segment2 = 0;
*segment2_len = 0;
circ_buf->stop_ind += len;
return 0;
}
// We need two segments. Setup the first one.
*segment1 = &circ_buf->buffer[circ_buf->stop_ind];
*segment1_len = buf_size - circ_buf->stop_ind;
len -= *segment1_len;
// Setup second segment
*segment2 = circ_buf->buffer;
*segment2_len = len;
// Update (wrapped) stop.
circ_buf->stop_ind = len;
return 0;
}
// Trim the end of the in use part of the buffer
// so that the new total length is target_len.
// All trimmed byte will be available for future
// circ_buf_alloc() calls.
//
void circ_buf_trim(circ_buf_t* circ_buf, uint32_t target_len)
{
uint32_t in_use = circ_buf_in_use(circ_buf);
if (in_use <= target_len)
return;
// target_len: 1
// Before
// Index: [0][1][2][3][4]
// Data: A B C D -
// Start/stop S s
//
// After
// Index: [0][1][2][3][4]
// Data: A B - - -
// Start/stop S s
//
if (circ_buf->stop_ind > target_len) {
circ_buf->stop_ind -= in_use - target_len;
return;
}
// We need to wrap stop_ind back to the end of the buffer.
//
// target_len: 1
// Before
// Index: [0][1][2][3][4]
// Data: C D - A B
// Start/stop s S
//
// After
// Index: [0][1][2][3][4]
// Data: - - - A -
// Start/stop S s
//
circ_buf->stop_ind = (circ_buf->start_ind + target_len) % circ_buf->size;
return;
}
// Return number of bytes left in use after discarding data.
int circ_buf_free(circ_buf_t* circ_buf, uint32_t size, uint32_t* in_use)
{
uint32_t len = 0;
if (!circ_buf)
return 0;
if (!size) {
if (in_use)
*in_use = circ_buf_in_use(circ_buf);
return 0;
}
len = circ_buf_in_use(circ_buf);
// Can we do a quickie?
// If we can reset the buffer we have a greater chance
// of circ_buf_alloc and circ_buf read being able to
// to fit the entire operation into one continuous
// segment of the buffer, this avoiding having to
// do split segments.
if (size >= len) {
circ_buf->start_ind = 0;
circ_buf->stop_ind = 0;
if (in_use)
*in_use = 0;
return 0;
}
circ_buf->start_ind += size;
circ_buf->start_ind %= circ_buf->size;
if (in_use)
*in_use = len - size;
return 0;
}
int circ_buf_read_offset(circ_buf_t* circ_buf,
uint32_t offset,
uint8_t* target,
uint32_t size,
uint32_t* bytes_read)
{
uint32_t len = 0;
uint32_t in_use = circ_buf_in_use(circ_buf);
uint32_t segment_len = 0;
uint32_t copied_bytes = 0;
uint32_t start_ind = 0;
if (!circ_buf || !target || offset > in_use)
return EINVAL;
len = RMC_MIN(size, in_use - offset);
start_ind = (circ_buf->start_ind + offset) % circ_buf->size;
// If the entire data of buffer is stored without
// wrapping the buffer we can just copy it in one operation
// and be done.
if (circ_buf->stop_ind >= start_ind) {
memcpy(target, circ_buf->buffer + start_ind, len);
if (bytes_read)
*bytes_read = len;
return 0;
}
// Copy out the first part of the circular buffer, spanning from
// start_ind (first data byte) to end either of buffer or the
// length that we want, whichever is smaller.
segment_len = circ_buf->size - start_ind;
segment_len = RMC_MIN(segment_len, len);
memcpy(target, circ_buf->buffer + start_ind, segment_len);
copied_bytes += segment_len;
// Return if the copied data was all that was requested by the
// caller.
if (len == segment_len) {
if (bytes_read)
*bytes_read = copied_bytes;
return 0;
}
// Copy out the secont part of the circular buffer, spanning from
// 0 to either stop_ind (last data byte) or the number of bytes left
// wanted by the caller, whichever is smaller.
len -= segment_len;
if (offset > segment_len)
offset -= segment_len;
else
offset = 0;
segment_len = RMC_MIN(len, circ_buf->stop_ind);
memcpy(target + copied_bytes, circ_buf->buffer + offset, segment_len);
copied_bytes += segment_len;
if (bytes_read)
*bytes_read = copied_bytes;
return 0;
}
int circ_buf_read(circ_buf_t* circ_buf,
uint8_t* target,
uint32_t size,
uint32_t* bytes_read)
{
return circ_buf_read_offset(circ_buf, 0, target, size, bytes_read);
}
// FIXME: Add test in circular_buffer_test.c
int circ_buf_read_segment(circ_buf_t* circ_buf,
uint32_t size,
uint8_t **segment1,
uint32_t* segment1_len,
uint8_t **segment2,
uint32_t* segment2_len)
{
uint32_t len = circ_buf_in_use(circ_buf);
uint32_t segment_len = 0;
uint32_t copied_bytes = 0;
if (!circ_buf ||
!segment1 || !segment1_len ||
!segment2 || !segment2_len)
return EINVAL;
// No data?
if (circ_buf->stop_ind == circ_buf->start_ind) {
*segment1 = 0;
*segment1_len = 0;
*segment2 = 0;
*segment2_len = 0;
return 0;
}
len = RMC_MIN(len, size);
*segment1 = circ_buf->buffer + circ_buf->start_ind;
*segment1_len = RMC_MIN(len, circ_buf->size - circ_buf->start_ind);
// Do we even need to set segment2?
if (len <= circ_buf->size - circ_buf->start_ind) {
*segment2 = 0;
*segment2_len = 0;
return 0;
}
len -= circ_buf->size - circ_buf->start_ind;
*segment2 = circ_buf->buffer;
*segment2_len = len;
return 0;
}