2 dnl Copyright (C) 2007, 2009-2022 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
7 # This is adapted with modifications from upstream Autoconf here:
8 # https://git.savannah.gnu.org/cgit/autoconf.git/tree/lib/autoconf/functions.m4?id=v2.70#n949
9 AC_DEFUN([_AC_FUNC_MALLOC_IF],
11 AC_REQUIRE([AC_CANONICAL_HOST])dnl for cross-compiles
12 AC_CACHE_CHECK([whether malloc (0) returns nonnull],
13 [ac_cv_func_malloc_0_nonnull],
18 [[void *p = malloc (0);
23 [ac_cv_func_malloc_0_nonnull=yes],
24 [ac_cv_func_malloc_0_nonnull=no],
26 # Guess yes on platforms where we know the result.
27 *-gnu* | freebsd* | netbsd* | openbsd* | bitrig* \
28 | gnu* | *-musl* | midnightbsd* \
29 | hpux* | solaris* | cygwin* | mingw* | msys* )
30 ac_cv_func_malloc_0_nonnull="guessing yes" ;;
31 # If we don't know, obey --enable-cross-guesses.
32 *) ac_cv_func_malloc_0_nonnull="$gl_cross_guess_normal" ;;
36 AS_CASE([$ac_cv_func_malloc_0_nonnull], [*yes], [$1], [$2])
37 ])# _AC_FUNC_MALLOC_IF
41 # Replace malloc if it is not compatible with GNU libc.
42 AC_DEFUN([gl_FUNC_MALLOC_GNU],
44 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
45 AC_REQUIRE([gl_FUNC_MALLOC_POSIX])
46 REPLACE_MALLOC_FOR_MALLOC_GNU="$REPLACE_MALLOC_FOR_MALLOC_POSIX"
47 if test $REPLACE_MALLOC_FOR_MALLOC_GNU = 0; then
48 _AC_FUNC_MALLOC_IF([], [REPLACE_MALLOC_FOR_MALLOC_GNU=1])
52 # gl_FUNC_MALLOC_PTRDIFF
53 # ----------------------
54 # Test whether malloc (N) reliably fails when N exceeds PTRDIFF_MAX,
55 # and replace malloc otherwise.
56 AC_DEFUN([gl_FUNC_MALLOC_PTRDIFF],
58 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
59 AC_REQUIRE([gl_CHECK_MALLOC_PTRDIFF])
60 test "$gl_cv_malloc_ptrdiff" = yes || REPLACE_MALLOC_FOR_MALLOC_POSIX=1
63 # Test whether malloc, realloc, calloc refuse to create objects
64 # larger than what can be expressed in ptrdiff_t.
65 # Set gl_cv_func_malloc_gnu to yes or no accordingly.
66 AC_DEFUN([gl_CHECK_MALLOC_PTRDIFF],
68 AC_CACHE_CHECK([whether malloc is ptrdiff_t safe],
69 [gl_cv_malloc_ptrdiff],
74 [[/* 64-bit ptrdiff_t is so wide that no practical platform
76 #define WIDE_PTRDIFF (PTRDIFF_MAX >> 31 >> 31 != 0)
78 /* On rare machines where size_t fits in ptrdiff_t there
80 #define NARROW_SIZE (SIZE_MAX <= PTRDIFF_MAX)
82 /* glibc 2.30 and later malloc refuses to exceed ptrdiff_t
83 bounds even on 32-bit platforms. We don't know which
84 non-glibc systems are safe. */
85 #define KNOWN_SAFE (2 < __GLIBC__ + (30 <= __GLIBC_MINOR__))
87 #if WIDE_PTRDIFF || NARROW_SIZE || KNOWN_SAFE
90 #error "malloc might not be ptrdiff_t safe"
94 [gl_cv_malloc_ptrdiff=yes],
95 [gl_cv_malloc_ptrdiff=no])
99 # gl_FUNC_MALLOC_POSIX
100 # --------------------
101 # Test whether 'malloc' is POSIX compliant (sets errno to ENOMEM when it
102 # fails, and doesn't mess up with ptrdiff_t overflow), and replace
103 # malloc if it is not.
104 AC_DEFUN([gl_FUNC_MALLOC_POSIX],
106 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
107 AC_REQUIRE([gl_FUNC_MALLOC_PTRDIFF])
108 AC_REQUIRE([gl_CHECK_MALLOC_POSIX])
109 if test "$gl_cv_func_malloc_posix" = yes; then
110 AC_DEFINE([HAVE_MALLOC_POSIX], [1],
111 [Define if malloc, realloc, and calloc set errno on allocation failure.])
113 REPLACE_MALLOC_FOR_MALLOC_POSIX=1
117 # Test whether malloc, realloc, calloc set errno to ENOMEM on failure.
118 # Set gl_cv_func_malloc_posix to yes or no accordingly.
119 AC_DEFUN([gl_CHECK_MALLOC_POSIX],
121 AC_REQUIRE([AC_CANONICAL_HOST])
122 AC_CACHE_CHECK([whether malloc, realloc, calloc set errno on failure],
123 [gl_cv_func_malloc_posix],
125 dnl It is too dangerous to try to allocate a large amount of memory:
126 dnl some systems go to their knees when you do that. So assume that
127 dnl all Unix implementations of the function set errno on failure,
128 dnl except on those platforms where we have seen 'test-malloc-gnu',
129 dnl 'test-realloc-gnu', 'test-calloc-gnu' fail.
132 gl_cv_func_malloc_posix=no ;;
134 dnl On IRIX 6.5, the three functions return NULL with errno unset
135 dnl when the argument is larger than PTRDIFF_MAX.
136 dnl On Solaris 11.3, the three functions return NULL with errno set
137 dnl to EAGAIN, not ENOMEM, when the argument is larger than
139 dnl Here is a test program:
140 m4_divert_push([KILL])
144 #define ptrdiff_t long
146 # define PTRDIFF_MAX ((ptrdiff_t) ((1UL << (8 * sizeof (ptrdiff_t) - 1)) - 1))
153 fprintf (stderr, "PTRDIFF_MAX = %lu\n", (unsigned long) PTRDIFF_MAX);
156 p = malloc ((unsigned long) PTRDIFF_MAX + 1);
157 fprintf (stderr, "p=%p errno=%d\n", p, errno);
160 p = calloc (PTRDIFF_MAX / 2 + 1, 2);
161 fprintf (stderr, "p=%p errno=%d\n", p, errno);
164 p = realloc (NULL, (unsigned long) PTRDIFF_MAX + 1);
165 fprintf (stderr, "p=%p errno=%d\n", p, errno);
169 m4_divert_pop([KILL])
170 gl_cv_func_malloc_posix=no ;;
172 gl_cv_func_malloc_posix=yes ;;