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,
20 #include <lttv/hook.h>
23 typedef struct _LttvHookClosure
{
30 gint
lttv_hooks_prio_compare(LttvHookClosure
*a
, LttvHookClosure
*b
)
32 if(a
->prio
< b
->prio
) return -1;
33 if(a
->prio
> b
->prio
) return 1;
38 LttvHooks
*lttv_hooks_new()
40 return g_array_new(FALSE
, FALSE
, sizeof(LttvHookClosure
));
44 void lttv_hooks_destroy(LttvHooks
*h
)
46 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_DEBUG
, "lttv_hooks_destroy()");
47 g_array_free(h
, TRUE
);
51 void lttv_hooks_add(LttvHooks
*h
, LttvHook f
, void *hook_data
, LttvHookPrio p
)
53 LttvHookClosure
*c
, new_c
;
56 if(h
== NULL
)g_error("Null hook added");
59 new_c
.hook_data
= hook_data
;
63 /* Preliminary check for duplication */
64 /* only hook and hook data is checked */
65 for(i
= 0; i
< h
->len
; i
++) {
66 c
= &g_array_index(h
, LttvHookClosure
, i
);
67 if(new_c
.hook
== c
->hook
&& new_c
.hook_data
== c
->hook_data
) {
68 g_assert(new_c
.prio
== c
->prio
);
75 for(i
= 0; i
< h
->len
; i
++) {
76 c
= &g_array_index(h
, LttvHookClosure
, i
);
77 if(new_c
.prio
< c
->prio
) {
78 g_array_insert_val(h
,i
,new_c
);
83 g_array_append_val(h
,new_c
);
86 /* lttv_hooks_add_list
88 * Adds a sorted list into another sorted list.
90 * Note : h->len is modified, but only incremented. This assures
91 * its coherence through the function.
93 * j is an index to the element following the last one added in the
96 void lttv_hooks_add_list(LttvHooks
*h
, const LttvHooks
*list
)
100 const LttvHookClosure
*new_c
;
102 if(list
== NULL
) return;
103 for(i
= 0, j
= 0 ; i
< list
->len
; i
++) {
104 new_c
= &g_array_index(list
, LttvHookClosure
, i
);
105 gboolean found
=FALSE
;
107 /* Preliminary check for duplication */
108 /* only hook and hook data is checked, not priority */
109 for(k
= 0; k
< h
->len
; k
++) {
110 c
= &g_array_index(h
, LttvHookClosure
, k
);
111 if(new_c
->hook
== c
->hook
&& new_c
->hook_data
== c
->hook_data
) {
112 /* Found another identical entry : increment its ref_count and
113 * jump over the source index */
114 g_assert(new_c
->prio
== c
->prio
);
122 /* If not found, add it to the destination array */
124 c
= &g_array_index(h
, LttvHookClosure
, j
);
125 if(new_c
->prio
< c
->prio
) {
126 g_array_insert_val(h
,j
,*new_c
);
133 g_array_append_val(h
,*new_c
);
141 void *lttv_hooks_remove(LttvHooks
*h
, LttvHook f
)
149 for(i
= 0 ; i
< h
->len
; i
++) {
150 c
= &g_array_index(h
, LttvHookClosure
, i
);
152 if(c
->ref_count
== 1) {
153 hook_data
= c
->hook_data
;
154 lttv_hooks_remove_by_position(h
, i
);
157 g_assert(c
->ref_count
!= 0);
159 return NULL
; /* We do not want anyone to free a hook_data
168 void lttv_hooks_remove_data(LttvHooks
*h
, LttvHook f
, void *hook_data
)
174 for(i
= 0 ; i
< h
->len
; i
++) {
175 c
= &g_array_index(h
, LttvHookClosure
, i
);
176 if(c
->hook
== f
&& c
->hook_data
== hook_data
) {
177 if(c
->ref_count
== 1) {
178 lttv_hooks_remove_by_position(h
, i
);
181 g_assert(c
->ref_count
!= 0);
190 void lttv_hooks_remove_list(LttvHooks
*h
, LttvHooks
*list
)
194 LttvHookClosure
*c
, *c_list
;
196 if(list
== NULL
) return;
197 for(i
= 0, j
= 0 ; i
< h
->len
&& j
< list
->len
;) {
198 c
= &g_array_index(h
, LttvHookClosure
, i
);
199 c_list
= &g_array_index(list
, LttvHookClosure
, j
);
200 if(c
->hook
== c_list
->hook
&& c
->hook_data
== c_list
->hook_data
) {
201 if(c
->ref_count
== 1) {
202 lttv_hooks_remove_by_position(h
, i
);
204 g_assert(c
->ref_count
!= 0);
212 /* Normally the hooks in h are ordered as in list. If this is not the case,
216 for(; j
< list
->len
; j
++) {
217 c_list
= &g_array_index(list
, LttvHookClosure
, j
);
218 lttv_hooks_remove_data(h
, c_list
->hook
, c_list
->hook_data
);
224 unsigned lttv_hooks_number(LttvHooks
*h
)
230 void lttv_hooks_get(LttvHooks
*h
, unsigned i
, LttvHook
*f
, void **hook_data
,
243 c
= &g_array_index(h
, LttvHookClosure
, i
);
245 *hook_data
= c
->hook_data
;
250 void lttv_hooks_remove_by_position(LttvHooks
*h
, unsigned i
)
252 g_array_remove_index(h
, i
);
255 gboolean
lttv_hooks_call(LttvHooks
*h
, void *call_data
)
257 gboolean ret
, sum_ret
= FALSE
;
264 for(i
= 0 ; i
< h
->len
; i
++) {
265 c
= &g_array_index(h
, LttvHookClosure
, i
);
266 ret
= c
->hook(c
->hook_data
,call_data
);
267 sum_ret
= sum_ret
|| ret
;
274 gboolean
lttv_hooks_call_check(LttvHooks
*h
, void *call_data
)
280 for(i
= 0 ; i
< h
->len
; i
++) {
281 c
= &g_array_index(h
, LttvHookClosure
, i
);
282 if(c
->hook(c
->hook_data
,call_data
)) return TRUE
;
287 gboolean
lttv_hooks_call_merge(LttvHooks
*h1
, void *call_data1
,
288 LttvHooks
*h2
, void *call_data2
)
290 gboolean ret
, sum_ret
= FALSE
;
292 LttvHookClosure
*c1
, *c2
;
296 if(h1
!= NULL
&& h2
!= NULL
) {
297 for(i
= 0, j
= 0 ; i
< h1
->len
&& j
< h2
->len
;) {
298 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
299 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
300 if(c1
->prio
<= c2
->prio
) {
301 ret
= c1
->hook(c1
->hook_data
,call_data1
);
302 sum_ret
= sum_ret
|| ret
;
306 ret
= c2
->hook(c2
->hook_data
,call_data2
);
307 sum_ret
= sum_ret
|| ret
;
311 /* Finish the last list with hooks left */
312 for(;i
< h1
->len
; i
++) {
313 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
314 ret
= c1
->hook(c1
->hook_data
,call_data1
);
315 sum_ret
= sum_ret
|| ret
;
317 for(;j
< h2
->len
; j
++) {
318 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
319 ret
= c2
->hook(c2
->hook_data
,call_data2
);
320 sum_ret
= sum_ret
|| ret
;
323 else if(h1
!= NULL
&& h2
== NULL
) {
324 for(i
= 0 ; i
< h1
->len
; i
++) {
325 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
326 ret
= c1
->hook(c1
->hook_data
,call_data1
);
327 sum_ret
= sum_ret
|| ret
;
330 else if(h1
== NULL
&& h2
!= NULL
) {
331 for(j
= 0 ; j
< h2
->len
; j
++) {
332 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
333 ret
= c2
->hook(c2
->hook_data
,call_data2
);
334 sum_ret
= sum_ret
|| ret
;
341 gboolean
lttv_hooks_call_check_merge(LttvHooks
*h1
, void *call_data1
,
342 LttvHooks
*h2
, void *call_data2
)
344 LttvHookClosure
*c1
, *c2
;
348 if(h1
!= NULL
&& h2
!= NULL
) {
349 for(i
= 0, j
= 0 ; i
< h1
->len
&& j
< h2
->len
;) {
350 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
351 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
352 if(c1
->prio
<= c2
->prio
) {
353 if(c1
->hook(c1
->hook_data
,call_data1
)) return TRUE
;
357 if(c2
->hook(c2
->hook_data
,call_data2
)) return TRUE
;
361 /* Finish the last list with hooks left */
362 for(;i
< h1
->len
; i
++) {
363 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
364 if(c1
->hook(c1
->hook_data
,call_data1
)) return TRUE
;
366 for(;j
< h2
->len
; j
++) {
367 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
368 if(c2
->hook(c2
->hook_data
,call_data2
)) return TRUE
;
371 else if(h1
!= NULL
&& h2
== NULL
) {
372 for(i
= 0 ; i
< h1
->len
; i
++) {
373 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
374 if(c1
->hook(c1
->hook_data
,call_data1
)) return TRUE
;
377 else if(h1
== NULL
&& h2
!= NULL
) {
378 for(j
= 0 ; j
< h2
->len
; j
++) {
379 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
380 if(c2
->hook(c2
->hook_data
,call_data2
)) return TRUE
;
389 LttvHooksById
*lttv_hooks_by_id_new()
391 return g_ptr_array_new();
395 void lttv_hooks_by_id_destroy(LttvHooksById
*h
)
399 for(i
= 0 ; i
< h
->len
; i
++) {
400 if(h
->pdata
[i
] != NULL
) lttv_hooks_destroy((LttvHooks
*)(h
->pdata
[i
]));
402 g_ptr_array_free(h
, TRUE
);
406 LttvHooks
*lttv_hooks_by_id_find(LttvHooksById
*h
, unsigned id
)
408 if(h
->len
<= id
) g_ptr_array_set_size(h
, id
+ 1);
409 if(h
->pdata
[id
] == NULL
) h
->pdata
[id
] = lttv_hooks_new();
414 unsigned lttv_hooks_by_id_max_id(LttvHooksById
*h
)
420 LttvHooks
*lttv_hooks_by_id_get(LttvHooksById
*h
, unsigned id
)
422 if(id
< h
->len
) return h
->pdata
[id
];
427 void lttv_hooks_by_id_remove(LttvHooksById
*h
, unsigned id
)
429 if(id
< h
->len
&& h
->pdata
[id
] != NULL
) {
430 lttv_hooks_destroy((LttvHooks
*)h
->pdata
[id
]);
This page took 0.056386 seconds and 4 git commands to generate.