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