Modernise and tidy the CMake a bit: - Use generator expressions for a lot of conditionals so that things are more reliable with multi-config generators (and less verbose). - Remove C as a needed language. None of the code was C but we were using C to test if headers worked. This was fragile because a build with `CMAKE_CXX_COMPILER` set might have checked things compiled with the system C compiler and then failed when the specified C++ compiler used different headers. - Rename the `BACKTRACE_HEADER` macro to `SNMALLOC_BACKTRACE_HEADER`. This is exposed into code that consumes snmalloc and so should be 'namespaced' (to the degree that's possible with C macros). - Clean up the options and use dependent options to hide options that are not always relevant. - Use functions instead of macros for better variable scoping. - Factor out some duplicated bits into functions. - Update to the latest way of telling CMake to use C++17 or C++20. - Migrate everything that's setting global properties to setting only per-target properties. - Link with -nostdlib++ if it's available. If it isn't, fall back to enabling the C language and linking with the C compiler. - Make the per-test log messages verbose outputs. These kept scrolling important messages off the top of the screen for me. - Make building as a header-only library a public option. - Add install targets that install all of the headers and provide a config option. This works with the header-only configuration for integration with things like vcpkg. - Fix a missing `#endif` in the `malloc_useable_size` check. This was failing co compile on all platforms because of the missing `#endif`. - Bump the minimum version to 3.14 so that we have access to target_link_options. This is necessary to use generator expressions for linker flags. - Make the linker error if the shim libraries depend on symbols that are not defined in the explicitly-provided libraries. - Make the old-Ubuntu CI jobs use C++17 explicitly (previously CMake was silently ignoring the fact that the compiler didn't support C++20) - Fix errors found by the more aggressive linking mode. With these changes, it's now possible to install snmalloc and then, in another project, do something like this: ```cmake find_package(snmalloc CONFIG REQUIRED) target_link_libraries(t1 snmalloc::snmalloc) target_link_libraries(t2 snmalloc::snmallocshim-static) ``` In this example, `t1` gets all of the compile flags necessary to include snmalloc headers for its build configuration. `t2` is additionally linked to the snmalloc static shim library.
74 lines
1.3 KiB
C++
74 lines
1.3 KiB
C++
#include "malloc.cc"
|
|
|
|
#ifdef _WIN32
|
|
# ifdef __clang__
|
|
# define EXCEPTSPEC noexcept
|
|
# else
|
|
# define EXCEPTSPEC
|
|
# endif
|
|
#else
|
|
# ifdef _GLIBCXX_USE_NOEXCEPT
|
|
# define EXCEPTSPEC _GLIBCXX_USE_NOEXCEPT
|
|
# elif defined(_NOEXCEPT)
|
|
# define EXCEPTSPEC _NOEXCEPT
|
|
# else
|
|
# define EXCEPTSPEC
|
|
# endif
|
|
#endif
|
|
|
|
using namespace snmalloc;
|
|
|
|
void* operator new(size_t size)
|
|
{
|
|
return ThreadAlloc::get().alloc(size);
|
|
}
|
|
|
|
void* operator new[](size_t size)
|
|
{
|
|
return ThreadAlloc::get().alloc(size);
|
|
}
|
|
|
|
void* operator new(size_t size, std::nothrow_t&)
|
|
{
|
|
return ThreadAlloc::get().alloc(size);
|
|
}
|
|
|
|
void* operator new[](size_t size, std::nothrow_t&)
|
|
{
|
|
return ThreadAlloc::get().alloc(size);
|
|
}
|
|
|
|
void operator delete(void* p)EXCEPTSPEC
|
|
{
|
|
ThreadAlloc::get().dealloc(p);
|
|
}
|
|
|
|
void operator delete(void* p, size_t size)EXCEPTSPEC
|
|
{
|
|
if (p == nullptr)
|
|
return;
|
|
ThreadAlloc::get().dealloc(p, size);
|
|
}
|
|
|
|
void operator delete(void* p, std::nothrow_t&)
|
|
{
|
|
ThreadAlloc::get().dealloc(p);
|
|
}
|
|
|
|
void operator delete[](void* p) EXCEPTSPEC
|
|
{
|
|
ThreadAlloc::get().dealloc(p);
|
|
}
|
|
|
|
void operator delete[](void* p, size_t size) EXCEPTSPEC
|
|
{
|
|
if (p == nullptr)
|
|
return;
|
|
ThreadAlloc::get().dealloc(p, size);
|
|
}
|
|
|
|
void operator delete[](void* p, std::nothrow_t&)
|
|
{
|
|
ThreadAlloc::get().dealloc(p);
|
|
}
|