From 3ca4c9f8b5a9324dccd2572794245e1f89854192 Mon Sep 17 00:00:00 2001 Message-Id: <3ca4c9f8b5a9324dccd2572794245e1f89854192.1714848707.git.mdw@distorted.org.uk> From: Mark Wooding Date: Tue, 6 Jul 1999 19:16:06 +0000 Subject: [PATCH 1/1] Simplify buffer-growing algorithm. Just double it each time. Organization: Straylight/Edgeware From: mdw --- dstr.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/dstr.c b/dstr.c index 86cde6e..e43e138 100644 --- a/dstr.c +++ b/dstr.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: dstr.c,v 1.8 1999/06/01 09:47:52 mdw Exp $ + * $Id: dstr.c,v 1.9 1999/07/06 19:16:06 mdw Exp $ * * Handle dynamically growing strings * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: dstr.c,v $ + * Revision 1.9 1999/07/06 19:16:06 mdw + * Simplify buffer-growing algorithm. Just double it each time. + * * Revision 1.8 1999/06/01 09:47:52 mdw * Fix nasty bugs in `dstr_vputf'. * @@ -72,8 +75,21 @@ /*----- Tunable constants -------------------------------------------------*/ +/* --- Buffer expansion parameters --- * + * + * If the buffer is empty, it is set to @DSTR_INITSZ@ bytes in size. + * Otherwise, it's set to the next power of two that's large enough. This is + * memory-hungry, but efficient. + */ + #define DSTR_INITSZ 256 /* Initial buffer size */ -#define DSTR_INCSZ 4096 /* Threshhold for doubling */ + +/* --- Parameters for @dstr_putf@ --- * + * + * For each format specifier, at least @DSTR_PUTFSTEP@ bytes are ensured + * before writing the formatted result. + */ + #define DSTR_PUTFSTEP 64 /* Buffer size for @putf@ */ /*----- Main code ---------------------------------------------------------*/ @@ -132,22 +148,14 @@ void dstr_ensure(dstr *d, size_t sz) if (rq <= d->sz) return; - /* --- Grow the buffer --- * - * - * For small buffers, just double the size. For big buffers, make them - * a multiple of some suitably large chunk size. - */ + /* --- Grow the buffer --- */ nsz = d->sz; - do { - if (nsz == 0) - nsz = DSTR_INITSZ; - else if (d->sz < 0x1000) - nsz <<= 1; - else - nsz = (rq + 0x0fff) & ~0x0fff; - } while (rq > nsz); + if (nsz == 0 && rq < DSTR_INITSZ) + nsz = DSTR_INITSZ; + else + do nsz <<= 1; while (nsz < rq); if (d->buf) d->buf = xrealloc(d->buf, nsz); -- [mdw]