+
+#ifdef MEMLEAK_CHECK
+typedef struct
+{
+ unsigned int address;
+ unsigned int size;
+ char *file;
+ void *backtrace[BACKTRACE_DEPTH];
+ unsigned char btcount;
+ unsigned short line;
+ unsigned char type;
+} ALLOC_INFO;
+
+typedef std::map<unsigned int, ALLOC_INFO> AllocList;
+
+extern AllocList *allocList;
+extern pthread_mutex_t memLock;
+
+static inline void AddTrack(unsigned int addr, unsigned int asize, const char *fname, unsigned int lnum, unsigned int type)
+{
+ ALLOC_INFO info;
+
+ if(!allocList)
+ allocList = new(AllocList);
+
+ info.address = addr;
+ info.file = strdup(fname);
+ info.line = lnum;
+ info.size = asize;
+ info.type = type;
+ info.btcount = backtrace( info.backtrace, BACKTRACE_DEPTH );
+ singleLock s(memLock);
+ (*allocList)[addr]=info;
+};
+
+static inline void RemoveTrack(unsigned int addr, unsigned int type)
+{
+ if(!allocList)
+ return;
+ AllocList::iterator i;
+ singleLock s(memLock);
+ i = allocList->find(addr);
+ if ( i != allocList->end() )
+ {
+ if ( i->second.type != type )
+ i->second.type=3;
+ else
+ {
+ free(i->second.file);
+ allocList->erase(i);
+ }
+ }
+};
+
+inline void * operator new(unsigned int size, const char *file, int line)
+{
+ void *ptr = (void *)malloc(size);
+ AddTrack((unsigned int)ptr, size, file, line, 1);
+ return(ptr);
+};
+
+inline void operator delete(void *p)
+{
+ RemoveTrack((unsigned int)p,1);
+ free(p);
+};
+
+inline void * operator new[](unsigned int size, const char *file, int line)
+{
+ void *ptr = (void *)malloc(size);
+ AddTrack((unsigned int)ptr, size, file, line, 2);
+ return(ptr);
+};
+
+inline void operator delete[](void *p)
+{
+ RemoveTrack((unsigned int)p, 2);
+ free(p);
+};
+
+void DumpUnfreed();
+#define new new(__FILE__, __LINE__)
+
+#endif // MEMLEAK_CHECK
+