From: Jim Meyering Date: Thu, 1 Feb 2007 23:53:04 +0000 (+0000) Subject: Give tools a better chance to allocate space for very large buffers. X-Git-Tag: cvs-readonly~1153 X-Git-Url: http://erislabs.org.uk/gitweb/?a=commitdiff_plain;h=9dd25cd0161c75210441a73709c93c7dcb087efa;p=gnulib.git Give tools a better chance to allocate space for very large buffers. * lib/xalloc.h (x2nrealloc): Use 3/2, not 2, as buffer size factor. --- diff --git a/ChangeLog b/ChangeLog index 53454f055..165931e69 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2007-02-01 Jim Meyering + + Give tools a better chance to allocate space for very large buffers. + * lib/xalloc.h (x2nrealloc): Use 3/2, not 2, as buffer size factor. + 2007-02-01 Karl Berry * config/srclist.txt (strtok_r.c): lose sync, no more strtok_r.h. diff --git a/lib/xalloc.h b/lib/xalloc.h index 17ab51421..fb5cdcab3 100644 --- a/lib/xalloc.h +++ b/lib/xalloc.h @@ -1,7 +1,7 @@ /* xalloc.h -- malloc with out-of-memory checking Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, - 1999, 2000, 2003, 2004, 2006 Free Software Foundation, Inc. + 1999, 2000, 2003, 2004, 2006, 2007 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -139,10 +139,10 @@ xnrealloc (void *p, size_t n, size_t s) allocating an initial block with a nonzero size, or by allocating a larger block. - In the following implementation, nonzero sizes are doubled so that - repeated reallocations have O(N log N) overall cost rather than - O(N**2) cost, but the specification for this function does not - guarantee that sizes are doubled. + In the following implementation, nonzero sizes are increased by a + factor of approximately 1.5 so that repeated reallocations have + O(N log N) overall cost rather than O(N**2) cost, but the + specification for this function does not guarantee that rate. Here is an example of use: @@ -204,9 +204,9 @@ x2nrealloc (void *p, size_t *pn, size_t s) } else { - if (((size_t) -1) / 2 / s < n) + if ((2 * (((size_t) -1 - 1) / 3)) / s < n) xalloc_die (); - n *= 2; + n = n + n / 2 + 1; } *pn = n;