Allocate space exponentially, not linearly.

This commit is contained in:
mycroft 1998-10-15 07:10:38 +00:00
parent d153ad6324
commit 285284da77

View File

@ -1,4 +1,4 @@
/* $NetBSD: fvwrite.c,v 1.9 1998/08/28 21:33:11 perry Exp $ */ /* $NetBSD: fvwrite.c,v 1.10 1998/10/15 07:10:38 mycroft Exp $ */
/*- /*-
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
@ -41,7 +41,7 @@
#if 0 #if 0
static char sccsid[] = "@(#)fvwrite.c 8.1 (Berkeley) 6/4/93"; static char sccsid[] = "@(#)fvwrite.c 8.1 (Berkeley) 6/4/93";
#else #else
__RCSID("$NetBSD: fvwrite.c,v 1.9 1998/08/28 21:33:11 perry Exp $"); __RCSID("$NetBSD: fvwrite.c,v 1.10 1998/10/15 07:10:38 mycroft Exp $");
#endif #endif
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
@ -122,18 +122,20 @@ __sfvwrite(fp, uio)
(__SALC | __SSTR) && fp->_w < len) { (__SALC | __SSTR) && fp->_w < len) {
size_t blen = fp->_p - fp->_bf._base; size_t blen = fp->_p - fp->_bf._base;
unsigned char *_base; unsigned char *_base;
int _size;
/* /* Allocate space exponentially. */
* Alloc an extra 128 bytes (+ 1 for NULL) _size = fp->_bf._size;
* so we don't call realloc(3) so often. do {
*/ _size = (_size << 1) + 1;
fp->_w = len + 128; } while (_size < blen + len);
fp->_bf._size = blen + len + 128; _base = realloc(fp->_bf._base, _size + 1);
_base = realloc(fp->_bf._base, fp->_bf._size + 1);
if (_base == NULL) if (_base == NULL)
goto err; goto err;
fp->_w += _size - fp->_bf._size;
fp->_bf._base = _base; fp->_bf._base = _base;
fp->_p = fp->_bf._base + blen; fp->_bf._size = _size;
fp->_p = _base + blen;
} }
w = fp->_w; w = fp->_w;
if (fp->_flags & __SSTR) { if (fp->_flags & __SSTR) {