2 dnl Copyright (C) 2007, 2009-2021 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 if test $REPLACE_MALLOC = 0; then
47 _AC_FUNC_MALLOC_IF([], [REPLACE_MALLOC=1])
51 # gl_FUNC_MALLOC_PTRDIFF
52 # ----------------------
53 # Test whether malloc (N) reliably fails when N exceeds PTRDIFF_MAX,
54 # and replace malloc otherwise.
55 AC_DEFUN([gl_FUNC_MALLOC_PTRDIFF],
57 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
58 AC_REQUIRE([gl_CHECK_MALLOC_PTRDIFF])
59 test "$gl_cv_malloc_ptrdiff" = yes || REPLACE_MALLOC=1
62 # Test whether malloc, realloc, calloc refuse to create objects
63 # larger than what can be expressed in ptrdiff_t.
64 # Set gl_cv_func_malloc_gnu to yes or no accordingly.
65 AC_DEFUN([gl_CHECK_MALLOC_PTRDIFF],
67 AC_CACHE_CHECK([whether malloc is ptrdiff_t safe],
68 [gl_cv_malloc_ptrdiff],
73 [[/* 64-bit ptrdiff_t is so wide that no practical platform
75 #define WIDE_PTRDIFF (PTRDIFF_MAX >> 31 >> 31 != 0)
77 /* On rare machines where size_t fits in ptrdiff_t there
79 #define NARROW_SIZE (SIZE_MAX <= PTRDIFF_MAX)
81 /* glibc 2.30 and later malloc refuses to exceed ptrdiff_t
82 bounds even on 32-bit platforms. We don't know which
83 non-glibc systems are safe. */
84 #define KNOWN_SAFE (2 < __GLIBC__ + (30 <= __GLIBC_MINOR__))
86 #if WIDE_PTRDIFF || NARROW_SIZE || KNOWN_SAFE
89 #error "malloc might not be ptrdiff_t safe"
93 [gl_cv_malloc_ptrdiff=yes],
94 [gl_cv_malloc_ptrdiff=no])
98 # gl_FUNC_MALLOC_POSIX
99 # --------------------
100 # Test whether 'malloc' is POSIX compliant (sets errno to ENOMEM when it
101 # fails, and doesn't mess up with ptrdiff_t overflow), and replace
102 # malloc if it is not.
103 AC_DEFUN([gl_FUNC_MALLOC_POSIX],
105 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
106 AC_REQUIRE([gl_FUNC_MALLOC_PTRDIFF])
107 AC_REQUIRE([gl_CHECK_MALLOC_POSIX])
108 if test "$gl_cv_func_malloc_posix" = yes; then
109 AC_DEFINE([HAVE_MALLOC_POSIX], [1],
110 [Define if malloc, realloc, and calloc set errno on allocation failure.])
116 # Test whether malloc, realloc, calloc set errno to ENOMEM on failure.
117 # Set gl_cv_func_malloc_posix to yes or no accordingly.
118 AC_DEFUN([gl_CHECK_MALLOC_POSIX],
120 AC_REQUIRE([AC_CANONICAL_HOST])
121 AC_CACHE_CHECK([whether malloc, realloc, calloc set errno on failure],
122 [gl_cv_func_malloc_posix],
124 dnl It is too dangerous to try to allocate a large amount of memory:
125 dnl some systems go to their knees when you do that. So assume that
126 dnl all Unix implementations of the function set errno on failure,
127 dnl except on those platforms where we have seen 'test-malloc-gnu',
128 dnl 'test-realloc-gnu', 'test-calloc-gnu' fail.
131 gl_cv_func_malloc_posix=no ;;
133 dnl On IRIX 6.5, the three functions return NULL with errno unset
134 dnl when the argument is larger than PTRDIFF_MAX.
135 dnl On Solaris 11.3, the three functions return NULL with errno set
136 dnl to EAGAIN, not ENOMEM, when the argument is larger than
138 dnl Here is a test program:
139 m4_divert_push([KILL])
143 #define ptrdiff_t long
145 # define PTRDIFF_MAX ((ptrdiff_t) ((1UL << (8 * sizeof (ptrdiff_t) - 1)) - 1))
152 fprintf (stderr, "PTRDIFF_MAX = %lu\n", (unsigned long) PTRDIFF_MAX);
155 p = malloc ((unsigned long) PTRDIFF_MAX + 1);
156 fprintf (stderr, "p=%p errno=%d\n", p, errno);
159 p = calloc (PTRDIFF_MAX / 2 + 1, 2);
160 fprintf (stderr, "p=%p errno=%d\n", p, errno);
163 p = realloc (NULL, (unsigned long) PTRDIFF_MAX + 1);
164 fprintf (stderr, "p=%p errno=%d\n", p, errno);
168 m4_divert_pop([KILL])
169 gl_cv_func_malloc_posix=no ;;
171 gl_cv_func_malloc_posix=yes ;;