Debian Codegraph @

utils/lzma/SRC/Common/NewHandler.cpp

raw
1 // NewHandler.cpp
2
3 #include "StdAfx.h"
4
5 #include <stdlib.h>
6
7 #include "NewHandler.h"
8
9 // #define DEBUG_MEMORY_LEAK
10
11 #ifndef DEBUG_MEMORY_LEAK
12
13 void *
14 #ifdef _MSC_VER
15 __cdecl
16 #endif
17 operator new(size_t size)
18 {
19 // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size);
20 void *p = ::malloc(size);
21 if (p == 0)
22 throw CNewException();
23 return p;
24 }
25
26 void
27 #ifdef _MSC_VER
28 __cdecl
29 #endif
30 operator delete(void *p) throw()
31 {
32 /*
33 if (p == 0)
34 return;
35 ::HeapFree(::GetProcessHeap(), 0, p);
36 */
37 ::free(p);
38 }
39
40 #else
41
42 #pragma init_seg(lib)
43 const int kDebugSize = 1000000;
44 static void *a[kDebugSize];
45 static int index = 0;
46
47 static int numAllocs = 0;
48 void * __cdecl operator new(size_t size)
49 {
50 numAllocs++;
51 void *p = HeapAlloc(GetProcessHeap(), 0, size);
52 if (index == 40)
53 {
54 int t = 1;
55 }
56 if (index < kDebugSize)
57 {
58 a[index] = p;
59 index++;
60 }
61 if (p == 0)
62 throw CNewException();
63 printf("Alloc %6d, size = %8d\n", numAllocs, size);
64 return p;
65 }
66
67 class CC
68 {
69 public:
70 CC()
71 {
72 for (int i = 0; i < kDebugSize; i++)
73 a[i] = 0;
74 }
75 ~CC()
76 {
77 for (int i = 0; i < kDebugSize; i++)
78 if (a[i] != 0)
79 return;
80 }
81 } g_CC;
82
83
84 void __cdecl operator delete(void *p)
85 {
86 if (p == 0)
87 return;
88 /*
89 for (int i = 0; i < index; i++)
90 if (a[i] == p)
91 a[i] = 0;
92 */
93 HeapFree(GetProcessHeap(), 0, p);
94 numAllocs--;
95 printf("Free %d\n", numAllocs);
96 }
97
98 #endif
99
100 /*
101 int MemErrorVC(size_t)
102 {
103 throw CNewException();
104 // return 1;
105 }
106 CNewHandlerSetter::CNewHandlerSetter()
107 {
108 // MemErrorOldVCFunction = _set_new_handler(MemErrorVC);
109 }
110 CNewHandlerSetter::~CNewHandlerSetter()
111 {
112 // _set_new_handler(MemErrorOldVCFunction);
113 }
114 */