Blame m4/malloc.m4

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