7 #include <libsig_comp.h>
9 // to use memleak check change the following in configure.ac
10 // * add -rdynamic to LD_FLAGS
11 // * add -DMEMLEAK_CHECK to CPP_FLAGS
14 #define BACKTRACE_DEPTH 5
15 // when you have c++filt and corresponding libs on your platform
16 // then add -DHAVE_CPP_FILT to CPP_FLAGS in configure.ac
18 #include <lib/base/elock.h>
22 #endif // MEMLEAK_CHECK
28 void eFatal(const char* fmt, ...);
30 enum { lvlDebug=1, lvlWarning=2, lvlFatal=4 };
33 extern Signal2<void, int, const std::string&> logOutput;
34 extern int logOutputConsole;
42 void eDebug(const char* fmt, ...);
43 void eDebugNoNewLine(const char* fmt, ...);
44 void eWarning(const char* fmt, ...);
46 #define ASSERT(x) { if (!(x)) eFatal("%s:%d ASSERTION %s FAILED!", __FILE__, __LINE__, #x); }
55 void *backtrace[BACKTRACE_DEPTH];
56 unsigned char btcount;
61 typedef std::map<unsigned int, ALLOC_INFO> AllocList;
63 extern AllocList *allocList;
64 extern pthread_mutex_t memLock;
66 static inline void AddTrack(unsigned int addr, unsigned int asize, const char *fname, unsigned int lnum, unsigned int type)
71 allocList = new(AllocList);
74 info.file = strdup(fname);
78 info.btcount = backtrace( info.backtrace, BACKTRACE_DEPTH );
79 singleLock s(memLock);
80 (*allocList)[addr]=info;
83 static inline void RemoveTrack(unsigned int addr, unsigned int type)
87 AllocList::iterator i;
88 singleLock s(memLock);
89 i = allocList->find(addr);
90 if ( i != allocList->end() )
92 if ( i->second.type != type )
102 inline void * operator new(unsigned int size, const char *file, int line)
104 void *ptr = (void *)malloc(size);
105 AddTrack((unsigned int)ptr, size, file, line, 1);
109 inline void operator delete(void *p)
111 RemoveTrack((unsigned int)p,1);
115 inline void * operator new[](unsigned int size, const char *file, int line)
117 void *ptr = (void *)malloc(size);
118 AddTrack((unsigned int)ptr, size, file, line, 2);
122 inline void operator delete[](void *p)
124 RemoveTrack((unsigned int)p, 2);
128 inline void DumpUnfreed()
130 AllocList::iterator i;
131 unsigned int totalSize = 0;
136 for(i = allocList->begin(); i != allocList->end(); i++)
139 printf("%s\tLINE %d\tADDRESS %p\t%d unfreed\ttype %d\n",
140 i->second.file, i->second.line, (void*)i->second.address, i->second.size, i->second.type);
141 totalSize += i->second.size;
142 char **bt_string = backtrace_symbols( i->second.backtrace, i->second.btcount );
143 for ( tmp=0; tmp < i->second.btcount; tmp++ )
145 if ( bt_string[tmp] )
148 char *beg = strchr(bt_string[tmp], '(');
151 std::string tmp1(beg+1);
152 int pos1=tmp1.find('+'), pos2=tmp1.find(')');
153 std::string tmp2(tmp1.substr(pos1,(pos2-pos1)-1));
154 std::string cmd="c++filt ";
155 cmd+=tmp1.substr(0,pos1);
156 FILE *f = popen(cmd.c_str(), "r");
160 size_t rd = fread(buf, 1, 255, f);
164 printf("%s %s\n", buf, tmp2.c_str() );
167 printf("%s\n", tmp1.substr(0,pos1).c_str());
172 #endif // HAVE_CPP_FILT
173 printf("%s\n", bt_string[tmp]);
180 printf("-----------------------------------------------------------\n");
181 printf("Total Unfreed: %d bytes\n", totalSize);
184 #define new new(__FILE__, __LINE__)
186 #endif // MEMLEAK_CHECK
190 inline void eDebug(const char* fmt, ...)
194 inline void eDebugNoNewLine(const char* fmt, ...)
198 inline void eWarning(const char* fmt, ...)
201 #define ASSERT(x) do { } while (0)
204 #endif // __E_ERROR__