8 #include <libsig_comp.h>
10 // to use memleak check change the following in configure.ac
11 // * add -rdynamic to LD_FLAGS
12 // * add -DMEMLEAK_CHECK to CPP_FLAGS
15 #define BACKTRACE_DEPTH 5
16 // when you have c++filt and corresponding libs on your platform
17 // then add -DHAVE_CPP_FILT to CPP_FLAGS in configure.ac
19 #include <lib/base/elock.h>
23 #endif // MEMLEAK_CHECK
29 void eFatal(const char* fmt, ...);
31 enum { lvlDebug=1, lvlWarning=2, lvlFatal=4 };
34 extern Signal2<void, int, const std::string&> logOutput;
35 extern int logOutputConsole;
43 void eDebug(const char* fmt, ...);
44 void eDebugNoNewLine(const char* fmt, ...);
45 void eWarning(const char* fmt, ...);
47 #define ASSERT(x) { if (!(x)) eFatal("%s:%d ASSERTION %s FAILED!", __FILE__, __LINE__, #x); }
56 void *backtrace[BACKTRACE_DEPTH];
57 unsigned char btcount;
62 typedef std::map<unsigned int, ALLOC_INFO> AllocList;
64 extern AllocList *allocList;
65 extern pthread_mutex_t memLock;
67 static inline void AddTrack(unsigned int addr, unsigned int asize, const char *fname, unsigned int lnum, unsigned int type)
72 allocList = new(AllocList);
75 info.file = strdup(fname);
79 info.btcount = backtrace( info.backtrace, BACKTRACE_DEPTH );
80 singleLock s(memLock);
81 (*allocList)[addr]=info;
84 static inline void RemoveTrack(unsigned int addr, unsigned int type)
88 AllocList::iterator i;
89 singleLock s(memLock);
90 i = allocList->find(addr);
91 if ( i != allocList->end() )
93 if ( i->second.type != type )
103 inline void * operator new(unsigned int size, const char *file, int line)
105 void *ptr = (void *)malloc(size);
106 AddTrack((unsigned int)ptr, size, file, line, 1);
110 inline void operator delete(void *p)
112 RemoveTrack((unsigned int)p,1);
116 inline void * operator new[](unsigned int size, const char *file, int line)
118 void *ptr = (void *)malloc(size);
119 AddTrack((unsigned int)ptr, size, file, line, 2);
123 inline void operator delete[](void *p)
125 RemoveTrack((unsigned int)p, 2);
129 inline void DumpUnfreed()
131 AllocList::iterator i;
132 unsigned int totalSize = 0;
137 for(i = allocList->begin(); i != allocList->end(); i++)
140 printf("%s\tLINE %d\tADDRESS %p\t%d unfreed\ttype %d\n",
141 i->second.file, i->second.line, (void*)i->second.address, i->second.size, i->second.type);
142 totalSize += i->second.size;
143 char **bt_string = backtrace_symbols( i->second.backtrace, i->second.btcount );
144 for ( tmp=0; tmp < i->second.btcount; tmp++ )
146 if ( bt_string[tmp] )
149 char *beg = strchr(bt_string[tmp], '(');
152 std::string tmp1(beg+1);
153 int pos1=tmp1.find('+'), pos2=tmp1.find(')');
154 std::string tmp2(tmp1.substr(pos1,(pos2-pos1)-1));
155 std::string cmd="c++filt ";
156 cmd+=tmp1.substr(0,pos1);
157 FILE *f = popen(cmd.c_str(), "r");
161 size_t rd = fread(buf, 1, 255, f);
165 printf("%s %s\n", buf, tmp2.c_str() );
168 printf("%s\n", tmp1.substr(0,pos1).c_str());
173 #endif // HAVE_CPP_FILT
174 printf("%s\n", bt_string[tmp]);
181 printf("-----------------------------------------------------------\n");
182 printf("Total Unfreed: %d bytes\n", totalSize);
185 #define new new(__FILE__, __LINE__)
187 #endif // MEMLEAK_CHECK
191 inline void eDebug(const char* fmt, ...)
195 inline void eDebugNoNewLine(const char* fmt, ...)
199 inline void eWarning(const char* fmt, ...)
202 #define ASSERT(x) do { } while (0)
205 #endif // __E_ERROR__