BCM WL 6.30.102.9 (r366174)
[tomato.git] / release / src-rt / linux / linux-2.6 / scripts / squashfs / lzma / C / Common / NewHandler.cpp
blob2f4e007bdb539f118897ab538388914529c45e59
1 // NewHandler.cpp
3 #include "StdAfx.h"
5 #include <stdlib.h>
7 #include "NewHandler.h"
9 // #define DEBUG_MEMORY_LEAK
11 #ifndef DEBUG_MEMORY_LEAK
13 void *
14 #ifdef _MSC_VER
15 __cdecl
16 #endif
17 operator new(size_t size)
19 // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size);
20 void *p = ::malloc(size);
21 if (p == 0)
22 throw CNewException();
23 return p;
26 void
27 #ifdef _MSC_VER
28 __cdecl
29 #endif
30 operator delete(void *p) throw()
33 if (p == 0)
34 return;
35 ::HeapFree(::GetProcessHeap(), 0, p);
37 ::free(p);
40 #else
42 #pragma init_seg(lib)
43 const int kDebugSize = 1000000;
44 static void *a[kDebugSize];
45 static int index = 0;
47 static int numAllocs = 0;
48 void * __cdecl operator new(size_t size)
50 numAllocs++;
51 void *p = HeapAlloc(GetProcessHeap(), 0, size);
52 if (index == 40)
54 int t = 1;
56 if (index < kDebugSize)
58 a[index] = p;
59 index++;
61 if (p == 0)
62 throw CNewException();
63 printf("Alloc %6d, size = %8d\n", numAllocs, size);
64 return p;
67 class CC
69 public:
70 CC()
72 for (int i = 0; i < kDebugSize; i++)
73 a[i] = 0;
75 ~CC()
77 for (int i = 0; i < kDebugSize; i++)
78 if (a[i] != 0)
79 return;
81 } g_CC;
84 void __cdecl operator delete(void *p)
86 if (p == 0)
87 return;
89 for (int i = 0; i < index; i++)
90 if (a[i] == p)
91 a[i] = 0;
93 HeapFree(GetProcessHeap(), 0, p);
94 numAllocs--;
95 printf("Free %d\n", numAllocs);
98 #endif
101 int MemErrorVC(size_t)
103 throw CNewException();
104 // return 1;
106 CNewHandlerSetter::CNewHandlerSetter()
108 // MemErrorOldVCFunction = _set_new_handler(MemErrorVC);
110 CNewHandlerSetter::~CNewHandlerSetter()
112 // _set_new_handler(MemErrorOldVCFunction);