Blame m4/malloc.m4

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