[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 04/14] calloc, malloc: tune a bit
From: |
Paul Eggert |
Subject: |
[PATCH 04/14] calloc, malloc: tune a bit |
Date: |
Mon, 4 Nov 2024 21:43:24 -0800 |
This applies mostly to non-glibc platforms, or to 32-bit
glibc before glibc 2.30 (2019).
* lib/calloc.c (rpl_calloc):
* lib/malloc.c (rpl_malloc):
Optimize away some of the code if !HAVE_MALLOC_0_NONNULL or if
!HAVE_MALLOC_PTRDIFF.
* m4/malloc.m4 (gl_FUNC_MALLOC_GNU): Define HAVE_MALLOC_0_NONNULL.
(gl_FUNC_MALLOC_PTRDIFF): Define HAVE_MALLOC_PTRDIFF.
---
ChangeLog | 10 ++++++++++
lib/calloc.c | 4 ++++
lib/malloc.c | 4 ++++
m4/malloc.m4 | 22 ++++++++++++++--------
4 files changed, 32 insertions(+), 8 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index f12ca64f17..5edadc4ea8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
2024-11-04 Paul Eggert <eggert@cs.ucla.edu>
+ calloc, malloc: tune a bit
+ This applies mostly to non-glibc platforms, or to 32-bit
+ glibc before glibc 2.30 (2019).
+ * lib/calloc.c (rpl_calloc):
+ * lib/malloc.c (rpl_malloc):
+ Optimize away some of the code if !HAVE_MALLOC_0_NONNULL or if
+ !HAVE_MALLOC_PTRDIFF.
+ * m4/malloc.m4 (gl_FUNC_MALLOC_GNU): Define HAVE_MALLOC_0_NONNULL.
+ (gl_FUNC_MALLOC_PTRDIFF): Define HAVE_MALLOC_PTRDIFF.
+
calloc: configure more like malloc
* m4/calloc.m4 (gl_FUNC_CALLOC_IF): Rename from _AC_FUNC_CALLOC_IF
since this is not derived from Autoconf. All uses changed.
diff --git a/lib/calloc.c b/lib/calloc.c
index 801b672078..124c1f9958 100644
--- a/lib/calloc.c
+++ b/lib/calloc.c
@@ -34,15 +34,19 @@
void *
rpl_calloc (size_t n, size_t s)
{
+#if !HAVE_MALLOC_0_NONNULL
if (n == 0 || s == 0)
n = s = 1;
+#endif
+#if !HAVE_MALLOC_PTRDIFF
ptrdiff_t signed_n;
if (ckd_mul (&signed_n, n, s))
{
errno = ENOMEM;
return NULL;
}
+#endif
void *result = calloc (n, s);
diff --git a/lib/malloc.c b/lib/malloc.c
index 90018863ea..fdb5348268 100644
--- a/lib/malloc.c
+++ b/lib/malloc.c
@@ -30,15 +30,19 @@
void *
rpl_malloc (size_t n)
{
+#if !HAVE_MALLOC_0_NONNULL
if (n == 0)
n = 1;
+#endif
+#if !HAVE_MALLOC_PTRDIFF
ptrdiff_t signed_n;
if (ckd_add (&signed_n, n, 0))
{
errno = ENOMEM;
return NULL;
}
+#endif
void *result = malloc (n);
diff --git a/m4/malloc.m4 b/m4/malloc.m4
index 657eed779c..deb0de24de 100644
--- a/m4/malloc.m4
+++ b/m4/malloc.m4
@@ -1,5 +1,5 @@
# malloc.m4
-# serial 40
+# serial 41
dnl Copyright (C) 2007, 2009-2024 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
@@ -45,6 +45,7 @@ AC_DEFUN([_AC_FUNC_MALLOC_IF],
# gl_FUNC_MALLOC_GNU
# ------------------
+# Define HAVE_MALLOC_0_NONNULL if malloc (0) returns nonnull.
# Replace malloc if it is not compatible with GNU libc.
AC_DEFUN([gl_FUNC_MALLOC_GNU],
[
@@ -55,11 +56,12 @@ AC_DEFUN([gl_FUNC_MALLOC_GNU],
dnl gets defined already before this macro gets invoked. This helps
dnl if !(__VEC__ || __AIXVEC), and doesn't hurt otherwise.
- REPLACE_MALLOC_FOR_MALLOC_GNU="$REPLACE_MALLOC_FOR_MALLOC_POSIX"
- if test $REPLACE_MALLOC_FOR_MALLOC_GNU = 0; then
- _AC_FUNC_MALLOC_IF([], [REPLACE_MALLOC_FOR_MALLOC_GNU=1],
- ["$gl_cross_guess_normal"])
- fi
+ _AC_FUNC_MALLOC_IF(
+ [AC_DEFINE([HAVE_MALLOC_0_NONNULL], [1],
+ [Define to 1 if malloc (0) returns nonnull.])
+ REPLACE_MALLOC_FOR_MALLOC_GNU=$REPLACE_MALLOC_FOR_MALLOC_POSIX],
+ [REPLACE_MALLOC_FOR_MALLOC_GNU=1],
+ ["$gl_cross_guess_normal"])
])
# gl_FUNC_MALLOC_PTRDIFF
@@ -70,12 +72,16 @@ AC_DEFUN([gl_FUNC_MALLOC_PTRDIFF],
[
AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
AC_REQUIRE([gl_CHECK_MALLOC_PTRDIFF])
- test "$gl_cv_malloc_ptrdiff" = yes || REPLACE_MALLOC_FOR_MALLOC_POSIX=1
+ AS_IF([test "$gl_cv_malloc_ptrdiff" = yes],
+ [AC_DEFINE([HAVE_MALLOC_PTRDIFF], 1,
+ [Define to 1 if malloc-like functions do not allocate objects
+ larger than PTRDIFF_MAX bytes.])],
+ [REPLACE_MALLOC_FOR_MALLOC_POSIX=1])
])
# Test whether malloc, realloc, calloc refuse to create objects
# larger than what can be expressed in ptrdiff_t.
-# Set gl_cv_func_malloc_gnu to yes or no accordingly.
+# Set gl_cv_func_malloc_gnu and define MALLOC_PTRDIFF accordingly.
AC_DEFUN([gl_CHECK_MALLOC_PTRDIFF],
[
AC_CACHE_CHECK([whether malloc is ptrdiff_t safe],
--
2.43.0
- [PATCH 01/14] calloc: depend on stdckdint not xalloc-oversized, Paul Eggert, 2024/11/05
- [PATCH 02/14] malloc-gnu: depend on stdckdint not xalloc-oversized, Paul Eggert, 2024/11/05
- [PATCH 03/14] calloc: configure more like malloc, Paul Eggert, 2024/11/05
- [PATCH 04/14] calloc, malloc: tune a bit,
Paul Eggert <=
- [PATCH 05/14] realloc: don’t require success for nongrowth, Paul Eggert, 2024/11/05
- [PATCH 06/14] stdlib: simplify preprocessor conditionals, Paul Eggert, 2024/11/05
- [PATCH 07/14] realloc-posix: realloc (..., 0) now returns nonnull, Paul Eggert, 2024/11/05
- [PATCH 08/14] realloc-posix: set CHERI bounds, Paul Eggert, 2024/11/05
- [PATCH 09/14] stdlib: make MB_CUR_MAX usable from extern inline, Paul Eggert, 2024/11/05