cleanup
[vuplus_dvbapp] / 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 -DMEMLEAK_CHECK and -rdynamic to CPP_FLAGS
11
12 #ifdef MEMLEAK_CHECK
13 #define BACKTRACE_DEPTH 5
14 #include <map>
15 #include <lib/base/elock.h>
16 #include <execinfo.h>
17 #include <string>
18 #include <new>
19 #include <cxxabi.h>
20 #endif // MEMLEAK_CHECK
21
22 #ifndef NULL
23 #define NULL 0
24 #endif
25
26 #ifdef ASSERT
27 #undef ASSERT
28 #endif
29
30 #ifndef SWIG
31
32 #define CHECKFORMAT __attribute__ ((__format__(__printf__, 1, 2)))
33
34 extern Signal2<void, int, const std::string&> logOutput;
35 extern int logOutputConsole;
36
37 void CHECKFORMAT eFatal(const char*, ...);
38 enum { lvlDebug=1, lvlWarning=2, lvlFatal=4 };
39
40 #ifdef DEBUG
41     void CHECKFORMAT eDebug(const char*, ...);
42     void CHECKFORMAT eDebugNoNewLine(const char*, ...);
43     void CHECKFORMAT eWarning(const char*, ...);
44     #define ASSERT(x) { if (!(x)) eFatal("%s:%d ASSERTION %s FAILED!", __FILE__, __LINE__, #x); }
45
46 #ifdef MEMLEAK_CHECK
47 typedef struct
48 {
49         unsigned int address;
50         unsigned int size;
51         char *file;
52         void *backtrace[BACKTRACE_DEPTH];
53         unsigned char btcount;
54         unsigned short line;
55         unsigned char type;
56 } ALLOC_INFO;
57
58 typedef std::map<unsigned int, ALLOC_INFO> AllocList;
59
60 extern AllocList *allocList;
61 extern pthread_mutex_t memLock;
62
63 static inline void AddTrack(unsigned int addr,  unsigned int asize,  const char *fname, unsigned int lnum, unsigned int type)
64 {
65         ALLOC_INFO info;
66
67         if(!allocList)
68                 allocList = new(AllocList);
69
70         info.address = addr;
71         info.file = strdup(fname);
72         info.line = lnum;
73         info.size = asize;
74         info.type = type;
75         info.btcount = backtrace( info.backtrace, BACKTRACE_DEPTH );
76         singleLock s(memLock);
77         (*allocList)[addr]=info;
78 };
79
80 static inline void RemoveTrack(unsigned int addr, unsigned int type)
81 {
82         if(!allocList)
83                 return;
84         AllocList::iterator i;
85         singleLock s(memLock);
86         i = allocList->find(addr);
87         if ( i != allocList->end() )
88         {
89                 if ( i->second.type != type )
90                         i->second.type=3;
91                 else
92                 {
93                         free(i->second.file);
94                         allocList->erase(i);
95                 }
96         }
97 };
98
99 inline void * operator new(unsigned int size, const char *file, int line)
100 {
101         void *ptr = (void *)malloc(size);
102         AddTrack((unsigned int)ptr, size, file, line, 1);
103         return(ptr);
104 };
105
106 inline void operator delete(void *p)
107 {
108         RemoveTrack((unsigned int)p,1);
109         free(p);
110 };
111
112 inline void * operator new[](unsigned int size, const char *file, int line)
113 {
114         void *ptr = (void *)malloc(size);
115         AddTrack((unsigned int)ptr, size, file, line, 2);
116         return(ptr);
117 };
118
119 inline void operator delete[](void *p)
120 {
121         RemoveTrack((unsigned int)p, 2);
122         free(p);
123 };
124
125 void DumpUnfreed();
126 #define new new(__FILE__, __LINE__)
127
128 #endif // MEMLEAK_CHECK
129
130 #else  // DEBUG
131     inline void eDebug(const char* fmt, ...)
132     {
133     }
134
135     inline void eDebugNoNewLine(const char* fmt, ...)
136     {
137     }
138
139     inline void eWarning(const char* fmt, ...)
140     {
141     }
142     #define ASSERT(x) do { } while (0)
143 #endif //DEBUG
144
145 void eWriteCrashdump();
146
147 #endif // SWIG
148
149 void ePythonOutput(const char *);
150
151 #endif // __E_ERROR__