>From 3374e597f208292530d40d180f32b6bbbafd7586 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 31 Dec 2020 22:18:05 +0100 Subject: [PATCH 3/3] memalign: Work around Solaris bug. * lib/memalign.c: New file. * m4/memalign.m4: New file. * modules/memalign (Files): Add them. (Depends-on): Add malloc-h. (configure.ac): Invoke gl_FUNC_MEMALIGN. Conditionally compile memalign.c. Set module indicator. (Include): Include unconditionally. * doc/glibc-functions/memalign.texi: Mention the Solaris issues. --- ChangeLog | 10 ++++++++ doc/glibc-functions/memalign.texi | 7 ++++++ lib/memalign.c | 35 ++++++++++++++++++++++++++++ m4/memalign.m4 | 48 +++++++++++++++++++++++++++++++++++++++ modules/memalign | 11 ++++++--- 5 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 lib/memalign.c create mode 100644 m4/memalign.m4 diff --git a/ChangeLog b/ChangeLog index bc173b9..7dd4ffd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,15 @@ 2020-12-31 Bruno Haible + memalign: Work around Solaris bug. + * lib/memalign.c: New file. + * m4/memalign.m4: New file. + * modules/memalign (Files): Add them. + (Depends-on): Add malloc-h. + (configure.ac): Invoke gl_FUNC_MEMALIGN. Conditionally compile + memalign.c. Set module indicator. + (Include): Include unconditionally. + * doc/glibc-functions/memalign.texi: Mention the Solaris issues. + malloc-h: Add tests. * tests/test-malloc-h.c: New file. * modules/malloc-h-tests: New file. diff --git a/doc/glibc-functions/memalign.texi b/doc/glibc-functions/memalign.texi index 7c07bc6..ec2f533 100644 --- a/doc/glibc-functions/memalign.texi +++ b/doc/glibc-functions/memalign.texi @@ -19,6 +19,13 @@ Gnulib module: memalign Portability problems fixed by Gnulib: @itemize +@item +This function is declared in @code{} instead of @code{} +on some platforms: +Solaris 11. +@item +This function doesn't accept an alignment of 1 or 2 on some platforms: +Solaris 11. @end itemize Portability problems not fixed by Gnulib: diff --git a/lib/memalign.c b/lib/memalign.c new file mode 100644 index 0000000..494dfb6 --- /dev/null +++ b/lib/memalign.c @@ -0,0 +1,35 @@ +/* Allocate memory with indefinite extent and specified alignment. + Copyright (C) 2020 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +/* Specification. */ +#include + +#include + +void * +memalign (size_t alignment, size_t size) +#undef memalign +{ + if (alignment < 4) + /* The malloc() result has an alignment of at least 4 on all platforms. + On platforms where memalign() exists, malloc() sets errno upon + failure. */ + return malloc (size); + + return memalign (alignment, size); +} diff --git a/m4/memalign.m4 b/m4/memalign.m4 new file mode 100644 index 0000000..a27055f --- /dev/null +++ b/m4/memalign.m4 @@ -0,0 +1,48 @@ +# memalign.m4 serial 1 +dnl Copyright (C) 2020 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. + +AC_DEFUN([gl_FUNC_MEMALIGN], +[ + AC_REQUIRE([gl_MALLOC_H_DEFAULTS]) + AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles + + AC_CHECK_FUNCS_ONCE([memalign]) + if test $ac_cv_func_memalign = yes; then + dnl On Solaris 11, memalign (2, n) always returns NULL. + AC_CACHE_CHECK([whether memalign works for small alignments], + [gl_cv_func_memalign_works], + [AC_RUN_IFELSE( + [AC_LANG_PROGRAM( + [[#include + #include + ]], + [[int result = 0; + if (memalign (1, 1) == NULL) + result |= 1; + if (memalign (2, 1) == NULL) + result |= 2; + return result; + ]]) + ], + [gl_cv_func_memalign_works=yes], + [gl_cv_func_memalign_works=no], + [case "$host_os" in + # Guess no on Solaris. + solaris*) gl_cv_func_memalign_works="guessing no" ;; + # If we don't know, obey --enable-cross-guesses. + *) gl_cv_func_memalign_works="$gl_cross_guess_normal" ;; + esac + ]) + ]) + case "$gl_cv_func_memalign_works" in + *yes) ;; + *) REPLACE_MEMALIGN=1 ;; + esac + else + dnl The system does not have memalign. + HAVE_MEMALIGN=0 + fi +]) diff --git a/modules/memalign b/modules/memalign index e8a292d..b383d19 100644 --- a/modules/memalign +++ b/modules/memalign @@ -2,18 +2,23 @@ Description: Allocate memory with indefinite extent and specified alignment. Files: +lib/memalign.c +m4/memalign.m4 Depends-on: +malloc-h configure.ac: -AC_CHECK_FUNCS([memalign]) +gl_FUNC_MEMALIGN +if test $REPLACE_MEMALIGN = 1; then + AC_LIBOBJ([memalign]) +fi +gl_MALLOC_MODULE_INDICATOR([memalign]) Makefile.am: Include: -#if HAVE_MEMALIGN #include -#endif License: LGPLv2+ -- 2.7.4