-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathmemory.cpp
More file actions
326 lines (263 loc) · 6 KB
/
Copy pathmemory.cpp
File metadata and controls
326 lines (263 loc) · 6 KB
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
319
320
321
322
323
324
325
326
// LAF Base Library
// Copyright (c) 2022-2024 Igara Studio S.A.
// Copyright (c) 2001-2016 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "base/debug.h"
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <mutex>
using namespace std;
#if !defined LAF_MEMLEAK // Without leak detection
void* base_malloc(size_t bytes)
{
return malloc(bytes);
}
void* base_malloc0(size_t bytes)
{
return calloc(1, bytes);
}
void* base_realloc(void* mem, size_t bytes)
{
return realloc(mem, bytes);
}
void base_free(void* mem)
{
assert(mem);
free(mem);
}
char* base_strdup(const char* string)
{
assert(string);
#ifdef _MSC_VER
return _strdup(string);
#else
return strdup(string);
#endif
}
#else // With leak detection
#define BACKTRACE_LEVELS 16
#ifdef _MSC_VER
#include <windows.h>
#include <dbghelp.h>
typedef USHORT(WINAPI* RtlCaptureStackBackTraceType)(ULONG, ULONG, PVOID*, PULONG);
static RtlCaptureStackBackTraceType pRtlCaptureStackBackTrace;
#endif
struct slot_t {
void* backtrace[BACKTRACE_LEVELS];
void* ptr;
size_t size;
struct slot_t* next;
};
static bool memleak_status = false;
static slot_t* headslot;
static std::mutex g_mutex;
void base_memleak_init()
{
#ifdef _MSC_VER
pRtlCaptureStackBackTrace = (RtlCaptureStackBackTraceType)(::GetProcAddress(
::LoadLibrary(L"kernel32.dll"),
"RtlCaptureStackBackTrace"));
#endif
assert(!memleak_status);
headslot = NULL;
memleak_status = true;
}
void base_memleak_exit()
{
assert(memleak_status);
memleak_status = false;
FILE* f = fopen("_ase_memlog.txt", "wt");
slot_t* it;
if (f != NULL) {
#ifdef _MSC_VER
struct SYMBOL_INFO_EX {
IMAGEHLP_SYMBOL64 header;
char filename[MAX_SYM_NAME];
} si;
si.header.SizeOfStruct = sizeof(SYMBOL_INFO_EX);
si.header.MaxNameLength = MAX_SYM_NAME;
IMAGEHLP_LINE64 line;
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
::SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS);
HANDLE hproc = ::GetCurrentProcess();
if (!::SymInitialize(hproc, NULL, TRUE))
fprintf(f, "Error initializing SymInitialize()\nGetLastError = %d\n", ::GetLastError());
char filename[MAX_PATH];
::GetModuleFileNameA(NULL, filename, sizeof(filename) / sizeof(filename[0]));
::SymLoadModule64(hproc, NULL, filename, NULL, 0, 0);
#endif
// Memory leaks
for (it = headslot; it != NULL; it = it->next) {
fprintf(f, "\nLEAK address: %p, size: %lu\n", it->ptr, it->size);
for (int c = 0; c < BACKTRACE_LEVELS; ++c) {
#ifdef _MSC_VER
DWORD displacement;
if (::SymGetLineFromAddr64(hproc, (DWORD)it->backtrace[c], &displacement, &line)) {
si.header.Name[0] = 0;
::SymGetSymFromAddr64(hproc, (DWORD)it->backtrace[c], NULL, &si.header);
fprintf(f,
"%p : %s(%lu) [%s]\n",
it->backtrace[c],
line.FileName,
line.LineNumber,
si.header.Name);
}
else
#endif
fprintf(f, "%p\n", it->backtrace[c]);
}
}
fclose(f);
#ifdef _MSC_VER
::SymCleanup(hproc);
#endif
}
}
static void addslot(void* ptr, size_t size)
{
if (!memleak_status)
return;
slot_t* p = reinterpret_cast<slot_t*>(malloc(sizeof(slot_t)));
assert(ptr);
// __builtin_return_address is a GCC extension
#if defined(__GNUC__)
p->backtrace[0] = __builtin_return_address(4);
p->backtrace[1] = __builtin_return_address(3);
p->backtrace[2] = __builtin_return_address(2);
p->backtrace[3] = __builtin_return_address(1);
#elif defined(_MSC_VER)
{
for (int c = 0; c < BACKTRACE_LEVELS; ++c)
p->backtrace[c] = 0;
pRtlCaptureStackBackTrace(0, BACKTRACE_LEVELS, p->backtrace, NULL);
}
#else
#error Not supported
#endif
p->ptr = ptr;
p->size = size;
const std::lock_guard lock(g_mutex);
p->next = headslot;
headslot = p;
}
static void delslot(void* ptr)
{
if (!memleak_status)
return;
slot_t *it, *prev = NULL;
assert(ptr);
const std::lock_guard lock(g_mutex);
for (it = headslot; it != nullptr; prev = it, it = it->next) {
if (it->ptr == ptr) {
if (prev)
prev->next = it->next;
else
headslot = it->next;
free(it);
break;
}
}
}
void* base_malloc(size_t bytes)
{
void* mem = malloc(bytes);
if (mem) {
addslot(mem, bytes);
return mem;
}
else
return nullptr;
}
void* base_malloc0(size_t bytes)
{
void* mem = calloc(1, bytes);
if (mem) {
addslot(mem, bytes);
return mem;
}
else
return nullptr;
}
void* base_realloc(void* mem, size_t bytes)
{
void* newmem = realloc(mem, bytes);
if (newmem) {
if (mem)
delslot(mem);
addslot(newmem, bytes);
return newmem;
}
else
return nullptr;
}
void base_free(void* mem)
{
assert(mem);
if (mem) {
delslot(mem);
free(mem);
}
}
char* base_strdup(const char* string)
{
assert(string);
char* mem = strdup(string);
if (mem)
addslot(mem, strlen(mem) + 1);
return mem;
}
// C++ operators
void* operator new(std::size_t size)
{
void* ptr = base_malloc(size);
if (!ptr)
throw std::bad_alloc();
return ptr;
}
void* operator new[](std::size_t size)
{
void* ptr = base_malloc(size);
if (!ptr)
throw std::bad_alloc();
return ptr;
}
void operator delete(void* ptr) LAF_NOEXCEPT
{
if (!ptr)
return;
base_free(ptr);
}
void operator delete[](void* ptr) LAF_NOEXCEPT
{
if (!ptr)
return;
base_free(ptr);
}
#endif
void* base_aligned_alloc(std::size_t bytes, std::size_t alignment)
{
#if LAF_WINDOWS
return _aligned_malloc(bytes, alignment);
#else
ASSERT(alignment > 0);
std::size_t misaligned = (bytes % alignment);
if (misaligned > 0)
bytes += alignment - misaligned;
return aligned_alloc(alignment, bytes);
#endif
}
void base_aligned_free(void* mem)
{
#if LAF_WINDOWS
_aligned_free(mem);
#else
free(mem);
#endif
}