Skip to content

Commit

Permalink
macOS: Switch to C++17 (like the other platforms) (surge-synthesizer#…
Browse files Browse the repository at this point in the history
…5124)

Library support is spotty on our current deployment target 10.11 "El
Capitan", but a consistent language level across platforms does make
life easier for devs, especially those who aren't on Mac. C++17 also
fixes under-aligned allocations on ARM Macs, potentially improving
performance.

To keep us pre-Mojave compatible, this implements aligned operators
new/delete in a new library "surge-platform" that will also become home
for other platform-specific code currently lurking between #ifdefs.
  • Loading branch information
mvf authored Sep 21, 2021
1 parent f21cfbb commit ffb84c5
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 13 deletions.
7 changes: 2 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ message(STATUS "Targeting ${SURGE_BITNESS} bit configuration")
# }}}
# Global compiler/linker settings {{{
set(CMAKE_CXX_EXTENSIONS OFF)
if(APPLE)
set(CMAKE_CXX_STANDARD 14)
else()
set(CMAKE_CXX_STANDARD 17)
endif()
set(CMAKE_CXX_STANDARD 17)

if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
# Any Clang or any GCC
Expand Down Expand Up @@ -91,6 +87,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
if(CMAKE_CXX_COMPILER_ID MATCHES "^AppleClang$")
# Apple Clang only
add_compile_options(
-faligned-allocation
-fasm-blocks
)
endif()
Expand Down
1 change: 1 addition & 0 deletions libs/catch2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ add_library(${PROJECT_NAME} INTERFACE)
target_sources(${PROJECT_NAME} INTERFACE include/${PROJECT_NAME}/catch2.hpp)
add_library(surge::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
target_include_directories(${PROJECT_NAME} INTERFACE include)
target_compile_definitions(${PROJECT_NAME} INTERFACE $<$<BOOL:${APPLE}>:CATCH_CONFIG_NO_CPP17_UNCAUGHT_EXCEPTIONS>)
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ endif()
message(STATUS "Building Surge JUCE as ${SURGE_JUCE_FORMATS}")

add_subdirectory(common)
add_subdirectory(platform)

if(SURGE_BUILD_TESTRUNNER)
add_subdirectory(surge-testrunner)
Expand Down
1 change: 1 addition & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC
surge::tinyxml
surge-common-binary
tuning-library
PRIVATE surge-platform
)
target_include_directories(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
Expand Down
4 changes: 0 additions & 4 deletions src/common/SurgeStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@
#include "MSEGModulationHelper.h"
// FIXME

#if __cplusplus < 201703L
constexpr float MSEGStorage::minimumDuration;
#endif

float sinctable alignas(16)[(FIRipol_M + 1) * FIRipol_N * 2];
float sinctable1X alignas(16)[(FIRipol_M + 1) * FIRipol_N];
short sinctableI16 alignas(16)[(FIRipol_M + 1) * FIRipolI16_N];
Expand Down
4 changes: 0 additions & 4 deletions src/common/dsp/vembertech/basic_dsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@

template <typename T> inline T limit_range(const T &x, const T &low, const T &high)
{
#if __cplusplus > 201402L
return std::clamp(x, low, high);
#else
return std::min(std::max(x, low), high);
#endif
}

template <typename T> inline T limit01(const T &x) { return limit_range(x, (T)0, (T)1); }
Expand Down
10 changes: 10 additions & 0 deletions src/platform/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# vi:set sw=2 et:
project(surge-platform)

if(APPLE)
add_library(${PROJECT_NAME})
target_sources(${PROJECT_NAME} PRIVATE macos/cpp17-aligned-new.cpp)
else()
# no sources yet on other platforms
add_library(${PROJECT_NAME} INTERFACE)
endif()
35 changes: 35 additions & 0 deletions src/platform/macos/cpp17-aligned-new.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
** Surge Synthesizer is Free and Open Source Software
**
** Surge is made available under the Gnu General Public License, v3.0
** https://www.gnu.org/licenses/gpl-3.0.en.html
**
** Copyright 2004-2021 by various individuals as described by the Git transaction log
**
** All source at: https://github.com/surge-synthesizer/surge.git
**
** Surge was a commercial product from 2004-2018, with Copyright and ownership
** in that period held by Claes Johanson at Vember Audio. Claes made Surge
** open source in September 2018.
*/

#include <cstdlib>
#include <new>

/*
* Since C++17, new'ing objects respects their alignas() requirements. If the required alignment
* exceeds that provided by operator new, alignment-aware overloads are called instead. On macOS,
* these are only present on 10.14 "Mojave" and up, so we ship our own implementation.
*
* Relevant discussion in GitHub issues #171, #188, #3589
*/

void *operator new(std::size_t size, std::align_val_t align)
{
void *result;
if (posix_memalign(&result, std::size_t(align), size))
throw std::bad_alloc{};
return result;
}

void operator delete(void *p, std::align_val_t) noexcept { free(p); }

0 comments on commit ffb84c5

Please sign in to comment.