summaryrefslogtreecommitdiffstats
path: root/gl/m4/calloc.m4
diff options
context:
space:
mode:
Diffstat (limited to 'gl/m4/calloc.m4')
-rw-r--r--gl/m4/calloc.m482
1 files changed, 82 insertions, 0 deletions
diff --git a/gl/m4/calloc.m4 b/gl/m4/calloc.m4
new file mode 100644
index 0000000..fe12b15
--- /dev/null
+++ b/gl/m4/calloc.m4
@@ -0,0 +1,82 @@
1# calloc.m4 serial 27
2
3# Copyright (C) 2004-2021 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 is compatible with GNU calloc, run IF-WORKS, otherwise, IF-NOT.
18AC_DEFUN([_AC_FUNC_CALLOC_IF],
19[
20 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
21 AC_CACHE_CHECK([whether calloc (0, n) and calloc (n, 0) return nonnull],
22 [ac_cv_func_calloc_0_nonnull],
23 [if test $cross_compiling != yes; then
24 ac_cv_func_calloc_0_nonnull=yes
25 AC_RUN_IFELSE(
26 [AC_LANG_PROGRAM(
27 [AC_INCLUDES_DEFAULT],
28 [[int result = 0;
29 char * volatile p = calloc (0, 0);
30 if (!p)
31 result |= 1;
32 free (p);
33 return result;
34 ]])],
35 [],
36 [ac_cv_func_calloc_0_nonnull=no])
37 else
38 case "$host_os" in
39 # Guess yes on glibc systems.
40 *-gnu* | gnu*) ac_cv_func_calloc_0_nonnull="guessing yes" ;;
41 # Guess yes on musl systems.
42 *-musl*) ac_cv_func_calloc_0_nonnull="guessing yes" ;;
43 # Guess yes on native Windows.
44 mingw*) ac_cv_func_calloc_0_nonnull="guessing yes" ;;
45 # If we don't know, obey --enable-cross-guesses.
46 *) ac_cv_func_calloc_0_nonnull="$gl_cross_guess_normal" ;;
47 esac
48 fi
49 ])
50 AS_CASE([$ac_cv_func_calloc_0_nonnull], [*yes], [$1], [$2])
51])
52
53
54# gl_FUNC_CALLOC_GNU
55# ------------------
56# Replace calloc if it is not compatible with GNU libc.
57AC_DEFUN([gl_FUNC_CALLOC_GNU],
58[
59 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
60 AC_REQUIRE([gl_FUNC_CALLOC_POSIX])
61 if test $REPLACE_CALLOC = 0; then
62 _AC_FUNC_CALLOC_IF([], [REPLACE_CALLOC=1])
63 fi
64])# gl_FUNC_CALLOC_GNU
65
66# gl_FUNC_CALLOC_POSIX
67# --------------------
68# Test whether 'calloc' is POSIX compliant (sets errno to ENOMEM when it
69# fails, and doesn't mess up with ptrdiff_t or size_t overflow),
70# and replace calloc if it is not.
71AC_DEFUN([gl_FUNC_CALLOC_POSIX],
72[
73 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
74 AC_REQUIRE([gl_FUNC_MALLOC_POSIX])
75 if test $REPLACE_MALLOC = 1; then
76 REPLACE_CALLOC=1
77 fi
78 dnl Although in theory we should also test for size_t overflow,
79 dnl in practice testing for ptrdiff_t overflow suffices
80 dnl since PTRDIFF_MAX <= SIZE_MAX on all known Gnulib porting targets.
81 dnl A separate size_t test would slow down 'configure'.
82])