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