1# malloc.m4 serial 20 2dnl Copyright (C) 2007, 2009-2020 Free Software Foundation, Inc. 3dnl This file is free software; the Free Software Foundation 4dnl gives unlimited permission to copy and/or distribute it, 5dnl with or without modifications, as long as this notice is preserved. 6 7# This is adapted with modifications from upstream Autoconf here: 8# https://git.savannah.gnu.org/cgit/autoconf.git/commit/?id=04be2b7a29d65d9a08e64e8e56e594c91749598c 9AC_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* | gnu* | *-musl* | freebsd* | netbsd* | openbsd* \ 34 | hpux* | solaris* | cygwin* | mingw*) 35 ac_cv_func_malloc_0_nonnull="guessing yes" ;; 36 # If we don't know, obey --enable-cross-guesses. 37 *) ac_cv_func_malloc_0_nonnull="$gl_cross_guess_normal" ;; 38 esac 39 ]) 40 ]) 41 case "$ac_cv_func_malloc_0_nonnull" in 42 *yes) 43 $1 44 ;; 45 *) 46 $2 47 ;; 48 esac 49])# _AC_FUNC_MALLOC_IF 50 51# gl_FUNC_MALLOC_GNU 52# ------------------ 53# Test whether 'malloc (0)' is handled like in GNU libc, and replace malloc if 54# it is not. 55AC_DEFUN([gl_FUNC_MALLOC_GNU], 56[ 57 AC_REQUIRE([gl_STDLIB_H_DEFAULTS]) 58 dnl _AC_FUNC_MALLOC_IF is defined in Autoconf. 59 _AC_FUNC_MALLOC_IF( 60 [AC_DEFINE([HAVE_MALLOC_GNU], [1], 61 [Define to 1 if your system has a GNU libc compatible 'malloc' 62 function, and to 0 otherwise.])], 63 [AC_DEFINE([HAVE_MALLOC_GNU], [0]) 64 REPLACE_MALLOC=1 65 ]) 66]) 67 68# gl_FUNC_MALLOC_POSIX 69# -------------------- 70# Test whether 'malloc' is POSIX compliant (sets errno to ENOMEM when it 71# fails), and replace malloc if it is not. 72AC_DEFUN([gl_FUNC_MALLOC_POSIX], 73[ 74 AC_REQUIRE([gl_STDLIB_H_DEFAULTS]) 75 AC_REQUIRE([gl_CHECK_MALLOC_POSIX]) 76 if test $gl_cv_func_malloc_posix = yes; then 77 AC_DEFINE([HAVE_MALLOC_POSIX], [1], 78 [Define if the 'malloc' function is POSIX compliant.]) 79 else 80 REPLACE_MALLOC=1 81 fi 82]) 83 84# Test whether malloc, realloc, calloc are POSIX compliant, 85# Set gl_cv_func_malloc_posix to yes or no accordingly. 86AC_DEFUN([gl_CHECK_MALLOC_POSIX], 87[ 88 AC_CACHE_CHECK([whether malloc, realloc, calloc are POSIX compliant], 89 [gl_cv_func_malloc_posix], 90 [ 91 dnl It is too dangerous to try to allocate a large amount of memory: 92 dnl some systems go to their knees when you do that. So assume that 93 dnl all Unix implementations of the function are POSIX compliant. 94 AC_COMPILE_IFELSE( 95 [AC_LANG_PROGRAM( 96 [[]], 97 [[#if defined _WIN32 && ! defined __CYGWIN__ 98 choke me 99 #endif 100 ]])], 101 [gl_cv_func_malloc_posix=yes], 102 [gl_cv_func_malloc_posix=no]) 103 ]) 104]) 105