2 dnl Copyright (C) 2007, 2009-2020 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.
7 # This is adapted with modifications from upstream Autoconf here:
8 # https://git.savannah.gnu.org/cgit/autoconf.git/commit/?id=04be2b7a29d65d9a08e64e8e56e594c91749598c
9 AC_DEFUN([_AC_FUNC_MALLOC_IF],
11 AC_REQUIRE([AC_CANONICAL_HOST])dnl for cross-compiles
12 AC_CACHE_CHECK([for GNU libc compatible malloc],
13 [ac_cv_func_malloc_0_nonnull],
18 [[char *p = malloc (0);
23 [ac_cv_func_malloc_0_nonnull=yes],
24 [ac_cv_func_malloc_0_nonnull=no],
26 # Guess yes on platforms where we know the result.
27 *-gnu* | gnu* | *-musl* | freebsd* | netbsd* | openbsd* \
28 | hpux* | solaris* | cygwin* | mingw*)
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" ;;
35 case "$ac_cv_func_malloc_0_nonnull" in
43 ])# _AC_FUNC_MALLOC_IF
47 # Test whether 'malloc (0)' is handled like in GNU libc, and replace malloc if
49 AC_DEFUN([gl_FUNC_MALLOC_GNU],
51 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
52 dnl _AC_FUNC_MALLOC_IF is defined in Autoconf.
54 [AC_DEFINE([HAVE_MALLOC_GNU], [1],
55 [Define to 1 if your system has a GNU libc compatible 'malloc'
56 function, and to 0 otherwise.])],
57 [AC_DEFINE([HAVE_MALLOC_GNU], [0])
62 # gl_FUNC_MALLOC_POSIX
63 # --------------------
64 # Test whether 'malloc' is POSIX compliant (sets errno to ENOMEM when it
65 # fails), and replace malloc if it is not.
66 AC_DEFUN([gl_FUNC_MALLOC_POSIX],
68 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
69 AC_REQUIRE([gl_CHECK_MALLOC_POSIX])
70 if test $gl_cv_func_malloc_posix = yes; then
71 AC_DEFINE([HAVE_MALLOC_POSIX], [1],
72 [Define if the 'malloc' function is POSIX compliant.])
78 # Test whether malloc, realloc, calloc are POSIX compliant,
79 # Set gl_cv_func_malloc_posix to yes or no accordingly.
80 AC_DEFUN([gl_CHECK_MALLOC_POSIX],
82 AC_CACHE_CHECK([whether malloc, realloc, calloc are POSIX compliant],
83 [gl_cv_func_malloc_posix],
85 dnl It is too dangerous to try to allocate a large amount of memory:
86 dnl some systems go to their knees when you do that. So assume that
87 dnl all Unix implementations of the function are POSIX compliant.
91 [[#if defined _WIN32 && ! defined __CYGWIN__
95 [gl_cv_func_malloc_posix=yes],
96 [gl_cv_func_malloc_posix=no])