malloc.m4 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # malloc.m4 serial 28
  2. dnl Copyright (C) 2007, 2009-2023 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. # This is adapted with modifications from upstream Autoconf here:
  7. # https://git.savannah.gnu.org/cgit/autoconf.git/tree/lib/autoconf/functions.m4?id=v2.70#n949
  8. AC_DEFUN([_AC_FUNC_MALLOC_IF],
  9. [
  10. AC_REQUIRE([AC_CANONICAL_HOST])dnl for cross-compiles
  11. AC_CACHE_CHECK([whether malloc (0) returns nonnull],
  12. [ac_cv_func_malloc_0_nonnull],
  13. [AC_RUN_IFELSE(
  14. [AC_LANG_PROGRAM(
  15. [[#include <stdlib.h>
  16. ]],
  17. [[void *p = malloc (0);
  18. int result = !p;
  19. free (p);
  20. return result;]])
  21. ],
  22. [ac_cv_func_malloc_0_nonnull=yes],
  23. [ac_cv_func_malloc_0_nonnull=no],
  24. [case "$host_os" in
  25. # Guess yes on platforms where we know the result.
  26. *-gnu* | freebsd* | netbsd* | openbsd* | bitrig* \
  27. | gnu* | *-musl* | midnightbsd* \
  28. | hpux* | solaris* | cygwin* | mingw* | msys* )
  29. ac_cv_func_malloc_0_nonnull="guessing yes" ;;
  30. # If we don't know, obey --enable-cross-guesses.
  31. *) ac_cv_func_malloc_0_nonnull="$gl_cross_guess_normal" ;;
  32. esac
  33. ])
  34. ])
  35. AS_CASE([$ac_cv_func_malloc_0_nonnull], [*yes], [$1], [$2])
  36. ])# _AC_FUNC_MALLOC_IF
  37. # gl_FUNC_MALLOC_GNU
  38. # ------------------
  39. # Replace malloc if it is not compatible with GNU libc.
  40. AC_DEFUN([gl_FUNC_MALLOC_GNU],
  41. [
  42. AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
  43. AC_REQUIRE([gl_FUNC_MALLOC_POSIX])
  44. REPLACE_MALLOC_FOR_MALLOC_GNU="$REPLACE_MALLOC_FOR_MALLOC_POSIX"
  45. if test $REPLACE_MALLOC_FOR_MALLOC_GNU = 0; then
  46. _AC_FUNC_MALLOC_IF([], [REPLACE_MALLOC_FOR_MALLOC_GNU=1])
  47. fi
  48. ])
  49. # gl_FUNC_MALLOC_PTRDIFF
  50. # ----------------------
  51. # Test whether malloc (N) reliably fails when N exceeds PTRDIFF_MAX,
  52. # and replace malloc otherwise.
  53. AC_DEFUN([gl_FUNC_MALLOC_PTRDIFF],
  54. [
  55. AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
  56. AC_REQUIRE([gl_CHECK_MALLOC_PTRDIFF])
  57. test "$gl_cv_malloc_ptrdiff" = yes || REPLACE_MALLOC_FOR_MALLOC_POSIX=1
  58. ])
  59. # Test whether malloc, realloc, calloc refuse to create objects
  60. # larger than what can be expressed in ptrdiff_t.
  61. # Set gl_cv_func_malloc_gnu to yes or no accordingly.
  62. AC_DEFUN([gl_CHECK_MALLOC_PTRDIFF],
  63. [
  64. AC_CACHE_CHECK([whether malloc is ptrdiff_t safe],
  65. [gl_cv_malloc_ptrdiff],
  66. [AC_COMPILE_IFELSE(
  67. [AC_LANG_PROGRAM(
  68. [[#include <stdint.h>
  69. ]],
  70. [[/* 64-bit ptrdiff_t is so wide that no practical platform
  71. can exceed it. */
  72. #define WIDE_PTRDIFF (PTRDIFF_MAX >> 31 >> 31 != 0)
  73. /* On rare machines where size_t fits in ptrdiff_t there
  74. is no problem. */
  75. #define NARROW_SIZE (SIZE_MAX <= PTRDIFF_MAX)
  76. /* glibc 2.30 and later malloc refuses to exceed ptrdiff_t
  77. bounds even on 32-bit platforms. We don't know which
  78. non-glibc systems are safe. */
  79. #define KNOWN_SAFE (2 < __GLIBC__ + (30 <= __GLIBC_MINOR__))
  80. #if WIDE_PTRDIFF || NARROW_SIZE || KNOWN_SAFE
  81. return 0;
  82. #else
  83. #error "malloc might not be ptrdiff_t safe"
  84. syntax error
  85. #endif
  86. ]])],
  87. [gl_cv_malloc_ptrdiff=yes],
  88. [gl_cv_malloc_ptrdiff=no])
  89. ])
  90. ])
  91. # gl_FUNC_MALLOC_POSIX
  92. # --------------------
  93. # Test whether 'malloc' is POSIX compliant (sets errno to ENOMEM when it
  94. # fails, and doesn't mess up with ptrdiff_t overflow), and replace
  95. # malloc if it is not.
  96. AC_DEFUN([gl_FUNC_MALLOC_POSIX],
  97. [
  98. AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
  99. AC_REQUIRE([gl_FUNC_MALLOC_PTRDIFF])
  100. AC_REQUIRE([gl_CHECK_MALLOC_POSIX])
  101. if test "$gl_cv_func_malloc_posix" = yes; then
  102. AC_DEFINE([HAVE_MALLOC_POSIX], [1],
  103. [Define if malloc, realloc, and calloc set errno on allocation failure.])
  104. else
  105. REPLACE_MALLOC_FOR_MALLOC_POSIX=1
  106. fi
  107. ])
  108. # Test whether malloc, realloc, calloc set errno to ENOMEM on failure.
  109. # Set gl_cv_func_malloc_posix to yes or no accordingly.
  110. AC_DEFUN([gl_CHECK_MALLOC_POSIX],
  111. [
  112. AC_REQUIRE([AC_CANONICAL_HOST])
  113. AC_CACHE_CHECK([whether malloc, realloc, calloc set errno on failure],
  114. [gl_cv_func_malloc_posix],
  115. [
  116. dnl It is too dangerous to try to allocate a large amount of memory:
  117. dnl some systems go to their knees when you do that. So assume that
  118. dnl all Unix implementations of the function set errno on failure,
  119. dnl except on those platforms where we have seen 'test-malloc-gnu',
  120. dnl 'test-realloc-gnu', 'test-calloc-gnu' fail.
  121. case "$host_os" in
  122. mingw*)
  123. gl_cv_func_malloc_posix=no ;;
  124. irix* | solaris*)
  125. dnl On IRIX 6.5, the three functions return NULL with errno unset
  126. dnl when the argument is larger than PTRDIFF_MAX.
  127. dnl On Solaris 11.3, the three functions return NULL with errno set
  128. dnl to EAGAIN, not ENOMEM, when the argument is larger than
  129. dnl PTRDIFF_MAX.
  130. dnl Here is a test program:
  131. m4_divert_push([KILL])
  132. #include <errno.h>
  133. #include <stdio.h>
  134. #include <stdlib.h>
  135. #define ptrdiff_t long
  136. #ifndef PTRDIFF_MAX
  137. # define PTRDIFF_MAX ((ptrdiff_t) ((1UL << (8 * sizeof (ptrdiff_t) - 1)) - 1))
  138. #endif
  139. int main ()
  140. {
  141. void *p;
  142. fprintf (stderr, "PTRDIFF_MAX = %lu\n", (unsigned long) PTRDIFF_MAX);
  143. errno = 0;
  144. p = malloc ((unsigned long) PTRDIFF_MAX + 1);
  145. fprintf (stderr, "p=%p errno=%d\n", p, errno);
  146. errno = 0;
  147. p = calloc (PTRDIFF_MAX / 2 + 1, 2);
  148. fprintf (stderr, "p=%p errno=%d\n", p, errno);
  149. errno = 0;
  150. p = realloc (NULL, (unsigned long) PTRDIFF_MAX + 1);
  151. fprintf (stderr, "p=%p errno=%d\n", p, errno);
  152. return 0;
  153. }
  154. m4_divert_pop([KILL])
  155. gl_cv_func_malloc_posix=no ;;
  156. *)
  157. gl_cv_func_malloc_posix=yes ;;
  158. esac
  159. ])
  160. ])