summaryrefslogtreecommitdiffstats
path: root/gl/m4/malloc.m4
diff options
context:
space:
mode:
Diffstat (limited to 'gl/m4/malloc.m4')
-rw-r--r--gl/m4/malloc.m4139
1 files changed, 97 insertions, 42 deletions
diff --git a/gl/m4/malloc.m4 b/gl/m4/malloc.m4
index 41a46937..547b4e4d 100644
--- a/gl/m4/malloc.m4
+++ b/gl/m4/malloc.m4
@@ -1,12 +1,24 @@
1# malloc.m4 1# malloc.m4
2# serial 31 2# serial 43.1
3dnl Copyright (C) 2007, 2009-2024 Free Software Foundation, Inc. 3dnl Copyright (C) 2007, 2009-2025 Free Software Foundation, Inc.
4dnl This file is free software; the Free Software Foundation 4dnl This file is free software; the Free Software Foundation
5dnl gives unlimited permission to copy and/or distribute it, 5dnl gives unlimited permission to copy and/or distribute it,
6dnl with or without modifications, as long as this notice is preserved. 6dnl with or without modifications, as long as this notice is preserved.
7dnl This file is offered as-is, without any warranty.
7 8
8# This is adapted with modifications from upstream Autoconf here: 9m4_version_prereq([2.73], [], [
9# https://git.savannah.gnu.org/cgit/autoconf.git/tree/lib/autoconf/functions.m4?id=v2.70#n949 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).
10AC_DEFUN([_AC_FUNC_MALLOC_IF], 22AC_DEFUN([_AC_FUNC_MALLOC_IF],
11[ 23[
12 AC_REQUIRE([AC_CANONICAL_HOST])dnl for cross-compiles 24 AC_REQUIRE([AC_CANONICAL_HOST])dnl for cross-compiles
@@ -15,56 +27,81 @@ AC_DEFUN([_AC_FUNC_MALLOC_IF],
15 [AC_RUN_IFELSE( 27 [AC_RUN_IFELSE(
16 [AC_LANG_PROGRAM( 28 [AC_LANG_PROGRAM(
17 [[#include <stdlib.h> 29 [[#include <stdlib.h>
18 ]], 30 /* Use pmalloc to test; 'volatile' prevents the compiler
19 [[void *p = malloc (0); 31 from optimizing the malloc call away. */
20 void * volatile vp = p; 32 void *(*volatile pmalloc) (size_t) = malloc;]],
21 int result = !vp; 33 [[void *p = pmalloc (0);
34 int result = !p;
22 free (p); 35 free (p);
23 return result;]]) 36 return result;]])],
24 ],
25 [ac_cv_func_malloc_0_nonnull=yes], 37 [ac_cv_func_malloc_0_nonnull=yes],
26 [ac_cv_func_malloc_0_nonnull=no], 38 [ac_cv_func_malloc_0_nonnull=no],
27 [case "$host_os" in 39 [AS_CASE([$host_os],
28 # Guess yes on platforms where we know the result. 40 [# Guess yes on platforms where we know the result.
29 *-gnu* | freebsd* | netbsd* | openbsd* | bitrig* \ 41 *-gnu* | freebsd* | netbsd* | openbsd* | bitrig* \
30 | gnu* | *-musl* | midipix* | midnightbsd* \ 42 | gnu* | *-musl* | midipix* | midnightbsd* \
31 | hpux* | solaris* | cygwin* | mingw* | windows* | msys* ) 43 | hpux* | solaris* | cygwin* | mingw* | windows* | msys*],
32 ac_cv_func_malloc_0_nonnull="guessing yes" ;; 44 [ac_cv_func_malloc_0_nonnull="guessing yes"],
33 # If we don't know, obey --enable-cross-guesses. 45 [# Guess as follows if we don't know.
34 *) ac_cv_func_malloc_0_nonnull="$gl_cross_guess_normal" ;; 46 ac_cv_func_malloc_0_nonnull=m4_default([$3], ["guessing no"])])])])
35 esac
36 ])
37 ])
38 AS_CASE([$ac_cv_func_malloc_0_nonnull], [*yes], [$1], [$2]) 47 AS_CASE([$ac_cv_func_malloc_0_nonnull], [*yes], [$1], [$2])
39])# _AC_FUNC_MALLOC_IF 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.
57AC_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])
40 65
41# gl_FUNC_MALLOC_GNU 66# gl_FUNC_MALLOC_GNU
42# ------------------ 67# ------------------
43# Replace malloc if it is not compatible with GNU libc. 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.
44AC_DEFUN([gl_FUNC_MALLOC_GNU], 74AC_DEFUN([gl_FUNC_MALLOC_GNU],
45[ 75[
46 AC_REQUIRE([gl_STDLIB_H_DEFAULTS]) 76 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
47 AC_REQUIRE([gl_FUNC_MALLOC_POSIX]) 77 AC_REQUIRE([gl_FUNC_MALLOC_POSIX])
48 REPLACE_MALLOC_FOR_MALLOC_GNU="$REPLACE_MALLOC_FOR_MALLOC_POSIX" 78 AC_REQUIRE([gl_FUNC_MALLOC_0_NONNULL])
49 if test $REPLACE_MALLOC_FOR_MALLOC_GNU = 0; then 79
50 _AC_FUNC_MALLOC_IF([], [REPLACE_MALLOC_FOR_MALLOC_GNU=1]) 80 AS_CASE([$ac_cv_func_malloc_0_nonnull],
51 fi 81 [*yes],
82 [REPLACE_MALLOC_FOR_MALLOC_GNU=$REPLACE_MALLOC_FOR_MALLOC_POSIX],
83 [REPLACE_MALLOC_FOR_MALLOC_GNU=1])
52]) 84])
53 85
54# gl_FUNC_MALLOC_PTRDIFF 86# gl_FUNC_MALLOC_PTRDIFF
55# ---------------------- 87# ----------------------
56# Test whether malloc (N) reliably fails when N exceeds PTRDIFF_MAX, 88# Test whether malloc (N) reliably fails when N exceeds PTRDIFF_MAX.
57# and replace malloc otherwise. 89# Define HAVE_MALLOC_PTRDIFF if yes.
90# Replace malloc if not.
58AC_DEFUN([gl_FUNC_MALLOC_PTRDIFF], 91AC_DEFUN([gl_FUNC_MALLOC_PTRDIFF],
59[ 92[
60 AC_REQUIRE([gl_STDLIB_H_DEFAULTS]) 93 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
61 AC_REQUIRE([gl_CHECK_MALLOC_PTRDIFF]) 94 AC_REQUIRE([gl_CHECK_MALLOC_PTRDIFF])
62 test "$gl_cv_malloc_ptrdiff" = yes || REPLACE_MALLOC_FOR_MALLOC_POSIX=1 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])
63]) 100])
64 101
65# Test whether malloc, realloc, calloc refuse to create objects 102# Test whether malloc, calloc refuse to create objects
66# larger than what can be expressed in ptrdiff_t. 103# larger than what can be expressed in ptrdiff_t.
67# Set gl_cv_func_malloc_gnu to yes or no accordingly. 104# Set gl_cv_func_malloc_gnu.
68AC_DEFUN([gl_CHECK_MALLOC_PTRDIFF], 105AC_DEFUN([gl_CHECK_MALLOC_PTRDIFF],
69[ 106[
70 AC_CACHE_CHECK([whether malloc is ptrdiff_t safe], 107 AC_CACHE_CHECK([whether malloc is ptrdiff_t safe],
@@ -108,30 +145,48 @@ AC_DEFUN([gl_FUNC_MALLOC_POSIX],
108 AC_REQUIRE([gl_STDLIB_H_DEFAULTS]) 145 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
109 AC_REQUIRE([gl_FUNC_MALLOC_PTRDIFF]) 146 AC_REQUIRE([gl_FUNC_MALLOC_PTRDIFF])
110 AC_REQUIRE([gl_CHECK_MALLOC_POSIX]) 147 AC_REQUIRE([gl_CHECK_MALLOC_POSIX])
111 if test "$gl_cv_func_malloc_posix" = yes; then 148 case "$gl_cv_func_malloc_posix" in
112 AC_DEFINE([HAVE_MALLOC_POSIX], [1], 149 *yes)
113 [Define if malloc, realloc, and calloc set errno on allocation failure.]) 150 AC_DEFINE([HAVE_MALLOC_POSIX], [1],
114 else 151 [Define if malloc and calloc set errno on allocation failure.])
115 REPLACE_MALLOC_FOR_MALLOC_POSIX=1 152 ;;
116 fi 153 *)
154 REPLACE_MALLOC_FOR_MALLOC_POSIX=1
155 ;;
156 esac
117]) 157])
118 158
119# Test whether malloc, realloc, calloc set errno to ENOMEM on failure. 159# Test whether malloc, calloc set errno to ENOMEM on failure.
120# Set gl_cv_func_malloc_posix to yes or no accordingly. 160# Set gl_cv_func_malloc_posix to *yes or *no accordingly.
121AC_DEFUN([gl_CHECK_MALLOC_POSIX], 161AC_DEFUN([gl_CHECK_MALLOC_POSIX],
122[ 162[
123 AC_REQUIRE([AC_CANONICAL_HOST]) 163 AC_REQUIRE([AC_CANONICAL_HOST])
124 AC_CACHE_CHECK([whether malloc, realloc, calloc set errno on failure], 164 AC_CACHE_CHECK([whether malloc, calloc set errno on failure],
125 [gl_cv_func_malloc_posix], 165 [gl_cv_func_malloc_posix],
126 [ 166 [
127 dnl It is too dangerous to try to allocate a large amount of memory: 167 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 168 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, 169 dnl all Unix implementations of the function set errno on failure,
130 dnl except on those platforms where we have seen 'test-malloc-gnu', 170 dnl except on those platforms where we have seen 'test-malloc-gnu',
131 dnl 'test-realloc-gnu', 'test-calloc-gnu' fail. 171 dnl 'test-realloc-posix', 'test-calloc-gnu' fail. For platforms
172 dnl where only 'test-realloc-posix', see realloc.m4.
132 case "$host_os" in 173 case "$host_os" in
133 mingw* | windows*) 174 mingw* | windows*)
134 gl_cv_func_malloc_posix=no ;; 175 dnl Old MSVCRT from 2001 did not set errno=ENOMEM when malloc failed.
176 dnl More recent MSVCRT from 2019 does so.
177 dnl UCRT is the successor of MSVCRT. Assume that UCRT does so as well.
178 AC_COMPILE_IFELSE(
179 [AC_LANG_PROGRAM(
180 [[#include <stdio.h>
181 #ifndef _UCRT
182 msvcrt yuck
183 #endif
184 ]],
185 [[]])
186 ],
187 [gl_cv_func_malloc_posix="guessing yes"],
188 [gl_cv_func_malloc_posix="guessing no"])
189 ;;
135 irix* | solaris*) 190 irix* | solaris*)
136 dnl On IRIX 6.5, the three functions return NULL with errno unset 191 dnl On IRIX 6.5, the three functions return NULL with errno unset
137 dnl when the argument is larger than PTRDIFF_MAX. 192 dnl when the argument is larger than PTRDIFF_MAX.