]> Savannah Git Hosting - gnulib.git/commitdiff
memalign: Work around Solaris bug.
authorBruno Haible <bruno@clisp.org>
Thu, 31 Dec 2020 21:18:05 +0000 (22:18 +0100)
committerBruno Haible <bruno@clisp.org>
Thu, 31 Dec 2020 21:18:05 +0000 (22:18 +0100)
* 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.

ChangeLog
doc/glibc-functions/memalign.texi
lib/memalign.c [new file with mode: 0644]
m4/memalign.m4 [new file with mode: 0644]
modules/memalign

index bc173b96e95870b49715a0cc05f954793d60a863..7dd4ffdd6ecb580365387666393474f5d71c220a 100644 (file)
--- 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.
index 7c07bc6ee7374e3358576f58c3bf21038a00895e..ec2f5336782218fec674cd2650b34487ce5bffcd 100644 (file)
@@ -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 (file)
index 0000000..494dfb6
--- /dev/null
@@ -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 (file)
index 0000000..a27055f
--- /dev/null
@@ -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
+])
index e8a292d92d38cb36beeccc2ccb7b86dcf315f967..b383d196d1ce902f1a226027a2ffef7bc7d998ec 100644 (file)
@@ -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+