* m4/calloc.m4 (gl_FUNC_CALLOC_IF): Rename from _AC_FUNC_CALLOC_IF
since this is not derived from Autoconf.  All uses changed.
Redo implementation so that it is more like _AC_FUNC_MALLOC_IF;
this fixes some problems with "volatile" and modernizes the known
platforms.
---
 ChangeLog    |  7 +++++++
 m4/calloc.m4 | 57 +++++++++++++++++++++++-----------------------------
 2 files changed, 32 insertions(+), 32 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index d9c9cad52c..f12ca64f17 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2024-11-04  Paul Eggert  <egg...@cs.ucla.edu>
 
+       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.
+       Redo implementation so that it is more like _AC_FUNC_MALLOC_IF;
+       this fixes some problems with "volatile" and modernizes the known
+       platforms.
+
        malloc-gnu: depend on stdckdint not xalloc-oversized
        * lib/malloc.c: Include stdckdint.h, xalloc-oversized.h.
        (rpl_malloc): Use ckd_mul instead of xalloc_oversized.
diff --git a/m4/calloc.m4 b/m4/calloc.m4
index 77b0d9fd91..7feddbe21c 100644
--- a/m4/calloc.m4
+++ b/m4/calloc.m4
@@ -1,5 +1,5 @@
 # calloc.m4
-# serial 34
+# serial 35
 dnl Copyright (C) 2004-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,
@@ -13,41 +13,34 @@ dnl This file is offered as-is, without any warranty.
 # If so, define HAVE_CALLOC.  Otherwise, define calloc to rpl_calloc
 # and arrange to use a calloc wrapper function that does work in that case.
 
-# _AC_FUNC_CALLOC_IF([IF-WORKS], [IF-NOT])
-# -------------------------------------
+# gl_FUNC_CALLOC_IF([IF-WORKS], [IF-NOT])
+# ---------------------------------------
 # If calloc is compatible with GNU calloc, run IF-WORKS, otherwise, IF-NOT.
-AC_DEFUN([_AC_FUNC_CALLOC_IF],
+AC_DEFUN([gl_FUNC_CALLOC_IF],
 [
   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
   AC_CACHE_CHECK([whether calloc (0, n) and calloc (n, 0) return nonnull],
     [ac_cv_func_calloc_0_nonnull],
-    [if test $cross_compiling != yes; then
-       ac_cv_func_calloc_0_nonnull=yes
-       AC_RUN_IFELSE(
-         [AC_LANG_PROGRAM(
-            [AC_INCLUDES_DEFAULT],
-            [[int result = 0;
-              char * volatile p = calloc (0, 0);
-              if (!p)
-                result |= 1;
-              free (p);
-              return result;
-            ]])],
-         [],
-         [ac_cv_func_calloc_0_nonnull=no])
-     else
-       case "$host_os" in
-                             # Guess yes on glibc systems.
-         *-gnu* | gnu*)      ac_cv_func_calloc_0_nonnull="guessing yes" ;;
-                             # Guess yes on musl systems.
-         *-musl* | midipix*) ac_cv_func_calloc_0_nonnull="guessing yes" ;;
-                             # Guess yes on native Windows.
-         mingw* | windows*)  ac_cv_func_calloc_0_nonnull="guessing yes" ;;
-                             # If we don't know, obey --enable-cross-guesses.
-         *)                  
ac_cv_func_calloc_0_nonnull="$gl_cross_guess_normal" ;;
-       esac
-     fi
-    ])
+    [AC_RUN_IFELSE(
+       [AC_LANG_PROGRAM(
+          [[#include <stdlib.h>
+            /* Use pcalloc to test; "volatile" prevents the compiler
+               from optimizing the calloc call away.  */
+            void *(*volatile pcalloc) (size_t, size_t) = calloc;]],
+          [[void *p = pcalloc (0, 0);
+            int result = !p;
+            free (p);
+            return result;]])],
+       [ac_cv_func_calloc_0_nonnull=yes],
+       [ac_cv_func_calloc_0_nonnull=no],
+       [AS_CASE([$host_os],
+          [# Guess yes on platforms where we know the result.
+           *-gnu* | freebsd* | netbsd* | openbsd* | bitrig* \
+           | gnu* | *-musl* | midipix* | midnightbsd* \
+           | hpux* | solaris* | cygwin* | mingw* | windows* | msys*],
+            [ac_cv_func_malloc_0_nonnull="guessing yes"],
+          [# Guess as follows if we don't know.
+           ac_cv_func_malloc_0_nonnull=$gl_cross_guess_normal])])])
   AS_CASE([$ac_cv_func_calloc_0_nonnull], [*yes], [$1], [$2])
 ])
 
@@ -66,7 +59,7 @@ AC_DEFUN([gl_FUNC_CALLOC_GNU],
 
   REPLACE_CALLOC_FOR_CALLOC_GNU="$REPLACE_CALLOC_FOR_CALLOC_POSIX"
   if test $REPLACE_CALLOC_FOR_CALLOC_GNU = 0; then
-    _AC_FUNC_CALLOC_IF([], [REPLACE_CALLOC_FOR_CALLOC_GNU=1])
+    gl_FUNC_CALLOC_IF([], [REPLACE_CALLOC_FOR_CALLOC_GNU=1])
   fi
 ])# gl_FUNC_CALLOC_GNU
 
-- 
2.43.0


Reply via email to