summaryrefslogtreecommitdiff
path: root/lib/libc/stdio/fvwrite.c
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>1998-11-20 06:13:27 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>1998-11-20 06:13:27 +0000
commite2f43b6cd997f5ff3f7ae872a0382fad32dc2573 (patch)
treeb4c5d8bc81bf1b2a91b1b7f0ad2cc71a9073e7b1 /lib/libc/stdio/fvwrite.c
parent53b7dfe1db26ccf6de987962ad8ed94a75c255f8 (diff)
Allocate space exponentially, not linearly for v?asprintf; mycroft@netbsd.org
Diffstat (limited to 'lib/libc/stdio/fvwrite.c')
-rw-r--r--lib/libc/stdio/fvwrite.c20
1 files changed, 11 insertions, 9 deletions
diff --git a/lib/libc/stdio/fvwrite.c b/lib/libc/stdio/fvwrite.c
index 07c4bc7a06d..ef854a3f9b0 100644
--- a/lib/libc/stdio/fvwrite.c
+++ b/lib/libc/stdio/fvwrite.c
@@ -35,7 +35,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char rcsid[] = "$OpenBSD: fvwrite.c,v 1.7 1998/08/14 21:39:40 deraadt Exp $";
+static char rcsid[] = "$OpenBSD: fvwrite.c,v 1.8 1998/11/20 06:13:26 millert Exp $";
#endif /* LIBC_SCCS and not lint */
#include <stdio.h>
@@ -114,18 +114,20 @@ __sfvwrite(fp, uio)
(__SALC | __SSTR) && fp->_w < len) {
size_t blen = fp->_p - fp->_bf._base;
unsigned char *_base;
+ int _size;
- /*
- * Alloc an extra 128 bytes (+ 1 for NULL)
- * so we don't call realloc(3) so often.
- */
- fp->_w = len + 128;
- fp->_bf._size = blen + len + 128;
- _base = realloc(fp->_bf._base, fp->_bf._size + 1);
+ /* Allocate space exponentially. */
+ _size = fp->_bf._size;
+ do {
+ _size = (_size << 1) + 1;
+ } while (_size < blen + len);
+ _base = realloc(fp->_bf._base, _size + 1);
if (_base == NULL)
goto err;
+ fp->_w += _size - fp->_bf._size;
fp->_bf._base = _base;
- fp->_p = fp->_bf._base + blen;
+ fp->_bf._size = _size;
+ fp->_p = _base + blen;
}
w = fp->_w;
if (fp->_flags & __SSTR) {