bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] xalloc: allow x2nrealloc (P, PN, S) where P && !*PN


From: Paul Eggert
Subject: [PATCH] xalloc: allow x2nrealloc (P, PN, S) where P && !*PN
Date: Sat, 12 Apr 2014 10:45:47 -0700

* lib/xalloc.h (x2nrealloc): Extend slightly, to allow the current
size to be zero even when the pointer is nonnull.  This
accommodates the use case where P is malloc (0) and *PN is 0 on a
host where malloc (0) yields nonnull.
---
 ChangeLog    |  8 ++++++++
 lib/xalloc.h | 13 ++++++-------
 2 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 381b269..6bf7c00 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2014-04-12  Paul Eggert  <address@hidden>
+
+       xalloc: allow x2nrealloc (P, PN, S) where P && !*PN
+       * lib/xalloc.h (x2nrealloc): Extend slightly, to allow the current
+       size to be zero even when the pointer is nonnull.  This
+       accommodates the use case where P is malloc (0) and *PN is 0 on a
+       host where malloc (0) yields nonnull.
+
 2014-04-09  Eric Blake  <address@hidden>
 
        fts: avoid unnecessary strlen calls
diff --git a/lib/xalloc.h b/lib/xalloc.h
index 5f89145..0bd6bce 100644
--- a/lib/xalloc.h
+++ b/lib/xalloc.h
@@ -122,10 +122,9 @@ xnrealloc (void *p, size_t n, size_t s)
 
 /* If P is null, allocate a block of at least *PN such objects;
    otherwise, reallocate P so that it contains more than *PN objects
-   each of S bytes.  *PN must be nonzero unless P is null, and S must
-   be nonzero.  Set *PN to the new number of objects, and return the
-   pointer to the new block.  *PN is never set to zero, and the
-   returned pointer is never null.
+   each of S bytes.  S must be nonzero.  Set *PN to the new number of
+   objects, and return the pointer to the new block.  *PN is never set
+   to zero, and the returned pointer is never null.
 
    Repeated reallocations are guaranteed to make progress, either by
    allocating an initial block with a nonzero size, or by allocating a
@@ -196,13 +195,13 @@ x2nrealloc (void *p, size_t *pn, size_t s)
     }
   else
     {
-      /* Set N = ceil (1.5 * N) so that progress is made if N == 1.
+      /* Set N = floor (1.5 * N) + 1 so that progress is made even if N == 0.
          Check for overflow, so that N * S stays in size_t range.
-         The check is slightly conservative, but an exact check isn't
+         The check may be slightly conservative, but an exact check isn't
          worth the trouble.  */
       if ((size_t) -1 / 3 * 2 / s <= n)
         xalloc_die ();
-      n += (n + 1) / 2;
+      n += n / 2 + 1;
     }
 
   *pn = n;
-- 
1.9.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]