[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] reallocarray: a bit more tuning
From: |
Paul Eggert |
Subject: |
[PATCH] reallocarray: a bit more tuning |
Date: |
Sun, 25 Apr 2021 23:12:42 -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 | 9 +++++++++
lib/reallocarray.c | 7 ++++---
modules/reallocarray | 4 ++--
3 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 03b01b3d7..6a449380e 100644
--- 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
diff --git a/lib/reallocarray.c b/lib/reallocarray.c
index 03197c601..d5cfa05d5 100644
--- a/lib/reallocarray.c
+++ b/lib/reallocarray.c
@@ -22,17 +22,18 @@
#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);
}
diff --git a/modules/reallocarray b/modules/reallocarray
index 6d762c157..dcf851d5f 100644
--- a/modules/reallocarray
+++ b/modules/reallocarray
@@ -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:
--
2.27.0
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [PATCH] reallocarray: a bit more tuning,
Paul Eggert <=