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
;
62 /* Preliminary check for duplication */
63 /* only hook and hook data is checked */
64 for(i
= 0; i
< h
->len
; i
++) {
65 c
= &g_array_index(h
, LttvHookClosure
, i
);
66 if(new_c
.hook
== c
->hook
&& new_c
.hook_data
== c
->hook_data
) {
67 g_assert(new_c
.prio
== c
->prio
);
74 for(i
= 0; i
< h
->len
; i
++) {
75 c
= &g_array_index(h
, LttvHookClosure
, i
);
76 if(new_c
.prio
< c
->prio
) {
77 g_array_insert_val(h
,i
,new_c
);
82 g_array_append_val(h
,new_c
);
85 /* lttv_hooks_add_list
87 * Adds a sorted list into another sorted list.
89 * Note : h->len is modified, but only incremented. This assures
90 * its coherence through the function.
92 * j is an index to the element following the last one added in the
95 void lttv_hooks_add_list(LttvHooks
*h
, const LttvHooks
*list
)
99 const LttvHookClosure
*new_c
;
101 if(list
== NULL
) return;
102 for(i
= 0, j
= 0 ; i
< list
->len
; i
++) {
103 new_c
= &g_array_index(list
, LttvHookClosure
, i
);
104 gboolean found
=FALSE
;
106 /* Preliminary check for duplication */
107 /* only hook and hook data is checked, not priority */
108 for(k
= 0; k
< h
->len
; k
++) {
109 c
= &g_array_index(h
, LttvHookClosure
, k
);
110 if(new_c
->hook
== c
->hook
&& new_c
->hook_data
== c
->hook_data
) {
111 /* Found another identical entry : increment its ref_count and
112 * jump over the source index */
113 g_assert(new_c
->prio
== c
->prio
);
121 /* If not found, add it to the destination array */
123 c
= &g_array_index(h
, LttvHookClosure
, j
);
124 if(new_c
->prio
< c
->prio
) {
125 g_array_insert_val(h
,j
,*new_c
);
132 g_array_append_val(h
,*new_c
);
140 void *lttv_hooks_remove(LttvHooks
*h
, LttvHook f
)
148 for(i
= 0 ; i
< h
->len
; i
++) {
149 c
= &g_array_index(h
, LttvHookClosure
, i
);
151 if(c
->ref_count
== 1) {
152 hook_data
= c
->hook_data
;
153 lttv_hooks_remove_by_position(h
, i
);
156 g_assert(c
->ref_count
!= 0);
158 return NULL
; /* We do not want anyone to free a hook_data
167 void lttv_hooks_remove_data(LttvHooks
*h
, LttvHook f
, void *hook_data
)
173 for(i
= 0 ; i
< h
->len
; i
++) {
174 c
= &g_array_index(h
, LttvHookClosure
, i
);
175 if(c
->hook
== f
&& c
->hook_data
== hook_data
) {
176 if(c
->ref_count
== 1) {
177 lttv_hooks_remove_by_position(h
, i
);
180 g_assert(c
->ref_count
!= 0);
189 void lttv_hooks_remove_list(LttvHooks
*h
, LttvHooks
*list
)
193 LttvHookClosure
*c
, *c_list
;
195 if(list
== NULL
) return;
196 for(i
= 0, j
= 0 ; i
< h
->len
&& j
< list
->len
;) {
197 c
= &g_array_index(h
, LttvHookClosure
, i
);
198 c_list
= &g_array_index(list
, LttvHookClosure
, j
);
199 if(c
->hook
== c_list
->hook
&& c
->hook_data
== c_list
->hook_data
) {
200 if(c
->ref_count
== 1) {
201 lttv_hooks_remove_by_position(h
, i
);
203 g_assert(c
->ref_count
!= 0);
211 /* Normally the hooks in h are ordered as in list. If this is not the case,
215 for(; j
< list
->len
; j
++) {
216 c_list
= &g_array_index(list
, LttvHookClosure
, j
);
217 lttv_hooks_remove_data(h
, c_list
->hook
, c_list
->hook_data
);
223 unsigned lttv_hooks_number(LttvHooks
*h
)
229 void lttv_hooks_get(LttvHooks
*h
, unsigned i
, LttvHook
*f
, void **hook_data
,
242 c
= &g_array_index(h
, LttvHookClosure
, i
);
244 *hook_data
= c
->hook_data
;
249 void lttv_hooks_remove_by_position(LttvHooks
*h
, unsigned i
)
251 g_array_remove_index(h
, i
);
254 gboolean
lttv_hooks_call(LttvHooks
*h
, void *call_data
)
256 gboolean ret
, sum_ret
= FALSE
;
263 for(i
= 0 ; i
< h
->len
; i
++) {
264 c
= &g_array_index(h
, LttvHookClosure
, i
);
265 ret
= c
->hook(c
->hook_data
,call_data
);
266 sum_ret
= sum_ret
|| ret
;
273 gboolean
lttv_hooks_call_check(LttvHooks
*h
, void *call_data
)
279 for(i
= 0 ; i
< h
->len
; i
++) {
280 c
= &g_array_index(h
, LttvHookClosure
, i
);
281 if(c
->hook(c
->hook_data
,call_data
)) return TRUE
;
286 gboolean
lttv_hooks_call_merge(LttvHooks
*h1
, void *call_data1
,
287 LttvHooks
*h2
, void *call_data2
)
289 gboolean ret
, sum_ret
= FALSE
;
291 LttvHookClosure
*c1
, *c2
;
295 if(h1
!= NULL
&& h2
!= NULL
) {
296 for(i
= 0, j
= 0 ; i
< h1
->len
&& j
< h2
->len
;) {
297 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
298 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
299 if(c1
->prio
<= c2
->prio
) {
300 ret
= c1
->hook(c1
->hook_data
,call_data1
);
301 sum_ret
= sum_ret
|| ret
;
305 ret
= c2
->hook(c2
->hook_data
,call_data2
);
306 sum_ret
= sum_ret
|| ret
;
310 /* Finish the last list with hooks left */
311 for(;i
< h1
->len
; i
++) {
312 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
313 ret
= c1
->hook(c1
->hook_data
,call_data1
);
314 sum_ret
= sum_ret
|| ret
;
316 for(;j
< h2
->len
; j
++) {
317 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
318 ret
= c2
->hook(c2
->hook_data
,call_data2
);
319 sum_ret
= sum_ret
|| ret
;
322 else if(h1
!= NULL
&& h2
== NULL
) {
323 for(i
= 0 ; i
< h1
->len
; i
++) {
324 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
325 ret
= c1
->hook(c1
->hook_data
,call_data1
);
326 sum_ret
= sum_ret
|| ret
;
329 else if(h1
== NULL
&& h2
!= NULL
) {
330 for(j
= 0 ; 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
;
340 gboolean
lttv_hooks_call_check_merge(LttvHooks
*h1
, void *call_data1
,
341 LttvHooks
*h2
, void *call_data2
)
343 LttvHookClosure
*c1
, *c2
;
347 if(h1
!= NULL
&& h2
!= NULL
) {
348 for(i
= 0, j
= 0 ; i
< h1
->len
&& j
< h2
->len
;) {
349 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
350 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
351 if(c1
->prio
<= c2
->prio
) {
352 if(c1
->hook(c1
->hook_data
,call_data1
)) return TRUE
;
356 if(c2
->hook(c2
->hook_data
,call_data2
)) return TRUE
;
360 /* Finish the last list with hooks left */
361 for(;i
< h1
->len
; i
++) {
362 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
363 if(c1
->hook(c1
->hook_data
,call_data1
)) return TRUE
;
365 for(;j
< h2
->len
; j
++) {
366 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
367 if(c2
->hook(c2
->hook_data
,call_data2
)) return TRUE
;
370 else if(h1
!= NULL
&& h2
== NULL
) {
371 for(i
= 0 ; i
< h1
->len
; i
++) {
372 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
373 if(c1
->hook(c1
->hook_data
,call_data1
)) return TRUE
;
376 else if(h1
== NULL
&& h2
!= NULL
) {
377 for(j
= 0 ; j
< h2
->len
; j
++) {
378 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
379 if(c2
->hook(c2
->hook_data
,call_data2
)) return TRUE
;
388 LttvHooksById
*lttv_hooks_by_id_new()
390 return g_ptr_array_new();
394 void lttv_hooks_by_id_destroy(LttvHooksById
*h
)
398 for(i
= 0 ; i
< h
->len
; i
++) {
399 if(h
->pdata
[i
] != NULL
) lttv_hooks_destroy((LttvHooks
*)(h
->pdata
[i
]));
401 g_ptr_array_free(h
, TRUE
);
405 LttvHooks
*lttv_hooks_by_id_find(LttvHooksById
*h
, unsigned id
)
407 if(h
->len
<= id
) g_ptr_array_set_size(h
, id
+ 1);
408 if(h
->pdata
[id
] == NULL
) h
->pdata
[id
] = lttv_hooks_new();
413 unsigned lttv_hooks_by_id_max_id(LttvHooksById
*h
)
419 LttvHooks
*lttv_hooks_by_id_get(LttvHooksById
*h
, unsigned id
)
421 if(id
< h
->len
) return h
->pdata
[id
];
426 void lttv_hooks_by_id_remove(LttvHooksById
*h
, unsigned id
)
428 if(id
< h
->len
&& h
->pdata
[id
] != NULL
) {
429 lttv_hooks_destroy((LttvHooks
*)h
->pdata
[id
]);
This page took 0.043441 seconds and 4 git commands to generate.