Add debug function and some assert to lttv_hook_remove_list
[lttv.git] / lttv / lttv / hook.c
CommitLineData
9c312311 1/* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
16 * MA 02111-1307, USA.
17 */
18
4e4d11b3 19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
ffd54a90 22
3d218f2a 23#include <lttv/hook.h>
1d1df11d 24#include <ltt/compiler.h>
9d239bd9 25#include <ltt/ltt.h>
eccb5352 26
dc877563 27typedef struct _LttvHookClosure {
90e19f82
AM
28 LttvHook hook;
29 void *hook_data;
30 LttvHookPrio prio;
31 guint ref_count;
dc877563 32} LttvHookClosure;
eccb5352 33
47e76340 34gint lttv_hooks_prio_compare(LttvHookClosure *a, LttvHookClosure *b)
35{
90e19f82
AM
36 gint ret=0;
37 if(a->prio < b->prio) ret = -1;
38 else if(a->prio > b->prio) ret = 1;
39 return ret;
47e76340 40}
41
eccb5352 42
7a4bdb54 43LttvHooks *lttv_hooks_new(void)
dc877563 44{
90e19f82 45 return g_array_new(FALSE, FALSE, sizeof(LttvHookClosure));
eccb5352 46}
47
dc877563 48
49void lttv_hooks_destroy(LttvHooks *h)
50{
90e19f82
AM
51 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "lttv_hooks_destroy()");
52 g_array_free(h, TRUE);
eccb5352 53}
54
eccb5352 55
47e76340 56void lttv_hooks_add(LttvHooks *h, LttvHook f, void *hook_data, LttvHookPrio p)
dc877563 57{
90e19f82
AM
58 LttvHookClosure *c, new_c;
59 guint i;
60
61 if(unlikely(h == NULL))g_error("Null hook added");
62
63 new_c.hook = f;
64 new_c.hook_data = hook_data;
65 new_c.prio = p;
66 new_c.ref_count = 1;
67
68 /* Preliminary check for duplication */
69 /* only hook and hook data is checked */
70 for(i = 0; i < h->len; i++) {
71 c = &g_array_index(h, LttvHookClosure, i);
72 if(new_c.hook == c->hook && new_c.hook_data == c->hook_data) {
73 g_assert(new_c.prio == c->prio);
74 c->ref_count++;
75 return;
76 }
77 }
78
79
80 for(i = 0; i < h->len; i++) {
81 c = &g_array_index(h, LttvHookClosure, i);
82 if(new_c.prio < c->prio) {
83 g_array_insert_val(h,i,new_c);
84 return;
85 }
86 }
87 if(i == h->len)
88 g_array_append_val(h,new_c);
eccb5352 89}
90
bb545b3c 91/* lttv_hooks_add_list
92 *
93 * Adds a sorted list into another sorted list.
94 *
95 * Note : h->len is modified, but only incremented. This assures
96 * its coherence through the function.
97 *
98 * j is an index to the element following the last one added in the
99 * destination array.
100 */
101void lttv_hooks_add_list(LttvHooks *h, const LttvHooks *list)
dc877563 102{
90e19f82
AM
103 guint i,j,k;
104 LttvHookClosure *c;
105 const LttvHookClosure *new_c;
106
107 if(unlikely(list == NULL)) return;
108
109 for(i = 0, j = 0 ; i < list->len; i++) {
110 new_c = &g_array_index(list, LttvHookClosure, i);
111 gboolean found=FALSE;
112
113 /* Preliminary check for duplication */
114 /* only hook and hook data is checked, not priority */
115 for(k = 0; k < h->len; k++) {
116 c = &g_array_index(h, LttvHookClosure, k);
117 if(new_c->hook == c->hook && new_c->hook_data == c->hook_data) {
118 /* Found another identical entry : increment its ref_count and
119 * jump over the source index */
120 g_assert(new_c->prio == c->prio);
121 found=TRUE;
122 c->ref_count++;
123 break;
124 }
125 }
126
127 if(!found) {
128 /* If not found, add it to the destination array */
129 while(j < h->len) {
130 c = &g_array_index(h, LttvHookClosure, j);
131 if(new_c->prio < c->prio) {
132 g_array_insert_val(h,j,*new_c);
133 j++;
134 break;
135 }
136 else j++;
137 }
138 if(j == h->len) {
139 g_array_append_val(h,*new_c);
140 j++;
141 }
142 }
143 }
dc877563 144}
145
146
147void *lttv_hooks_remove(LttvHooks *h, LttvHook f)
148{
90e19f82
AM
149 unsigned i;
150
151 void *hook_data;
152
153 LttvHookClosure *c;
154
155 for(i = 0 ; i < h->len ; i++) {
156 c = &g_array_index(h, LttvHookClosure, i);
157 if(c->hook == f) {
158 if(c->ref_count == 1) {
159 hook_data = c->hook_data;
160 lttv_hooks_remove_by_position(h, i);
161 return hook_data;
162 } else {
163 g_assert(c->ref_count != 0);
164 c->ref_count--;
165 return NULL; /* We do not want anyone to free a hook_data
166 still referenced */
167 }
168 }
169 }
170 return NULL;
dc877563 171}
172
173
174void lttv_hooks_remove_data(LttvHooks *h, LttvHook f, void *hook_data)
eccb5352 175{
90e19f82
AM
176 unsigned i;
177
178 LttvHookClosure *c;
179
180 for(i = 0 ; i < h->len ; i++) {
181 c = &g_array_index(h, LttvHookClosure, i);
182 if(c->hook == f && c->hook_data == hook_data) {
183 if(c->ref_count == 1) {
184 lttv_hooks_remove_by_position(h, i);
185 return;
186 } else {
187 g_assert(c->ref_count != 0);
188 c->ref_count--;
189 return;
190 }
191 }
192 }
dc877563 193}
194
195
27fb4fd2 196void lttv_hooks_remove_list(LttvHooks *h, const LttvHooks *list)
dc877563 197{
90e19f82
AM
198 guint i, j;
199
200 LttvHookClosure *c, *c_list;
201
202 if(list == NULL) return;
27fb4fd2
YB
203 g_assert(h != list);
204 /* This iteration assume that both list are in the same order */
90e19f82
AM
205 for(i = 0, j = 0 ; i < h->len && j < list->len ;) {
206 c = &g_array_index(h, LttvHookClosure, i);
207 c_list = &g_array_index(list, LttvHookClosure, j);
208 if(c->hook == c_list->hook && c->hook_data == c_list->hook_data) {
209 if(c->ref_count == 1) {
27fb4fd2 210 int count = h->len;
90e19f82 211 lttv_hooks_remove_by_position(h, i);
27fb4fd2 212 g_assert((count-1) == h->len);
90e19f82
AM
213 } else {
214 g_assert(c->ref_count != 0);
215 c->ref_count--;
216 }
217 j++;
218 }
219 else i++;
220 }
221
222 /* Normally the hooks in h are ordered as in list. If this is not the case,
223 try harder here. */
224
225 if(unlikely(j < list->len)) {
226 for(; j < list->len ; j++) {
227 c_list = &g_array_index(list, LttvHookClosure, j);
228 lttv_hooks_remove_data(h, c_list->hook, c_list->hook_data);
229 }
230 }
eccb5352 231}
232
233
dc877563 234unsigned lttv_hooks_number(LttvHooks *h)
235{
90e19f82 236 return h->len;
dc877563 237}
eccb5352 238
dc877563 239
47e76340 240void lttv_hooks_get(LttvHooks *h, unsigned i, LttvHook *f, void **hook_data,
90e19f82 241 LttvHookPrio *p)
dc877563 242{
90e19f82
AM
243 LttvHookClosure *c;
244
245 if(unlikely(i >= h->len))
246 {
247 *f = NULL;
248 *hook_data = NULL;
249 *p = 0;
250 return;
251 }
252
253 c = &g_array_index(h, LttvHookClosure, i);
254 *f = c->hook;
255 *hook_data = c->hook_data;
256 *p = c->prio;
dc877563 257}
258
259
ffd54a90 260void lttv_hooks_remove_by_position(LttvHooks *h, unsigned i)
dc877563 261{
90e19f82 262 g_array_remove_index(h, i);
dc877563 263}
264
dc877563 265gboolean lttv_hooks_call(LttvHooks *h, void *call_data)
266{
90e19f82 267 gboolean ret, sum_ret = FALSE;
dc877563 268
90e19f82 269 LttvHookClosure *c;
dc877563 270
90e19f82 271 guint i;
ffd54a90 272
90e19f82
AM
273 if(likely(h != NULL)) {
274 for(i = 0 ; i < h->len ; i++) {
275 c = &g_array_index(h, LttvHookClosure, i);
276 ret = c->hook(c->hook_data,call_data);
277 sum_ret = sum_ret || ret;
278 }
279 }
280 return sum_ret;
dc877563 281}
282
283
284gboolean lttv_hooks_call_check(LttvHooks *h, void *call_data)
285{
90e19f82 286 LttvHookClosure *c;
dc877563 287
90e19f82 288 guint i;
ffd54a90 289
90e19f82
AM
290 for(i = 0 ; i < h->len ; i++) {
291 c = &g_array_index(h, LttvHookClosure, i);
292 if(unlikely(c->hook(c->hook_data,call_data))) return TRUE;
293 }
294 return FALSE;
dc877563 295}
296
1d1df11d 297/* Optimised for h1 == NULL, h2 != NULL. This is the case
298 * for optimised computation (with specific by id hooks, but
299 * no main hooks).
300 *
301 * The second case that should occur the most often is
302 * h1 != NULL , h2 == NULL.
303 */
78b82ded 304gint lttv_hooks_call_merge(LttvHooks *h1, void *call_data1,
90e19f82 305 LttvHooks *h2, void *call_data2)
47e76340 306{
90e19f82
AM
307 gint ret, sum_ret = 0;
308
309 LttvHookClosure *c1, *c2;
310
311 guint i, j;
312
313 if(unlikely(h1 != NULL)) {
314 if(unlikely(h2 != NULL)) {
315 for(i = 0, j = 0 ; i < h1->len && j < h2->len ;) {
316 c1 = &g_array_index(h1, LttvHookClosure, i);
317 c2 = &g_array_index(h2, LttvHookClosure, j);
318 if(c1->prio <= c2->prio) {
319 ret = c1->hook(c1->hook_data,call_data1);
320 sum_ret = sum_ret | ret;
321 i++;
322 } else {
323 ret = c2->hook(c2->hook_data,call_data2);
324 sum_ret = sum_ret | ret;
325 j++;
326 }
327 }
328 /* Finish the last list with hooks left */
329 for(;i < h1->len; i++) {
330 c1 = &g_array_index(h1, LttvHookClosure, i);
331 ret = c1->hook(c1->hook_data,call_data1);
332 sum_ret = sum_ret | ret;
333 }
334 for(;j < h2->len; j++) {
335 c2 = &g_array_index(h2, LttvHookClosure, j);
336 ret = c2->hook(c2->hook_data,call_data2);
337 sum_ret = sum_ret | ret;
338 }
339 } else { /* h1 != NULL && h2 == NULL */
340 for(i = 0 ; i < h1->len ; i++) {
341 c1 = &g_array_index(h1, LttvHookClosure, i);
342 ret = c1->hook(c1->hook_data,call_data1);
343 sum_ret = sum_ret | ret;
344 }
345 }
346 } else if(likely(h2 != NULL)) { /* h1 == NULL && h2 != NULL */
347 for(j = 0 ; j < h2->len ; j++) {
348 c2 = &g_array_index(h2, LttvHookClosure, j);
349 ret = c2->hook(c2->hook_data,call_data2);
350 sum_ret = sum_ret | ret;
351 }
352 }
353
354 return sum_ret;
47e76340 355}
356
357gboolean lttv_hooks_call_check_merge(LttvHooks *h1, void *call_data1,
90e19f82 358 LttvHooks *h2, void *call_data2)
47e76340 359{
90e19f82
AM
360 LttvHookClosure *c1, *c2;
361
362 guint i, j;
363
364 if(unlikely(h1 != NULL)) {
365 if(unlikely(h2 != NULL)) {
366 for(i = 0, j = 0 ; i < h1->len && j < h2->len ;) {
367 c1 = &g_array_index(h1, LttvHookClosure, i);
368 c2 = &g_array_index(h2, LttvHookClosure, j);
369 if(c1->prio <= c2->prio) {
370 if(c1->hook(c1->hook_data,call_data1)) return TRUE;
371 i++;
372 } else {
373 if(c2->hook(c2->hook_data,call_data2)) return TRUE;
374 j++;
375 }
376 }
377 /* Finish the last list with hooks left */
378 for(;i < h1->len; i++) {
379 c1 = &g_array_index(h1, LttvHookClosure, i);
380 if(c1->hook(c1->hook_data,call_data1)) return TRUE;
381 }
382 for(;j < h2->len; j++) {
383 c2 = &g_array_index(h2, LttvHookClosure, j);
384 if(c2->hook(c2->hook_data,call_data2)) return TRUE;
385 }
386 } else { /* h2 == NULL && h1 != NULL */
387 for(i = 0 ; i < h1->len ; i++) {
388 c1 = &g_array_index(h1, LttvHookClosure, i);
389 if(c1->hook(c1->hook_data,call_data1)) return TRUE;
390 }
391 }
392 } else if(likely(h2 != NULL)) { /* h1 == NULL && h2 != NULL */
393 for(j = 0 ; j < h2->len ; j++) {
394 c2 = &g_array_index(h2, LttvHookClosure, j);
395 if(c2->hook(c2->hook_data,call_data2)) return TRUE;
396 }
397 }
398
399 return FALSE;
750eb11a 400}
27fb4fd2
YB
401
402
403void lttv_hooks_print(const LttvHooks *h)
404{
405 gboolean ret, sum_ret = FALSE;
406
407 LttvHookClosure *c;
408
409 guint i;
410
411 if(likely(h != NULL)) {
412 for(i = 0 ; i < h->len ; i++) {
413 c = &g_array_index(h, LttvHookClosure, i);
414 printf("%p:%i:%i,", c->hook, c->ref_count, c->prio);
415 }
416 printf("\n");
417 }
418
419}
This page took 0.087079 seconds and 4 git commands to generate.