X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/2be33c7cbc32a245d360be404a5cc190f4ee0029..98ff9295493ed2b990f30768e11b18b6bc65eaa4:/struct/dstr.c?ds=inline diff --git a/struct/dstr.c b/struct/dstr.c index 7e154ae..52f33f5 100644 --- a/struct/dstr.c +++ b/struct/dstr.c @@ -28,11 +28,11 @@ /*----- Header files ------------------------------------------------------*/ #include -#include #include #include "alloc.h" #include "dstr.h" +#include "growbuf.h" /*----- Tunable constants -------------------------------------------------*/ @@ -92,27 +92,8 @@ void dstr_reset(dstr *d) { DRESET(d); } void dstr_ensure(dstr *d, size_t sz) { - size_t rq = d->len + sz; - size_t nsz; - - /* --- If we have enough space, just leave it --- */ - - if (rq <= d->sz) - return; - - /* --- Grow the buffer --- */ - - nsz = d->sz; - - if (nsz == 0) - nsz = (DSTR_INITSZ >> 1); - do nsz <<= 1; while (nsz < rq); - - if (d->buf) - d->buf = x_realloc(d->a, d->buf, nsz, d->sz); - else - d->buf = x_alloc(d->a, nsz); - d->sz = nsz; + GROWBUF_EXTEND(size_t, d->a, d->buf, d->sz, d->len + sz, + DSTR_INITSZ, 1); } /* --- @dstr_putc@ --- *