calloc.m4 2.76 KB
Newer Older
1
# calloc.m4 serial 17
2

Paul Eggert's avatar
Paul Eggert committed
3
# Copyright (C) 2004-2017 Free Software Foundation, Inc.
4 5 6
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
7 8 9 10 11

# Written by Jim Meyering.

# Determine whether calloc (N, S) returns non-NULL when N*S is zero,
# and returns NULL when N*S overflows.
12 13 14
# If so, define HAVE_CALLOC.  Otherwise, define calloc to rpl_calloc
# and arrange to use a calloc wrapper function that does work in that case.

15
# _AC_FUNC_CALLOC_IF([IF-WORKS], [IF-NOT])
16
# -------------------------------------
17
# If 'calloc (0, 0)' is properly handled, run IF-WORKS, otherwise, IF-NOT.
18
AC_DEFUN([_AC_FUNC_CALLOC_IF],
19 20
[
  AC_REQUIRE([AC_TYPE_SIZE_T])dnl
21
  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
22 23 24 25 26 27
  AC_CACHE_CHECK([for GNU libc compatible calloc],
    [ac_cv_func_calloc_0_nonnull],
    [AC_RUN_IFELSE(
       [AC_LANG_PROGRAM(
          [AC_INCLUDES_DEFAULT],
          [[int result = 0;
28 29
            char *p = calloc (0, 0);
            if (!p)
30
              result |= 1;
31 32 33
            free (p);
            p = calloc ((size_t) -1 / 8 + 1, 8);
            if (p)
34
              result |= 2;
35
            free (p);
36 37 38 39
            return result;
          ]])],
       [ac_cv_func_calloc_0_nonnull=yes],
       [ac_cv_func_calloc_0_nonnull=no],
40 41 42
       [case "$host_os" in
                  # Guess yes on glibc systems.
          *-gnu*) ac_cv_func_calloc_0_nonnull="guessing yes" ;;
43 44
                  # Guess yes on native Windows.
          mingw*) ac_cv_func_calloc_0_nonnull="guessing yes" ;;
45 46 47 48 49 50 51 52 53 54 55 56
                  # If we don't know, assume the worst.
          *)      ac_cv_func_calloc_0_nonnull="guessing no" ;;
        esac
       ])])
  case "$ac_cv_func_calloc_0_nonnull" in
    *yes)
      $1
      ;;
    *)
      $2
      ;;
  esac
57 58 59
])# AC_FUNC_CALLOC


60 61
# gl_FUNC_CALLOC_GNU
# ------------------
62
# Report whether 'calloc (0, 0)' is properly handled, and replace calloc if
63
# needed.
64 65 66 67
AC_DEFUN([gl_FUNC_CALLOC_GNU],
[
  AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
  _AC_FUNC_CALLOC_IF(
68
    [AC_DEFINE([HAVE_CALLOC_GNU], [1],
69
               [Define to 1 if your system has a GNU libc compatible 'calloc'
70
                function, and to 0 otherwise.])],
71
    [AC_DEFINE([HAVE_CALLOC_GNU], [0])
72
     REPLACE_CALLOC=1
73 74
    ])
])# gl_FUNC_CALLOC_GNU
75 76 77 78 79 80 81 82


# gl_FUNC_CALLOC_POSIX
# --------------------
# Test whether 'calloc' is POSIX compliant (sets errno to ENOMEM when it
# fails), and replace calloc if it is not.
AC_DEFUN([gl_FUNC_CALLOC_POSIX],
[
83
  AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
84 85
  AC_REQUIRE([gl_CHECK_MALLOC_POSIX])
  if test $gl_cv_func_malloc_posix = yes; then
86
    AC_DEFINE([HAVE_CALLOC_POSIX], [1],
87 88
      [Define if the 'calloc' function is POSIX compliant.])
  else
89
    REPLACE_CALLOC=1
90
  fi
91
])