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