]> Savannah Git Hosting - gnulib.git/commitdiff
reallocarray: a bit more tuning
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 26 Apr 2021 06:11:15 +0000 (23:11 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 26 Apr 2021 06:12:25 +0000 (23:12 -0700)
* lib/reallocarray.c: Include intprops.h.
(reallocarray): Use INT_MULTIPLY_WRAPV instead of
xalloc_oversized, as it now suffices and is likely a bit more
efficient (five fewer machine insns on Ubuntu 20.10 x86-64).
* modules/reallocarray (Depends-on): Depend on intprops, not
xalloc-oversized.  Use conditional dependencies to avoid building
realloc-gnu when not needed.

ChangeLog
lib/reallocarray.c
modules/reallocarray

index 03b01b3d7812e9dced7a46402c3984fcf1f7e244..6a449380e6de0b36e0836491807d8dbe0e5cfc4a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2021-04-25  Paul Eggert  <eggert@cs.ucla.edu>
 
+       reallocarray: a bit more tuning
+       * lib/reallocarray.c: Include intprops.h.
+       (reallocarray): Use INT_MULTIPLY_WRAPV instead of
+       xalloc_oversized, as it now suffices and is likely a bit more
+       efficient (five fewer machine insns on Ubuntu 20.10 x86-64).
+       * modules/reallocarray (Depends-on): Depend on intprops, not
+       xalloc-oversized.  Use conditional dependencies to avoid building
+       realloc-gnu when not needed.
+
        reallocarray: don’t crash if item size is 0
        This problem affects only platforms where xalloc_oversized
        divides a number by the size arg.  Fix this by defining
index 03197c601b484edae3bfde578017edeab7979e93..d5cfa05d57f1237bc47c7edb07e1d46768c042a5 100644 (file)
 #include <stdlib.h>
 #include <errno.h>
 
-#include "xalloc-oversized.h"
+#include "intprops.h"
 
 void *
 reallocarray (void *ptr, size_t nmemb, size_t size)
 {
-  if (xalloc_oversized (nmemb, size))
+  size_t nbytes;
+  if (INT_MULTIPLY_WRAPV (nmemb, size, &nbytes))
     {
       errno = ENOMEM;
       return NULL;
     }
 
   /* Rely on the semantics of GNU realloc.  */
-  return realloc (ptr, nmemb * size);
+  return realloc (ptr, nbytes);
 }
index 6d762c157a8480a34e0184c5eff38446e11fac0d..dcf851d5f01d226cd1fea71737190024170ea148 100644 (file)
@@ -8,8 +8,8 @@ m4/reallocarray.m4
 
 Depends-on:
 extensions
-xalloc-oversized
-realloc-gnu
+intprops       [test $HAVE_REALLOCARRAY = 0 || test $REPLACE_REALLOCARRAY = 1]
+realloc-gnu    [test $HAVE_REALLOCARRAY = 0 || test $REPLACE_REALLOCARRAY = 1]
 stdlib
 
 configure.ac: