Mercurial > hg > nnchat
comparison th_util.c @ 283:18e292701db4
Change ringbuffer functionality.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 09 Jun 2011 22:29:40 +0300 |
parents | 8ae649a4b738 |
children | dbf07a4eadd7 |
comparison
equal
deleted
inserted
replaced
282:3dc86d8eb0a9 | 283:18e292701db4 |
---|---|
423 void th_ringbuf_free(qringbuf_t *buf) | 423 void th_ringbuf_free(qringbuf_t *buf) |
424 { | 424 { |
425 int i; | 425 int i; |
426 | 426 |
427 for (i = 0; i < buf->n; i++) | 427 for (i = 0; i < buf->n; i++) |
428 buf->deallocator(buf->data[i]); | 428 if (buf->data[i] != NULL) |
429 buf->deallocator(buf->data[i]); | |
429 | 430 |
430 th_free(buf->data); | 431 th_free(buf->data); |
431 th_free(buf); | 432 th_free(buf); |
432 } | 433 } |
433 | 434 |
434 | 435 |
435 void th_ringbuf_add(qringbuf_t *buf, void *ptr) | 436 void th_ringbuf_add(qringbuf_t *buf, void *ptr) |
436 { | 437 { |
437 if (buf->n < buf->size) { | 438 if (buf->n < buf->size) |
438 buf->data[buf->n] = ptr; | |
439 buf->n++; | 439 buf->n++; |
440 } else { | 440 |
441 th_free(buf->data[0]); | 441 th_free(buf->data[0]); |
442 memmove(&(buf->data[0]), &(buf->data[1]), buf->size - 1); | 442 memmove(&(buf->data[0]), &(buf->data[1]), buf->size - 1); |
443 buf->data[buf->size - 1] = ptr; | 443 buf->data[buf->size - 1] = ptr; |
444 } | 444 } |
445 } | 445 |
446 |