Home | History | Annotate | Download | only in Common
      1 // NewHandler.cpp
      2 
      3 #include "StdAfx.h"
      4 
      5 #include <stdlib.h>
      6 
      7 #include "NewHandler.h"
      8 
      9 // #define DEBUG_MEMORY_LEAK
     10 
     11 #ifndef DEBUG_MEMORY_LEAK
     12 
     13 #ifdef _WIN32
     14 
     15 /*
     16 void * my_new(size_t size)
     17 {
     18   // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size);
     19   void *p = ::malloc(size);
     20   if (p == 0)
     21     throw CNewException();
     22   return p;
     23 }
     24 
     25 void my_delete(void *p) throw()
     26 {
     27   // if (p == 0) return; ::HeapFree(::GetProcessHeap(), 0, p);
     28   ::free(p);
     29 }
     30 
     31 void * my_Realloc(void *p, size_t newSize, size_t oldSize)
     32 {
     33   void *newBuf = my_new(newSize);
     34   memcpy(newBuf, p, oldSize);
     35   my_delete(p);
     36   return newBuf;
     37 }
     38 */
     39 
     40 void *
     41 #ifdef _MSC_VER
     42 __cdecl
     43 #endif
     44 operator new(size_t size)
     45 {
     46   // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size);
     47   void *p = ::malloc(size);
     48   if (p == 0)
     49     throw CNewException();
     50   return p;
     51 }
     52 
     53 void
     54 #ifdef _MSC_VER
     55 __cdecl
     56 #endif
     57 operator delete(void *p) throw()
     58 {
     59   // if (p == 0) return; ::HeapFree(::GetProcessHeap(), 0, p);
     60   ::free(p);
     61 }
     62 
     63 /*
     64 void *
     65 #ifdef _MSC_VER
     66 __cdecl
     67 #endif
     68 operator new[](size_t size)
     69 {
     70   // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size);
     71   void *p = ::malloc(size);
     72   if (p == 0)
     73     throw CNewException();
     74   return p;
     75 }
     76 
     77 void
     78 #ifdef _MSC_VER
     79 __cdecl
     80 #endif
     81 operator delete[](void *p) throw()
     82 {
     83   // if (p == 0) return; ::HeapFree(::GetProcessHeap(), 0, p);
     84   ::free(p);
     85 }
     86 */
     87 
     88 #endif
     89 
     90 #else
     91 
     92 #include <stdio.h>
     93 
     94 // #pragma init_seg(lib)
     95 const int kDebugSize = 1000000;
     96 static void *a[kDebugSize];
     97 static int index = 0;
     98 
     99 static int numAllocs = 0;
    100 void * __cdecl operator new(size_t size)
    101 {
    102   numAllocs++;
    103   void *p = HeapAlloc(GetProcessHeap(), 0, size);
    104   if (index < kDebugSize)
    105   {
    106     a[index] = p;
    107     index++;
    108   }
    109   if (p == 0)
    110     throw CNewException();
    111   printf("Alloc %6d, size = %8d\n", numAllocs, size);
    112   return p;
    113 }
    114 
    115 class CC
    116 {
    117 public:
    118   CC()
    119   {
    120     for (int i = 0; i < kDebugSize; i++)
    121       a[i] = 0;
    122   }
    123   ~CC()
    124   {
    125     for (int i = 0; i < kDebugSize; i++)
    126       if (a[i] != 0)
    127         return;
    128   }
    129 } g_CC;
    130 
    131 
    132 void __cdecl operator delete(void *p)
    133 {
    134   if (p == 0)
    135     return;
    136   /*
    137   for (int i = 0; i < index; i++)
    138     if (a[i] == p)
    139       a[i] = 0;
    140   */
    141   HeapFree(GetProcessHeap(), 0, p);
    142   numAllocs--;
    143   printf("Free %d\n", numAllocs);
    144 }
    145 
    146 #endif
    147 
    148 /*
    149 int MemErrorVC(size_t)
    150 {
    151   throw CNewException();
    152   // return 1;
    153 }
    154 CNewHandlerSetter::CNewHandlerSetter()
    155 {
    156   // MemErrorOldVCFunction = _set_new_handler(MemErrorVC);
    157 }
    158 CNewHandlerSetter::~CNewHandlerSetter()
    159 {
    160   // _set_new_handler(MemErrorOldVCFunction);
    161 }
    162 */
    163