diff options
author | iphydf <iphydf@users.noreply.github.com> | 2016-09-22 15:20:33 +0100 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2016-09-24 23:36:50 +0100 |
commit | f60900c4b813abbce213db6de217837645c6590e (patch) | |
tree | 02e9223f1627f1c647b42fa3f623b7354b6c6331 /toxcore/util.c | |
parent | 15cb4261665bab4ef02a5b1b9db48b9477c9b87a (diff) |
Move ring buffer out of toxcore/util into toxav.
Toxcore itself doesn't use this data structure. Only toxav does, so now
toxav owns the code for it.
Diffstat (limited to 'toxcore/util.c')
-rw-r--r-- | toxcore/util.c | 90 |
1 files changed, 0 insertions, 90 deletions
diff --git a/toxcore/util.c b/toxcore/util.c index 93a3d436..25068db6 100644 --- a/toxcore/util.c +++ b/toxcore/util.c | |||
@@ -193,93 +193,3 @@ int create_recursive_mutex(pthread_mutex_t *mutex) | |||
193 | 193 | ||
194 | return 0; | 194 | return 0; |
195 | } | 195 | } |
196 | |||
197 | |||
198 | struct RingBuffer { | ||
199 | uint16_t size; /* Max size */ | ||
200 | uint16_t start; | ||
201 | uint16_t end; | ||
202 | void **data; | ||
203 | }; | ||
204 | |||
205 | bool rb_full(const RingBuffer *b) | ||
206 | { | ||
207 | return (b->end + 1) % b->size == b->start; | ||
208 | } | ||
209 | bool rb_empty(const RingBuffer *b) | ||
210 | { | ||
211 | return b->end == b->start; | ||
212 | } | ||
213 | void *rb_write(RingBuffer *b, void *p) | ||
214 | { | ||
215 | void *rc = NULL; | ||
216 | |||
217 | if ((b->end + 1) % b->size == b->start) { /* full */ | ||
218 | rc = b->data[b->start]; | ||
219 | } | ||
220 | |||
221 | b->data[b->end] = p; | ||
222 | b->end = (b->end + 1) % b->size; | ||
223 | |||
224 | if (b->end == b->start) { | ||
225 | b->start = (b->start + 1) % b->size; | ||
226 | } | ||
227 | |||
228 | return rc; | ||
229 | } | ||
230 | bool rb_read(RingBuffer *b, void **p) | ||
231 | { | ||
232 | if (b->end == b->start) { /* Empty */ | ||
233 | *p = NULL; | ||
234 | return false; | ||
235 | } | ||
236 | |||
237 | *p = b->data[b->start]; | ||
238 | b->start = (b->start + 1) % b->size; | ||
239 | return true; | ||
240 | } | ||
241 | RingBuffer *rb_new(int size) | ||
242 | { | ||
243 | RingBuffer *buf = (RingBuffer *)calloc(sizeof(RingBuffer), 1); | ||
244 | |||
245 | if (!buf) { | ||
246 | return NULL; | ||
247 | } | ||
248 | |||
249 | buf->size = size + 1; /* include empty elem */ | ||
250 | |||
251 | if (!(buf->data = (void **)calloc(buf->size, sizeof(void *)))) { | ||
252 | free(buf); | ||
253 | return NULL; | ||
254 | } | ||
255 | |||
256 | return buf; | ||
257 | } | ||
258 | void rb_kill(RingBuffer *b) | ||
259 | { | ||
260 | if (b) { | ||
261 | free(b->data); | ||
262 | free(b); | ||
263 | } | ||
264 | } | ||
265 | uint16_t rb_size(const RingBuffer *b) | ||
266 | { | ||
267 | if (rb_empty(b)) { | ||
268 | return 0; | ||
269 | } | ||
270 | |||
271 | return | ||
272 | b->end > b->start ? | ||
273 | b->end - b->start : | ||
274 | (b->size - b->start) + b->end; | ||
275 | } | ||
276 | uint16_t rb_data(const RingBuffer *b, void **dest) | ||
277 | { | ||
278 | uint16_t i = 0; | ||
279 | |||
280 | for (; i < rb_size(b); i++) { | ||
281 | dest[i] = b->data[(b->start + i) % b->size]; | ||
282 | } | ||
283 | |||
284 | return i; | ||
285 | } | ||