1370b324cSopenharmony_ci// NewHandler.cpp 2370b324cSopenharmony_ci 3370b324cSopenharmony_ci#include "StdAfx.h" 4370b324cSopenharmony_ci 5370b324cSopenharmony_ci#include <stdlib.h> 6370b324cSopenharmony_ci 7370b324cSopenharmony_ci#include "NewHandler.h" 8370b324cSopenharmony_ci 9370b324cSopenharmony_ci// #define DEBUG_MEMORY_LEAK 10370b324cSopenharmony_ci 11370b324cSopenharmony_ci#ifndef DEBUG_MEMORY_LEAK 12370b324cSopenharmony_ci 13370b324cSopenharmony_ci#ifdef Z7_REDEFINE_OPERATOR_NEW 14370b324cSopenharmony_ci 15370b324cSopenharmony_ci/* 16370b324cSopenharmony_civoid * my_new(size_t size) 17370b324cSopenharmony_ci{ 18370b324cSopenharmony_ci // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size); 19370b324cSopenharmony_ci if (size == 0) 20370b324cSopenharmony_ci size = 1; 21370b324cSopenharmony_ci void *p = ::malloc(size); 22370b324cSopenharmony_ci if (!p) 23370b324cSopenharmony_ci throw CNewException(); 24370b324cSopenharmony_ci return p; 25370b324cSopenharmony_ci} 26370b324cSopenharmony_ci 27370b324cSopenharmony_civoid my_delete(void *p) throw() 28370b324cSopenharmony_ci{ 29370b324cSopenharmony_ci // if (!p) return; ::HeapFree(::GetProcessHeap(), 0, p); 30370b324cSopenharmony_ci ::free(p); 31370b324cSopenharmony_ci} 32370b324cSopenharmony_ci 33370b324cSopenharmony_civoid * my_Realloc(void *p, size_t newSize, size_t oldSize) 34370b324cSopenharmony_ci{ 35370b324cSopenharmony_ci void *newBuf = my_new(newSize); 36370b324cSopenharmony_ci if (oldSize != 0) 37370b324cSopenharmony_ci memcpy(newBuf, p, oldSize); 38370b324cSopenharmony_ci my_delete(p); 39370b324cSopenharmony_ci return newBuf; 40370b324cSopenharmony_ci} 41370b324cSopenharmony_ci*/ 42370b324cSopenharmony_ci 43370b324cSopenharmony_civoid * 44370b324cSopenharmony_ci#ifdef _MSC_VER 45370b324cSopenharmony_ci__cdecl 46370b324cSopenharmony_ci#endif 47370b324cSopenharmony_cioperator new(size_t size) 48370b324cSopenharmony_ci{ 49370b324cSopenharmony_ci /* by C++ specification: 50370b324cSopenharmony_ci if (size == 0), operator new(size) returns non_NULL pointer. 51370b324cSopenharmony_ci If (operator new(0) returns NULL), it's out of specification. 52370b324cSopenharmony_ci but some calling code can work correctly even in this case too. */ 53370b324cSopenharmony_ci // if (size == 0) return NULL; // for debug only. don't use it 54370b324cSopenharmony_ci 55370b324cSopenharmony_ci /* malloc(0) returns non_NULL in main compilers, as we need here. 56370b324cSopenharmony_ci But specification also allows malloc(0) to return NULL. 57370b324cSopenharmony_ci So we change (size=0) to (size=1) here to get real non_NULL pointer */ 58370b324cSopenharmony_ci if (size == 0) 59370b324cSopenharmony_ci size = 1; 60370b324cSopenharmony_ci // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size); 61370b324cSopenharmony_ci // void *p = ::MyAlloc(size); // note: MyAlloc(0) returns NULL 62370b324cSopenharmony_ci void *p = ::malloc(size); 63370b324cSopenharmony_ci if (!p) 64370b324cSopenharmony_ci throw CNewException(); 65370b324cSopenharmony_ci return p; 66370b324cSopenharmony_ci} 67370b324cSopenharmony_ci 68370b324cSopenharmony_civoid 69370b324cSopenharmony_ci#ifdef _MSC_VER 70370b324cSopenharmony_ci__cdecl 71370b324cSopenharmony_ci#endif 72370b324cSopenharmony_cioperator delete(void *p) throw() 73370b324cSopenharmony_ci{ 74370b324cSopenharmony_ci // if (!p) return; ::HeapFree(::GetProcessHeap(), 0, p); 75370b324cSopenharmony_ci // MyFree(p); 76370b324cSopenharmony_ci ::free(p); 77370b324cSopenharmony_ci} 78370b324cSopenharmony_ci 79370b324cSopenharmony_ci/* 80370b324cSopenharmony_civoid * 81370b324cSopenharmony_ci#ifdef _MSC_VER 82370b324cSopenharmony_ci__cdecl 83370b324cSopenharmony_ci#endif 84370b324cSopenharmony_cioperator new[](size_t size) 85370b324cSopenharmony_ci{ 86370b324cSopenharmony_ci // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size); 87370b324cSopenharmony_ci if (size == 0) 88370b324cSopenharmony_ci size = 1; 89370b324cSopenharmony_ci void *p = ::malloc(size); 90370b324cSopenharmony_ci if (!p) 91370b324cSopenharmony_ci throw CNewException(); 92370b324cSopenharmony_ci return p; 93370b324cSopenharmony_ci} 94370b324cSopenharmony_ci 95370b324cSopenharmony_civoid 96370b324cSopenharmony_ci#ifdef _MSC_VER 97370b324cSopenharmony_ci__cdecl 98370b324cSopenharmony_ci#endif 99370b324cSopenharmony_cioperator delete[](void *p) throw() 100370b324cSopenharmony_ci{ 101370b324cSopenharmony_ci // if (!p) return; ::HeapFree(::GetProcessHeap(), 0, p); 102370b324cSopenharmony_ci ::free(p); 103370b324cSopenharmony_ci} 104370b324cSopenharmony_ci*/ 105370b324cSopenharmony_ci 106370b324cSopenharmony_ci#endif 107370b324cSopenharmony_ci 108370b324cSopenharmony_ci#else 109370b324cSopenharmony_ci 110370b324cSopenharmony_ci#include <stdio.h> 111370b324cSopenharmony_ci 112370b324cSopenharmony_ci// #pragma init_seg(lib) 113370b324cSopenharmony_ci/* 114370b324cSopenharmony_ciconst int kDebugSize = 1000000; 115370b324cSopenharmony_cistatic void *a[kDebugSize]; 116370b324cSopenharmony_cistatic int g_index = 0; 117370b324cSopenharmony_ci 118370b324cSopenharmony_ciclass CC 119370b324cSopenharmony_ci{ 120370b324cSopenharmony_cipublic: 121370b324cSopenharmony_ci CC() 122370b324cSopenharmony_ci { 123370b324cSopenharmony_ci for (int i = 0; i < kDebugSize; i++) 124370b324cSopenharmony_ci a[i] = 0; 125370b324cSopenharmony_ci } 126370b324cSopenharmony_ci ~CC() 127370b324cSopenharmony_ci { 128370b324cSopenharmony_ci printf("\nDestructor: %d\n", numAllocs); 129370b324cSopenharmony_ci for (int i = 0; i < kDebugSize; i++) 130370b324cSopenharmony_ci if (a[i] != 0) 131370b324cSopenharmony_ci return; 132370b324cSopenharmony_ci } 133370b324cSopenharmony_ci} g_CC; 134370b324cSopenharmony_ci*/ 135370b324cSopenharmony_ci 136370b324cSopenharmony_ci#ifdef _WIN32 137370b324cSopenharmony_cistatic bool wasInit = false; 138370b324cSopenharmony_cistatic CRITICAL_SECTION cs; 139370b324cSopenharmony_ci#endif 140370b324cSopenharmony_ci 141370b324cSopenharmony_cistatic int numAllocs = 0; 142370b324cSopenharmony_ci 143370b324cSopenharmony_civoid * 144370b324cSopenharmony_ci#ifdef _MSC_VER 145370b324cSopenharmony_ci__cdecl 146370b324cSopenharmony_ci#endif 147370b324cSopenharmony_cioperator new(size_t size) 148370b324cSopenharmony_ci{ 149370b324cSopenharmony_ci #ifdef _WIN32 150370b324cSopenharmony_ci if (!wasInit) 151370b324cSopenharmony_ci { 152370b324cSopenharmony_ci InitializeCriticalSection(&cs); 153370b324cSopenharmony_ci wasInit = true; 154370b324cSopenharmony_ci } 155370b324cSopenharmony_ci EnterCriticalSection(&cs); 156370b324cSopenharmony_ci 157370b324cSopenharmony_ci numAllocs++; 158370b324cSopenharmony_ci int loc = numAllocs; 159370b324cSopenharmony_ci void *p = HeapAlloc(GetProcessHeap(), 0, size); 160370b324cSopenharmony_ci /* 161370b324cSopenharmony_ci if (g_index < kDebugSize) 162370b324cSopenharmony_ci { 163370b324cSopenharmony_ci a[g_index] = p; 164370b324cSopenharmony_ci g_index++; 165370b324cSopenharmony_ci } 166370b324cSopenharmony_ci */ 167370b324cSopenharmony_ci printf("Alloc %6d, size = %8u\n", loc, (unsigned)size); 168370b324cSopenharmony_ci LeaveCriticalSection(&cs); 169370b324cSopenharmony_ci if (!p) 170370b324cSopenharmony_ci throw CNewException(); 171370b324cSopenharmony_ci return p; 172370b324cSopenharmony_ci #else 173370b324cSopenharmony_ci numAllocs++; 174370b324cSopenharmony_ci int loc = numAllocs; 175370b324cSopenharmony_ci if (size == 0) 176370b324cSopenharmony_ci size = 1; 177370b324cSopenharmony_ci void *p = malloc(size); 178370b324cSopenharmony_ci /* 179370b324cSopenharmony_ci if (g_index < kDebugSize) 180370b324cSopenharmony_ci { 181370b324cSopenharmony_ci a[g_index] = p; 182370b324cSopenharmony_ci g_index++; 183370b324cSopenharmony_ci } 184370b324cSopenharmony_ci */ 185370b324cSopenharmony_ci printf("Alloc %6d, size = %8u\n", loc, (unsigned)size); 186370b324cSopenharmony_ci if (!p) 187370b324cSopenharmony_ci throw CNewException(); 188370b324cSopenharmony_ci return p; 189370b324cSopenharmony_ci #endif 190370b324cSopenharmony_ci} 191370b324cSopenharmony_ci 192370b324cSopenharmony_civoid 193370b324cSopenharmony_ci#ifdef _MSC_VER 194370b324cSopenharmony_ci__cdecl 195370b324cSopenharmony_ci#endif 196370b324cSopenharmony_cioperator delete(void *p) throw() 197370b324cSopenharmony_ci{ 198370b324cSopenharmony_ci if (!p) 199370b324cSopenharmony_ci return; 200370b324cSopenharmony_ci #ifdef _WIN32 201370b324cSopenharmony_ci EnterCriticalSection(&cs); 202370b324cSopenharmony_ci /* 203370b324cSopenharmony_ci for (int i = 0; i < g_index; i++) 204370b324cSopenharmony_ci if (a[i] == p) 205370b324cSopenharmony_ci a[i] = 0; 206370b324cSopenharmony_ci */ 207370b324cSopenharmony_ci HeapFree(GetProcessHeap(), 0, p); 208370b324cSopenharmony_ci if (numAllocs == 0) 209370b324cSopenharmony_ci numAllocs = numAllocs; // ERROR 210370b324cSopenharmony_ci numAllocs--; 211370b324cSopenharmony_ci if (numAllocs == 0) 212370b324cSopenharmony_ci numAllocs = numAllocs; // OK: all objects were deleted 213370b324cSopenharmony_ci printf("Free %d\n", numAllocs); 214370b324cSopenharmony_ci LeaveCriticalSection(&cs); 215370b324cSopenharmony_ci #else 216370b324cSopenharmony_ci free(p); 217370b324cSopenharmony_ci numAllocs--; 218370b324cSopenharmony_ci printf("Free %d\n", numAllocs); 219370b324cSopenharmony_ci #endif 220370b324cSopenharmony_ci} 221370b324cSopenharmony_ci 222370b324cSopenharmony_ci/* 223370b324cSopenharmony_civoid * 224370b324cSopenharmony_ci#ifdef _MSC_VER 225370b324cSopenharmony_ci__cdecl 226370b324cSopenharmony_ci#endif 227370b324cSopenharmony_cioperator new[](size_t size) 228370b324cSopenharmony_ci{ 229370b324cSopenharmony_ci printf("operator_new[] : "); 230370b324cSopenharmony_ci return operator new(size); 231370b324cSopenharmony_ci} 232370b324cSopenharmony_ci 233370b324cSopenharmony_civoid 234370b324cSopenharmony_ci#ifdef _MSC_VER 235370b324cSopenharmony_ci__cdecl 236370b324cSopenharmony_ci#endif 237370b324cSopenharmony_cioperator delete(void *p, size_t sz) throw(); 238370b324cSopenharmony_ci 239370b324cSopenharmony_civoid 240370b324cSopenharmony_ci#ifdef _MSC_VER 241370b324cSopenharmony_ci__cdecl 242370b324cSopenharmony_ci#endif 243370b324cSopenharmony_cioperator delete(void *p, size_t sz) throw() 244370b324cSopenharmony_ci{ 245370b324cSopenharmony_ci if (!p) 246370b324cSopenharmony_ci return; 247370b324cSopenharmony_ci printf("operator_delete_size : size=%d : ", (unsigned)sz); 248370b324cSopenharmony_ci operator delete(p); 249370b324cSopenharmony_ci} 250370b324cSopenharmony_ci 251370b324cSopenharmony_civoid 252370b324cSopenharmony_ci#ifdef _MSC_VER 253370b324cSopenharmony_ci__cdecl 254370b324cSopenharmony_ci#endif 255370b324cSopenharmony_cioperator delete[](void *p) throw() 256370b324cSopenharmony_ci{ 257370b324cSopenharmony_ci if (!p) 258370b324cSopenharmony_ci return; 259370b324cSopenharmony_ci printf("operator_delete[] : "); 260370b324cSopenharmony_ci operator delete(p); 261370b324cSopenharmony_ci} 262370b324cSopenharmony_ci 263370b324cSopenharmony_civoid 264370b324cSopenharmony_ci#ifdef _MSC_VER 265370b324cSopenharmony_ci__cdecl 266370b324cSopenharmony_ci#endif 267370b324cSopenharmony_cioperator delete[](void *p, size_t sz) throw(); 268370b324cSopenharmony_ci 269370b324cSopenharmony_civoid 270370b324cSopenharmony_ci#ifdef _MSC_VER 271370b324cSopenharmony_ci__cdecl 272370b324cSopenharmony_ci#endif 273370b324cSopenharmony_cioperator delete[](void *p, size_t sz) throw() 274370b324cSopenharmony_ci{ 275370b324cSopenharmony_ci if (!p) 276370b324cSopenharmony_ci return; 277370b324cSopenharmony_ci printf("operator_delete_size[] : size=%d : ", (unsigned)sz); 278370b324cSopenharmony_ci operator delete(p); 279370b324cSopenharmony_ci} 280370b324cSopenharmony_ci*/ 281370b324cSopenharmony_ci 282370b324cSopenharmony_ci#endif 283370b324cSopenharmony_ci 284370b324cSopenharmony_ci/* 285370b324cSopenharmony_ciint MemErrorVC(size_t) 286370b324cSopenharmony_ci{ 287370b324cSopenharmony_ci throw CNewException(); 288370b324cSopenharmony_ci // return 1; 289370b324cSopenharmony_ci} 290370b324cSopenharmony_ciCNewHandlerSetter::CNewHandlerSetter() 291370b324cSopenharmony_ci{ 292370b324cSopenharmony_ci // MemErrorOldVCFunction = _set_new_handler(MemErrorVC); 293370b324cSopenharmony_ci} 294370b324cSopenharmony_ciCNewHandlerSetter::~CNewHandlerSetter() 295370b324cSopenharmony_ci{ 296370b324cSopenharmony_ci // _set_new_handler(MemErrorOldVCFunction); 297370b324cSopenharmony_ci} 298370b324cSopenharmony_ci*/ 299