From 2c5b0eeed583b4ff73b5ac071cfac7e0ee391a2d Mon Sep 17 00:00:00 2001
From: Simon Praetorius <simon.praetorius@tu-dresden.de>
Date: Tue, 16 Jan 2018 16:50:05 +0100
Subject: [PATCH] Better test whether standard library supports aligned_alloc()

---
 cmake/modules/CheckCXXFeatures.cmake | 11 +++++++++++
 config.h.cmake                       |  3 +++
 dune/common/alignedallocator.hh      |  8 ++++++--
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/cmake/modules/CheckCXXFeatures.cmake b/cmake/modules/CheckCXXFeatures.cmake
index 707d14d83..67b7bf4be 100644
--- a/cmake/modules/CheckCXXFeatures.cmake
+++ b/cmake/modules/CheckCXXFeatures.cmake
@@ -484,6 +484,17 @@ check_cxx_source_compiles("
 " DUNE_SUPPORTS_CXX_THROW_IN_CONSTEXPR
   )
 
+# Check whether the stadard library supports aligned_alloc()
+check_cxx_source_compiles("
+  #include <cstdlib>
+  int main()
+  {
+    int* p = static_cast<int*>(aligned_alloc(64, 64*sizeof *p));
+  }
+" DUNE_HAVE_C_ALIGNED_ALLOC
+  )
+
+
 
 # ******************************************************************************
 #
diff --git a/config.h.cmake b/config.h.cmake
index e06c65b73..f0dd6722a 100644
--- a/config.h.cmake
+++ b/config.h.cmake
@@ -35,6 +35,9 @@
 /* does the compiler support conditionally throwing exceptions in constexpr context? */
 #cmakedefine DUNE_SUPPORTS_CXX_THROW_IN_CONSTEXPR 1
 
+/* does the standard library provides aligned_alloc()? */
+#cmakedefine DUNE_HAVE_C_ALIGNED_ALLOC 1
+
 /* does the standard library provide <experimental/type_traits> ? */
 #cmakedefine DUNE_HAVE_HEADER_EXPERIMENTAL_TYPE_TRAITS 1
 
diff --git a/dune/common/alignedallocator.hh b/dune/common/alignedallocator.hh
index cef4d6680..ac9e20302 100644
--- a/dune/common/alignedallocator.hh
+++ b/dune/common/alignedallocator.hh
@@ -6,6 +6,10 @@
 #include "mallocallocator.hh"
 #include <cstdlib>
 
+#if !(DUNE_HAVE_C_ALIGNED_ALLOC || (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600))
+  #error Need either aligned_alloc() or posix_memalign() to compile AlignedAllocator
+#endif
+
 namespace Dune
 {
 
@@ -19,7 +23,7 @@ namespace Dune
   template<class T, int Alignment = -1>
   class AlignedAllocator : public MallocAllocator<T> {
 
-#if __APPLE__
+#if !DUNE_HAVE_C_ALIGNED_ALLOC
 
     /*
      * posix_memalign() on macOS has pretty draconian restrictions on the
@@ -67,7 +71,7 @@ namespace Dune
       if (n > this->max_size())
         throw std::bad_alloc();
 
-#if __APPLE__
+#if !DUNE_HAVE_C_ALIGNED_ALLOC
       /*
        * Apple's standard library doesn't have aligned_alloc() - C11 is still something
        * from the future in Cupertino. Luckily, they got around to finally implementing
-- 
GitLab