comparison th_util.c @ 276:1807059fb8f2

Move ringbuffer implementation.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 09 Jun 2011 11:37:34 +0300
parents 4ec36204d34e
children 8ae649a4b738
comparison
equal deleted inserted replaced
275:66b90d71548b 276:1807059fb8f2
392 curr = curr->next; 392 curr = curr->next;
393 } 393 }
394 394
395 return NULL; 395 return NULL;
396 } 396 }
397
398
399 qringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *data))
400 {
401 qringbuf_t *res = th_calloc(1, sizeof(qringbuf_t));
402
403 res->data = (void **) th_malloc(size * sizeof(void *));
404 res->size = size;
405 res->n = 0;
406 res->deallocator = mdeallocator;
407
408 return res;
409 }
410
411
412 BOOL th_ringbuf_grow(qringbuf_t *buf, const size_t n)
413 {
414 buf->size += n;
415 buf->data = (void **) th_realloc(buf->data, buf->size * sizeof(void *));
416 return (buf->data != NULL);
417 }
418
419
420 void th_ringbuf_free(qringbuf_t *buf)
421 {
422 int i;
423
424 for (i = 0; i < buf->n; i++)
425 buf->deallocator(buf->data[i]);
426
427 th_free(buf->data);
428 th_free(buf);
429 }
430
431
432 void th_ringbuf_add(qringbuf_t *buf, void *ptr)
433 {
434 if (buf->n < buf->size) {
435 buf->data[buf->n] = ptr;
436 buf->n++;
437 } else {
438 th_free(buf->data[0]);
439 memmove(&(buf->data[0]), &(buf->data[1]), buf->size - 1);
440 buf->data[buf->size - 1] = ptr;
441 }
442 }
443
444