RRR coding to BRW
[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 unsigned long GetHeapConsumption();
39
40     // Prototypes for hooks
41     static void InitHooks(void);
42     static void *MallocHook(size_t, const void *);
43     static void FreeHook(void *, const void *);
44     static void *ReallocHook(void *, size_t, const void *);
45
46 private:
47     static unsigned long consumption, maxConsumption;
48     // Variables to save original hooks.
49     static void *(*old_malloc_hook)(size_t, const void *);
50     static void (*old_free_hook)(void *, const void *);
51     static void *(*old_realloc_hook)(void *, size_t, const void *);
52     static AllocatedBlocks blocks;
53 };
54
55
56 #endif