chiark / gitweb /
@@@ tty mess
[mLib] / struct / dstr.c
index 7e154aee5cca1a791b53acecc3895648cc23284b..52f33f5385b59d36608fc3e86028ab7549e13388 100644 (file)
 /*----- Header files ------------------------------------------------------*/
 
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 
 #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@ --- *