Blame m4/malloc.m4

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