Files
gnulib/m4/aligned_alloc.m4
Bruno Haible 0b416a8a26 alignalloc: Don't use aligned_alloc if it is buggy.
Reported by <gnulib@mifar.in> in
<https://lists.gnu.org/archive/html/bug-gnulib/2026-09/msg00097.html>.

* m4/aligned_alloc.m4 (gl_FUNC_ALIGNED_ALLOC_WORKS): New macro,
extracted from gl_FUNC_ALIGNED_ALLOC.
(gl_FUNC_ALIGNED_ALLOC): Use it.
* m4/alignalloc.m4 (gl_ALIGNALLOC): Invoke gl_FUNC_ALIGNED_ALLOC_WORKS
and define HAVE_WORKING_ALIGNED_ALLOC.
* lib/alignalloc.h (ALIGNALLOC_VIA_ALIGNED_ALLOC): Test
HAVE_WORKING_ALIGNED_ALLOC, instead of testing for an address sanitizer.
* modules/alignalloc (Files): Add m4/aligned_alloc.m4.
2026-09-23 23:43:28 +02:00

92 lines
3.2 KiB
Plaintext

# aligned_alloc.m4
# serial 10
dnl Copyright (C) 2020-2026 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
dnl This file is offered as-is, without any warranty.
AC_DEFUN([gl_FUNC_ALIGNED_ALLOC],
[
AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
gl_FUNC_ALIGNED_ALLOC_WORKS
if test $ac_cv_func_aligned_alloc = yes; then
dnl The system has aligned_alloc.
case "$gl_cv_func_aligned_alloc_works" in
*yes) ;;
*) REPLACE_ALIGNED_ALLOC=1 ;;
esac
else
dnl The system does not have aligned_alloc.
HAVE_ALIGNED_ALLOC=0
case "$gl_cv_onwards_func_aligned_alloc" in
future*) REPLACE_ALIGNED_ALLOC=1 ;;
esac
fi
])
dnl Test whether aligned_alloc is defined and works.
AC_DEFUN_ONCE([gl_FUNC_ALIGNED_ALLOC_WORKS],
[
AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
dnl Persuade glibc and OpenBSD <stdlib.h> to declare aligned_alloc().
AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
gl_CHECK_FUNCS_ANDROID([aligned_alloc], [[#include <stdlib.h>]])
if test $ac_cv_func_aligned_alloc = yes; then
dnl On macOS 11.1 and AIX 7.3.1, aligned_alloc returns NULL when the
dnl alignment argument is smaller than sizeof (void *).
dnl On Solaris 11.4 or with gcc's ASAN or with clang's ASAN or Scudo,
dnl aligned_alloc returns NULL if the size is not a multiple of the
dnl alignment.
dnl On macOS 15, AIX 7.3, Solaris 11.4, aligned_alloc with a zero size
dnl returns NULL.
AC_CACHE_CHECK([whether aligned_alloc works for small alignments and sizes],
[gl_cv_func_aligned_alloc_works],
[AC_RUN_IFELSE(
[AC_LANG_PROGRAM(
[[#include <stdlib.h>
/* Use paligned_alloc to test; 'volatile' prevents the compiler
from optimizing the aligned_alloc call away. */
void *(*volatile paligned_alloc) (size_t, size_t)
= aligned_alloc;]],
[[int result = 0;
{
void *p = paligned_alloc (2, 18);
if (p == NULL)
result |= 1;
}
{
void *p = paligned_alloc (sizeof (void *), sizeof (void *) + 1);
if (p == NULL)
result |= 2;
}
{
void *p = paligned_alloc (sizeof (void *), 0);
if (p == NULL)
result |= 4;
}
return result;
]])
],
[gl_cv_func_aligned_alloc_works=yes],
[gl_cv_func_aligned_alloc_works=no],
[case "$host_os" in
# Guess no on macOS.
darwin*) gl_cv_func_aligned_alloc_works="guessing no" ;;
# Guess no on AIX.
aix*) gl_cv_func_aligned_alloc_works="guessing no" ;;
# Guess no on Solaris.
solaris*) gl_cv_func_aligned_alloc_works="guessing no" ;;
# If we don't know, obey --enable-cross-guesses.
*) gl_cv_func_aligned_alloc_works="$gl_cross_guess_normal" ;;
esac
])
])
else
gl_cv_func_aligned_alloc_works=no
fi
])