Blame gl/m4/malloc.m4

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