diff options
Diffstat (limited to '')
-rw-r--r-- | CPP/Common/NewHandler.cpp | 184 |
1 files changed, 151 insertions, 33 deletions
diff --git a/CPP/Common/NewHandler.cpp b/CPP/Common/NewHandler.cpp index 65ef41d..c95833e 100644 --- a/CPP/Common/NewHandler.cpp +++ b/CPP/Common/NewHandler.cpp | |||
@@ -10,21 +10,23 @@ | |||
10 | 10 | ||
11 | #ifndef DEBUG_MEMORY_LEAK | 11 | #ifndef DEBUG_MEMORY_LEAK |
12 | 12 | ||
13 | #ifdef _7ZIP_REDEFINE_OPERATOR_NEW | 13 | #ifdef Z7_REDEFINE_OPERATOR_NEW |
14 | 14 | ||
15 | /* | 15 | /* |
16 | void * my_new(size_t size) | 16 | void * my_new(size_t size) |
17 | { | 17 | { |
18 | // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size); | 18 | // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size); |
19 | if (size == 0) | ||
20 | size = 1; | ||
19 | void *p = ::malloc(size); | 21 | void *p = ::malloc(size); |
20 | if (p == 0) | 22 | if (!p) |
21 | throw CNewException(); | 23 | throw CNewException(); |
22 | return p; | 24 | return p; |
23 | } | 25 | } |
24 | 26 | ||
25 | void my_delete(void *p) throw() | 27 | void my_delete(void *p) throw() |
26 | { | 28 | { |
27 | // if (p == 0) return; ::HeapFree(::GetProcessHeap(), 0, p); | 29 | // if (!p) return; ::HeapFree(::GetProcessHeap(), 0, p); |
28 | ::free(p); | 30 | ::free(p); |
29 | } | 31 | } |
30 | 32 | ||
@@ -44,9 +46,21 @@ __cdecl | |||
44 | #endif | 46 | #endif |
45 | operator new(size_t size) | 47 | operator new(size_t size) |
46 | { | 48 | { |
49 | /* by C++ specification: | ||
50 | if (size == 0), operator new(size) returns non_NULL pointer. | ||
51 | If (operator new(0) returns NULL), it's out of specification. | ||
52 | but some calling code can work correctly even in this case too. */ | ||
53 | // if (size == 0) return NULL; // for debug only. don't use it | ||
54 | |||
55 | /* malloc(0) returns non_NULL in main compilers, as we need here. | ||
56 | But specification also allows malloc(0) to return NULL. | ||
57 | So we change (size=0) to (size=1) here to get real non_NULL pointer */ | ||
58 | if (size == 0) | ||
59 | size = 1; | ||
47 | // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size); | 60 | // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size); |
61 | // void *p = ::MyAlloc(size); // note: MyAlloc(0) returns NULL | ||
48 | void *p = ::malloc(size); | 62 | void *p = ::malloc(size); |
49 | if (p == 0) | 63 | if (!p) |
50 | throw CNewException(); | 64 | throw CNewException(); |
51 | return p; | 65 | return p; |
52 | } | 66 | } |
@@ -57,7 +71,8 @@ __cdecl | |||
57 | #endif | 71 | #endif |
58 | operator delete(void *p) throw() | 72 | operator delete(void *p) throw() |
59 | { | 73 | { |
60 | // if (p == 0) return; ::HeapFree(::GetProcessHeap(), 0, p); | 74 | // if (!p) return; ::HeapFree(::GetProcessHeap(), 0, p); |
75 | // MyFree(p); | ||
61 | ::free(p); | 76 | ::free(p); |
62 | } | 77 | } |
63 | 78 | ||
@@ -69,8 +84,10 @@ __cdecl | |||
69 | operator new[](size_t size) | 84 | operator new[](size_t size) |
70 | { | 85 | { |
71 | // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size); | 86 | // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size); |
87 | if (size == 0) | ||
88 | size = 1; | ||
72 | void *p = ::malloc(size); | 89 | void *p = ::malloc(size); |
73 | if (p == 0) | 90 | if (!p) |
74 | throw CNewException(); | 91 | throw CNewException(); |
75 | return p; | 92 | return p; |
76 | } | 93 | } |
@@ -81,7 +98,7 @@ __cdecl | |||
81 | #endif | 98 | #endif |
82 | operator delete[](void *p) throw() | 99 | operator delete[](void *p) throw() |
83 | { | 100 | { |
84 | // if (p == 0) return; ::HeapFree(::GetProcessHeap(), 0, p); | 101 | // if (!p) return; ::HeapFree(::GetProcessHeap(), 0, p); |
85 | ::free(p); | 102 | ::free(p); |
86 | } | 103 | } |
87 | */ | 104 | */ |
@@ -93,16 +110,43 @@ operator delete[](void *p) throw() | |||
93 | #include <stdio.h> | 110 | #include <stdio.h> |
94 | 111 | ||
95 | // #pragma init_seg(lib) | 112 | // #pragma init_seg(lib) |
113 | /* | ||
96 | const int kDebugSize = 1000000; | 114 | const int kDebugSize = 1000000; |
97 | static void *a[kDebugSize]; | 115 | static void *a[kDebugSize]; |
98 | static int index = 0; | 116 | static int g_index = 0; |
117 | |||
118 | class CC | ||
119 | { | ||
120 | public: | ||
121 | CC() | ||
122 | { | ||
123 | for (int i = 0; i < kDebugSize; i++) | ||
124 | a[i] = 0; | ||
125 | } | ||
126 | ~CC() | ||
127 | { | ||
128 | printf("\nDestructor: %d\n", numAllocs); | ||
129 | for (int i = 0; i < kDebugSize; i++) | ||
130 | if (a[i] != 0) | ||
131 | return; | ||
132 | } | ||
133 | } g_CC; | ||
134 | */ | ||
99 | 135 | ||
136 | #ifdef _WIN32 | ||
100 | static bool wasInit = false; | 137 | static bool wasInit = false; |
101 | static CRITICAL_SECTION cs; | 138 | static CRITICAL_SECTION cs; |
139 | #endif | ||
102 | 140 | ||
103 | static int numAllocs = 0; | 141 | static int numAllocs = 0; |
104 | void * __cdecl operator new(size_t size) | 142 | |
143 | void * | ||
144 | #ifdef _MSC_VER | ||
145 | __cdecl | ||
146 | #endif | ||
147 | operator new(size_t size) | ||
105 | { | 148 | { |
149 | #ifdef _WIN32 | ||
106 | if (!wasInit) | 150 | if (!wasInit) |
107 | { | 151 | { |
108 | InitializeCriticalSection(&cs); | 152 | InitializeCriticalSection(&cs); |
@@ -114,52 +158,126 @@ void * __cdecl operator new(size_t size) | |||
114 | int loc = numAllocs; | 158 | int loc = numAllocs; |
115 | void *p = HeapAlloc(GetProcessHeap(), 0, size); | 159 | void *p = HeapAlloc(GetProcessHeap(), 0, size); |
116 | /* | 160 | /* |
117 | if (index < kDebugSize) | 161 | if (g_index < kDebugSize) |
118 | { | 162 | { |
119 | a[index] = p; | 163 | a[g_index] = p; |
120 | index++; | 164 | g_index++; |
121 | } | 165 | } |
122 | */ | 166 | */ |
123 | printf("Alloc %6d, size = %8u\n", loc, (unsigned)size); | 167 | printf("Alloc %6d, size = %8u\n", loc, (unsigned)size); |
124 | LeaveCriticalSection(&cs); | 168 | LeaveCriticalSection(&cs); |
125 | if (p == 0) | 169 | if (!p) |
126 | throw CNewException(); | 170 | throw CNewException(); |
127 | return p; | 171 | return p; |
128 | } | 172 | #else |
129 | 173 | numAllocs++; | |
130 | class CC | 174 | int loc = numAllocs; |
131 | { | 175 | if (size == 0) |
132 | public: | 176 | size = 1; |
133 | CC() | 177 | void *p = malloc(size); |
134 | { | 178 | /* |
135 | for (int i = 0; i < kDebugSize; i++) | 179 | if (g_index < kDebugSize) |
136 | a[i] = 0; | ||
137 | } | ||
138 | ~CC() | ||
139 | { | 180 | { |
140 | printf("\nDestructor: %d\n", numAllocs); | 181 | a[g_index] = p; |
141 | for (int i = 0; i < kDebugSize; i++) | 182 | g_index++; |
142 | if (a[i] != 0) | ||
143 | return; | ||
144 | } | 183 | } |
145 | } g_CC; | 184 | */ |
146 | 185 | printf("Alloc %6d, size = %8u\n", loc, (unsigned)size); | |
186 | if (!p) | ||
187 | throw CNewException(); | ||
188 | return p; | ||
189 | #endif | ||
190 | } | ||
147 | 191 | ||
148 | void __cdecl operator delete(void *p) | 192 | void |
193 | #ifdef _MSC_VER | ||
194 | __cdecl | ||
195 | #endif | ||
196 | operator delete(void *p) throw() | ||
149 | { | 197 | { |
150 | if (p == 0) | 198 | if (!p) |
151 | return; | 199 | return; |
200 | #ifdef _WIN32 | ||
152 | EnterCriticalSection(&cs); | 201 | EnterCriticalSection(&cs); |
153 | /* | 202 | /* |
154 | for (int i = 0; i < index; i++) | 203 | for (int i = 0; i < g_index; i++) |
155 | if (a[i] == p) | 204 | if (a[i] == p) |
156 | a[i] = 0; | 205 | a[i] = 0; |
157 | */ | 206 | */ |
158 | HeapFree(GetProcessHeap(), 0, p); | 207 | HeapFree(GetProcessHeap(), 0, p); |
208 | if (numAllocs == 0) | ||
209 | numAllocs = numAllocs; // ERROR | ||
159 | numAllocs--; | 210 | numAllocs--; |
211 | if (numAllocs == 0) | ||
212 | numAllocs = numAllocs; // OK: all objects were deleted | ||
160 | printf("Free %d\n", numAllocs); | 213 | printf("Free %d\n", numAllocs); |
161 | LeaveCriticalSection(&cs); | 214 | LeaveCriticalSection(&cs); |
215 | #else | ||
216 | free(p); | ||
217 | numAllocs--; | ||
218 | printf("Free %d\n", numAllocs); | ||
219 | #endif | ||
220 | } | ||
221 | |||
222 | /* | ||
223 | void * | ||
224 | #ifdef _MSC_VER | ||
225 | __cdecl | ||
226 | #endif | ||
227 | operator new[](size_t size) | ||
228 | { | ||
229 | printf("operator_new[] : "); | ||
230 | return operator new(size); | ||
231 | } | ||
232 | |||
233 | void | ||
234 | #ifdef _MSC_VER | ||
235 | __cdecl | ||
236 | #endif | ||
237 | operator delete(void *p, size_t sz) throw(); | ||
238 | |||
239 | void | ||
240 | #ifdef _MSC_VER | ||
241 | __cdecl | ||
242 | #endif | ||
243 | operator delete(void *p, size_t sz) throw() | ||
244 | { | ||
245 | if (!p) | ||
246 | return; | ||
247 | printf("operator_delete_size : size=%d : ", (unsigned)sz); | ||
248 | operator delete(p); | ||
249 | } | ||
250 | |||
251 | void | ||
252 | #ifdef _MSC_VER | ||
253 | __cdecl | ||
254 | #endif | ||
255 | operator delete[](void *p) throw() | ||
256 | { | ||
257 | if (!p) | ||
258 | return; | ||
259 | printf("operator_delete[] : "); | ||
260 | operator delete(p); | ||
261 | } | ||
262 | |||
263 | void | ||
264 | #ifdef _MSC_VER | ||
265 | __cdecl | ||
266 | #endif | ||
267 | operator delete[](void *p, size_t sz) throw(); | ||
268 | |||
269 | void | ||
270 | #ifdef _MSC_VER | ||
271 | __cdecl | ||
272 | #endif | ||
273 | operator delete[](void *p, size_t sz) throw() | ||
274 | { | ||
275 | if (!p) | ||
276 | return; | ||
277 | printf("operator_delete_size[] : size=%d : ", (unsigned)sz); | ||
278 | operator delete(p); | ||
162 | } | 279 | } |
280 | */ | ||
163 | 281 | ||
164 | #endif | 282 | #endif |
165 | 283 | ||