aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <chris.kcat@gmail.com>2020-05-19 08:13:13 -0700
committerChris Robinson <chris.kcat@gmail.com>2020-05-19 08:13:13 -0700
commit463591663c2421b3436edc446d173380d6a6e106 (patch)
treef8e738f589a63fc7392eb2ea5e1570672bb24bac
parent400a108eade05d616ed0560024b7fd6f5be5fd1d (diff)
Check that aligned_alloc is available with cmake
Some compilers support C++17 even on targets that lack required functions. Projects that want to force C++17 will then run into a problem with std::aligned_alloc not existing on those targets, so it needs to be explicitly checked for. The alternative is to simply never use it even when it would be available.
-rw-r--r--CMakeLists.txt13
-rw-r--r--common/almalloc.cpp4
-rw-r--r--config.h.in3
3 files changed, 15 insertions, 5 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 208a275f..64c8116e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -449,9 +449,16 @@ IF(HAVE_INTRIN_H)
}" HAVE_BITSCANFORWARD_INTRINSIC)
ENDIF()
-CHECK_SYMBOL_EXISTS(posix_memalign stdlib.h HAVE_POSIX_MEMALIGN)
-CHECK_SYMBOL_EXISTS(_aligned_malloc malloc.h HAVE__ALIGNED_MALLOC)
-CHECK_SYMBOL_EXISTS(proc_pidpath libproc.h HAVE_PROC_PIDPATH)
+check_cxx_source_compiles("#include <cstdlib>
+int main()
+{
+ void *ptr{std::aligned_alloc(alignof(int), sizeof(int))};
+ std::free(ptr);
+ return 0;
+}" HAVE_STD_ALIGNED_ALLOC)
+check_symbol_exists(posix_memalign stdlib.h HAVE_POSIX_MEMALIGN)
+check_symbol_exists(_aligned_malloc malloc.h HAVE__ALIGNED_MALLOC)
+check_symbol_exists(proc_pidpath libproc.h HAVE_PROC_PIDPATH)
IF(NOT WIN32)
# We need pthreads outside of Windows, for semaphores. It's also used to
diff --git a/common/almalloc.cpp b/common/almalloc.cpp
index 5b679b75..806cb4ab 100644
--- a/common/almalloc.cpp
+++ b/common/almalloc.cpp
@@ -17,7 +17,7 @@ void *al_malloc(size_t alignment, size_t size)
assert((alignment & (alignment-1)) == 0);
alignment = std::max(alignment, alignof(std::max_align_t));
-#if __cplusplus >= 201703L
+#if defined(HAVE_STD_ALIGNED_ALLOC)
size = (size+(alignment-1))&~(alignment-1);
return std::aligned_alloc(alignment, size);
#elif defined(HAVE_POSIX_MEMALIGN)
@@ -48,7 +48,7 @@ void *al_calloc(size_t alignment, size_t size)
void al_free(void *ptr) noexcept
{
-#if (__cplusplus >= 201703L) || defined(HAVE_POSIX_MEMALIGN)
+#if defined(HAVE_STD_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
std::free(ptr);
#elif defined(HAVE__ALIGNED_MALLOC)
_aligned_free(ptr);
diff --git a/config.h.in b/config.h.in
index 29a10b20..75aacc0d 100644
--- a/config.h.in
+++ b/config.h.in
@@ -8,6 +8,9 @@
/* Define if HRTF data is embedded in the library */
#cmakedefine ALSOFT_EMBED_HRTF_DATA
+/* Define if we have the std::aligned_alloc function */
+#cmakedefine HAVE_STD_ALIGNED_ALLOC
+
/* Define if we have the posix_memalign function */
#cmakedefine HAVE_POSIX_MEMALIGN