bump standards-version
[gnulib.git] / m4 / calloc.m4
1 # calloc.m4 serial 14
2
3 # Copyright (C) 2004-2012 Free Software Foundation, Inc.
4 # This file is free software; the Free Software Foundation
5 # gives unlimited permission to copy and/or distribute it,
6 # with or without modifications, as long as this notice is preserved.
7
8 # Written by Jim Meyering.
9
10 # Determine whether calloc (N, S) returns non-NULL when N*S is zero,
11 # and returns NULL when N*S overflows.
12 # If so, define HAVE_CALLOC.  Otherwise, define calloc to rpl_calloc
13 # and arrange to use a calloc wrapper function that does work in that case.
14
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_CACHE_CHECK([for GNU libc compatible calloc],
22     [ac_cv_func_calloc_0_nonnull],
23     [AC_RUN_IFELSE(
24        [AC_LANG_PROGRAM(
25           [AC_INCLUDES_DEFAULT],
26           [[int result = 0;
27             if (!calloc (0, 0))
28               result |= 1;
29             if (calloc ((size_t) -1 / 8 + 1, 8))
30               result |= 2;
31             return result;
32           ]])],
33        [ac_cv_func_calloc_0_nonnull=yes],
34        [ac_cv_func_calloc_0_nonnull=no],
35        [ac_cv_func_calloc_0_nonnull=no])])
36   AS_IF([test $ac_cv_func_calloc_0_nonnull = yes], [$1], [$2])
37 ])# AC_FUNC_CALLOC
38
39
40 # gl_FUNC_CALLOC_GNU
41 # ------------------
42 # Report whether 'calloc (0, 0)' is properly handled, and replace calloc if
43 # needed.
44 AC_DEFUN([gl_FUNC_CALLOC_GNU],
45 [
46   AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
47   _AC_FUNC_CALLOC_IF(
48     [AC_DEFINE([HAVE_CALLOC_GNU], [1],
49                [Define to 1 if your system has a GNU libc compatible 'calloc'
50                 function, and to 0 otherwise.])],
51     [AC_DEFINE([HAVE_CALLOC_GNU], [0])
52      REPLACE_CALLOC=1
53     ])
54 ])# gl_FUNC_CALLOC_GNU
55
56
57 # gl_FUNC_CALLOC_POSIX
58 # --------------------
59 # Test whether 'calloc' is POSIX compliant (sets errno to ENOMEM when it
60 # fails), and replace calloc if it is not.
61 AC_DEFUN([gl_FUNC_CALLOC_POSIX],
62 [
63   AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
64   AC_REQUIRE([gl_CHECK_MALLOC_POSIX])
65   if test $gl_cv_func_malloc_posix = yes; then
66     AC_DEFINE([HAVE_CALLOC_POSIX], [1],
67       [Define if the 'calloc' function is POSIX compliant.])
68   else
69     REPLACE_CALLOC=1
70   fi
71 ])