Blame m4/malloc.m4

Packit 33f14e
# malloc.m4 serial 15
Packit 33f14e
dnl Copyright (C) 2007, 2009-2017 Free Software Foundation, Inc.
Packit 33f14e
dnl This file is free software; the Free Software Foundation
Packit 33f14e
dnl gives unlimited permission to copy and/or distribute it,
Packit 33f14e
dnl with or without modifications, as long as this notice is preserved.
Packit 33f14e
Packit 33f14e
m4_version_prereq([2.70], [] ,[
Packit 33f14e
Packit 33f14e
# This is adapted with modifications from upstream Autoconf here:
Packit 33f14e
# http://git.savannah.gnu.org/cgit/autoconf.git/commit/?id=04be2b7a29d65d9a08e64e8e56e594c91749598c
Packit 33f14e
AC_DEFUN([_AC_FUNC_MALLOC_IF],
Packit 33f14e
[
Packit 33f14e
  AC_REQUIRE([AC_HEADER_STDC])dnl
Packit 33f14e
  AC_REQUIRE([AC_CANONICAL_HOST])dnl for cross-compiles
Packit 33f14e
  AC_CHECK_HEADERS([stdlib.h])
Packit 33f14e
  AC_CACHE_CHECK([for GNU libc compatible malloc],
Packit 33f14e
    [ac_cv_func_malloc_0_nonnull],
Packit 33f14e
    [AC_RUN_IFELSE(
Packit 33f14e
       [AC_LANG_PROGRAM(
Packit 33f14e
          [[#if defined STDC_HEADERS || defined HAVE_STDLIB_H
Packit 33f14e
            # include <stdlib.h>
Packit 33f14e
            #else
Packit 33f14e
            char *malloc ();
Packit 33f14e
            #endif
Packit 33f14e
          ]],
Packit 33f14e
          [[char *p = malloc (0);
Packit 33f14e
            int result = !p;
Packit 33f14e
            free (p);
Packit 33f14e
            return result;]])
Packit 33f14e
       ],
Packit 33f14e
       [ac_cv_func_malloc_0_nonnull=yes],
Packit 33f14e
       [ac_cv_func_malloc_0_nonnull=no],
Packit 33f14e
       [case "$host_os" in
Packit 33f14e
          # Guess yes on platforms where we know the result.
Packit 33f14e
          *-gnu* | freebsd* | netbsd* | openbsd* \
Packit 33f14e
          | hpux* | solaris* | cygwin* | mingw*)
Packit 33f14e
            ac_cv_func_malloc_0_nonnull=yes ;;
Packit 33f14e
          # If we don't know, assume the worst.
Packit 33f14e
          *) ac_cv_func_malloc_0_nonnull=no ;;
Packit 33f14e
        esac
Packit 33f14e
       ])
Packit 33f14e
    ])
Packit 33f14e
  AS_IF([test $ac_cv_func_malloc_0_nonnull = yes], [$1], [$2])
Packit 33f14e
])# _AC_FUNC_MALLOC_IF
Packit 33f14e
Packit 33f14e
])
Packit 33f14e
Packit 33f14e
# gl_FUNC_MALLOC_GNU
Packit 33f14e
# ------------------
Packit 33f14e
# Test whether 'malloc (0)' is handled like in GNU libc, and replace malloc if
Packit 33f14e
# it is not.
Packit 33f14e
AC_DEFUN([gl_FUNC_MALLOC_GNU],
Packit 33f14e
[
Packit 33f14e
  AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
Packit 33f14e
  dnl _AC_FUNC_MALLOC_IF is defined in Autoconf.
Packit 33f14e
  _AC_FUNC_MALLOC_IF(
Packit 33f14e
    [AC_DEFINE([HAVE_MALLOC_GNU], [1],
Packit 33f14e
               [Define to 1 if your system has a GNU libc compatible 'malloc'
Packit 33f14e
                function, and to 0 otherwise.])],
Packit 33f14e
    [AC_DEFINE([HAVE_MALLOC_GNU], [0])
Packit 33f14e
     REPLACE_MALLOC=1
Packit 33f14e
    ])
Packit 33f14e
])
Packit 33f14e
Packit 33f14e
# gl_FUNC_MALLOC_POSIX
Packit 33f14e
# --------------------
Packit 33f14e
# Test whether 'malloc' is POSIX compliant (sets errno to ENOMEM when it
Packit 33f14e
# fails), and replace malloc if it is not.
Packit 33f14e
AC_DEFUN([gl_FUNC_MALLOC_POSIX],
Packit 33f14e
[
Packit 33f14e
  AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
Packit 33f14e
  AC_REQUIRE([gl_CHECK_MALLOC_POSIX])
Packit 33f14e
  if test $gl_cv_func_malloc_posix = yes; then
Packit 33f14e
    AC_DEFINE([HAVE_MALLOC_POSIX], [1],
Packit 33f14e
      [Define if the 'malloc' function is POSIX compliant.])
Packit 33f14e
  else
Packit 33f14e
    REPLACE_MALLOC=1
Packit 33f14e
  fi
Packit 33f14e
])
Packit 33f14e
Packit 33f14e
# Test whether malloc, realloc, calloc are POSIX compliant,
Packit 33f14e
# Set gl_cv_func_malloc_posix to yes or no accordingly.
Packit 33f14e
AC_DEFUN([gl_CHECK_MALLOC_POSIX],
Packit 33f14e
[
Packit 33f14e
  AC_CACHE_CHECK([whether malloc, realloc, calloc are POSIX compliant],
Packit 33f14e
    [gl_cv_func_malloc_posix],
Packit 33f14e
    [
Packit 33f14e
      dnl It is too dangerous to try to allocate a large amount of memory:
Packit 33f14e
      dnl some systems go to their knees when you do that. So assume that
Packit 33f14e
      dnl all Unix implementations of the function are POSIX compliant.
Packit 33f14e
      AC_COMPILE_IFELSE(
Packit 33f14e
        [AC_LANG_PROGRAM(
Packit 33f14e
           [[]],
Packit 33f14e
           [[#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit 33f14e
             choke me
Packit 33f14e
             #endif
Packit 33f14e
            ]])],
Packit 33f14e
        [gl_cv_func_malloc_posix=yes],
Packit 33f14e
        [gl_cv_func_malloc_posix=no])
Packit 33f14e
    ])
Packit 33f14e
])