1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
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;
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.
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,
23 #include <lttv/hook.h>
24 #include <ltt/compiler.h>
27 typedef struct _LttvHookClosure
{
34 gint
lttv_hooks_prio_compare(LttvHookClosure
*a
, LttvHookClosure
*b
)
37 if(a
->prio
< b
->prio
) ret
= -1;
38 else if(a
->prio
> b
->prio
) ret
= 1;
43 LttvHooks
*lttv_hooks_new()
45 return g_array_new(FALSE
, FALSE
, sizeof(LttvHookClosure
));
49 void lttv_hooks_destroy(LttvHooks
*h
)
51 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_DEBUG
, "lttv_hooks_destroy()");
52 g_array_free(h
, TRUE
);
56 void lttv_hooks_add(LttvHooks
*h
, LttvHook f
, void *hook_data
, LttvHookPrio p
)
58 LttvHookClosure
*c
, new_c
;
61 if(unlikely(h
== NULL
))g_error("Null hook added");
64 new_c
.hook_data
= hook_data
;
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
);
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
);
88 g_array_append_val(h
,new_c
);
91 /* lttv_hooks_add_list
93 * Adds a sorted list into another sorted list.
95 * Note : h->len is modified, but only incremented. This assures
96 * its coherence through the function.
98 * j is an index to the element following the last one added in the
101 void lttv_hooks_add_list(LttvHooks
*h
, const LttvHooks
*list
)
105 const LttvHookClosure
*new_c
;
107 if(unlikely(list
== NULL
)) return;
109 for(i
= 0, j
= 0 ; i
< list
->len
; i
++) {
110 new_c
= &g_array_index(list
, LttvHookClosure
, i
);
111 gboolean found
=FALSE
;
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
);
128 /* If not found, add it to the destination array */
130 c
= &g_array_index(h
, LttvHookClosure
, j
);
131 if(new_c
->prio
< c
->prio
) {
132 g_array_insert_val(h
,j
,*new_c
);
139 g_array_append_val(h
,*new_c
);
147 void *lttv_hooks_remove(LttvHooks
*h
, LttvHook f
)
155 for(i
= 0 ; i
< h
->len
; i
++) {
156 c
= &g_array_index(h
, LttvHookClosure
, i
);
158 if(c
->ref_count
== 1) {
159 hook_data
= c
->hook_data
;
160 lttv_hooks_remove_by_position(h
, i
);
163 g_assert(c
->ref_count
!= 0);
165 return NULL
; /* We do not want anyone to free a hook_data
174 void lttv_hooks_remove_data(LttvHooks
*h
, LttvHook f
, void *hook_data
)
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
);
187 g_assert(c
->ref_count
!= 0);
196 void lttv_hooks_remove_list(LttvHooks
*h
, LttvHooks
*list
)
200 LttvHookClosure
*c
, *c_list
;
202 if(list
== NULL
) return;
203 for(i
= 0, j
= 0 ; i
< h
->len
&& j
< list
->len
;) {
204 c
= &g_array_index(h
, LttvHookClosure
, i
);
205 c_list
= &g_array_index(list
, LttvHookClosure
, j
);
206 if(c
->hook
== c_list
->hook
&& c
->hook_data
== c_list
->hook_data
) {
207 if(c
->ref_count
== 1) {
208 lttv_hooks_remove_by_position(h
, i
);
210 g_assert(c
->ref_count
!= 0);
218 /* Normally the hooks in h are ordered as in list. If this is not the case,
221 if(unlikely(j
< list
->len
)) {
222 for(; j
< list
->len
; j
++) {
223 c_list
= &g_array_index(list
, LttvHookClosure
, j
);
224 lttv_hooks_remove_data(h
, c_list
->hook
, c_list
->hook_data
);
230 unsigned lttv_hooks_number(LttvHooks
*h
)
236 void lttv_hooks_get(LttvHooks
*h
, unsigned i
, LttvHook
*f
, void **hook_data
,
241 if(unlikely(i
>= h
->len
))
249 c
= &g_array_index(h
, LttvHookClosure
, i
);
251 *hook_data
= c
->hook_data
;
256 void lttv_hooks_remove_by_position(LttvHooks
*h
, unsigned i
)
258 g_array_remove_index(h
, i
);
261 gboolean
lttv_hooks_call(LttvHooks
*h
, void *call_data
)
263 gboolean ret
, sum_ret
= FALSE
;
269 if(likely(h
!= NULL
)) {
270 for(i
= 0 ; i
< h
->len
; i
++) {
271 c
= &g_array_index(h
, LttvHookClosure
, i
);
272 ret
= c
->hook(c
->hook_data
,call_data
);
273 sum_ret
= sum_ret
|| ret
;
280 gboolean
lttv_hooks_call_check(LttvHooks
*h
, void *call_data
)
286 for(i
= 0 ; i
< h
->len
; i
++) {
287 c
= &g_array_index(h
, LttvHookClosure
, i
);
288 if(unlikely(c
->hook(c
->hook_data
,call_data
))) return TRUE
;
293 /* Optimised for h1 == NULL, h2 != NULL. This is the case
294 * for optimised computation (with specific by id hooks, but
297 * The second case that should occur the most often is
298 * h1 != NULL , h2 == NULL.
300 gint
lttv_hooks_call_merge(LttvHooks
*h1
, void *call_data1
,
301 LttvHooks
*h2
, void *call_data2
)
303 gint ret
, sum_ret
= 0;
305 LttvHookClosure
*c1
, *c2
;
309 if(unlikely(h1
!= NULL
)) {
310 if(unlikely(h2
!= NULL
)) {
311 for(i
= 0, j
= 0 ; i
< h1
->len
&& j
< h2
->len
;) {
312 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
313 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
314 if(c1
->prio
<= c2
->prio
) {
315 ret
= c1
->hook(c1
->hook_data
,call_data1
);
316 sum_ret
= sum_ret
| ret
;
319 ret
= c2
->hook(c2
->hook_data
,call_data2
);
320 sum_ret
= sum_ret
| ret
;
324 /* Finish the last list with hooks left */
325 for(;i
< h1
->len
; i
++) {
326 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
327 ret
= c1
->hook(c1
->hook_data
,call_data1
);
328 sum_ret
= sum_ret
| ret
;
330 for(;j
< h2
->len
; j
++) {
331 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
332 ret
= c2
->hook(c2
->hook_data
,call_data2
);
333 sum_ret
= sum_ret
| ret
;
335 } else { /* h1 != NULL && h2 == NULL */
336 for(i
= 0 ; i
< h1
->len
; i
++) {
337 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
338 ret
= c1
->hook(c1
->hook_data
,call_data1
);
339 sum_ret
= sum_ret
| ret
;
342 } else if(likely(h2
!= NULL
)) { /* h1 == NULL && h2 != NULL */
343 for(j
= 0 ; j
< h2
->len
; j
++) {
344 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
345 ret
= c2
->hook(c2
->hook_data
,call_data2
);
346 sum_ret
= sum_ret
| ret
;
353 gboolean
lttv_hooks_call_check_merge(LttvHooks
*h1
, void *call_data1
,
354 LttvHooks
*h2
, void *call_data2
)
356 LttvHookClosure
*c1
, *c2
;
360 if(unlikely(h1
!= NULL
)) {
361 if(unlikely(h2
!= NULL
)) {
362 for(i
= 0, j
= 0 ; i
< h1
->len
&& j
< h2
->len
;) {
363 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
364 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
365 if(c1
->prio
<= c2
->prio
) {
366 if(c1
->hook(c1
->hook_data
,call_data1
)) return TRUE
;
369 if(c2
->hook(c2
->hook_data
,call_data2
)) return TRUE
;
373 /* Finish the last list with hooks left */
374 for(;i
< h1
->len
; i
++) {
375 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
376 if(c1
->hook(c1
->hook_data
,call_data1
)) return TRUE
;
378 for(;j
< h2
->len
; j
++) {
379 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
380 if(c2
->hook(c2
->hook_data
,call_data2
)) return TRUE
;
382 } else { /* h2 == NULL && h1 != NULL */
383 for(i
= 0 ; i
< h1
->len
; i
++) {
384 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
385 if(c1
->hook(c1
->hook_data
,call_data1
)) return TRUE
;
388 } else if(likely(h2
!= NULL
)) { /* h1 == NULL && h2 != NULL */
389 for(j
= 0 ; j
< h2
->len
; j
++) {
390 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
391 if(c2
->hook(c2
->hook_data
,call_data2
)) return TRUE
;
399 /* Two pointer arrays :
400 * * one indexed by id for quick search :
402 * typically 4 bytes * 256 facilities * 10 events = 10kbytes
403 * * another array that keeps a list of used numbers (for later deletion)
404 * size : number of ids used.
407 LttvHooksById
*lttv_hooks_by_id_new()
409 LttvHooksById
*h
= g_new(LttvHooksById
, 1);
410 h
->index
= g_ptr_array_sized_new(PREALLOC_EVENTS
);
411 h
->array
= g_array_sized_new(FALSE
, FALSE
, sizeof(guint
), PREALLOC_EVENTS
);
416 void lttv_hooks_by_id_destroy(LttvHooksById
*h
)
420 for(i
= 0 ; i
< h
->array
->len
; i
++) {
421 guint index
= g_array_index(h
->array
, guint
, i
);
422 if(h
->index
->pdata
[index
] != NULL
) { /* hook may have been removed */
423 lttv_hooks_destroy(h
->index
->pdata
[index
]);
424 h
->index
->pdata
[index
] = NULL
; /* Must be there in case of
425 multiple addition of the same index */
428 g_ptr_array_free(h
->index
, TRUE
);
429 g_array_free(h
->array
, TRUE
);
432 /* Optimised for searching an existing hook */
433 LttvHooks
*lttv_hooks_by_id_find(LttvHooksById
*h
, unsigned id
)
435 if(unlikely(h
->index
->len
<= id
)) g_ptr_array_set_size(h
->index
, id
+ 1);
436 if(unlikely(h
->index
->pdata
[id
] == NULL
)) {
437 h
->index
->pdata
[id
] = lttv_hooks_new();
438 g_array_append_val(h
->array
, id
);
440 return h
->index
->pdata
[id
];
444 unsigned lttv_hooks_by_id_max_id(LttvHooksById
*h
)
446 return h
->index
->len
;
449 /* We don't bother removing the used slot array id : lttv_hooks_by_id_destroy is
450 * almost never called and is able to deal with used slot repetition. */
451 void lttv_hooks_by_id_remove(LttvHooksById
*h
, unsigned id
)
453 if(likely(id
< h
->index
->len
&& h
->index
->pdata
[id
] != NULL
)) {
454 lttv_hooks_destroy((LttvHooks
*)h
->index
->pdata
[id
]);
455 h
->index
->pdata
[id
] = NULL
;
459 void lttv_hooks_by_id_copy(LttvHooksById
*dest
, LttvHooksById
*src
)
463 for(i
= 0 ; i
< src
->array
->len
; i
++) {
464 guint index
= g_array_index(src
->array
, guint
, i
);
465 LttvHooks
*srch
= lttv_hooks_by_id_find(src
, index
);
466 LttvHooks
*desth
= lttv_hooks_by_id_find(dest
, index
);
467 lttv_hooks_add_list(desth
, srch
);
471 LttvHooksByIdChannelArray
*lttv_hooks_by_id_channel_new(void)
473 LttvHooksByIdChannelArray
*h
= g_new(LttvHooksByIdChannelArray
, 1);
475 h
->array
= g_array_new(FALSE
, FALSE
, sizeof(LttvHooksByIdChannel
));
479 void lttv_hooks_by_id_channel_destroy(LttvHooksByIdChannelArray
*h
)
481 LttvHooksByIdChannel
*hid
;
484 for (i
= 0; i
< h
->array
->len
; i
++) {
485 hid
= &g_array_index(h
->array
, LttvHooksByIdChannel
, i
);
486 lttv_hooks_by_id_destroy(hid
->hooks_by_id
);
488 g_array_free(h
->array
, TRUE
);
492 static LttvHooksByIdChannel
*
493 lttv_hooks_by_id_channel_find_channel(LttvHooksByIdChannelArray
*h
,
496 LttvHooksByIdChannel
*hid
;
499 for (i
= 0; i
< h
->array
->len
; i
++) {
500 hid
= &g_array_index(h
->array
, LttvHooksByIdChannel
, i
);
501 if (hid
->channel
== channel
) {
507 g_array_set_size(h
->array
, h
->array
->len
+ 1);
508 hid
= &g_array_index(h
->array
, LttvHooksByIdChannel
, h
->array
->len
- 1);
509 hid
->channel
= channel
;
510 hid
->hooks_by_id
= lttv_hooks_by_id_new();
515 /* get, or create if not found */
516 LttvHooks
*lttv_hooks_by_id_channel_find(LttvHooksByIdChannelArray
*h
,
517 GQuark channel
, guint16 id
)
519 LttvHooksByIdChannel
*hid
;
520 hid
= lttv_hooks_by_id_channel_find_channel(h
, channel
);
521 return lttv_hooks_by_id_find(hid
->hooks_by_id
, id
);