From 38a663163dc778ad8b4ea682ba965a90472347db Mon Sep 17 00:00:00 2001 From: Rafael Sadowski Date: Sat, 26 Oct 2019 22:50:49 +0200 Subject: [PATCH] Check include malloc.h and malloc_usable_size(3) One some operating systems malloc(3) is not in malloc.h nor in malloc_np.h, instead it is in stdlib.h. In addition, not all systems support malloc_usable_size(3). You could argue it's not safe. This patch tries to be portable and it fix the build on OpenBSD. --- CMakeLists.txt | 8 ++++++++ src/core/Alloc.cpp | 6 ++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 22646098..2c79261f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -438,6 +438,14 @@ if(UNIX) int main() { prctl(PR_SET_DUMPABLE, 0); return 0; }" HAVE_PR_SET_DUMPABLE) + check_cxx_source_compiles("#include + int main() { return 0; }" + HAVE_MALLOC_H) + + check_cxx_source_compiles("#include + int main() { malloc_usable_size(NULL, 0); return 0; }" + HAVE_MALLOC_USABLE_SIZE) + check_cxx_source_compiles("#include int main() { struct rlimit limit; diff --git a/src/core/Alloc.cpp b/src/core/Alloc.cpp index 967b4e3e..625386a3 100644 --- a/src/core/Alloc.cpp +++ b/src/core/Alloc.cpp @@ -23,8 +23,10 @@ #include #elif defined(Q_OS_FREEBSD) #include -#else +#elif defined(HAVE_MALLOC_H) #include +#else +#include #endif #if defined(NDEBUG) && !defined(__cpp_sized_deallocation) @@ -64,7 +66,7 @@ void operator delete(void* ptr) noexcept ::operator delete(ptr, _msize(ptr)); #elif defined(Q_OS_MACOS) ::operator delete(ptr, malloc_size(ptr)); -#elif defined(Q_OS_UNIX) +#elif defined(HAVE_MALLOC_USABLE_SIZE) ::operator delete(ptr, malloc_usable_size(ptr)); #else // whatever OS this is, give up and simply free stuff