Increase portability to systems that don't have cfree available.
authorMilian Wolff <mail@milianw.de>
Wed, 8 Jun 2016 12:48:59 +0000 (14:48 +0200)
committerMilian Wolff <mail@milianw.de>
Wed, 8 Jun 2016 12:48:59 +0000 (14:48 +0200)
cfree should not be used, according to its man page, but if it's
being used then we need to overload it. On systems where it is not
available, simply skip this step to keep heaptrack compiling.

heaptrack_inject.cpp
heaptrack_preload.cpp

index 4a55001..e5d5693 100644 (file)
@@ -41,6 +41,9 @@
 #error unsupported word size
 #endif
 
+#define HAVE_ALIGNED_ALLOC defined(_ISOC11_SOURCE)
+#define HAVE_CFREE (defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || defined(__USE_MISC))
+
 namespace {
 
 void overwrite_symbols() noexcept;
@@ -98,6 +101,7 @@ struct calloc
     }
 };
 
+#if HAVE_CFREE
 struct cfree
 {
     static constexpr auto name = "cfree";
@@ -109,6 +113,7 @@ struct cfree
         original(ptr);
     }
 };
+#endif
 
 struct dlopen
 {
@@ -178,7 +183,9 @@ constexpr hook list[] = {
     hook::wrap<free>(),
     hook::wrap<realloc>(),
     hook::wrap<calloc>(),
+#if HAVE_CFREE
     hook::wrap<cfree>(),
+#endif
     hook::wrap<posix_memalign>(),
     hook::wrap<dlopen>(),
     hook::wrap<dlclose>(),
index 125fef9..443e17a 100644 (file)
@@ -30,6 +30,7 @@
 using namespace std;
 
 #define HAVE_ALIGNED_ALLOC defined(_ISOC11_SOURCE)
+#define HAVE_CFREE (defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || defined(__USE_MISC))
 
 namespace {
 
@@ -69,7 +70,9 @@ struct hook
 HOOK(malloc);
 HOOK(free);
 HOOK(calloc);
+#if HAVE_CFREE
 HOOK(cfree);
+#endif
 HOOK(realloc);
 HOOK(posix_memalign);
 HOOK(valloc);
@@ -111,7 +114,9 @@ void init()
         hooks::malloc.init();
         hooks::free.init();
         hooks::calloc.init();
+#if HAVE_CFREE
         hooks::cfree.init();
+#endif
         hooks::realloc.init();
         hooks::posix_memalign.init();
         hooks::valloc.init();
@@ -188,6 +193,7 @@ void* calloc(size_t num, size_t size) noexcept
     return ret;
 }
 
+#if HAVE_CFREE
 void cfree(void* ptr) noexcept
 {
     if (!hooks::cfree) {
@@ -203,6 +209,7 @@ void cfree(void* ptr) noexcept
 
     hooks::cfree(ptr);
 }
+#endif
 
 int posix_memalign(void **memptr, size_t alignment, size_t size) noexcept
 {