malloc.m4 3.3 KB

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