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
{
29 gint
lttv_hooks_prio_compare(LttvHookClosure
*a
, LttvHookClosure
*b
)
31 if(a
->prio
< b
->prio
) return -1;
32 if(a
->prio
> b
->prio
) return 1;
37 LttvHooks
*lttv_hooks_new()
39 return g_array_new(FALSE
, FALSE
, sizeof(LttvHookClosure
));
43 void lttv_hooks_destroy(LttvHooks
*h
)
45 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_DEBUG
, "lttv_hooks_destroy()");
46 g_array_free(h
, TRUE
);
50 void lttv_hooks_add(LttvHooks
*h
, LttvHook f
, void *hook_data
, LttvHookPrio p
)
52 LttvHookClosure
*c
, new_c
;
55 if(h
== NULL
)g_error("Null hook added");
58 new_c
.hook_data
= hook_data
;
60 for(i
= 0; i
< h
->len
; i
++) {
62 c
= &g_array_index(h
, LttvHookClosure
, i
);
63 if(new_c
.prio
< c
->prio
) {
64 g_array_insert_val(h
,i
,new_c
);
69 g_array_append_val(h
,new_c
);
72 /* lttv_hooks_add_list
74 * Adds a sorted list into another sorted list.
76 * Note : h->len is modified, but only incremented. This assures
77 * its coherence through the function.
79 * j is an index to the element following the last one added in the
82 void lttv_hooks_add_list(LttvHooks
*h
, const LttvHooks
*list
)
86 const LttvHookClosure
*new_c
;
88 if(list
== NULL
) return;
89 for(i
= 0, j
= 0 ; i
< list
->len
; i
++) {
90 new_c
= &g_array_index(list
, LttvHookClosure
, i
);
92 c
= &g_array_index(h
, LttvHookClosure
, j
);
93 if(new_c
->prio
< c
->prio
) {
94 g_array_insert_val(h
,j
,*new_c
);
101 g_array_append_val(h
,*new_c
);
108 void *lttv_hooks_remove(LttvHooks
*h
, LttvHook f
)
116 for(i
= 0 ; i
< h
->len
; i
++) {
117 c
= &g_array_index(h
, LttvHookClosure
, i
);
119 hook_data
= c
->hook_data
;
120 lttv_hooks_remove_by_position(h
, i
);
128 void lttv_hooks_remove_data(LttvHooks
*h
, LttvHook f
, void *hook_data
)
134 for(i
= 0 ; i
< h
->len
; i
++) {
135 c
= &g_array_index(h
, LttvHookClosure
, i
);
136 if(c
->hook
== f
&& c
->hook_data
== hook_data
) {
137 lttv_hooks_remove_by_position(h
, i
);
144 void lttv_hooks_remove_list(LttvHooks
*h
, LttvHooks
*list
)
148 LttvHookClosure
*c
, *c_list
;
150 if(list
== NULL
) return;
151 for(i
= 0, j
= 0 ; i
< h
->len
&& j
< list
->len
;) {
152 c
= &g_array_index(h
, LttvHookClosure
, i
);
153 c_list
= &g_array_index(list
, LttvHookClosure
, j
);
154 if(c
->hook
== c_list
->hook
&& c
->hook_data
== c_list
->hook_data
) {
155 lttv_hooks_remove_by_position(h
, i
);
161 /* Normally the hooks in h are ordered as in list. If this is not the case,
165 for(; j
< list
->len
; j
++) {
166 c_list
= &g_array_index(list
, LttvHookClosure
, j
);
167 lttv_hooks_remove_data(h
, c_list
->hook
, c_list
->hook_data
);
173 unsigned lttv_hooks_number(LttvHooks
*h
)
179 void lttv_hooks_get(LttvHooks
*h
, unsigned i
, LttvHook
*f
, void **hook_data
,
192 c
= &g_array_index(h
, LttvHookClosure
, i
);
194 *hook_data
= c
->hook_data
;
199 void lttv_hooks_remove_by_position(LttvHooks
*h
, unsigned i
)
201 g_array_remove_index(h
, i
);
204 gboolean
lttv_hooks_call(LttvHooks
*h
, void *call_data
)
206 gboolean ret
, sum_ret
= FALSE
;
213 for(i
= 0 ; i
< h
->len
; i
++) {
214 c
= &g_array_index(h
, LttvHookClosure
, i
);
215 ret
= c
->hook(c
->hook_data
,call_data
);
216 sum_ret
= sum_ret
|| ret
;
223 gboolean
lttv_hooks_call_check(LttvHooks
*h
, void *call_data
)
229 for(i
= 0 ; i
< h
->len
; i
++) {
230 c
= &g_array_index(h
, LttvHookClosure
, i
);
231 if(c
->hook(c
->hook_data
,call_data
)) return TRUE
;
236 gboolean
lttv_hooks_call_merge(LttvHooks
*h1
, void *call_data1
,
237 LttvHooks
*h2
, void *call_data2
)
239 gboolean ret
, sum_ret
= FALSE
;
241 LttvHookClosure
*c1
, *c2
;
245 if(h1
!= NULL
&& h2
!= NULL
) {
246 for(i
= 0, j
= 0 ; i
< h1
->len
&& j
< h2
->len
;) {
247 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
248 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
249 if(c1
->prio
<= c2
->prio
) {
250 ret
= c1
->hook(c1
->hook_data
,call_data1
);
251 sum_ret
= sum_ret
|| ret
;
255 ret
= c2
->hook(c2
->hook_data
,call_data2
);
256 sum_ret
= sum_ret
|| ret
;
260 /* Finish the last list with hooks left */
261 for(;i
< h1
->len
; i
++) {
262 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
263 ret
= c1
->hook(c1
->hook_data
,call_data1
);
264 sum_ret
= sum_ret
|| ret
;
266 for(;j
< h2
->len
; j
++) {
267 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
268 ret
= c2
->hook(c2
->hook_data
,call_data2
);
269 sum_ret
= sum_ret
|| ret
;
272 else if(h1
!= NULL
&& h2
== NULL
) {
273 for(i
= 0 ; i
< h1
->len
; i
++) {
274 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
275 ret
= c1
->hook(c1
->hook_data
,call_data1
);
276 sum_ret
= sum_ret
|| ret
;
279 else if(h1
== NULL
&& h2
!= NULL
) {
280 for(j
= 0 ; j
< h2
->len
; j
++) {
281 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
282 ret
= c2
->hook(c2
->hook_data
,call_data2
);
283 sum_ret
= sum_ret
|| ret
;
290 gboolean
lttv_hooks_call_check_merge(LttvHooks
*h1
, void *call_data1
,
291 LttvHooks
*h2
, void *call_data2
)
293 LttvHookClosure
*c1
, *c2
;
297 if(h1
!= NULL
&& h2
!= NULL
) {
298 for(i
= 0, j
= 0 ; i
< h1
->len
&& j
< h2
->len
;) {
299 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
300 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
301 if(c1
->prio
<= c2
->prio
) {
302 if(c1
->hook(c1
->hook_data
,call_data1
)) return TRUE
;
306 if(c2
->hook(c2
->hook_data
,call_data2
)) return TRUE
;
310 /* Finish the last list with hooks left */
311 for(;i
< h1
->len
; i
++) {
312 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
313 if(c1
->hook(c1
->hook_data
,call_data1
)) return TRUE
;
315 for(;j
< h2
->len
; j
++) {
316 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
317 if(c2
->hook(c2
->hook_data
,call_data2
)) return TRUE
;
320 else if(h1
!= NULL
&& h2
== NULL
) {
321 for(i
= 0 ; i
< h1
->len
; i
++) {
322 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
323 if(c1
->hook(c1
->hook_data
,call_data1
)) return TRUE
;
326 else if(h1
== NULL
&& h2
!= NULL
) {
327 for(j
= 0 ; j
< h2
->len
; j
++) {
328 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
329 if(c2
->hook(c2
->hook_data
,call_data2
)) return TRUE
;
338 LttvHooksById
*lttv_hooks_by_id_new()
340 return g_ptr_array_new();
344 void lttv_hooks_by_id_destroy(LttvHooksById
*h
)
348 for(i
= 0 ; i
< h
->len
; i
++) {
349 if(h
->pdata
[i
] != NULL
) lttv_hooks_destroy((LttvHooks
*)(h
->pdata
[i
]));
351 g_ptr_array_free(h
, TRUE
);
355 LttvHooks
*lttv_hooks_by_id_find(LttvHooksById
*h
, unsigned id
)
357 if(h
->len
<= id
) g_ptr_array_set_size(h
, id
+ 1);
358 if(h
->pdata
[id
] == NULL
) h
->pdata
[id
] = lttv_hooks_new();
363 unsigned lttv_hooks_by_id_max_id(LttvHooksById
*h
)
369 LttvHooks
*lttv_hooks_by_id_get(LttvHooksById
*h
, unsigned id
)
371 if(id
< h
->len
) return h
->pdata
[id
];
376 void lttv_hooks_by_id_remove(LttvHooksById
*h
, unsigned id
)
378 if(id
< h
->len
&& h
->pdata
[id
] != NULL
) {
379 lttv_hooks_destroy((LttvHooks
*)h
->pdata
[id
]);
This page took 0.037949 seconds and 4 git commands to generate.