| 1 |
# calloc.m4 |
| 2 |
# serial 36 |
| 3 |
dnl Copyright (C) 2004-2024 Free Software Foundation, Inc. |
| 4 |
dnl This file is free software; the Free Software Foundation |
| 5 |
dnl gives unlimited permission to copy and/or distribute it, |
| 6 |
dnl with or without modifications, as long as this notice is preserved. |
| 7 |
dnl This file is offered as-is, without any warranty. |
| 8 |
|
| 9 |
# Written by Jim Meyering. |
| 10 |
|
| 11 |
# Determine whether calloc (N, S) returns non-NULL when N*S is zero, |
| 12 |
# and returns NULL when N*S overflows. |
| 13 |
# If so, define HAVE_CALLOC. Otherwise, define calloc to rpl_calloc |
| 14 |
# and arrange to use a calloc wrapper function that does work in that case. |
| 15 |
|
| 16 |
# gl_FUNC_CALLOC_IF([IF-WORKS], [IF-NOT]) |
| 17 |
# --------------------------------------- |
| 18 |
# If calloc is compatible with GNU calloc, run IF-WORKS, otherwise, IF-NOT. |
| 19 |
AC_DEFUN([gl_FUNC_CALLOC_IF], |
| 20 |
[ |
| 21 |
AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles |
| 22 |
AC_CACHE_CHECK([whether calloc (0, n) and calloc (n, 0) return nonnull], |
| 23 |
[gl_cv_func_calloc_0_nonnull], |
| 24 |
[AC_RUN_IFELSE( |
| 25 |
[AC_LANG_PROGRAM( |
| 26 |
[[#include <stdlib.h> |
| 27 |
/* Use pcalloc to test; "volatile" prevents the compiler |
| 28 |
from optimizing the calloc call away. */ |
| 29 |
void *(*volatile pcalloc) (size_t, size_t) = calloc;]], |
| 30 |
[[void *p = pcalloc (0, 0); |
| 31 |
int result = !p; |
| 32 |
free (p); |
| 33 |
return result;]])], |
| 34 |
[gl_cv_func_calloc_0_nonnull=yes], |
| 35 |
[gl_cv_func_calloc_0_nonnull=no], |
| 36 |
[AS_CASE([$host_os], |
| 37 |
[# Guess yes on platforms where we know the result. |
| 38 |
*-gnu* | freebsd* | netbsd* | openbsd* | bitrig* \ |
| 39 |
| gnu* | *-musl* | midipix* | midnightbsd* \ |
| 40 |
| hpux* | solaris* | cygwin* | mingw* | windows* | msys*], |
| 41 |
[gl_cv_func_calloc_0_nonnull="guessing yes"], |
| 42 |
[# If we don't know, obey --enable-cross-guesses. |
| 43 |
gl_cv_func_calloc_0_nonnull="$gl_cross_guess_normal"])])]) |
| 44 |
AS_CASE([$gl_cv_func_calloc_0_nonnull], [*yes], [$1], [$2]) |
| 45 |
]) |
| 46 |
|
| 47 |
|
| 48 |
# gl_FUNC_CALLOC_GNU |
| 49 |
# ------------------ |
| 50 |
# Replace calloc if it is not compatible with GNU libc. |
| 51 |
AC_DEFUN([gl_FUNC_CALLOC_GNU], |
| 52 |
[ |
| 53 |
AC_REQUIRE([gl_STDLIB_H_DEFAULTS]) |
| 54 |
AC_REQUIRE([gl_FUNC_CALLOC_POSIX]) |
| 55 |
|
| 56 |
dnl Through the dependency on module extensions-aix, _LINUX_SOURCE_COMPAT |
| 57 |
dnl gets defined already before this macro gets invoked. This helps |
| 58 |
dnl if !(__VEC__ || __AIXVEC), and doesn't hurt otherwise. |
| 59 |
|
| 60 |
REPLACE_CALLOC_FOR_CALLOC_GNU="$REPLACE_CALLOC_FOR_CALLOC_POSIX" |
| 61 |
if test $REPLACE_CALLOC_FOR_CALLOC_GNU = 0; then |
| 62 |
gl_FUNC_CALLOC_IF([], [REPLACE_CALLOC_FOR_CALLOC_GNU=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. |
| 71 |
AC_DEFUN([gl_FUNC_CALLOC_POSIX], |
| 72 |
[ |
| 73 |
AC_REQUIRE([gl_STDLIB_H_DEFAULTS]) |
| 74 |
AC_REQUIRE([gl_FUNC_MALLOC_POSIX]) |
| 75 |
REPLACE_CALLOC_FOR_CALLOC_POSIX=$REPLACE_MALLOC_FOR_MALLOC_POSIX |
| 76 |
dnl Although in theory we should also test for size_t overflow, |
| 77 |
dnl in practice testing for ptrdiff_t overflow suffices |
| 78 |
dnl since PTRDIFF_MAX <= SIZE_MAX on all known Gnulib porting targets. |
| 79 |
dnl A separate size_t test would slow down 'configure'. |
| 80 |
]) |