diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,6 +73,12 @@ message(FATAL_ERROR "You are missing some Linux headers required to compile heaptrack.") endif() +include(CheckSymbolExists) +check_symbol_exists(cfree malloc.h HAVE_CFREE) +if (${HAVE_CFREE}) + add_definitions(-DHAVE_CFREE) +endif() + set(BIN_INSTALL_DIR "bin") set(LIB_SUFFIX "" CACHE STRING "Define suffix of directory name (32/64)") set(LIB_INSTALL_DIR "lib${LIB_SUFFIX}") diff --git a/src/track/heaptrack_inject.cpp b/src/track/heaptrack_inject.cpp --- a/src/track/heaptrack_inject.cpp +++ b/src/track/heaptrack_inject.cpp @@ -43,12 +43,6 @@ #error unsupported word size #endif -#if defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || defined(__USE_MISC) -#define HAVE_CFREE 1 -#else -#define HAVE_CFREE 0 -#endif - namespace { namespace Elf { @@ -115,7 +109,7 @@ } }; -#if HAVE_CFREE +#if defined(HAVE_CFREE) struct cfree { static constexpr auto name = "cfree"; @@ -208,7 +202,7 @@ // TODO: use std::apply once we can rely on C++17 hook(symname, addr, restore) || hook(symname, addr, restore) || hook(symname, addr, restore) || hook(symname, addr, restore) -#if HAVE_CFREE +#if defined(HAVE_CFREE) || hook(symname, addr, restore) #endif || hook(symname, addr, restore) || hook(symname, addr, restore) diff --git a/src/track/heaptrack_preload.cpp b/src/track/heaptrack_preload.cpp --- a/src/track/heaptrack_preload.cpp +++ b/src/track/heaptrack_preload.cpp @@ -34,11 +34,6 @@ #else # define HAVE_ALIGNED_ALLOC 0 #endif -#if defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || defined(__USE_MISC) -# define HAVE_CFREE 1 -#else -# define HAVE_CFREE 0 -#endif namespace { @@ -80,7 +75,7 @@ HOOK(malloc); HOOK(free); HOOK(calloc); -#if HAVE_CFREE +#if defined(HAVE_CFREE) HOOK(cfree); #endif HOOK(realloc); @@ -129,7 +124,7 @@ hooks::malloc.init(); hooks::free.init(); hooks::calloc.init(); -#if HAVE_CFREE +#if defined(HAVE_CFREE) hooks::cfree.init(); #endif hooks::realloc.init(); @@ -207,7 +202,7 @@ return ret; } -#if HAVE_CFREE +#if defined(HAVE_CFREE) void cfree(void* ptr) noexcept { if (!hooks::cfree) { diff --git a/tests/manual/test.cpp b/tests/manual/test.cpp --- a/tests/manual/test.cpp +++ b/tests/manual/test.cpp @@ -64,7 +64,11 @@ buf = calloc(5, 5); printf("calloc: %p\n", buf); +#if defined(HAVE_CFREE) cfree(buf); +#else + free(buf); +#endif #if HAVE_ALIGNED_ALLOC buf = aligned_alloc(16, 160);