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