Implement C++17 aligned-new support for macOS < 10.14
This commit is contained in:
parent
38b102fc97
commit
f0b827d423
3 changed files with 104 additions and 2 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
include(CMakeDependentOption)
|
include(CMakeDependentOption)
|
||||||
include(CheckCCompilerFlag)
|
include(CheckCCompilerFlag)
|
||||||
include(CheckCXXCompilerFlag)
|
include(CheckCXXCompilerFlag)
|
||||||
|
include(CheckCXXSourceCompiles)
|
||||||
include(GNUWarnings)
|
include(GNUWarnings)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 11 CACHE STRING "C++ standard to be used")
|
set(CMAKE_CXX_STANDARD 11 CACHE STRING "C++ standard to be used")
|
||||||
|
|
@ -52,7 +53,30 @@ endif()
|
||||||
|
|
||||||
# Set macOS compatibility level
|
# Set macOS compatibility level
|
||||||
if(APPLE)
|
if(APPLE)
|
||||||
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14")
|
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.9")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# If using C++17, check if aligned-new has runtime support on the platform;
|
||||||
|
# on macOS, this depends on the deployment target.
|
||||||
|
if(CMAKE_CXX_STANDARD LESS 17)
|
||||||
|
# not necessary on older C++, it will call ordinary new and delete
|
||||||
|
set(SFIZZ_IMPLEMENT_CXX17_ALIGNED_NEW_SUPPORT FALSE)
|
||||||
|
else()
|
||||||
|
check_cxx_source_compiles("
|
||||||
|
struct Test { alignas(1024) int z; };
|
||||||
|
int main() { new Test; return 0; }
|
||||||
|
" SFIZZ_HAVE_CXX17_ALIGNED_NEW)
|
||||||
|
# if support is absent, sfizz will provide a substitute implementation
|
||||||
|
if(SFIZZ_HAVE_CXX17_ALIGNED_NEW)
|
||||||
|
set(SFIZZ_IMPLEMENT_CXX17_ALIGNED_NEW_SUPPORT FALSE)
|
||||||
|
else()
|
||||||
|
# on macOS, this mandatory flag tells that allocation functions are user-provided
|
||||||
|
check_cxx_compiler_flag("-faligned-allocation" SFIZZ_HAVE_CXXFLAG_FALIGNED_ALLOCATION)
|
||||||
|
if(SFIZZ_HAVE_CXXFLAG_FALIGNED_ALLOCATION)
|
||||||
|
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:-faligned-allocation>")
|
||||||
|
endif()
|
||||||
|
set(SFIZZ_IMPLEMENT_CXX17_ALIGNED_NEW_SUPPORT TRUE)
|
||||||
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Do not define macros `min` and `max`
|
# Do not define macros `min` and `max`
|
||||||
|
|
|
||||||
|
|
@ -193,7 +193,8 @@ set(SFIZZ_SOURCES
|
||||||
sfizz/effects/impl/ResonantStringAVX.cpp
|
sfizz/effects/impl/ResonantStringAVX.cpp
|
||||||
sfizz/effects/impl/ResonantArray.cpp
|
sfizz/effects/impl/ResonantArray.cpp
|
||||||
sfizz/effects/impl/ResonantArraySSE.cpp
|
sfizz/effects/impl/ResonantArraySSE.cpp
|
||||||
sfizz/effects/impl/ResonantArrayAVX.cpp)
|
sfizz/effects/impl/ResonantArrayAVX.cpp
|
||||||
|
sfizz/utility/c++17/AlignedMemorySupport.cpp)
|
||||||
|
|
||||||
include(SfizzSIMDSourceFiles)
|
include(SfizzSIMDSourceFiles)
|
||||||
sfizz_add_simd_sources(SFIZZ_SOURCES ".")
|
sfizz_add_simd_sources(SFIZZ_SOURCES ".")
|
||||||
|
|
@ -303,6 +304,9 @@ endif()
|
||||||
if(SFIZZ_RELEASE_ASSERTS)
|
if(SFIZZ_RELEASE_ASSERTS)
|
||||||
target_compile_definitions(sfizz_internal PUBLIC "SFIZZ_ENABLE_RELEASE_ASSERT=1")
|
target_compile_definitions(sfizz_internal PUBLIC "SFIZZ_ENABLE_RELEASE_ASSERT=1")
|
||||||
endif()
|
endif()
|
||||||
|
if(SFIZZ_IMPLEMENT_CXX17_ALIGNED_NEW_SUPPORT)
|
||||||
|
target_compile_definitions(sfizz_internal PRIVATE "SFIZZ_IMPLEMENT_CXX17_ALIGNED_NEW_SUPPORT=1")
|
||||||
|
endif()
|
||||||
sfizz_enable_fast_math(sfizz_internal)
|
sfizz_enable_fast_math(sfizz_internal)
|
||||||
|
|
||||||
# Check that sfizz and cmake-side definitions are matching
|
# Check that sfizz and cmake-side definitions are matching
|
||||||
|
|
|
||||||
74
src/sfizz/utility/c++17/AlignedMemorySupport.cpp
Normal file
74
src/sfizz/utility/c++17/AlignedMemorySupport.cpp
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
// SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
|
||||||
|
// This code is part of the sfizz library and is licensed under a BSD 2-clause
|
||||||
|
// license. You should have receive a LICENSE.md file along with the code.
|
||||||
|
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
|
||||||
|
|
||||||
|
#if defined(SFIZZ_IMPLEMENT_CXX17_ALIGNED_NEW_SUPPORT)
|
||||||
|
#include <new>
|
||||||
|
#if defined(_WIN32)
|
||||||
|
# include <malloc.h>
|
||||||
|
#else
|
||||||
|
# include <stdlib.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void* operator new(std::size_t count, std::align_val_t al, const std::nothrow_t&) noexcept
|
||||||
|
{
|
||||||
|
void *ptr;
|
||||||
|
#if defined(_WIN32)
|
||||||
|
ptr = ::_aligned_malloc(count, static_cast<std::size_t>(al));
|
||||||
|
#else
|
||||||
|
if (::posix_memalign(&ptr, static_cast<std::size_t>(al), count) != 0)
|
||||||
|
ptr = nullptr;
|
||||||
|
#endif
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator delete(void* ptr, std::align_val_t, const std::nothrow_t&) noexcept
|
||||||
|
{
|
||||||
|
#if defined(_WIN32)
|
||||||
|
::_aligned_free(ptr);
|
||||||
|
#else
|
||||||
|
::free(ptr);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void* operator new(std::size_t count, std::align_val_t al)
|
||||||
|
{
|
||||||
|
void *ptr = operator new(count, al, std::nothrow);
|
||||||
|
if (!ptr)
|
||||||
|
throw std::bad_alloc();
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator delete(void* ptr, std::align_val_t al) noexcept
|
||||||
|
{
|
||||||
|
operator delete(ptr, al, std::nothrow);
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void* operator new[](std::size_t count, std::align_val_t al)
|
||||||
|
{
|
||||||
|
return operator new(count, al);
|
||||||
|
}
|
||||||
|
|
||||||
|
void* operator new[](std::size_t count, std::align_val_t al, const std::nothrow_t& tag) noexcept
|
||||||
|
{
|
||||||
|
return operator new(count, al, tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator delete[](void* ptr, std::align_val_t al) noexcept
|
||||||
|
{
|
||||||
|
operator delete(ptr, al);
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator delete[](void* ptr, std::align_val_t al, const std::nothrow_t& tag) noexcept
|
||||||
|
{
|
||||||
|
operator delete(ptr, al, tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif // defined(SFIZZ_IMPLEMENT_CXX17_ALIGNED_NEW_SUPPORT)
|
||||||
Loading…
Add table
Reference in a new issue