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