1# realloc.m4 serial 19
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/commit/?id=04be2b7a29d65d9a08e64e8e56e594c91749598c
9AC_DEFUN([_AC_FUNC_REALLOC_IF],
10[
11  AC_REQUIRE([AC_CANONICAL_HOST])dnl for cross-compiles
12  AC_CACHE_CHECK([for GNU libc compatible realloc],
13    [ac_cv_func_realloc_0_nonnull],
14    [AC_RUN_IFELSE(
15       [AC_LANG_PROGRAM(
16          [[#include <stdlib.h>
17          ]],
18          [[char *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* | gnu* | *-musl* | freebsd* | netbsd* | openbsd* \
28          | hpux* | solaris* | cygwin* | mingw*)
29            ac_cv_func_realloc_0_nonnull="guessing yes" ;;
30          # If we don't know, obey --enable-cross-guesses.
31          *) ac_cv_func_realloc_0_nonnull="$gl_cross_guess_normal" ;;
32        esac
33       ])
34    ])
35  case "$ac_cv_func_realloc_0_nonnull" in
36    *yes)
37      $1
38      ;;
39    *)
40      $2
41      ;;
42  esac
43])# AC_FUNC_REALLOC
44
45# gl_FUNC_REALLOC_GNU
46# -------------------
47# Test whether 'realloc (0, 0)' is handled like in GNU libc, and replace
48# realloc if it is not.
49AC_DEFUN([gl_FUNC_REALLOC_GNU],
50[
51  AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
52  dnl _AC_FUNC_REALLOC_IF is defined in Autoconf.
53  _AC_FUNC_REALLOC_IF(
54    [AC_DEFINE([HAVE_REALLOC_GNU], [1],
55               [Define to 1 if your system has a GNU libc compatible 'realloc'
56                function, and to 0 otherwise.])],
57    [AC_DEFINE([HAVE_REALLOC_GNU], [0])
58     REPLACE_REALLOC=1
59    ])
60])# gl_FUNC_REALLOC_GNU
61
62# gl_FUNC_REALLOC_POSIX
63# ---------------------
64# Test whether 'realloc' is POSIX compliant (sets errno to ENOMEM when it
65# fails), and replace realloc if it is not.
66AC_DEFUN([gl_FUNC_REALLOC_POSIX],
67[
68  AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
69  AC_REQUIRE([gl_CHECK_MALLOC_POSIX])
70  if test $gl_cv_func_malloc_posix = yes; then
71    AC_DEFINE([HAVE_REALLOC_POSIX], [1],
72      [Define if the 'realloc' function is POSIX compliant.])
73  else
74    REPLACE_REALLOC=1
75  fi
76])
77