remove whitespace at eol
[goodguy/history.git] / cinelerra-5.1 / guicast / bctrace.h
1 #ifndef __BC_TRACE_H__
2 #define __BC_TRACE_H__
3
4 #include "arraylist.h"
5 #include "linklist.h"
6 #include "bctrace.inc"
7 #include "bcwindowbase.inc"
8 #include "cstrdup.h"
9 #include <pthread.h>
10
11
12 class BC_Trace
13 {
14 public:
15         BC_Trace();
16         ~BC_Trace();
17
18         static BC_Trace *global_trace;
19         static void reset_locks();
20
21         static void delete_temps();
22         static void set_temp(char *string);
23         static void unset_temp(char *string);
24
25         static void enable_locks();
26         static void disable_locks();
27         static int set_lock(const char *title, const char *location, trace_info *info);
28         static void set_lock2(int table_id, trace_info *info);
29         static void unset_lock2(int table_id, trace_info *info);
30         static void unset_lock(trace_info *info);
31 // Used in lock destructors so takes away all references
32         static void unset_all_locks(trace_info *info);
33         static void clear_locks_tid(pthread_t tid);
34
35         static void new_trace(const char *text);
36         static void new_trace(const char *file, const char *function, int line);
37         static void delete_traces();
38
39         static void enable_memory();
40         static void disable_memory();
41         static void set_buffer(int size, void *ptr, const char* location);
42 // This one returns 1 if the buffer wasn't found.
43         static int unset_buffer(void *ptr);
44         static void lock_locks(const char *s);
45         static void unlock_locks();
46
47         static void dump_traces(FILE *fp=stdout);
48         static void dump_locks(FILE *fp=stdout);
49         static void dump_buffers(FILE *fp=stdout);
50         static void dump_threads(FILE *fp=stdout);
51 };
52
53 class bc_trace_list : public List<trace_item> {
54 public:
55         void clear() { while( last ) remove(last); }
56         bc_trace_list() {}
57         ~bc_trace_list() { clear(); }
58 };
59
60 class bc_trace_t : public bc_trace_list {
61 public:
62         int size;
63         bc_trace_t() : size(0) {}
64         ~bc_trace_t() {}
65 };
66
67 class bc_trace_spin : public bc_trace_t {
68         pthread_spinlock_t spin;
69 public:
70         void *operator new(size_t n) { return (void*) malloc(n); }
71         void operator delete(void *t, size_t n) { free(t); }
72
73         void lock() { pthread_spin_lock(&spin); }
74         void unlock() { pthread_spin_unlock(&spin); }
75         bc_trace_spin() { pthread_spin_init(&spin, PTHREAD_PROCESS_PRIVATE); }
76         ~bc_trace_spin() { pthread_spin_destroy(&spin); }
77 };
78
79 class bc_trace_mutex : public bc_trace_t {
80         pthread_mutex_t mutex;
81 public:
82         void *operator new(size_t n) { return (void*) malloc(n); }
83         void operator delete(void *t, size_t n) { free(t); }
84
85         void lock() { pthread_mutex_lock(&mutex); }
86         void unlock() { pthread_mutex_unlock(&mutex); }
87         bc_trace_mutex() { pthread_mutex_init(&mutex, 0); }
88         ~bc_trace_mutex() { pthread_mutex_destroy(&mutex); }
89 };
90
91 extern bc_trace_mutex execution_table;
92 extern bc_trace_mutex memory_table;
93 extern bc_trace_mutex lock_table;
94 extern bc_trace_mutex file_table;
95 extern "C" void dump();
96
97 class trace_item : public ListItem<trace_item> {
98 public:
99         bc_trace_t &table;
100         trace_item(bc_trace_t &t);
101         ~trace_item();
102 };
103
104 class execution_item : public trace_item {
105 public:
106         void *operator new(size_t n) { return (void*) malloc(n); }
107         void operator delete(void *t, size_t n) { free(t); }
108
109         const char *value;
110         void clear() { delete [] value;  value = 0; }
111         void set(const char *v) { delete [] value;  value = cstrdup(v); }
112
113         execution_item() : trace_item(execution_table) { value = 0; }
114         ~execution_item() { clear(); }
115 };
116
117 class lock_item : public trace_item {
118         static int table_id;
119 public:
120         void *operator new(size_t n) { return (void*) malloc(n); }
121         void operator delete(void *t, size_t n) { free(t); }
122
123         trace_info *info;
124         const char *title;
125         const char *loc;
126         int is_owner;
127         int id;
128         pthread_t tid;
129         void set(trace_info *info, const char *title, const char *loc) {
130                 this->info = info;  this->title = title;
131                 this->loc = loc;  this->is_owner = 0;
132                 this->id = table_id++;  this->tid = pthread_self();
133         }
134         void clear() {
135                 this->info = 0;  this->title = 0; this->loc = 0;
136                 this->is_owner = 0;  this->id = -1;  this->tid = 0;
137         }
138
139         lock_item() : trace_item(lock_table) { clear(); }
140         ~lock_item() {}
141 };
142
143 class memory_item : public trace_item {
144 public:
145         void *operator new(size_t n) { return (void*) malloc(n); }
146         void operator delete(void *t, size_t n) { free(t); }
147
148         int size;
149         void *ptr;
150         const char *loc;
151
152         memory_item(int size, void *ptr, const char *loc)
153          : trace_item(memory_table) {
154                 this->size = size; this->ptr = ptr; this->loc = loc;
155         }
156         ~memory_item() {}
157 };
158
159 class file_item : public trace_item {
160 public:
161         void *operator new(size_t n) { return (void*) malloc(n); }
162         void operator delete(void *t, size_t n) { free(t); }
163
164         const char *value;
165         void clear() { delete [] value;  value = 0; }
166         void set(const char *v) { delete [] value;  value = cstrdup(v); }
167
168         file_item() : trace_item(file_table) { value = 0; }
169         ~file_item() { clear(); }
170 };
171
172 // track unjoined threads at termination
173 #ifdef TRACE_THREADS
174
175 class TheLock {
176 public:
177         pthread_mutex_t the_lock;
178
179         void lock() { pthread_mutex_lock(&the_lock); }
180         void unlock() { pthread_mutex_unlock(&the_lock); }
181
182         TheLock() {
183                 pthread_mutexattr_t attr;
184                 pthread_mutexattr_init(&attr);
185                 pthread_mutex_init(&the_lock, &attr);
186         }
187         ~TheLock() {
188                 pthread_mutex_destroy(&the_lock);
189         }
190 };
191
192 class TheLocker {
193 public:
194         static TheLock the_lock;
195
196         TheLocker() { the_lock.lock(); }
197         ~TheLocker() { the_lock.unlock(); }
198 };
199
200 class TheDbg {
201 public:
202         pthread_t tid, owner;  const char *name;
203         TheDbg(pthread_t t, pthread_t o, const char *nm) { tid = t; owner = o; name = nm; }
204         ~TheDbg() {}
205 };
206
207
208 class TheList : public ArrayList<TheDbg *> {
209 public:
210         static TheList the_list;
211         static void dump_threads(FILE *fp);
212         static void dbg_add(pthread_t tid, pthread_t owner, const char *nm);
213         static void dbg_del(pthread_t tid);
214
215          TheList() {}
216         ~TheList() {
217                 TheLocker the_locker;
218                 remove_all_objects();
219         }
220 };
221
222 class TheChk {
223 public:
224         static TheChk the_chk;
225
226         TheChk() {}
227         ~TheChk() {
228                 int i = TheList::the_list.size();
229                 if( !i ) return;
230                 printf("unjoined tids / owner %d\n", i);
231                 while( --i >= 0 ) printf("  %016lx / %016lx %s\n",
232                         (unsigned long)TheList::the_list[i]->tid,
233                         (unsigned long)TheList::the_list[i]->owner,
234                         TheList::the_list[i]->name);
235         }
236 };
237
238 #endif
239
240 #endif