malloc.m4 3.2 KB

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