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