Blame gnulib/m4/malloc.m4

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