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.m483
1 files changed, 83 insertions, 0 deletions
diff --git a/gl/m4/calloc.m4 b/gl/m4/calloc.m4
new file mode 100644
index 00000000..23c0dd9f
--- /dev/null
+++ b/gl/m4/calloc.m4
@@ -0,0 +1,83 @@
1# calloc.m4 serial 29
2
3# Copyright (C) 2004-2023 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 REPLACE_CALLOC_FOR_CALLOC_GNU="$REPLACE_CALLOC_FOR_CALLOC_POSIX"
62 if test $REPLACE_CALLOC_FOR_CALLOC_GNU = 0; then
63 _AC_FUNC_CALLOC_IF([], [REPLACE_CALLOC_FOR_CALLOC_GNU=1])
64 fi
65])# gl_FUNC_CALLOC_GNU
66
67# gl_FUNC_CALLOC_POSIX
68# --------------------
69# Test whether 'calloc' is POSIX compliant (sets errno to ENOMEM when it
70# fails, and doesn't mess up with ptrdiff_t or size_t overflow),
71# and replace calloc if it is not.
72AC_DEFUN([gl_FUNC_CALLOC_POSIX],
73[
74 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
75 AC_REQUIRE([gl_FUNC_MALLOC_POSIX])
76 if test $REPLACE_MALLOC_FOR_MALLOC_POSIX = 1; then
77 REPLACE_CALLOC_FOR_CALLOC_POSIX=1
78 fi
79 dnl Although in theory we should also test for size_t overflow,
80 dnl in practice testing for ptrdiff_t overflow suffices
81 dnl since PTRDIFF_MAX <= SIZE_MAX on all known Gnulib porting targets.
82 dnl A separate size_t test would slow down 'configure'.
83])