Debian Codegraph @

m4/calloc.m4

raw
1 # calloc.m4 serial 20
2
3 # Copyright (C) 2004-2020 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_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
22 AC_CACHE_CHECK([for GNU libc compatible calloc],
23 [ac_cv_func_calloc_0_nonnull],
24 [AC_RUN_IFELSE(
25 [AC_LANG_PROGRAM(
26 [AC_INCLUDES_DEFAULT],
27 [[int result = 0;
28 char *p = calloc (0, 0);
29 if (!p)
30 result |= 1;
31 free (p);
32 p = calloc ((size_t) -1 / 8 + 1, 8);
33 if (p)
34 result |= 2;
35 free (p);
36 return result;
37 ]])],
38 [ac_cv_func_calloc_0_nonnull=yes],
39 [ac_cv_func_calloc_0_nonnull=no],
40 [case "$host_os" in
41 # Guess yes on glibc systems.
42 *-gnu* | gnu*) ac_cv_func_calloc_0_nonnull="guessing yes" ;;
43 # Guess yes on musl systems.
44 *-musl*) ac_cv_func_calloc_0_nonnull="guessing yes" ;;
45 # Guess yes on native Windows.
46 mingw*) ac_cv_func_calloc_0_nonnull="guessing yes" ;;
47 # If we don't know, obey --enable-cross-guesses.
48 *) ac_cv_func_calloc_0_nonnull="$gl_cross_guess_normal" ;;
49 esac
50 ])])
51 case "$ac_cv_func_calloc_0_nonnull" in
52 *yes)
53 $1
54 ;;
55 *)
56 $2
57 ;;
58 esac
59 ])# AC_FUNC_CALLOC
60
61
62 # gl_FUNC_CALLOC_GNU
63 # ------------------
64 # Report whether 'calloc (0, 0)' is properly handled, and replace calloc if
65 # needed.
66 AC_DEFUN([gl_FUNC_CALLOC_GNU],
67 [
68 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
69 _AC_FUNC_CALLOC_IF(
70 [AC_DEFINE([HAVE_CALLOC_GNU], [1],
71 [Define to 1 if your system has a GNU libc compatible 'calloc'
72 function, and to 0 otherwise.])],
73 [AC_DEFINE([HAVE_CALLOC_GNU], [0])
74 REPLACE_CALLOC=1
75 ])
76 ])# gl_FUNC_CALLOC_GNU
77
78
79 # gl_FUNC_CALLOC_POSIX
80 # --------------------
81 # Test whether 'calloc' is POSIX compliant (sets errno to ENOMEM when it
82 # fails), and replace calloc if it is not.
83 AC_DEFUN([gl_FUNC_CALLOC_POSIX],
84 [
85 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
86 AC_REQUIRE([gl_CHECK_MALLOC_POSIX])
87 if test $gl_cv_func_malloc_posix = yes; then
88 AC_DEFINE([HAVE_CALLOC_POSIX], [1],
89 [Define if the 'calloc' function is POSIX compliant.])
90 else
91 REPLACE_CALLOC=1
92 fi
93 ])