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