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
)
54 if(h
== NULL
)g_error("Null hook added");
57 c
.hook_data
= hook_data
;
59 g_array_append_val(h
,c
);
60 g_array_sort(h
, (GCompareFunc
)lttv_hooks_prio_compare
);
64 void lttv_hooks_add_list(LttvHooks
*h
, LttvHooks
*list
)
68 if(list
== NULL
) return;
69 for(i
= 0 ; i
< list
->len
; i
++) {
70 g_array_append_val(h
,g_array_index(list
, LttvHookClosure
, i
));
72 g_array_sort(h
, (GCompareFunc
)lttv_hooks_prio_compare
);
76 void *lttv_hooks_remove(LttvHooks
*h
, LttvHook f
)
84 for(i
= 0 ; i
< h
->len
; i
++) {
85 c
= &g_array_index(h
, LttvHookClosure
, i
);
87 hook_data
= c
->hook_data
;
88 lttv_hooks_remove_by_position(h
, i
);
96 void lttv_hooks_remove_data(LttvHooks
*h
, LttvHook f
, void *hook_data
)
102 for(i
= 0 ; i
< h
->len
; i
++) {
103 c
= &g_array_index(h
, LttvHookClosure
, i
);
104 if(c
->hook
== f
&& c
->hook_data
== hook_data
) {
105 lttv_hooks_remove_by_position(h
, i
);
112 void lttv_hooks_remove_list(LttvHooks
*h
, LttvHooks
*list
)
116 LttvHookClosure
*c
, *c_list
;
118 if(list
== NULL
) return;
119 for(i
= 0, j
= 0 ; i
< h
->len
&& j
< list
->len
;) {
120 c
= &g_array_index(h
, LttvHookClosure
, i
);
121 c_list
= &g_array_index(list
, LttvHookClosure
, j
);
122 if(c
->hook
== c_list
->hook
&& c
->hook_data
== c_list
->hook_data
) {
123 lttv_hooks_remove_by_position(h
, i
);
129 /* Normally the hooks in h are ordered as in list. If this is not the case,
133 for(; j
< list
->len
; j
++) {
134 c_list
= &g_array_index(list
, LttvHookClosure
, j
);
135 lttv_hooks_remove_data(h
, c_list
->hook
, c_list
->hook_data
);
141 unsigned lttv_hooks_number(LttvHooks
*h
)
147 void lttv_hooks_get(LttvHooks
*h
, unsigned i
, LttvHook
*f
, void **hook_data
,
160 c
= &g_array_index(h
, LttvHookClosure
, i
);
162 *hook_data
= c
->hook_data
;
167 void lttv_hooks_remove_by_position(LttvHooks
*h
, unsigned i
)
169 g_array_remove_index(h
, i
);
172 gboolean
lttv_hooks_call(LttvHooks
*h
, void *call_data
)
174 gboolean ret
, sum_ret
= FALSE
;
181 for(i
= 0 ; i
< h
->len
; i
++) {
182 c
= &g_array_index(h
, LttvHookClosure
, i
);
183 ret
= c
->hook(c
->hook_data
,call_data
);
184 sum_ret
= sum_ret
|| ret
;
191 gboolean
lttv_hooks_call_check(LttvHooks
*h
, void *call_data
)
197 for(i
= 0 ; i
< h
->len
; i
++) {
198 c
= &g_array_index(h
, LttvHookClosure
, i
);
199 if(c
->hook(c
->hook_data
,call_data
)) return TRUE
;
204 gboolean
lttv_hooks_call_merge(LttvHooks
*h1
, void *call_data1
,
205 LttvHooks
*h2
, void *call_data2
)
207 gboolean ret
, sum_ret
= FALSE
;
209 LttvHookClosure
*c1
, *c2
;
213 if(h1
!= NULL
&& h2
!= NULL
) {
214 for(i
= 0, j
= 0 ; i
< h1
->len
&& j
< h2
->len
;) {
215 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
216 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
217 if(c1
->prio
<= c2
->prio
) {
218 ret
= c1
->hook(c1
->hook_data
,call_data1
);
219 sum_ret
= sum_ret
|| ret
;
223 ret
= c2
->hook(c2
->hook_data
,call_data2
);
224 sum_ret
= sum_ret
|| ret
;
228 /* Finish the last list with hooks left */
229 for(;i
< h1
->len
; i
++) {
230 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
231 ret
= c1
->hook(c1
->hook_data
,call_data1
);
232 sum_ret
= sum_ret
|| ret
;
234 for(;j
< h2
->len
; j
++) {
235 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
236 ret
= c2
->hook(c2
->hook_data
,call_data2
);
237 sum_ret
= sum_ret
|| ret
;
240 else if(h1
!= NULL
&& h2
== NULL
) {
241 for(i
= 0 ; i
< h1
->len
; i
++) {
242 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
243 ret
= c1
->hook(c1
->hook_data
,call_data1
);
244 sum_ret
= sum_ret
|| ret
;
247 else if(h1
== NULL
&& h2
!= NULL
) {
248 for(j
= 0 ; j
< h2
->len
; j
++) {
249 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
250 ret
= c2
->hook(c2
->hook_data
,call_data2
);
251 sum_ret
= sum_ret
|| ret
;
258 gboolean
lttv_hooks_call_check_merge(LttvHooks
*h1
, void *call_data1
,
259 LttvHooks
*h2
, void *call_data2
)
261 LttvHookClosure
*c1
, *c2
;
265 if(h1
!= NULL
&& h2
!= NULL
) {
266 for(i
= 0, j
= 0 ; i
< h1
->len
&& j
< h2
->len
;) {
267 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
268 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
269 if(c1
->prio
<= c2
->prio
) {
270 if(c1
->hook(c1
->hook_data
,call_data1
)) return TRUE
;
274 if(c2
->hook(c2
->hook_data
,call_data2
)) return TRUE
;
278 /* Finish the last list with hooks left */
279 for(;i
< h1
->len
; i
++) {
280 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
281 if(c1
->hook(c1
->hook_data
,call_data1
)) return TRUE
;
283 for(;j
< h2
->len
; j
++) {
284 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
285 if(c2
->hook(c2
->hook_data
,call_data2
)) return TRUE
;
288 else if(h1
!= NULL
&& h2
== NULL
) {
289 for(i
= 0 ; i
< h1
->len
; i
++) {
290 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
291 if(c1
->hook(c1
->hook_data
,call_data1
)) return TRUE
;
294 else if(h1
== NULL
&& h2
!= NULL
) {
295 for(j
= 0 ; j
< h2
->len
; j
++) {
296 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
297 if(c2
->hook(c2
->hook_data
,call_data2
)) return TRUE
;
306 LttvHooksById
*lttv_hooks_by_id_new()
308 return g_ptr_array_new();
312 void lttv_hooks_by_id_destroy(LttvHooksById
*h
)
316 for(i
= 0 ; i
< h
->len
; i
++) {
317 if(h
->pdata
[i
] != NULL
) lttv_hooks_destroy((LttvHooks
*)(h
->pdata
[i
]));
319 g_ptr_array_free(h
, TRUE
);
323 LttvHooks
*lttv_hooks_by_id_find(LttvHooksById
*h
, unsigned id
)
325 if(h
->len
<= id
) g_ptr_array_set_size(h
, id
+ 1);
326 if(h
->pdata
[id
] == NULL
) h
->pdata
[id
] = lttv_hooks_new();
331 unsigned lttv_hooks_by_id_max_id(LttvHooksById
*h
)
337 LttvHooks
*lttv_hooks_by_id_get(LttvHooksById
*h
, unsigned id
)
339 if(id
< h
->len
) return h
->pdata
[id
];
344 void lttv_hooks_by_id_remove(LttvHooksById
*h
, unsigned id
)
346 if(id
< h
->len
&& h
->pdata
[id
] != NULL
) {
347 lttv_hooks_destroy((LttvHooks
*)h
->pdata
[id
]);
This page took 0.035534 seconds and 4 git commands to generate.