-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.cpp
More file actions
49 lines (41 loc) · 1.06 KB
/
debug.cpp
File metadata and controls
49 lines (41 loc) · 1.06 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
/* ************************************************************************ *
* Written by Alex de Kruijff 21 May 2009 *
* ************************************************************************ *
* This source was written with a tabstop every four characters *
* In vi type :set ts=4 *
* ************************************************************************ */
#include "debug.h"
// using std::new_handler;
using std::bad_alloc;
long varDynamic = 0;
void *operator new(size_t sz) throw (std::bad_alloc)
{
if (sz == 0)
sz = 1;
void *ptr = (void *) malloc (sz);
++varDynamic;
while (ptr == 0)
{
// new_handler handler = __new_handler;
#ifdef __EXCEPTIONS
throw bad_alloc();
#else
std::abort();
#endif
ptr = (void *) malloc (sz);
}
return ptr;
}
void operator delete(void *ptr)
{
if (ptr)
{
if (--varDynamic == 0)
fprintf(stderr, "debug: no leaks!\n");
free(ptr);
}
}
void checkDynamic()
{
fprintf(stderr, "debug: leaking %u items\n", varDynamic);
}