]> Savannah Git Hosting - gnulib.git/commitdiff
aligned-malloc: Optionally use aligned_alloc.
authorBruno Haible <bruno@clisp.org>
Tue, 21 Jul 2020 16:09:35 +0000 (18:09 +0200)
committerBruno Haible <bruno@clisp.org>
Tue, 21 Jul 2020 16:20:01 +0000 (18:20 +0200)
* lib/aligned-malloc.h: Verify the alignment.
(aligned_malloc): Use aligned_alloc as an alternative.
* modules/aligned-malloc (configure.ac): Test for aligned_alloc.
* doc/posix-functions/aligned_alloc.texi: Mention the modules
'aligned-malloc' and 'pagealign_alloc'.

ChangeLog
doc/posix-functions/aligned_alloc.texi
lib/aligned-malloc.h
modules/aligned-malloc

index 8da483c857a6b90802a2f14bf859fc148f9cf5b5..8cac948ad262a8ec5e9e28a68f54440cd8e85536 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2020-07-21  Bruno Haible  <bruno@clisp.org>
+
+       aligned-malloc: Optionally use aligned_alloc.
+       * lib/aligned-malloc.h: Verify the alignment.
+       (aligned_malloc): Use aligned_alloc as an alternative.
+       * modules/aligned-malloc (configure.ac): Test for aligned_alloc.
+       * doc/posix-functions/aligned_alloc.texi: Mention the modules
+       'aligned-malloc' and 'pagealign_alloc'.
+
 2020-07-21  Bruno Haible  <bruno@clisp.org>
 
        aligned-malloc: Add tests.
index c938f30d71768f06c1efb08e6998cc4a7be2908b..a4897b91679d7ce4bc34f2c0ef2418e5a8e65d9c 100644 (file)
@@ -16,3 +16,9 @@ Portability problems not fixed by Gnulib:
 This function is missing on all non-glibc platforms:
 glibc 2.15, Mac OS X 10.5, FreeBSD 6.4, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, Solaris 11.3, Cygwin, mingw, MSVC 14, Android 8.1.
 @end itemize
+
+The Gnulib module @code{aligned-malloc} provides functions for
+allocating and freeing blocks of suitably aligned memory.
+
+The Gnulib module @code{pagealign_alloc} provides a similar API for
+allocating and freeing blocks of memory aligned on a system page boundary.
index 86382fd25b212166ada5fceb7609202b0f937d36..9baaab4cfe74b86e0422141882b6338b960e56f2 100644 (file)
    The block can be freed through aligned_free(), NOT through free().
    Upon failure, it returns NULL.  */
 
-/* This module exists instead of a posix_memalign() or memalign() emulation,
-   because we can't reasonably emulate posix_memalign() or memalign():
+/* This module exists instead of a posix_memalign(), aligned_alloc(), or
+   memalign() emulation, because we can't reasonably emulate posix_memalign(),
+   aligned_alloc(), or memalign():
    If malloc() returned p, only free (p) is allowed, not free (p + 1),
    free (p + 2), free (p + 4), free (p + 8), or similar.
 
-   We can use posix_memalign().  On older systems, we can alternatively use
-   memalign() instead.  In the Solaris documentation of memalign() it is not
-   specified how a memory block returned by memalign() can be freed, but
-   it actually can be freed with free().  */
+   We can use posix_memalign(), a POSIX function.
 
-#if ((ALIGNMENT) <= MALLOC_ALIGNMENT) || HAVE_POSIX_MEMALIGN || HAVE_MEMALIGN
+   We can also use aligned_alloc(), an ISO C11 and POSIX function.  But it's
+   a bit more awkward to use.
+
+   On older systems, we can alternatively use memalign() instead.  In the
+   Solaris documentation of memalign() it is not specified how a memory block
+   returned by memalign() can be freed, but it actually can be freed with
+   free().  */
+
+#if !defined ALIGNMENT
+# error "ALIGNMENT is not defined"
+#endif
+#if !((ALIGNMENT) > 0 && ((ALIGNMENT) & ((ALIGNMENT) - 1)) == 0)
+# error "ALIGNMENT is not a power of 2"
+#endif
+#if ((ALIGNMENT) <= MALLOC_ALIGNMENT) || HAVE_POSIX_MEMALIGN || HAVE_ALIGNED_ALLOC || HAVE_MEMALIGN
 
 # if (ALIGNMENT) <= MALLOC_ALIGNMENT
 /* Simply use malloc.  */
@@ -70,6 +82,23 @@ aligned_malloc (size_t size)
     return NULL;
 }
 
+# elif HAVE_ALIGNED_ALLOC
+/* Use aligned_alloc.  */
+
+static inline void *
+aligned_malloc (size_t size)
+{
+  /* Round up SIZE to the next multiple of ALIGNMENT,
+     namely (SIZE + ALIGNMENT - 1) & ~(ALIGNMENT - 1).  */
+  size += (ALIGNMENT) - 1;
+  if (size >= (ALIGNMENT) - 1) /* no overflow? */
+    {
+      size &= ~(size_t)((ALIGNMENT) - 1);
+      return aligned_alloc ((ALIGNMENT), size);
+    }
+  return NULL;
+}
+
 # elif HAVE_MEMALIGN                    /* HP-UX, IRIX, Solaris <= 10 */
 /* Use memalign.  */
 
index 29483d83d7dfa2a024ddb76783ad7cb835248e31..d562e3d6b99ee6effa84557c269355c15bdb7b69 100644 (file)
@@ -11,7 +11,7 @@ stdint
 configure.ac:
 gl_MALLOC_ALIGNMENT
 AC_REQUIRE([AC_C_INLINE])
-AC_CHECK_FUNCS([posix_memalign memalign])
+AC_CHECK_FUNCS([posix_memalign aligned_alloc memalign])
 
 Makefile.am: