1# realloc.m4 serial 24
2dnl Copyright (C) 2007, 2009-2021 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7# This is adapted with modifications from upstream Autoconf here:
8# https://git.savannah.gnu.org/cgit/autoconf.git/tree/lib/autoconf/functions.m4?id=v2.70#n1455
9AC_DEFUN([_AC_FUNC_REALLOC_IF],
10[
11  AC_REQUIRE([AC_CANONICAL_HOST])dnl for cross-compiles
12  AC_CACHE_CHECK([whether realloc (0, 0) returns nonnull],
13    [ac_cv_func_realloc_0_nonnull],
14    [AC_RUN_IFELSE(
15       [AC_LANG_PROGRAM(
16          [[#include <stdlib.h>
17          ]],
18          [[void *p = realloc (0, 0);
19            int result = !p;
20            free (p);
21            return result;]])
22       ],
23       [ac_cv_func_realloc_0_nonnull=yes],
24       [ac_cv_func_realloc_0_nonnull=no],
25       [case "$host_os" in
26          # Guess yes on platforms where we know the result.
27          *-gnu* | freebsd* | netbsd* | openbsd* | bitrig* \
28          | gnu* | *-musl* | midnightbsd* \
29          | hpux* | solaris* | cygwin* | mingw* | msys* )
30            ac_cv_func_realloc_0_nonnull="guessing yes" ;;
31          # If we don't know, obey --enable-cross-guesses.
32          *) ac_cv_func_realloc_0_nonnull="$gl_cross_guess_normal" ;;
33        esac
34       ])
35    ])
36  AS_CASE([$ac_cv_func_realloc_0_nonnull], [*yes], [$1], [$2])
37])# AC_FUNC_REALLOC
38
39# gl_FUNC_REALLOC_GNU
40# -------------------
41# Replace realloc if it is not compatible with GNU libc.
42AC_DEFUN([gl_FUNC_REALLOC_GNU],
43[
44  AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
45  AC_REQUIRE([gl_FUNC_REALLOC_POSIX])
46  if test $REPLACE_REALLOC = 0; then
47    _AC_FUNC_REALLOC_IF([], [REPLACE_REALLOC=1])
48  fi
49])# gl_FUNC_REALLOC_GNU
50
51# gl_FUNC_REALLOC_POSIX
52# ---------------------
53# Test whether 'realloc' is POSIX compliant (sets errno to ENOMEM when it
54# fails, and doesn't mess up with ptrdiff_t overflow),
55# and replace realloc if it is not.
56AC_DEFUN([gl_FUNC_REALLOC_POSIX],
57[
58  AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
59  AC_REQUIRE([gl_FUNC_MALLOC_POSIX])
60  if test $REPLACE_MALLOC = 1; then
61    REPLACE_REALLOC=1
62  fi
63])
64