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.
This commit is contained in:
Chris Robinson 2020-05-19 08:13:13 -07:00
parent 400a108ead
commit 463591663c
3 changed files with 15 additions and 5 deletions

View File

@ -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

View File

@ -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);

View File

@ -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