1 | # malloc.m4 serial 27
|
---|
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.
|
---|
6 |
|
---|
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],
|
---|
10 | [
|
---|
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],
|
---|
14 | [AC_RUN_IFELSE(
|
---|
15 | [AC_LANG_PROGRAM(
|
---|
16 | [[#include <stdlib.h>
|
---|
17 | ]],
|
---|
18 | [[void *p = malloc (0);
|
---|
19 | int result = !p;
|
---|
20 | free (p);
|
---|
21 | return result;]])
|
---|
22 | ],
|
---|
23 | [ac_cv_func_malloc_0_nonnull=yes],
|
---|
24 | [ac_cv_func_malloc_0_nonnull=no],
|
---|
25 | [case "$host_os" in
|
---|
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" ;;
|
---|
33 | esac
|
---|
34 | ])
|
---|
35 | ])
|
---|
36 | AS_CASE([$ac_cv_func_malloc_0_nonnull], [*yes], [$1], [$2])
|
---|
37 | ])# _AC_FUNC_MALLOC_IF
|
---|
38 |
|
---|
39 | # gl_FUNC_MALLOC_GNU
|
---|
40 | # ------------------
|
---|
41 | # Replace malloc if it is not compatible with GNU libc.
|
---|
42 | AC_DEFUN([gl_FUNC_MALLOC_GNU],
|
---|
43 | [
|
---|
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])
|
---|
48 | fi
|
---|
49 | ])
|
---|
50 |
|
---|
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],
|
---|
56 | [
|
---|
57 | AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
|
---|
58 | AC_REQUIRE([gl_CHECK_MALLOC_PTRDIFF])
|
---|
59 | test "$gl_cv_malloc_ptrdiff" = yes || REPLACE_MALLOC=1
|
---|
60 | ])
|
---|
61 |
|
---|
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],
|
---|
66 | [
|
---|
67 | AC_CACHE_CHECK([whether malloc is ptrdiff_t safe],
|
---|
68 | [gl_cv_malloc_ptrdiff],
|
---|
69 | [AC_COMPILE_IFELSE(
|
---|
70 | [AC_LANG_PROGRAM(
|
---|
71 | [[#include <stdint.h>
|
---|
72 | ]],
|
---|
73 | [[/* 64-bit ptrdiff_t is so wide that no practical platform
|
---|
74 | can exceed it. */
|
---|
75 | #define WIDE_PTRDIFF (PTRDIFF_MAX >> 31 >> 31 != 0)
|
---|
76 |
|
---|
77 | /* On rare machines where size_t fits in ptrdiff_t there
|
---|
78 | is no problem. */
|
---|
79 | #define NARROW_SIZE (SIZE_MAX <= PTRDIFF_MAX)
|
---|
80 |
|
---|
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__))
|
---|
85 |
|
---|
86 | #if WIDE_PTRDIFF || NARROW_SIZE || KNOWN_SAFE
|
---|
87 | return 0;
|
---|
88 | #else
|
---|
89 | #error "malloc might not be ptrdiff_t safe"
|
---|
90 | syntax error
|
---|
91 | #endif
|
---|
92 | ]])],
|
---|
93 | [gl_cv_malloc_ptrdiff=yes],
|
---|
94 | [gl_cv_malloc_ptrdiff=no])
|
---|
95 | ])
|
---|
96 | ])
|
---|
97 |
|
---|
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],
|
---|
104 | [
|
---|
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.])
|
---|
111 | else
|
---|
112 | REPLACE_MALLOC=1
|
---|
113 | fi
|
---|
114 | ])
|
---|
115 |
|
---|
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],
|
---|
119 | [
|
---|
120 | AC_REQUIRE([AC_CANONICAL_HOST])
|
---|
121 | AC_CACHE_CHECK([whether malloc, realloc, calloc set errno on failure],
|
---|
122 | [gl_cv_func_malloc_posix],
|
---|
123 | [
|
---|
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.
|
---|
129 | case "$host_os" in
|
---|
130 | mingw*)
|
---|
131 | gl_cv_func_malloc_posix=no ;;
|
---|
132 | irix* | solaris*)
|
---|
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
|
---|
137 | dnl PTRDIFF_MAX.
|
---|
138 | dnl Here is a test program:
|
---|
139 | m4_divert_push([KILL])
|
---|
140 | #include <errno.h>
|
---|
141 | #include <stdio.h>
|
---|
142 | #include <stdlib.h>
|
---|
143 | #define ptrdiff_t long
|
---|
144 | #ifndef PTRDIFF_MAX
|
---|
145 | # define PTRDIFF_MAX ((ptrdiff_t) ((1UL << (8 * sizeof (ptrdiff_t) - 1)) - 1))
|
---|
146 | #endif
|
---|
147 |
|
---|
148 | int main ()
|
---|
149 | {
|
---|
150 | void *p;
|
---|
151 |
|
---|
152 | fprintf (stderr, "PTRDIFF_MAX = %lu\n", (unsigned long) PTRDIFF_MAX);
|
---|
153 |
|
---|
154 | errno = 0;
|
---|
155 | p = malloc ((unsigned long) PTRDIFF_MAX + 1);
|
---|
156 | fprintf (stderr, "p=%p errno=%d\n", p, errno);
|
---|
157 |
|
---|
158 | errno = 0;
|
---|
159 | p = calloc (PTRDIFF_MAX / 2 + 1, 2);
|
---|
160 | fprintf (stderr, "p=%p errno=%d\n", p, errno);
|
---|
161 |
|
---|
162 | errno = 0;
|
---|
163 | p = realloc (NULL, (unsigned long) PTRDIFF_MAX + 1);
|
---|
164 | fprintf (stderr, "p=%p errno=%d\n", p, errno);
|
---|
165 |
|
---|
166 | return 0;
|
---|
167 | }
|
---|
168 | m4_divert_pop([KILL])
|
---|
169 | gl_cv_func_malloc_posix=no ;;
|
---|
170 | *)
|
---|
171 | gl_cv_func_malloc_posix=yes ;;
|
---|
172 | esac
|
---|
173 | ])
|
---|
174 | ])
|
---|