optimize code
authorAndreas Monzner <andreas.monzner@multimedia-labs.de>
Fri, 21 Apr 2006 10:57:34 +0000 (10:57 +0000)
committerAndreas Monzner <andreas.monzner@multimedia-labs.de>
Fri, 21 Apr 2006 10:57:34 +0000 (10:57 +0000)
lib/base/eerror.cpp
lib/base/eerror.h

index 3239ddc54023450b96314c78d63031146573ab2a..3fc38ade39af12b81c033934dc74194e9edaa2f3 100644 (file)
 AllocList *allocList;
 pthread_mutex_t memLock =
        PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
 AllocList *allocList;
 pthread_mutex_t memLock =
        PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+
+void DumpUnfreed()
+{
+       AllocList::iterator i;
+       unsigned int totalSize = 0;
+
+       if(!allocList)
+               return;
+
+       size_t len = 1024;
+       char *buffer = (char*)malloc(1024);
+       for(i = allocList->begin(); i != allocList->end(); i++)
+       {
+               unsigned int tmp;
+               printf("%s\tLINE %d\tADDRESS %p\t%d unfreed\ttype %d (btcount %d)\n",
+                       i->second.file, i->second.line, (void*)i->second.address, i->second.size, i->second.type, i->second.btcount);
+               totalSize += i->second.size;
+               char **bt_string = backtrace_symbols( i->second.backtrace, i->second.btcount );
+               for ( tmp=0; tmp < i->second.btcount; tmp++ )
+               {
+                       if ( bt_string[tmp] )
+                       {
+                               char *beg = strchr(bt_string[tmp], '(');
+                               if ( beg )
+                               {
+                                       std::string tmp1(beg+1);
+                                       int pos1=tmp1.find('+'), pos2=tmp1.find(')');
+                                       if ( pos1 != std::string::npos && pos2 != std::string::npos )
+                                       {
+                                               std::string tmp2(tmp1.substr(pos1,(pos2-pos1)));
+                                               tmp1.erase(pos1);
+                                               if (tmp1.length())
+                                               {
+                                                       int state;
+                                                       abi::__cxa_demangle(tmp1.c_str(), buffer, &len, &state);
+                                                       if (!state)
+                                                               printf("%d %s%s\n", tmp, buffer,tmp2.c_str());
+                                                       else
+                                                               printf("%d %s\n", tmp, bt_string[tmp]);
+                                               }
+                                       }
+                               }
+                               else
+                                       printf("%d %s\n", tmp, bt_string[tmp]);
+                       }
+               }
+               free(bt_string);
+               printf("\n");
+       }
+       free(buffer);
+
+       printf("-----------------------------------------------------------\n");
+       printf("Total Unfreed: %d bytes\n", totalSize);
+       fflush(stdout);
+};
 #else
        #include <lib/base/elock.h>
 #endif
 #else
        #include <lib/base/elock.h>
 #endif
index 36e43388403f8087ad361681b008d92bb076c4e3..6d38bcc74ec723ae2034e9f2ad22810ae4e1814d 100644 (file)
@@ -7,18 +7,16 @@
 #include <libsig_comp.h>
 
 // to use memleak check change the following in configure.ac
 #include <libsig_comp.h>
 
 // to use memleak check change the following in configure.ac
-// * add -rdynamic to LD_FLAGS
-// * add -DMEMLEAK_CHECK to CPP_FLAGS
+// * add -DMEMLEAK_CHECK and -rdynamic to CPP_FLAGS
 
 #ifdef MEMLEAK_CHECK
 #define BACKTRACE_DEPTH 5
 
 #ifdef MEMLEAK_CHECK
 #define BACKTRACE_DEPTH 5
-// when you have c++filt and corresponding libs on your platform
-// then add -DHAVE_CPP_FILT to CPP_FLAGS in configure.ac
 #include <map>
 #include <lib/base/elock.h>
 #include <execinfo.h>
 #include <string>
 #include <new>
 #include <map>
 #include <lib/base/elock.h>
 #include <execinfo.h>
 #include <string>
 #include <new>
+#include <cxxabi.h>
 #endif // MEMLEAK_CHECK
 
 #ifndef NULL
 #endif // MEMLEAK_CHECK
 
 #ifndef NULL
@@ -131,67 +129,11 @@ inline void operator delete[](void *p)
        free(p);
 };
 
        free(p);
 };
 
-inline void DumpUnfreed()
-{
-       AllocList::iterator i;
-       unsigned int totalSize = 0;
-
-       if(!allocList)
-               return;
-
-       for(i = allocList->begin(); i != allocList->end(); i++)
-       {
-               unsigned int tmp;
-               printf("%s\tLINE %d\tADDRESS %p\t%d unfreed\ttype %d\n",
-                       i->second.file, i->second.line, (void*)i->second.address, i->second.size, i->second.type);
-               totalSize += i->second.size;
-               char **bt_string = backtrace_symbols( i->second.backtrace, i->second.btcount );
-               for ( tmp=0; tmp < i->second.btcount; tmp++ )
-               {
-                       if ( bt_string[tmp] )
-                       {
-#ifdef HAVE_CPP_FILT
-                               char *beg = strchr(bt_string[tmp], '(');
-                               if ( beg )
-                               {
-                                       std::string tmp1(beg+1);
-                                       int pos1=tmp1.find('+'), pos2=tmp1.find(')');
-                                       std::string tmp2(tmp1.substr(pos1,(pos2-pos1)-1));
-                                       std::string cmd="c++filt ";
-                                       cmd+=tmp1.substr(0,pos1);
-                                       FILE *f = popen(cmd.c_str(), "r");
-                                       char buf[256];
-                                       if (f)
-                                       {
-                                               size_t rd = fread(buf, 1, 255, f);
-                                               if ( rd > 0 )
-                                               {
-                                                       buf[rd-1]=0;
-                                                       printf("%s %s\n", buf, tmp2.c_str() );
-                                               }
-                                               else
-                                                       printf("%s\n", tmp1.substr(0,pos1).c_str());
-                                               fclose(f);
-                                       }
-                               }
-                               else
-#endif // HAVE_CPP_FILT
-                                       printf("%s\n", bt_string[tmp]);
-                       }
-               }
-               free(bt_string);
-               printf("\n");
-       }
-
-       printf("-----------------------------------------------------------\n");
-       printf("Total Unfreed: %d bytes\n", totalSize);
-       fflush(stdout);
-};
+void DumpUnfreed();
 #define new new(__FILE__, __LINE__)
 
 #endif // MEMLEAK_CHECK
 
 #define new new(__FILE__, __LINE__)
 
 #endif // MEMLEAK_CHECK
 
-
 #else
     inline void eDebug(const char* fmt, ...)
     {
 #else
     inline void eDebug(const char* fmt, ...)
     {