autoupdate
[gnulib.git] / m4 / malloc.m4
blob41a46937ead15cbaba7f1f52b444db5f60b82885
1 # malloc.m4
2 # serial 31
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],
15     [AC_RUN_IFELSE(
16        [AC_LANG_PROGRAM(
17           [[#include <stdlib.h>
18           ]],
19           [[void *p = malloc (0);
20             void * volatile vp = p;
21             int result = !vp;
22             free (p);
23             return result;]])
24        ],
25        [ac_cv_func_malloc_0_nonnull=yes],
26        [ac_cv_func_malloc_0_nonnull=no],
27        [case "$host_os" in
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" ;;
35         esac
36        ])
37     ])
38   AS_CASE([$ac_cv_func_malloc_0_nonnull], [*yes], [$1], [$2])
39 ])# _AC_FUNC_MALLOC_IF
41 # gl_FUNC_MALLOC_GNU
42 # ------------------
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])
51   fi
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],
72     [AC_COMPILE_IFELSE(
73        [AC_LANG_PROGRAM(
74           [[#include <stdint.h>
75           ]],
76           [[/* 64-bit ptrdiff_t is so wide that no practical platform
77                can exceed it.  */
78             #define WIDE_PTRDIFF (PTRDIFF_MAX >> 31 >> 31 != 0)
80             /* On rare machines where size_t fits in ptrdiff_t there
81                is no problem.  */
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
90               return 0;
91             #else
92               #error "malloc might not be ptrdiff_t safe"
93               syntax error
94             #endif
95           ]])],
96        [gl_cv_malloc_ptrdiff=yes],
97        [gl_cv_malloc_ptrdiff=no])
98     ])
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.])
114   else
115     REPLACE_MALLOC_FOR_MALLOC_POSIX=1
116   fi
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],
126     [
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.
132       case "$host_os" in
133         mingw* | windows*)
134           gl_cv_func_malloc_posix=no ;;
135         irix* | solaris*)
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
140           dnl PTRDIFF_MAX.
141           dnl Here is a test program:
142 m4_divert_push([KILL])
143 #include <errno.h>
144 #include <stdio.h>
145 #include <stdlib.h>
146 #define ptrdiff_t long
147 #ifndef PTRDIFF_MAX
148 # define PTRDIFF_MAX ((ptrdiff_t) ((1UL << (8 * sizeof (ptrdiff_t) - 1)) - 1))
149 #endif
151 int main ()
153   void *p;
155   fprintf (stderr, "PTRDIFF_MAX = %lu\n", (unsigned long) PTRDIFF_MAX);
157   errno = 0;
158   p = malloc ((unsigned long) PTRDIFF_MAX + 1);
159   fprintf (stderr, "p=%p errno=%d\n", p, errno);
161   errno = 0;
162   p = calloc (PTRDIFF_MAX / 2 + 1, 2);
163   fprintf (stderr, "p=%p errno=%d\n", p, errno);
165   errno = 0;
166   p = realloc (NULL, (unsigned long) PTRDIFF_MAX + 1);
167   fprintf (stderr, "p=%p errno=%d\n", p, errno);
169   return 0;
171 m4_divert_pop([KILL])
172           gl_cv_func_malloc_posix=no ;;
173         *)
174           gl_cv_func_malloc_posix=yes ;;
175       esac
176     ])