Index: src/Common/Heap.cc
===================================================================
--- src/Common/Heap.cc	(revision 4ea632eaf73e03a88c47bc73708a1b47cf292ed0)
+++ src/Common/Heap.cc	(revision 43c461da57a6b7e67b030127cc6b531975a67091)
@@ -7,9 +7,9 @@
 // Heap.cc --
 //
-// Author           : Peter A. Buhr
-// Created On       :
+// Author           : Thierry Delisle
+// Created On       : Thu May  3 16:16:10 2018
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Thu May  3 16:16:10 2018
-// Update Count     : 2
+// Last Modified On : Fri May  4 17:27:31 2018
+// Update Count     : 28
 //
 
@@ -21,82 +21,158 @@
 #include <iostream>
 
+//#define WITH_HEAP_STATISTICS
+
 namespace HeapStats {
-	#if !defined( WITH_HEAP_STATISTICS )
-		void newPass( const char * const ) {}
+#if !defined( WITH_HEAP_STATISTICS )
+	void newPass( const char * const ) {}
 
-		void printStats() {}
-	#else
-		struct StatBlock {
-			const char * name = nullptr;
-			size_t mallocs    = 0;
-			size_t frees      = 0;
-		};
+	void printStats() {}
+#else
+	struct StatBlock {
+		const char * name = nullptr;
+		size_t mallocs    = 0;
+		size_t frees      = 0;
+	};
 
-		StatBlock    passes[100];
-		const size_t passes_size = sizeof(passes) / sizeof(passes[0]);
-		size_t       passes_cnt = 0;
+	StatBlock    passes[100] = {{ "Pre-Parse", 0, 0 }};
+	const size_t passes_size = sizeof(passes) / sizeof(passes[0]);
+	size_t       passes_cnt = 1;
 
-		void newPass( const char * const name ) {
-			passes[passes_cnt].name    = name;
-			passes[passes_cnt].mallocs = 0;
-			passes[passes_cnt].frees   = 0;
-			passes_cnt++;
+	void newPass( const char * const name ) {
+		passes[passes_cnt].name    = name;
+		passes[passes_cnt].mallocs = 0;
+		passes[passes_cnt].frees   = 0;
+		passes_cnt++;
 
-			assertf(passes_cnt < passes_size, "Too many passes for HeapStats, increase the size of the array in Heap.h");
+		assertf(passes_cnt < passes_size, "Too many passes for HeapStats, increase the size of the array in Heap.h");
+	}
+
+	void print(size_t value, size_t total) {
+		std::cerr << std::setw(12) << value;
+		std::cerr << "(" << std::setw(3);
+		std::cerr << (value == 0 ? 0 : value * 100 / total);
+		std::cerr << "%) | ";
+	}
+
+	void print(const StatBlock& stat, size_t nc, size_t total_mallocs, size_t total_frees) {
+		std::cerr << std::setw(nc) << stat.name;
+		std::cerr << " | ";
+
+		print(stat.mallocs, total_mallocs);
+		print(stat.frees  , total_frees  );
+		std::cerr << "\n";
+	}
+
+	void print(char c, size_t nc) {
+		for(size_t i = 0; i < nc; i++) {
+			std::cerr << c;
+		}
+		std::cerr << '\n';
+	}
+
+	void printStats() {
+		size_t nc = 0;
+		size_t total_mallocs = 0;
+		size_t total_frees   = 0;
+		for(size_t i = 0; i < passes_cnt; i++) {
+			nc = std::max(nc, std::strlen(passes[i].name));
+			total_mallocs += passes[i].mallocs;
+			total_frees   += passes[i].frees;
+		}
+		size_t nct = nc + 44;
+
+		const char * const title = "Heap Usage Statistic";
+		print('=', nct);
+		for(size_t i = 0; i < (nct - std::strlen(title)) / 2; i++) std::cerr << ' ';
+		std::cerr << title << std::endl;
+		print('-', nct);
+		std::cerr << std::setw(nc) << "Pass";
+		std::cerr << " |       Malloc Count |         Free Count |" << std::endl;
+
+		print('-', nct);
+		for(size_t i = 0; i < passes_cnt; i++) {
+			print(passes[i], nc, total_mallocs, total_frees);
+		}
+		print('-', nct);
+		print({"Sum", total_mallocs, total_frees}, nc, total_mallocs, total_frees);
+
+	}
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+	extern "C" {
+#include <dlfcn.h>
+#include <execinfo.h>
+	}
+
+//=============================================================================================
+// Interposing helpers
+//=============================================================================================
+
+	typedef void (* generic_fptr_t)(void);
+	generic_fptr_t interpose_symbol( const char * symbol, const char * version ) {
+		const char * error;
+
+		static void * library;
+		if ( ! library ) {
+#if defined( RTLD_NEXT )
+			library = RTLD_NEXT;
+#else
+			// missing RTLD_NEXT => must hard-code library name, assuming libstdc++
+			library = dlopen( "libc.so.6", RTLD_LAZY );
+			error = dlerror();
+			if ( error ) {
+				std::cerr << "interpose_symbol : failed to open libc, " << error << std::endl;
+				abort();
+			}
+#endif
+		} // if
+
+		generic_fptr_t fptr;
+
+#if defined( _GNU_SOURCE )
+		if ( version ) {
+			fptr = (generic_fptr_t)dlvsym( library, symbol, version );
+		} else {
+			fptr = (generic_fptr_t)dlsym( library, symbol );
+		}
+#else
+		fptr = (generic_fptr_t)dlsym( library, symbol );
+#endif // _GNU_SOURCE
+
+	extern "C" {
+		void * malloc( size_t size ) __attribute__((malloc));
+		void * malloc( size_t size ) {
+			static auto __malloc = reinterpret_cast<void * (*)(size_t)>(interpose_symbol( "malloc", nullptr ));
+			if( passes_cnt > 0 ) passes[passes_cnt - 1].mallocs++;
+			return __malloc( size );
 		}
 
-		void print(size_t value, size_t total) {
-			std::cerr << std::setw(12) << value;
-			std::cerr << "(" << std::setw(3);
-			std::cerr << (value == 0 ? 0 : value * 100 / total);
-			std::cerr << "%) | ";
+		void free( void * ptr ) {
+			static auto __free = reinterpret_cast<void   (*)(void *)>(interpose_symbol( "free", nullptr ));
+			if( passes_cnt > 0 ) passes[passes_cnt - 1].frees++;
+			return __free( ptr );
 		}
 
-		void print(const StatBlock& stat, size_t nc, size_t total_mallocs, size_t total_frees) {
-			std::cerr << std::setw(nc) << stat.name;
-			std::cerr << " | ";
-
-			print(stat.mallocs, total_mallocs);
-			print(stat.frees  , total_frees  );
-			std::cerr << "\n";
+		void * calloc( size_t nelem, size_t size ) {
+			static auto __calloc = reinterpret_cast<void * (*)(size_t, size_t)>(interpose_symbol( "calloc", nullptr ));
+			if( passes_cnt > 0 ) passes[passes_cnt - 1].mallocs++;
+			return __calloc( nelem, size );
 		}
 
-		void print(char c, size_t nc) {
-			for(size_t i = 0; i < nc; i++) {
-				std::cerr << c;
-			}
-			std::cerr << '\n';
+		void * realloc( void * ptr, size_t size ) {
+			static auto __realloc = reinterpret_cast<void * (*)(void *, size_t)>(interpose_symbol( "realloc", nullptr ));
+			void * s = __realloc( ptr, size );
+			if ( s != ptr && passes_cnt > 0 ) {			// did realloc get new storage ?
+				passes[passes_cnt - 1].mallocs++;
+				passes[passes_cnt - 1].frees++;
+			} // if
+			return s;
 		}
-
-		void printStats() {
-			size_t nc = 0;
-			size_t total_mallocs = 0;
-			size_t total_frees   = 0;
-			for(size_t i = 0; i < passes_cnt; i++) {
-				nc = std::max(nc, std::strlen(passes[i].name));
-				total_mallocs += passes[i].mallocs;
-				total_frees   += passes[i].frees;
-			}
-			size_t nct = nc + 44;
-
-			const char * const title = "Heap Usage Statistic";
-			print('=', nct);
-			for(size_t i = 0; i < (nct - std::strlen(title)) / 2; i++) std::cerr << ' ';
-			std::cerr << title << std::endl;
-
-			print('-', nct);
-			std::cerr << std::setw(nc) << "Pass";
-			std::cerr << " |       Malloc Count |         Free Count |" << std::endl;
-
-			print('-', nct);
-			for(size_t i = 0; i < passes_cnt; i++) {
-				print(passes[i], nc, total_mallocs, total_frees);
-			}
-
-			print('-', nct);
-			print({"Sum", total_mallocs, total_frees}, nc, total_mallocs, total_frees);
-
-		}
-	#endif
+	}
+#endif
 }
-
Index: src/Common/Heap.h
===================================================================
--- src/Common/Heap.h	(revision 4ea632eaf73e03a88c47bc73708a1b47cf292ed0)
+++ src/Common/Heap.h	(revision 43c461da57a6b7e67b030127cc6b531975a67091)
@@ -7,9 +7,9 @@
 // Heap.h --
 //
-// Author           : Peter A. Buhr
-// Created On       :
+// Author           : Thierry Delisle
+// Created On       : Thu May  3 16:16:10 2018
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Thu May  3 16:16:10 2018
-// Update Count     : 2
+// Last Modified On : Fri May  4 14:34:08 2018
+// Update Count     : 3
 //
 
Index: src/libcfa/interpose.c
===================================================================
--- src/libcfa/interpose.c	(revision 4ea632eaf73e03a88c47bc73708a1b47cf292ed0)
+++ src/libcfa/interpose.c	(revision 43c461da57a6b7e67b030127cc6b531975a67091)
@@ -10,19 +10,18 @@
 // Created On       : Wed Mar 29 16:10:31 2017
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Tue May  1 15:05:35 2018
-// Update Count     : 83
+// Last Modified On : Fri May  4 14:35:13 2018
+// Update Count     : 97
 //
 
 #include <stdarg.h>
 #include <stddef.h>
-
-extern "C" {
 #include <stdio.h>
 #include <string.h>
-#include <dlfcn.h>
 #include <unistd.h>
 #define __USE_GNU
 #include <signal.h>
 #undef __USE_GNU
+extern "C" {
+#include <dlfcn.h>
 #include <execinfo.h>
 }
@@ -37,6 +36,6 @@
 //=============================================================================================
 
-typedef void (*generic_fptr_t)(void);
-generic_fptr_t interpose_symbol( const char* symbol, const char *version ) {
+typedef void (* generic_fptr_t)(void);
+generic_fptr_t interpose_symbol( const char * symbol, const char * version ) {
 	const char * error;
 
@@ -55,5 +54,5 @@
 	} // if
 
-	union { generic_fptr_t fptr; void* ptr; } originalFunc;
+	union { generic_fptr_t fptr; void * ptr; } originalFunc;
 
 	#if defined( _GNU_SOURCE )
@@ -73,20 +72,8 @@
 }
 
-forall(dtype T)
-static inline void ptr_from_symbol( T** symbol_ptr, const char * symbol_name, const char * version) {
-	union {
-		generic_fptr_t gp;
-		T* tp;
-	} u;
-
-	u.gp = interpose_symbol( symbol_name, version );
-
-	*symbol_ptr = u.tp;
-}
-
-#define INTERPOSE_LIBC( x, ver ) ptr_from_symbol( (void**)&__cabi_libc.x, #x, ver)
-
-//=============================================================================================
-// Terminating Signals logic
+#define INTERPOSE_LIBC( x, ver ) __cabi_libc.x = (typeof(__cabi_libc.x))interpose_symbol( #x, ver )
+
+//=============================================================================================
+// Interposition Startup logic
 //=============================================================================================
 
