michael@0: // NewHandler.cpp michael@0: michael@0: #include "StdAfx.h" michael@0: michael@0: #include michael@0: michael@0: #include "NewHandler.h" michael@0: michael@0: // #define DEBUG_MEMORY_LEAK michael@0: michael@0: #ifndef DEBUG_MEMORY_LEAK michael@0: michael@0: #ifdef _WIN32 michael@0: void * michael@0: #ifdef _MSC_VER michael@0: __cdecl michael@0: #endif michael@0: operator new(size_t size) michael@0: { michael@0: // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size); michael@0: void *p = ::malloc(size); michael@0: if (p == 0) michael@0: throw CNewException(); michael@0: return p; michael@0: } michael@0: michael@0: void michael@0: #ifdef _MSC_VER michael@0: __cdecl michael@0: #endif michael@0: operator delete(void *p) throw() michael@0: { michael@0: /* michael@0: if (p == 0) michael@0: return; michael@0: ::HeapFree(::GetProcessHeap(), 0, p); michael@0: */ michael@0: ::free(p); michael@0: } michael@0: #endif michael@0: michael@0: #else michael@0: michael@0: #pragma init_seg(lib) michael@0: const int kDebugSize = 1000000; michael@0: static void *a[kDebugSize]; michael@0: static int index = 0; michael@0: michael@0: static int numAllocs = 0; michael@0: void * __cdecl operator new(size_t size) michael@0: { michael@0: numAllocs++; michael@0: void *p = HeapAlloc(GetProcessHeap(), 0, size); michael@0: if (index == 40) michael@0: { michael@0: int t = 1; michael@0: } michael@0: if (index < kDebugSize) michael@0: { michael@0: a[index] = p; michael@0: index++; michael@0: } michael@0: if (p == 0) michael@0: throw CNewException(); michael@0: printf("Alloc %6d, size = %8d\n", numAllocs, size); michael@0: return p; michael@0: } michael@0: michael@0: class CC michael@0: { michael@0: public: michael@0: CC() michael@0: { michael@0: for (int i = 0; i < kDebugSize; i++) michael@0: a[i] = 0; michael@0: } michael@0: ~CC() michael@0: { michael@0: for (int i = 0; i < kDebugSize; i++) michael@0: if (a[i] != 0) michael@0: return; michael@0: } michael@0: } g_CC; michael@0: michael@0: michael@0: void __cdecl operator delete(void *p) michael@0: { michael@0: if (p == 0) michael@0: return; michael@0: /* michael@0: for (int i = 0; i < index; i++) michael@0: if (a[i] == p) michael@0: a[i] = 0; michael@0: */ michael@0: HeapFree(GetProcessHeap(), 0, p); michael@0: numAllocs--; michael@0: printf("Free %d\n", numAllocs); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: /* michael@0: int MemErrorVC(size_t) michael@0: { michael@0: throw CNewException(); michael@0: // return 1; michael@0: } michael@0: CNewHandlerSetter::CNewHandlerSetter() michael@0: { michael@0: // MemErrorOldVCFunction = _set_new_handler(MemErrorVC); michael@0: } michael@0: CNewHandlerSetter::~CNewHandlerSetter() michael@0: { michael@0: // _set_new_handler(MemErrorOldVCFunction); michael@0: } michael@0: */