Debian Codegraph @

m4/malloc.m4

raw
1 # malloc.m4
2 # serial 43
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.
7 dnl This file is offered as-is, without any warranty.
8
9 m4_version_prereq([2.73], [], [
10 # Modules that use this macro directly or indirectly should depend
11 # on extensions-aix, so that _LINUX_SOURCE_COMPAT gets defined
12 # before this macro gets invoked. This helps on AIX 7.2 and earlier
13 # if !(__VEC__ || __AIXVEC), and doesn't hurt otherwise.
14 #
15 # This is copied from upstream Autoconf here:
16 # https://git.savannah.gnu.org/cgit/autoconf.git/tree/lib/autoconf/functions.m4?id=1f38316f6af7bf63e5e7dd187ff6456e07ad743e#n971
17 # _AC_FUNC_MALLOC_IF(IF-WORKS, IF-NOT[, UNKNOWN-ASSUME])
18 # ------------------------------------------------------
19 # If 'malloc (0)' returns nonnull, run IF-WORKS, otherwise, IF-NOT.
20 # If it is not known whether it works, assume the shell word UNKNOWN-ASSUME,
21 # which should end in "yes" or in something else (the latter is the default).
22 AC_DEFUN([_AC_FUNC_MALLOC_IF],
23 [
24 AC_REQUIRE([AC_CANONICAL_HOST])dnl for cross-compiles
25 AC_CACHE_CHECK([whether malloc (0) returns nonnull],
26 [ac_cv_func_malloc_0_nonnull],
27 [AC_RUN_IFELSE(
28 [AC_LANG_PROGRAM(
29 [[#include <stdlib.h>
30 /* Use pmalloc to test; 'volatile' prevents the compiler
31 from optimizing the malloc call away. */
32 void *(*volatile pmalloc) (size_t) = malloc;]],
33 [[void *p = pmalloc (0);
34 int result = !p;
35 free (p);
36 return result;]])],
37 [ac_cv_func_malloc_0_nonnull=yes],
38 [ac_cv_func_malloc_0_nonnull=no],
39 [AS_CASE([$host_os],
40 [# Guess yes on platforms where we know the result.
41 *-gnu* | freebsd* | netbsd* | openbsd* | bitrig* \
42 | gnu* | *-musl* | midipix* | midnightbsd* \
43 | hpux* | solaris* | cygwin* | mingw* | windows* | msys*],
44 [ac_cv_func_malloc_0_nonnull="guessing yes"],
45 [# Guess as follows if we don't know.
46 ac_cv_func_malloc_0_nonnull=m4_default([$3], ["guessing no"])])])])
47 AS_CASE([$ac_cv_func_malloc_0_nonnull], [*yes], [$1], [$2])
48 ])# _AC_FUNC_MALLOC_IF
49 ])
50
51 # gl_FUNC_MALLOC_0_NONNULL
52 # ------------------------
53 # If 'malloc (0)' returns nonnull define HAVE_MALLOC_0_NONNULL.
54 # Also, set ac_cv_func_malloc_0_nonnull to a string that ends in
55 # "yes", otherwise set it to something else. If unknown whether
56 # malloc (0) works, guess as normal for cross-builds.
57 AC_DEFUN([gl_FUNC_MALLOC_0_NONNULL],
58 [
59 _AC_FUNC_MALLOC_IF(
60 [AC_DEFINE([HAVE_MALLOC_0_NONNULL], [1],
61 [Define to 1 if malloc (0) returns nonnull.])],
62 [],
63 ["$gl_cross_guess_normal"])
64 ])
65
66 # gl_FUNC_MALLOC_GNU
67 # ------------------
68 # Test whether malloc (0) is compatible with GNU libc.
69 # Replace malloc if not.
70 # Define HAVE_MALLOC_0_NONNULL if malloc (0) returns nonnull (except upon
71 # out-of-memory).
72 # Define HAVE_MALLOC_PTRDIFF if malloc (N) reliably fails when N exceeds
73 # PTRDIFF_MAX.
74 AC_DEFUN([gl_FUNC_MALLOC_GNU],
75 [
76 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
77 AC_REQUIRE([gl_FUNC_MALLOC_POSIX])
78 AC_REQUIRE([gl_FUNC_MALLOC_0_NONNULL])
79
80 AS_CASE([$ac_cv_func_malloc_0_nonnull],
81 [*yes],
82 [REPLACE_MALLOC_FOR_MALLOC_GNU=$REPLACE_MALLOC_FOR_MALLOC_POSIX],
83 [REPLACE_MALLOC_FOR_MALLOC_GNU=1])
84 ])
85
86 # gl_FUNC_MALLOC_PTRDIFF
87 # ----------------------
88 # Test whether malloc (N) reliably fails when N exceeds PTRDIFF_MAX.
89 # Define HAVE_MALLOC_PTRDIFF if yes.
90 # Replace malloc if not.
91 AC_DEFUN([gl_FUNC_MALLOC_PTRDIFF],
92 [
93 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
94 AC_REQUIRE([gl_CHECK_MALLOC_PTRDIFF])
95 AS_IF([test "$gl_cv_malloc_ptrdiff" = yes],
96 [AC_DEFINE([HAVE_MALLOC_PTRDIFF], 1,
97 [Define to 1 if malloc-like functions do not allocate objects
98 larger than PTRDIFF_MAX bytes.])],
99 [REPLACE_MALLOC_FOR_MALLOC_POSIX=1])
100 ])
101
102 # Test whether malloc, realloc, calloc refuse to create objects
103 # larger than what can be expressed in ptrdiff_t.
104 # Set gl_cv_func_malloc_gnu.
105 AC_DEFUN([gl_CHECK_MALLOC_PTRDIFF],
106 [
107 AC_CACHE_CHECK([whether malloc is ptrdiff_t safe],
108 [gl_cv_malloc_ptrdiff],
109 [AC_COMPILE_IFELSE(
110 [AC_LANG_PROGRAM(
111 [[#include <stdint.h>
112 ]],
113 [[/* 64-bit ptrdiff_t is so wide that no practical platform
114 can exceed it. */
115 #define WIDE_PTRDIFF (PTRDIFF_MAX >> 31 >> 31 != 0)
116
117 /* On rare machines where size_t fits in ptrdiff_t there
118 is no problem. */
119 #define NARROW_SIZE (SIZE_MAX <= PTRDIFF_MAX)
120
121 /* glibc 2.30 and later malloc refuses to exceed ptrdiff_t
122 bounds even on 32-bit platforms. We don't know which
123 non-glibc systems are safe. */
124 #define KNOWN_SAFE (2 < __GLIBC__ + (30 <= __GLIBC_MINOR__))
125
126 #if WIDE_PTRDIFF || NARROW_SIZE || KNOWN_SAFE
127 return 0;
128 #else
129 #error "malloc might not be ptrdiff_t safe"
130 syntax error
131 #endif
132 ]])],
133 [gl_cv_malloc_ptrdiff=yes],
134 [gl_cv_malloc_ptrdiff=no])
135 ])
136 ])
137
138 # gl_FUNC_MALLOC_POSIX
139 # --------------------
140 # Test whether 'malloc' is POSIX compliant (sets errno to ENOMEM when it
141 # fails, and doesn't mess up with ptrdiff_t overflow), and replace
142 # malloc if it is not.
143 AC_DEFUN([gl_FUNC_MALLOC_POSIX],
144 [
145 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
146 AC_REQUIRE([gl_FUNC_MALLOC_PTRDIFF])
147 AC_REQUIRE([gl_CHECK_MALLOC_POSIX])
148 case "$gl_cv_func_malloc_posix" in
149 *yes)
150 AC_DEFINE([HAVE_MALLOC_POSIX], [1],
151 [Define if malloc, realloc, and calloc set errno on allocation failure.])
152 ;;
153 *)
154 REPLACE_MALLOC_FOR_MALLOC_POSIX=1
155 ;;
156 esac
157 ])
158
159 # Test whether malloc, realloc, calloc set errno to ENOMEM on failure.
160 # Set gl_cv_func_malloc_posix to *yes or *no accordingly.
161 AC_DEFUN([gl_CHECK_MALLOC_POSIX],
162 [
163 AC_REQUIRE([AC_CANONICAL_HOST])
164 AC_CACHE_CHECK([whether malloc, realloc, calloc set errno on failure],
165 [gl_cv_func_malloc_posix],
166 [
167 dnl It is too dangerous to try to allocate a large amount of memory:
168 dnl some systems go to their knees when you do that. So assume that
169 dnl all Unix implementations of the function set errno on failure,
170 dnl except on those platforms where we have seen 'test-malloc-gnu',
171 dnl 'test-realloc-posix', 'test-calloc-gnu' fail.
172 case "$host_os" in
173 mingw* | windows*)
174 dnl Old MSVCRT from 2001 did not set errno=ENOMEM when malloc failed.
175 dnl More recent MSVCRT from 2019 does so.
176 dnl UCRT is the successor of MSVCRT. Assume that UCRT does so as well.
177 AC_COMPILE_IFELSE(
178 [AC_LANG_PROGRAM(
179 [[#include <stdio.h>
180 #ifndef _UCRT
181 msvcrt yuck
182 #endif
183 ]],
184 [[]])
185 ],
186 [gl_cv_func_malloc_posix="guessing yes"],
187 [gl_cv_func_malloc_posix="guessing no"])
188 ;;
189 irix* | solaris*)
190 dnl On IRIX 6.5, the three functions return NULL with errno unset
191 dnl when the argument is larger than PTRDIFF_MAX.
192 dnl On Solaris 11.3, the three functions return NULL with errno set
193 dnl to EAGAIN, not ENOMEM, when the argument is larger than
194 dnl PTRDIFF_MAX.
195 dnl Here is a test program:
196 m4_divert_push([KILL])
197 #include <errno.h>
198 #include <stdio.h>
199 #include <stdlib.h>
200 #define ptrdiff_t long
201 #ifndef PTRDIFF_MAX
202 # define PTRDIFF_MAX ((ptrdiff_t) ((1UL << (8 * sizeof (ptrdiff_t) - 1)) - 1))
203 #endif
204
205 int main ()
206 {
207 void *p;
208
209 fprintf (stderr, "PTRDIFF_MAX = %lu\n", (unsigned long) PTRDIFF_MAX);
210
211 errno = 0;
212 p = malloc ((unsigned long) PTRDIFF_MAX + 1);
213 fprintf (stderr, "p=%p errno=%d\n", p, errno);
214
215 errno = 0;
216 p = calloc (PTRDIFF_MAX / 2 + 1, 2);
217 fprintf (stderr, "p=%p errno=%d\n", p, errno);
218
219 errno = 0;
220 p = realloc (NULL, (unsigned long) PTRDIFF_MAX + 1);
221 fprintf (stderr, "p=%p errno=%d\n", p, errno);
222
223 return 0;
224 }
225 m4_divert_pop([KILL])
226 gl_cv_func_malloc_posix=no ;;
227 *)
228 gl_cv_func_malloc_posix=yes ;;
229 esac
230 ])
231 ])