remove generated file "config.h":
[enigma2.git] / lib / base / eerror.h
1 #ifndef __E_ERROR__
2 #define __E_ERROR__
3
4 #include <string>
5 #include <map>       
6 #include <new>
7 #include <libsig_comp.h>
8
9 // to use memleak check change the following in configure.ac
10 // * add -rdynamic to LD_FLAGS
11 // * add -DMEMLEAK_CHECK to CPP_FLAGS
12
13 #ifdef MEMLEAK_CHECK
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
17 #include <map>
18 #include <lib/base/elock.h>
19 #include <execinfo.h>
20 #include <string>
21 #include <new>
22 #endif // MEMLEAK_CHECK
23
24 #ifndef NULL
25 #define NULL 0
26 #endif
27
28 void eFatal(const char* fmt, ...);
29
30 enum { lvlDebug=1, lvlWarning=2, lvlFatal=4 };
31
32 #ifndef SWIG
33 extern Signal2<void, int, const std::string&> logOutput;
34 extern int logOutputConsole;
35 #endif
36
37 #ifdef ASSERT
38 #undef ASSERT
39 #endif
40
41 #ifdef DEBUG
42     void eDebug(const char* fmt, ...);
43     void eDebugNoNewLine(const char* fmt, ...);
44     void eWarning(const char* fmt, ...);
45 #ifndef SWIG
46     #define ASSERT(x) { if (!(x)) eFatal("%s:%d ASSERTION %s FAILED!", __FILE__, __LINE__, #x); }
47 #endif
48
49 #ifdef MEMLEAK_CHECK
50 typedef struct
51 {
52         unsigned int address;
53         unsigned int size;
54         char *file;
55         void *backtrace[BACKTRACE_DEPTH];
56         unsigned char btcount;
57         unsigned short line;
58         unsigned char type;
59 } ALLOC_INFO;
60
61 typedef std::map<unsigned int, ALLOC_INFO> AllocList;
62
63 extern AllocList *allocList;
64 extern pthread_mutex_t memLock;
65
66 static inline void AddTrack(unsigned int addr,  unsigned int asize,  const char *fname, unsigned int lnum, unsigned int type)
67 {
68         ALLOC_INFO info;
69
70         if(!allocList)
71                 allocList = new(AllocList);
72
73         info.address = addr;
74         info.file = strdup(fname);
75         info.line = lnum;
76         info.size = asize;
77         info.type = type;
78         info.btcount = backtrace( info.backtrace, BACKTRACE_DEPTH );
79         singleLock s(memLock);
80         (*allocList)[addr]=info;
81 };
82
83 static inline void RemoveTrack(unsigned int addr, unsigned int type)
84 {
85         if(!allocList)
86                 return;
87         AllocList::iterator i;
88         singleLock s(memLock);
89         i = allocList->find(addr);
90         if ( i != allocList->end() )
91         {
92                 if ( i->second.type != type )
93                         i->second.type=3;
94                 else
95                 {
96                         free(i->second.file);
97                         allocList->erase(i);
98                 }
99         }
100 };
101
102 inline void * operator new(unsigned int size, const char *file, int line)
103 {
104         void *ptr = (void *)malloc(size);
105         AddTrack((unsigned int)ptr, size, file, line, 1);
106         return(ptr);
107 };
108
109 inline void operator delete(void *p)
110 {
111         RemoveTrack((unsigned int)p,1);
112         free(p);
113 };
114
115 inline void * operator new[](unsigned int size, const char *file, int line)
116 {
117         void *ptr = (void *)malloc(size);
118         AddTrack((unsigned int)ptr, size, file, line, 2);
119         return(ptr);
120 };
121
122 inline void operator delete[](void *p)
123 {
124         RemoveTrack((unsigned int)p, 2);
125         free(p);
126 };
127
128 inline void DumpUnfreed()
129 {
130         AllocList::iterator i;
131         unsigned int totalSize = 0;
132
133         if(!allocList)
134                 return;
135
136         for(i = allocList->begin(); i != allocList->end(); i++)
137         {
138                 unsigned int tmp;
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++ )
144                 {
145                         if ( bt_string[tmp] )
146                         {
147 #ifdef HAVE_CPP_FILT
148                                 char *beg = strchr(bt_string[tmp], '(');
149                                 if ( beg )
150                                 {
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");
157                                         char buf[256];
158                                         if (f)
159                                         {
160                                                 size_t rd = fread(buf, 1, 255, f);
161                                                 if ( rd > 0 )
162                                                 {
163                                                         buf[rd-1]=0;
164                                                         printf("%s %s\n", buf, tmp2.c_str() );
165                                                 }
166                                                 else
167                                                         printf("%s\n", tmp1.substr(0,pos1).c_str());
168                                                 fclose(f);
169                                         }
170                                 }
171                                 else
172 #endif // HAVE_CPP_FILT
173                                         printf("%s\n", bt_string[tmp]);
174                         }
175                 }
176                 free(bt_string);
177                 printf("\n");
178         }
179
180         printf("-----------------------------------------------------------\n");
181         printf("Total Unfreed: %d bytes\n", totalSize);
182         fflush(stdout);
183 };
184 #define new new(__FILE__, __LINE__)
185
186 #endif // MEMLEAK_CHECK
187
188
189 #else
190     inline void eDebug(const char* fmt, ...)
191     {
192     }
193
194     inline void eDebugNoNewLine(const char* fmt, ...)
195     {
196     }
197
198     inline void eWarning(const char* fmt, ...)
199     {
200     }
201     #define ASSERT(x) do { } while (0)
202 #endif //DEBUG
203
204 #endif // __E_ERROR__