-
Notifications
You must be signed in to change notification settings - Fork 5
/
ManagedHeap.cpp
318 lines (253 loc) · 7.44 KB
/
ManagedHeap.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include "ManagedHeap.hpp"
#ifdef ___INANITY_TRACE_HEAP
#include "CriticalCode.hpp"
#include "Log.hpp"
#endif
#include <map>
#include <sstream>
#include <algorithm>
#ifdef ___INANITY_PLATFORM_EMSCRIPTEN
#include <emscripten/emscripten.h>
#endif
BEGIN_INANITY
#ifdef ___INANITY_TRACE_PTR
/// Выключить трассировку указателей?
/** Она очень замедляет работу в debug. */
static const bool disableTracePtr = true;
#endif
ManagedHeap managedHeap;
#ifdef ___INANITY_TRACE_HEAP
ManagedHeap::AllocationInfo::AllocationInfo(size_t number, size_t size) : number(number), size(size)
{
}
#endif
ManagedHeap::ManagedHeap()
#ifdef ___INANITY_TRACE_HEAP
: totalAllocationsCount(0), totalAllocationsSize(0)
#endif
{
#ifdef ___INANITY_PLATFORM_WINDOWS
// создать кучу (растущую, с нулевым начальным размером)
heap = HeapCreate(0, 0, 0);
if(!heap)
exit(1);
// сделать кучу low-fragmentation
ULONG heapFragValue = 2;
// обработка ошибок не производится, чтобы работало на не-XP и не-Vista
HeapSetInformation(heap, HeapCompatibilityInformation, &heapFragValue, sizeof(heapFragValue));
#endif // ___INANITY_PLATFORM_WINDOWS
}
ManagedHeap::~ManagedHeap()
{
#ifdef ___INANITY_PLATFORM_WINDOWS
// free all heap's memory
HeapDestroy(heap);
#endif
#ifdef ___INANITY_TRACE_HEAP
// create memory report
Log::Message("======= INANITY MANAGED HEAP REPORT =======");
Log::Message("allocations: ", totalAllocationsCount, ", total size: ", totalAllocationsSize);
if(allocations.size())
{
std::ostringstream stream;
PrintAllocations(stream);
Log::Warning("ATTENTION! SOME LEAKS DETECTED!\n", stream.str());
}
else
Log::Message("NO LEAKS DETECTED");
#ifdef ___INANITY_TRACE_PTR
if(!disableTracePtr)
{
std::ostringstream stream;
PrintPtrs(stream);
Log::Message("======= TRACE PTR REPORT =======\n", stream.str());
}
#endif
Log::Message("======= END REPORT =======");
#endif
}
void* ManagedHeap::Allocate(size_t size)
{
#ifdef ___INANITY_PLATFORM_WINDOWS
void* data = HeapAlloc(heap, 0, size);
if(!data)
ExitProcess(1);
#else
void* data = malloc(size);
#endif
#ifdef ___INANITY_TRACE_HEAP
{
CriticalCode code(criticalSection);
allocations.insert(std::make_pair(data, AllocationInfo(totalAllocationsCount++, size)));
totalAllocationsSize += size;
}
#endif
return data;
}
void ManagedHeap::Free(void *data)
{
// освободить память
#ifdef ___INANITY_PLATFORM_WINDOWS
if(!HeapFree(heap, 0, data))
ExitProcess(1);
#else
free(data);
#endif
// отметить, что память удалена
#ifdef ___INANITY_TRACE_HEAP
{
CriticalCode code(criticalSection);
allocations.erase(data);
}
#endif
}
#ifdef ___INANITY_TRACE_HEAP
void ManagedHeap::PrintAllocations(std::ostream& stream)
{
CriticalCode code(criticalSection);
for(Allocations::const_iterator i = allocations.begin(); i != allocations.end(); ++i)
{
stream << i->first << " : #" << i->second.number << ", " << i->second.size << " bytes\n";
if(i->second.info)
stream << " " << i->second.info << "\n";
}
}
void ManagedHeap::SetAllocationInfo(void* data, const char* info)
{
CriticalCode code(criticalSection);
Allocations::iterator i = allocations.find(data);
if(i != allocations.end())
i->second.info = info;
}
void ManagedHeapSetAllocationInfo(void* data, const char* info)
{
managedHeap.SetAllocationInfo(data, info);
}
#ifdef ___INANITY_TRACE_PTR
class ManagedHeap::PtrTracer
{
struct Object
{
void* data;
size_t size;
const char* info;
Object(void* data, size_t size, const char* info) : data(data), size(size), info(info) {}
};
struct Sorter
{
bool operator()(const Object& a, const Object& b) const
{
return a.data < b.data;
}
bool operator()(void* a, const Object& b) const
{
return a < b.data;
}
bool operator()(const Object& a, void* b) const
{
return a.data < b;
}
} sorter;
typedef std::vector<Object> Objects;
Objects objects;
typedef std::multimap<Objects::const_iterator, Objects::const_iterator> Links;
Links links;
std::vector<bool> levels;
std::vector<Objects::const_iterator> levelObjects;
public:
PtrTracer(const ManagedHeap& heap)
{
// сформировать список объектов
objects.reserve(heap.allocations.size());
for(Allocations::const_iterator i = heap.allocations.begin(); i != heap.allocations.end(); ++i)
objects.push_back(Object(i->first, i->second.size, i->second.info));
std::sort(objects.begin(), objects.end(), sorter);
// сформировать карту ссылок
for(Ptrs::const_iterator i = heap.ptrs.begin(); i != heap.ptrs.end(); ++i)
{
// найти объект, в котором содержится указатель
Objects::const_iterator j = std::upper_bound(objects.begin(), objects.end(), i->first, sorter);
if(j > objects.begin())
{
--j;
if((size_t)((char*)i->first - (char*)j->data) < j->size)
// да, указатель содержится в этом объекте
// получить объект, на который указывает указатель, и добавить ссылку
links.insert(std::make_pair(j, std::lower_bound(objects.begin(), objects.end(), i->second, sorter)));
}
}
}
void Print(std::ostream& stream, Objects::const_iterator object, bool last = false)
{
for(size_t i = 0; i < levels.size(); ++i)
if(i == levels.size() - 1)
stream << "|_";
else if(levels[i])
stream << "| ";
else
stream << " ";
bool cyclicReference = std::find(levelObjects.begin(), levelObjects.end(), object) != levelObjects.end();
if(cyclicReference)
stream << "CYCLIC ";
stream << object->data << ", " << object->info << "\n";
if(cyclicReference)
return;
if(last)
levels[levels.size() - 1] = false;
levelObjects.push_back(object);
Links::const_iterator link = links.lower_bound(object);
Links::const_iterator endLink = links.upper_bound(object);
if(link != endLink)
{
levels.push_back(true);
Links::const_iterator nextLink;
for(; link != links.end() && link->first == object; link = nextLink)
{
nextLink = link;
++nextLink;
Print(stream, link->second, nextLink == links.end() || nextLink->first != object);
}
levels.pop_back();
}
levelObjects.pop_back();
}
void Print(std::ostream& stream)
{
std::vector<Objects::const_iterator> referencedObjects;
referencedObjects.reserve(links.size());
for(Links::const_iterator i = links.begin(); i != links.end(); ++i)
referencedObjects.push_back(i->second);
std::sort(referencedObjects.begin(), referencedObjects.end());
referencedObjects.resize(std::unique(referencedObjects.begin(), referencedObjects.end()) - referencedObjects.begin());
// пока просто вывести
for(Objects::const_iterator object = objects.begin(); object != objects.end(); ++object)
{
if(std::binary_search(referencedObjects.begin(), referencedObjects.end(), object))
continue;
Print(stream, object);
}
}
};
void ManagedHeap::PrintPtrs(std::ostream& stream)
{
CriticalCode code(criticalSection);
PtrTracer tracer(*this);
tracer.Print(stream);
}
void ManagedHeap::TracePtr(void* ptr, void* object)
{
if(disableTracePtr)
return;
CriticalCode code(criticalSection);
if(object)
ptrs[ptr] = object;
else
ptrs.erase(ptr);
}
void ManagedHeapTracePtr(void* ptr, void* object)
{
managedHeap.TracePtr(ptr, object);
}
#endif
#endif
END_INANITY