malloc.m4 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # malloc.m4 serial 21
  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. # This is adapted with modifications from upstream Autoconf here:
  7. # https://git.savannah.gnu.org/cgit/autoconf.git/commit/?id=04be2b7a29d65d9a08e64e8e56e594c91749598c
  8. AC_DEFUN([_AC_FUNC_MALLOC_IF],
  9. [
  10. AC_REQUIRE([AC_CANONICAL_HOST])dnl for cross-compiles
  11. AC_CACHE_CHECK([for GNU libc compatible malloc],
  12. [ac_cv_func_malloc_0_nonnull],
  13. [AC_RUN_IFELSE(
  14. [AC_LANG_PROGRAM(
  15. [[#include <stdlib.h>
  16. ]],
  17. [[char *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* | gnu* | *-musl* | freebsd* | netbsd* | openbsd* \
  27. | hpux* | solaris* | cygwin* | mingw*)
  28. ac_cv_func_malloc_0_nonnull="guessing yes" ;;
  29. # If we don't know, obey --enable-cross-guesses.
  30. *) ac_cv_func_malloc_0_nonnull="$gl_cross_guess_normal" ;;
  31. esac
  32. ])
  33. ])
  34. case "$ac_cv_func_malloc_0_nonnull" in
  35. *yes)
  36. $1
  37. ;;
  38. *)
  39. $2
  40. ;;
  41. esac
  42. ])# _AC_FUNC_MALLOC_IF
  43. # gl_FUNC_MALLOC_GNU
  44. # ------------------
  45. # Test whether 'malloc (0)' is handled like in GNU libc, and replace malloc if
  46. # it is not.
  47. AC_DEFUN([gl_FUNC_MALLOC_GNU],
  48. [
  49. AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
  50. dnl _AC_FUNC_MALLOC_IF is defined in Autoconf.
  51. _AC_FUNC_MALLOC_IF(
  52. [AC_DEFINE([HAVE_MALLOC_GNU], [1],
  53. [Define to 1 if your system has a GNU libc compatible 'malloc'
  54. function, and to 0 otherwise.])],
  55. [AC_DEFINE([HAVE_MALLOC_GNU], [0])
  56. REPLACE_MALLOC=1
  57. ])
  58. ])
  59. # gl_FUNC_MALLOC_POSIX
  60. # --------------------
  61. # Test whether 'malloc' is POSIX compliant (sets errno to ENOMEM when it
  62. # fails), and replace malloc if it is not.
  63. AC_DEFUN([gl_FUNC_MALLOC_POSIX],
  64. [
  65. AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
  66. AC_REQUIRE([gl_CHECK_MALLOC_POSIX])
  67. if test $gl_cv_func_malloc_posix = yes; then
  68. AC_DEFINE([HAVE_MALLOC_POSIX], [1],
  69. [Define if the 'malloc' function is POSIX compliant.])
  70. else
  71. REPLACE_MALLOC=1
  72. fi
  73. ])
  74. # Test whether malloc, realloc, calloc are POSIX compliant,
  75. # Set gl_cv_func_malloc_posix to yes or no accordingly.
  76. AC_DEFUN([gl_CHECK_MALLOC_POSIX],
  77. [
  78. AC_CACHE_CHECK([whether malloc, realloc, calloc are POSIX compliant],
  79. [gl_cv_func_malloc_posix],
  80. [
  81. dnl It is too dangerous to try to allocate a large amount of memory:
  82. dnl some systems go to their knees when you do that. So assume that
  83. dnl all Unix implementations of the function are POSIX compliant.
  84. AC_COMPILE_IFELSE(
  85. [AC_LANG_PROGRAM(
  86. [[]],
  87. [[#if defined _WIN32 && ! defined __CYGWIN__
  88. choke me
  89. #endif
  90. ]])],
  91. [gl_cv_func_malloc_posix=yes],
  92. [gl_cv_func_malloc_posix=no])
  93. ])
  94. ])