Debug swcsa
[SXSI/TextCollection.git] / HeapProfiler.h
1 /***************************************************************************
2  *   Installs handlers for malloc(), realloc() and free()                  *
3  *                                                                         *
4  *   Copyright (C) 2006 by Niko Välimäki                                   *
5  *                                                                         *
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  *   This program is distributed in the hope that it will be useful,       *
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15  *   GNU General Public License for more details.                          *
16  *                                                                         *
17  *   You should have received a copy of the GNU General Public License     *
18  *   along with this program; if not, write to the                         *
19  *   Free Software Foundation, Inc.,                                       *
20  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
21  ***************************************************************************/
22
23 #ifndef _HEAPPROFILER_H_
24 #define _HEAPPROFILER_H_
25
26 #include <map>
27 #include <iostream>
28 #include <malloc.h>
29
30 //#define DEBUG_HEAPPROFILER
31
32 typedef std::map<void *, unsigned long> AllocatedBlocks;
33
34 class HeapProfiler
35 {
36 public:
37     static unsigned long GetMaxHeapConsumption();
38     static void ResetMaxHeapConsumption();
39     static unsigned long GetHeapConsumption();
40
41     // Prototypes for hooks
42     static void InitHooks(void);
43     static void *MallocHook(size_t, const void *);
44     static void FreeHook(void *, const void *);
45     static void *ReallocHook(void *, size_t, const void *);
46
47 private:
48     static unsigned long consumption, maxConsumption;
49     // Variables to save original hooks.
50     static void *(*old_malloc_hook)(size_t, const void *);
51     static void (*old_free_hook)(void *, const void *);
52     static void *(*old_realloc_hook)(void *, size_t, const void *);
53     static AllocatedBlocks blocks;
54 };
55
56
57 #endif