-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMemWatcher.cpp
119 lines (106 loc) · 3.01 KB
/
CMemWatcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//===- CMemWatcher.cpp - Implementation for Memory Watcher--*- C++ -*------===//
//
// The ArchC Project - Compiler Backend Generation
//
//===----------------------------------------------------------------------===//
//
//
//===----------------------------------------------------------------------===//
#include "CMemWatcher.h"
extern "C" {
#include <malloc.h>
}
using namespace helper;
CMemWatcher* CMemWatcher::pInstance = NULL;
// Singleton instance access point
CMemWatcher* CMemWatcher::Instance() {
if (pInstance == NULL) {
pInstance = new CMemWatcher();
}
return pInstance;
}
void CMemWatcher::Destroy() {
if (pInstance != NULL)
delete pInstance;
}
// Data storage used by hooks
static void** AllPointers;
static unsigned CurSize;
static unsigned CurPointer;
static void (*old_free_hook) (void *__ptr, const void *caller);
static void *(*old_malloc_hook) (size_t __size, const void *caller);
const static unsigned STEP = 10000;
// Hooks implementation
static void my_free_hook (void *ptr, const void *caller);
static void *my_malloc_hook (size_t size, const void *caller) {
void *result;
// Restore old hooks
__free_hook = old_free_hook;
__malloc_hook = old_malloc_hook;
// Call recursively
result = malloc(size);
// Stores the pointer
if (CurPointer == CurSize) {
CurSize += STEP;
AllPointers = (void **) realloc(AllPointers, sizeof(void*)*CurSize);
}
AllPointers[CurPointer++] = result;
// Save underlying hooks
old_free_hook = __free_hook;
old_malloc_hook = __malloc_hook;
// Restores our hooks
__free_hook = my_free_hook;
__malloc_hook = my_malloc_hook;
return result;
}
static void my_free_hook (void *ptr, const void *caller) {
// Restore old hooks
__free_hook = old_free_hook;
__malloc_hook = old_malloc_hook;
// Call recursively
free(ptr);
// Save underlying hooks
old_free_hook = __free_hook;
old_malloc_hook = __malloc_hook;
// Removes this pointer from our tracking
for (unsigned i = 0; i < CurPointer; ++i) {
if (AllPointers[i] == ptr) {
for (unsigned i2 = i; i2 < CurPointer-1; ++i2) {
AllPointers[i2] = AllPointers[i2+1];
}
--CurPointer;
break;
}
}
// Restores our hooks
__free_hook = my_free_hook;
__malloc_hook = my_malloc_hook;
return;
}
// CMemWatcher member functions implementation
void CMemWatcher::InstallHooks() {
AllPointers = (void **)malloc(sizeof(void*)*STEP);
CurSize = STEP;
CurPointer = 0;
old_free_hook = __free_hook;
old_malloc_hook = __malloc_hook;
__free_hook = my_free_hook;
__malloc_hook = my_malloc_hook;
}
void CMemWatcher::ReportStatistics(std::ostream &S) {
S << "Total pointers to allocated regions: " << CurPointer
<< "\n";
}
void CMemWatcher::UninstallHooks() {
// Restore old hooks (uninstall)
__free_hook = old_free_hook;
__malloc_hook = old_malloc_hook;
}
void CMemWatcher::FreeAll() {
// Deallocates everything
for (unsigned i = 0; i < CurPointer; ++i) {
free(AllPointers[i]);
}
// Deallocated our data structures
free(AllPointers);
}