3 dnl Copyright (C) 2007, 2009-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 # This is adapted with modifications from upstream Autoconf here:
9 # https://git.savannah.gnu.org/cgit/autoconf.git/tree/lib/autoconf/functions.m4?id=v2.70#n949
10 AC_DEFUN([_AC_FUNC_MALLOC_IF],
12 AC_REQUIRE([AC_CANONICAL_HOST])dnl for cross-compiles
13 AC_CACHE_CHECK([whether malloc (0) returns nonnull],
14 [ac_cv_func_malloc_0_nonnull],
19 [[void *p = malloc (0);
20 void * volatile vp = p;
25 [ac_cv_func_malloc_0_nonnull=yes],
26 [ac_cv_func_malloc_0_nonnull=no],
28 # Guess yes on platforms where we know the result.
29 *-gnu* | freebsd* | netbsd* | openbsd* | bitrig* \
30 | gnu* | *-musl* | midipix* | midnightbsd* \
31 | hpux* | solaris* | cygwin* | mingw* | windows* | msys* )
32 ac_cv_func_malloc_0_nonnull="guessing yes" ;;
33 # If we don't know, obey --enable-cross-guesses.
34 *) ac_cv_func_malloc_0_nonnull="$gl_cross_guess_normal" ;;
38 AS_CASE([$ac_cv_func_malloc_0_nonnull], [*yes], [$1], [$2])
39 ])# _AC_FUNC_MALLOC_IF
43 # Replace malloc if it is not compatible with GNU libc.
44 AC_DEFUN([gl_FUNC_MALLOC_GNU],
46 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
47 AC_REQUIRE([gl_FUNC_MALLOC_POSIX])
48 REPLACE_MALLOC_FOR_MALLOC_GNU="$REPLACE_MALLOC_FOR_MALLOC_POSIX"
49 if test $REPLACE_MALLOC_FOR_MALLOC_GNU = 0; then
50 _AC_FUNC_MALLOC_IF([], [REPLACE_MALLOC_FOR_MALLOC_GNU=1])
54 # gl_FUNC_MALLOC_PTRDIFF
55 # ----------------------
56 # Test whether malloc (N) reliably fails when N exceeds PTRDIFF_MAX,
57 # and replace malloc otherwise.
58 AC_DEFUN([gl_FUNC_MALLOC_PTRDIFF],
60 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
61 AC_REQUIRE([gl_CHECK_MALLOC_PTRDIFF])
62 test "$gl_cv_malloc_ptrdiff" = yes || REPLACE_MALLOC_FOR_MALLOC_POSIX=1
65 # Test whether malloc, realloc, calloc refuse to create objects
66 # larger than what can be expressed in ptrdiff_t.
67 # Set gl_cv_func_malloc_gnu to yes or no accordingly.
68 AC_DEFUN([gl_CHECK_MALLOC_PTRDIFF],
70 AC_CACHE_CHECK([whether malloc is ptrdiff_t safe],
71 [gl_cv_malloc_ptrdiff],
76 [[/* 64-bit ptrdiff_t is so wide that no practical platform
78 #define WIDE_PTRDIFF (PTRDIFF_MAX >> 31 >> 31 != 0)
80 /* On rare machines where size_t fits in ptrdiff_t there
82 #define NARROW_SIZE (SIZE_MAX <= PTRDIFF_MAX)
84 /* glibc 2.30 and later malloc refuses to exceed ptrdiff_t
85 bounds even on 32-bit platforms. We don't know which
86 non-glibc systems are safe. */
87 #define KNOWN_SAFE (2 < __GLIBC__ + (30 <= __GLIBC_MINOR__))
89 #if WIDE_PTRDIFF || NARROW_SIZE || KNOWN_SAFE
92 #error "malloc might not be ptrdiff_t safe"
96 [gl_cv_malloc_ptrdiff=yes],
97 [gl_cv_malloc_ptrdiff=no])
101 # gl_FUNC_MALLOC_POSIX
102 # --------------------
103 # Test whether 'malloc' is POSIX compliant (sets errno to ENOMEM when it
104 # fails, and doesn't mess up with ptrdiff_t overflow), and replace
105 # malloc if it is not.
106 AC_DEFUN([gl_FUNC_MALLOC_POSIX],
108 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
109 AC_REQUIRE([gl_FUNC_MALLOC_PTRDIFF])
110 AC_REQUIRE([gl_CHECK_MALLOC_POSIX])
111 if test "$gl_cv_func_malloc_posix" = yes; then
112 AC_DEFINE([HAVE_MALLOC_POSIX], [1],
113 [Define if malloc, realloc, and calloc set errno on allocation failure.])
115 REPLACE_MALLOC_FOR_MALLOC_POSIX=1
119 # Test whether malloc, realloc, calloc set errno to ENOMEM on failure.
120 # Set gl_cv_func_malloc_posix to yes or no accordingly.
121 AC_DEFUN([gl_CHECK_MALLOC_POSIX],
123 AC_REQUIRE([AC_CANONICAL_HOST])
124 AC_CACHE_CHECK([whether malloc, realloc, calloc set errno on failure],
125 [gl_cv_func_malloc_posix],
127 dnl It is too dangerous to try to allocate a large amount of memory:
128 dnl some systems go to their knees when you do that. So assume that
129 dnl all Unix implementations of the function set errno on failure,
130 dnl except on those platforms where we have seen 'test-malloc-gnu',
131 dnl 'test-realloc-gnu', 'test-calloc-gnu' fail.
134 gl_cv_func_malloc_posix=no ;;
136 dnl On IRIX 6.5, the three functions return NULL with errno unset
137 dnl when the argument is larger than PTRDIFF_MAX.
138 dnl On Solaris 11.3, the three functions return NULL with errno set
139 dnl to EAGAIN, not ENOMEM, when the argument is larger than
141 dnl Here is a test program:
142 m4_divert_push([KILL])
146 #define ptrdiff_t long
148 # define PTRDIFF_MAX ((ptrdiff_t) ((1UL << (8 * sizeof (ptrdiff_t) - 1)) - 1))
155 fprintf (stderr, "PTRDIFF_MAX = %lu\n", (unsigned long) PTRDIFF_MAX);
158 p = malloc ((unsigned long) PTRDIFF_MAX + 1);
159 fprintf (stderr, "p=%p errno=%d\n", p, errno);
162 p = calloc (PTRDIFF_MAX / 2 + 1, 2);
163 fprintf (stderr, "p=%p errno=%d\n", p, errno);
166 p = realloc (NULL, (unsigned long) PTRDIFF_MAX + 1);
167 fprintf (stderr, "p=%p errno=%d\n", p, errno);
171 m4_divert_pop([KILL])
172 gl_cv_func_malloc_posix=no ;;
174 gl_cv_func_malloc_posix=yes ;;