3432493f491625532b0cbb934349305b1696dc1a
[gnulib.git] / m4 / calloc.m4
1 # calloc.m4 serial 7
2
3 # Copyright (C) 2004, 2005, 2006, 2007 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 [AC_REQUIRE([AC_TYPE_SIZE_T])dnl
20 AC_CACHE_CHECK([for GNU libc compatible calloc], ac_cv_func_calloc_0_nonnull,
21 [AC_RUN_IFELSE([AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT],
22                   [exit (!calloc (0, 0) || calloc ((size_t) -1 / 8 + 1, 8));])],
23                [ac_cv_func_calloc_0_nonnull=yes],
24                [ac_cv_func_calloc_0_nonnull=no],
25                [ac_cv_func_calloc_0_nonnull=no])])
26 AS_IF([test $ac_cv_func_calloc_0_nonnull = yes], [$1], [$2])
27 ])# AC_FUNC_CALLOC
28
29
30 # AC_FUNC_CALLOC
31 # ---------------
32 # Report whether `calloc (0, 0)' is properly handled, and replace calloc if
33 # needed.
34 AC_DEFUN([AC_FUNC_CALLOC],
35 [_AC_FUNC_CALLOC_IF(
36   [AC_DEFINE([HAVE_CALLOC], 1,
37              [Define to 1 if your system has a GNU libc compatible `calloc'
38               function, and to 0 otherwise.])],
39   [AC_DEFINE([HAVE_CALLOC], 0)
40    AC_LIBOBJ([calloc])
41    AC_DEFINE([calloc], [rpl_calloc],
42       [Define to rpl_calloc if the replacement function should be used.])])
43 ])# AC_FUNC_CALLOC
44
45
46 # gl_FUNC_CALLOC_POSIX
47 # --------------------
48 # Test whether 'calloc' is POSIX compliant (sets errno to ENOMEM when it
49 # fails), and replace calloc if it is not.
50 AC_DEFUN([gl_FUNC_CALLOC_POSIX],
51 [
52   AC_CACHE_CHECK([whether calloc is POSIX compliant],
53     [gl_cv_func_calloc_posix],
54     [
55       dnl It is too dangerous to try to allocate a large amount of memory:
56       dnl some systems go to their knees when you do that. So assume that
57       dnl all Unix implementations of the function are POSIX compliant.
58       AC_TRY_COMPILE([],
59         [#if !((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
60          choke me
61          #endif
62         ], [gl_cv_func_calloc_posix=yes], [gl_cv_func_calloc_posix=no])
63     ])
64   if test $gl_cv_func_calloc_posix = yes; then
65     HAVE_CALLOC_POSIX=1
66     AC_DEFINE([HAVE_CALLOC_POSIX], 1,
67       [Define if the 'calloc' function is POSIX compliant.])
68   else
69     AC_LIBOBJ([calloc])
70     HAVE_CALLOC_POSIX=0
71   fi
72   AC_SUBST([HAVE_CALLOC_POSIX])
73 ])