Mercurial > hg > th-libs
changeset 115:13f001f1ad6e
More work on growbuf stuff.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 21 Jun 2014 23:11:36 +0300 |
parents | b44a0308b53f |
children | bc540c5bfaf9 |
files | th_util.c th_util.h |
diffstat | 2 files changed, 43 insertions(+), 30 deletions(-) [+] |
line wrap: on
line diff
--- a/th_util.c Sat Jun 21 22:59:53 2014 +0300 +++ b/th_util.c Sat Jun 21 23:11:36 2014 +0300 @@ -557,37 +557,8 @@ /* - * Growing buffer implementations + * Simple legacy string growing buffer */ -void th_growbuf_init(th_growbuf_t *buf, const size_t mingrow) -{ - memset(buf, 0, sizeof(th_growbuf_t)); - buf->mingrow = mingrow; -} - - -th_growbuf_t *th_growbuf_new(const size_t mingrow) -{ - th_growbuf_t *buf; - - if ((buf = th_malloc(sizeof(th_growbuf_t))) == NULL) - return NULL; - - th_growbuf_init(buf, mingrow); - - return buf; -} - - -void th_growbuf_free(th_growbuf_t *buf) -{ - th_free(buf->data); - - if (buf->allocated) - th_free(buf); -} - - BOOL th_strbuf_grow(char **buf, size_t *bufsize, size_t *len, size_t grow) { if (*buf == NULL) @@ -633,6 +604,47 @@ } +/* + * Growing buffer + */ +void th_growbuf_clear(th_growbuf_t *buf) +{ + // Simply reset the current "length" + buf->len = 0; +} + + +void th_growbuf_init(th_growbuf_t *buf, const size_t mingrow) +{ + // Initialize the buffer structure + memset(buf, 0, sizeof(th_growbuf_t)); + buf->mingrow = mingrow; +} + + +th_growbuf_t *th_growbuf_new(const size_t mingrow) +{ + th_growbuf_t *buf; + + if ((buf = th_malloc(sizeof(th_growbuf_t))) == NULL) + return NULL; + + th_growbuf_init(buf, mingrow); + buf->allocated = TRUE; + + return buf; +} + + +void th_growbuf_free(th_growbuf_t *buf) +{ + th_free(buf->data); + + if (buf->allocated) + th_free(buf); +} + + BOOL th_growbuf_grow(th_growbuf_t *buf, const size_t grow) { if (buf == NULL)
--- a/th_util.h Sat Jun 21 22:59:53 2014 +0300 +++ b/th_util.h Sat Jun 21 23:11:36 2014 +0300 @@ -210,6 +210,7 @@ /* Growing byte buffer */ void th_growbuf_init(th_growbuf_t *buf, const size_t mingrow); +void th_growbuf_clear(th_growbuf_t *buf); th_growbuf_t *th_growbuf_new(const size_t mingrow); void th_growbuf_free(th_growbuf_t *buf);