summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2020-12-31 22:18:05 +0100
committerBruno Haible <bruno@clisp.org>2020-12-31 22:18:05 +0100
commit3374e597f208292530d40d180f32b6bbbafd7586 (patch)
treef950ae2b3854188a9e534586b30305eb736ec7df
parentec9385e0feffe6257bba8c47a568fcdddb8d5772 (diff)
downloadgnulib-3374e597f208292530d40d180f32b6bbbafd7586.tar.gz
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 <malloc.h> unconditionally. * doc/glibc-functions/memalign.texi: Mention the Solaris issues.
-rw-r--r--ChangeLog10
-rw-r--r--doc/glibc-functions/memalign.texi7
-rw-r--r--lib/memalign.c35
-rw-r--r--m4/memalign.m448
-rw-r--r--modules/memalign11
5 files changed, 108 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index bc173b96e9..7dd4ffdd6e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
2020-12-31 Bruno Haible <bruno@clisp.org>
+ 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 <malloc.h> 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 7c07bc6ee7..ec2f533678 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{<stdlib.h>} instead of @code{<malloc.h>}
+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 0000000000..494dfb602d
--- /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 <https://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+/* Specification. */
+#include <malloc.h>
+
+#include <stdlib.h>
+
+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 0000000000..a27055fc19
--- /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 <malloc.h>
+ #include <stdlib.h>
+ ]],
+ [[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 e8a292d92d..b383d196d1 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 <malloc.h>
-#endif
License:
LGPLv2+