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
29 #define CHECKFORMAT __attribute__ ((__format__(__printf__, 1, 2)))
34 void CHECKFORMAT eFatal(const char*, ...);
36 enum { lvlDebug=1, lvlWarning=2, lvlFatal=4 };
39 extern Signal2<void, int, const std::string&> logOutput;
40 extern int logOutputConsole;
48 void CHECKFORMAT eDebug(const char*, ...);
49 void CHECKFORMAT eDebugNoNewLine(const char*, ...);
50 void CHECKFORMAT eWarning(const char*, ...);
52 #define ASSERT(x) { if (!(x)) eFatal("%s:%d ASSERTION %s FAILED!", __FILE__, __LINE__, #x); }
61 void *backtrace[BACKTRACE_DEPTH];
62 unsigned char btcount;
67 typedef std::map<unsigned int, ALLOC_INFO> AllocList;
69 extern AllocList *allocList;
70 extern pthread_mutex_t memLock;
72 static inline void AddTrack(unsigned int addr, unsigned int asize, const char *fname, unsigned int lnum, unsigned int type)
77 allocList = new(AllocList);
80 info.file = strdup(fname);
84 info.btcount = backtrace( info.backtrace, BACKTRACE_DEPTH );
85 singleLock s(memLock);
86 (*allocList)[addr]=info;
89 static inline void RemoveTrack(unsigned int addr, unsigned int type)
93 AllocList::iterator i;
94 singleLock s(memLock);
95 i = allocList->find(addr);
96 if ( i != allocList->end() )
98 if ( i->second.type != type )
102 free(i->second.file);
108 inline void * operator new(unsigned int size, const char *file, int line)
110 void *ptr = (void *)malloc(size);
111 AddTrack((unsigned int)ptr, size, file, line, 1);
115 inline void operator delete(void *p)
117 RemoveTrack((unsigned int)p,1);
121 inline void * operator new[](unsigned int size, const char *file, int line)
123 void *ptr = (void *)malloc(size);
124 AddTrack((unsigned int)ptr, size, file, line, 2);
128 inline void operator delete[](void *p)
130 RemoveTrack((unsigned int)p, 2);
134 inline void DumpUnfreed()
136 AllocList::iterator i;
137 unsigned int totalSize = 0;
142 for(i = allocList->begin(); i != allocList->end(); i++)
145 printf("%s\tLINE %d\tADDRESS %p\t%d unfreed\ttype %d\n",
146 i->second.file, i->second.line, (void*)i->second.address, i->second.size, i->second.type);
147 totalSize += i->second.size;
148 char **bt_string = backtrace_symbols( i->second.backtrace, i->second.btcount );
149 for ( tmp=0; tmp < i->second.btcount; tmp++ )
151 if ( bt_string[tmp] )
154 char *beg = strchr(bt_string[tmp], '(');
157 std::string tmp1(beg+1);
158 int pos1=tmp1.find('+'), pos2=tmp1.find(')');
159 std::string tmp2(tmp1.substr(pos1,(pos2-pos1)-1));
160 std::string cmd="c++filt ";
161 cmd+=tmp1.substr(0,pos1);
162 FILE *f = popen(cmd.c_str(), "r");
166 size_t rd = fread(buf, 1, 255, f);
170 printf("%s %s\n", buf, tmp2.c_str() );
173 printf("%s\n", tmp1.substr(0,pos1).c_str());
178 #endif // HAVE_CPP_FILT
179 printf("%s\n", bt_string[tmp]);
186 printf("-----------------------------------------------------------\n");
187 printf("Total Unfreed: %d bytes\n", totalSize);
190 #define new new(__FILE__, __LINE__)
192 #endif // MEMLEAK_CHECK
196 inline void eDebug(const char* fmt, ...)
200 inline void eDebugNoNewLine(const char* fmt, ...)
204 inline void eWarning(const char* fmt, ...)
207 #define ASSERT(x) do { } while (0)
210 void ePythonOutput(const char *);
211 void eWriteCrashdump();
213 #endif // __E_ERROR__