Give tools a better chance to allocate space for very large buffers.
authorJim Meyering <jim@meyering.net>
Thu, 1 Feb 2007 23:53:04 +0000 (23:53 +0000)
committerJim Meyering <jim@meyering.net>
Thu, 1 Feb 2007 23:53:04 +0000 (23:53 +0000)
* lib/xalloc.h (x2nrealloc): Use 3/2, not 2, as buffer size factor.

ChangeLog
lib/xalloc.h

index 53454f0..165931e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2007-02-01  Jim Meyering  <jim@meyering.net>
+
+       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  <karl@gnu.org>
 
        * config/srclist.txt (strtok_r.c): lose sync, no more strtok_r.h.
index 17ab514..fb5cdca 100644 (file)
@@ -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;