Added the testCase and memory profiler
[SXSI/TextCollection.git] / HeapProfiler.h
diff --git a/HeapProfiler.h b/HeapProfiler.h
new file mode 100644 (file)
index 0000000..e1c0378
--- /dev/null
@@ -0,0 +1,56 @@
+/***************************************************************************
+ *   Installs handlers for malloc(), realloc() and free()                  *
+ *                                                                         *
+ *   Copyright (C) 2006 by Niko Välimäki                                   *
+ *                                                                         *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
+ ***************************************************************************/
+
+#ifndef _HEAPPROFILER_H_
+#define _HEAPPROFILER_H_
+
+#include <map>
+#include <iostream>
+#include <malloc.h>
+
+//#define DEBUG_HEAPPROFILER
+
+typedef std::map<void *, unsigned long> AllocatedBlocks;
+
+class HeapProfiler
+{
+public:
+    static unsigned long GetMaxHeapConsumption();
+    static unsigned long GetHeapConsumption();
+
+    // Prototypes for hooks
+    static void InitHooks(void);
+    static void *MallocHook(size_t, const void *);
+    static void FreeHook(void *, const void *);
+    static void *ReallocHook(void *, size_t, const void *);
+
+private:
+    static unsigned long consumption, maxConsumption;
+    // Variables to save original hooks.
+    static void *(*old_malloc_hook)(size_t, const void *);
+    static void (*old_free_hook)(void *, const void *);
+    static void *(*old_realloc_hook)(void *, size_t, const void *);
+    static AllocatedBlocks blocks;
+};
+
+
+#endif