forked from Tsukihime/OpenExpertSDR
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ringb.c
294 lines (263 loc) · 7.73 KB
/
ringb.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
/*
Memory-mapped ringbuffer
Derived from jack/ringbuffer.h
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Original
Copyright (C) 2000 Paul Davis
Copyright (C) 2003 Rohan Drape
Derived
Copyright (C) 2004, 2005, 2006 by Frank Brickle, AB2KT and Bob McGwier, N4HY
*/
#include <stdlib.h>
#include <string.h>
#include <ringb.h>
#include <bufvec.h>
ringb_t*
ringb_create(size_t sz2)
{
ringb_t *rb = (ringb_t *)safealloc(1, sizeof(ringb_t), "Ring creation");
rb->buf = safealloc(1, sz2, "Ring buffer buf");
rb->size = sz2; // power-of-2-sized
rb->mask = rb->size - 1;
rb->wptr = rb->rptr = 0;
return (rb);
}
ringb_float_t*
ringb_float_create(size_t sz2)
{
ringb_float_t *rb = (ringb_float_t *)safealloc(1, sizeof(ringb_float_t),
"Float Ring creation");
rb->buf =
(float *)safealloc(1, sz2 * sizeof(float), "Ring buffer float buf");
rb->size = sz2; // power-of-2-sized
rb->mask = rb->size - 1;
rb->wptr = rb->rptr = 0;
return (rb);
}
ringb_t*
ringb_create_usemem(char *usemem, size_t sz2)
{
ringb_t *rb = (ringb_t *)usemem;
rb->buf = usemem + sizeof(ringb_t);
rb->size = sz2; // power-of-2-sized
rb->mask = rb->size - 1;
rb->wptr = rb->rptr = 0;
return (rb);
}
void
ringb_free(ringb_t *rb)
{
safefree(rb->buf);
safefree((char *)rb);
}
void
ringb_float_free(ringb_float_t *rb)
{
safefree((void *)rb->buf);
safefree((char *)rb);
}
void
ringb_reset(ringb_t *rb)
{
// NB not thread-safe
rb->rptr = 0;
rb->wptr = 0;
}
void
ringb_float_reset(ringb_float_t *rb)
{
// NB not thread-safe
rb->rptr = 0;
rb->wptr = 0;
}
void
ringb_clear(ringb_t *rb, size_t nbytes)
{
size_t i;
char zero = 0;
for (i = 0; i < nbytes; i++) ringb_write(rb, &zero, 1);
}
void
ringb_float_clear(ringb_float_t *rb, size_t nfloats)
{
size_t i;
float zero = 0;
for (i = 0; i < nfloats; i++) ringb_float_write(rb, &zero, 1);
}
void
ringb_restart(ringb_t *rb, size_t nbytes)
{
ringb_reset(rb);
ringb_clear(rb, nbytes);
}
void
ringb_float_restart(ringb_float_t *rb, size_t nfloats)
{
ringb_float_reset(rb);
ringb_float_clear(rb, nfloats);
}
size_t
ringb_read_space(const ringb_t *rb)
{
size_t w = rb->wptr, r = rb->rptr;
if(w > r) return (w - r);
else return ((w - r + rb->size) & rb->mask);
}
size_t
ringb_float_read_space(const ringb_float_t *rb)
{
size_t w = rb->wptr, r = rb->rptr;
if(w > r) return (w - r);
else return ((w - r + rb->size) & rb->mask);
}
size_t
ringb_write_space(const ringb_t *rb)
{
size_t w = rb->wptr, r = rb->rptr;
if(w > r) return (((r - w + rb->size) & rb->mask) - 1);
else if(w < r) return (r - w - 1);
else return (rb->size - 1);
}
size_t
ringb_float_write_space(const ringb_float_t *rb)
{
size_t w = rb->wptr, r = rb->rptr;
if(w > r) return (((r - w + rb->size) & rb->mask) - 1);
else if(w < r) return (r - w - 1);
else return (rb->size - 1);
}
size_t
ringb_read(ringb_t *rb, char *dest, size_t cnt)
{
size_t free_cnt, cnt2, to_read, n1, n2;
if((free_cnt = ringb_read_space(rb)) == 0) return (0);
to_read = cnt > free_cnt ? free_cnt : cnt;
if((cnt2 = rb->rptr + to_read) > rb->size) n1 = rb->size - rb->rptr, n2 = cnt2 & rb->mask;
else n1 = to_read, n2 = 0;
memcpy(dest, &(rb->buf[rb->rptr]), n1);
rb->rptr = (rb->rptr + n1) & rb->mask;
if(n2)
{
memcpy(dest + n1, &(rb->buf[rb->rptr]), n2);
rb->rptr = (rb->rptr + n2) & rb->mask;
}
return (to_read);
}
size_t
ringb_float_read(ringb_float_t *rb, float *dest, size_t cnt)
{
size_t free_cnt, cnt2, to_read, n1, n2;
if((free_cnt = ringb_float_read_space(rb)) == 0) return (0);
to_read = cnt > free_cnt ? free_cnt : cnt;
if((cnt2 = rb->rptr + to_read) > rb->size) n1 = rb->size - rb->rptr, n2 = cnt2 & rb->mask;
else n1 = to_read, n2 = 0;
memcpy(dest, &(rb->buf[rb->rptr]), n1 * sizeof(float));
rb->rptr = (rb->rptr + n1) & rb->mask;
if(n2)
{
memcpy(dest + n1, &(rb->buf[rb->rptr]), n2 * sizeof(float));
rb->rptr = (rb->rptr + n2) & rb->mask;
}
return (to_read);
}
size_t
ringb_peek(ringb_t *rb, char *dest, size_t cnt)
{
size_t free_cnt, cnt2, to_read, n1, n2, tmp_rptr;
tmp_rptr = rb->rptr;
if((free_cnt = ringb_read_space(rb)) == 0) return (0);
to_read = cnt > free_cnt ? free_cnt : cnt;
if((cnt2 = tmp_rptr + to_read) > rb->size) n1 = rb->size - tmp_rptr, n2 = cnt2 & rb->mask;
else n1 = to_read, n2 = 0;
memcpy(dest, &(rb->buf[tmp_rptr]), n1);
tmp_rptr = (tmp_rptr + n1) & rb->mask;
if(n2) memcpy(dest + n1, &(rb->buf[tmp_rptr]), n2);
return (to_read);
}
size_t
ringb_write(ringb_t *rb, const char *src, size_t cnt)
{
size_t free_cnt, cnt2, to_write, n1, n2;
if((free_cnt = ringb_write_space(rb)) == 0) return (0);
to_write = cnt > free_cnt ? free_cnt : cnt;
if((cnt2 = rb->wptr + to_write) > rb->size) n1 = rb->size - rb->wptr, n2 = cnt2 & rb->mask;
else n1 = to_write, n2 = 0;
memcpy(&(rb->buf[rb->wptr]), src, n1);
rb->wptr = (rb->wptr + n1) & rb->mask;
if(n2)
{
memcpy(&(rb->buf[rb->wptr]), src + n1, n2);
rb->wptr = (rb->wptr + n2) & rb->mask;
}
return (to_write);
}
size_t
ringb_float_write(ringb_float_t *rb, const float *src, size_t cnt)
{
size_t free_cnt, cnt2, to_write, n1, n2;
if((free_cnt = ringb_float_write_space(rb)) == 0) return (0);
to_write = cnt > free_cnt ? free_cnt : cnt;
if((cnt2 = rb->wptr + to_write) > rb->size) n1 = rb->size - rb->wptr, n2 = cnt2 & rb->mask;
else n1 = to_write, n2 = 0;
memcpy(&(rb->buf[rb->wptr]), src, n1 * sizeof(float));
rb->wptr = (rb->wptr + n1) & rb->mask;
if(n2)
{
memcpy(&(rb->buf[rb->wptr]), src + n1, n2 * sizeof(float));
rb->wptr = (rb->wptr + n2) & rb->mask;
}
return (to_write);
}
void
ringb_read_advance(ringb_t *rb, size_t cnt)
{
rb->rptr = (rb->rptr + cnt) & rb->mask;
}
void
ringb_write_advance(ringb_t *rb, size_t cnt)
{
rb->wptr = (rb->wptr + cnt) & rb->mask;
}
void
ringb_get_read_vector(const ringb_t *rb, ringb_data_t *vec)
{
size_t free_cnt, cnt2, w = rb->wptr, r = rb->rptr;
if(w > r) free_cnt = w - r;
else free_cnt = (w - r + rb->size) & rb->mask;
if((cnt2 = r + free_cnt) > rb->size)
{
vec[0].buf = &(rb->buf[r]), vec[0].len = rb->size - r;
vec[1].buf = rb->buf, vec[1].len = cnt2 & rb->mask;
} else
{
vec[0].buf = &(rb->buf[r]), vec[0].len = free_cnt;
vec[1].len = 0;
}
}
void
ringb_get_write_vector(const ringb_t *rb, ringb_data_t *vec)
{
size_t free_cnt, cnt2, w = rb->wptr, r = rb->rptr;
if(w > r) free_cnt = ((r - w + rb->size) & rb->mask) - 1;
else if(w < r) free_cnt = r - w - 1;
else free_cnt = rb->size - 1;
if((cnt2 = w + free_cnt) > rb->size)
{
vec[0].buf = &(rb->buf[w]), vec[0].len = rb->size - w;
vec[1].buf = rb->buf, vec[1].len = cnt2 & rb->mask;
} else
{
vec[0].buf = &(rb->buf[w]), vec[0].len = free_cnt;
vec[1].len = 0;
}
}