changeset 8065:25c6999713ef

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.
author Jim Meyering <jim@meyering.net>
date Thu, 01 Feb 2007 23:53:04 +0000
parents e3008dbaee22
children 83332347ff8e
files ChangeLog lib/xalloc.h
diffstat 2 files changed, 12 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- 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.
--- 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 @@
    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 @@
     }
   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;