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>
25 typedef struct _LttvHookClosure
{
32 gint
lttv_hooks_prio_compare(LttvHookClosure
*a
, LttvHookClosure
*b
)
35 if(a
->prio
< b
->prio
) ret
= -1;
36 else if(a
->prio
> b
->prio
) ret
= 1;
41 LttvHooks
*lttv_hooks_new(void)
43 return g_array_new(FALSE
, FALSE
, sizeof(LttvHookClosure
));
47 void lttv_hooks_destroy(LttvHooks
*h
)
49 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_DEBUG
, "lttv_hooks_destroy()");
50 g_array_free(h
, TRUE
);
54 void lttv_hooks_add(LttvHooks
*h
, LttvHook f
, void *hook_data
, LttvHookPrio p
)
56 LttvHookClosure
*c
, new_c
;
59 if(unlikely(h
== NULL
))g_error("Null hook added");
62 new_c
.hook_data
= hook_data
;
66 /* Preliminary check for duplication */
67 /* only hook and hook data is checked */
68 for(i
= 0; i
< h
->len
; i
++) {
69 c
= &g_array_index(h
, LttvHookClosure
, i
);
70 if(new_c
.hook
== c
->hook
&& new_c
.hook_data
== c
->hook_data
) {
71 g_assert(new_c
.prio
== c
->prio
);
78 for(i
= 0; i
< h
->len
; i
++) {
79 c
= &g_array_index(h
, LttvHookClosure
, i
);
80 if(new_c
.prio
< c
->prio
) {
81 g_array_insert_val(h
,i
,new_c
);
86 g_array_append_val(h
,new_c
);
89 /* lttv_hooks_add_list
91 * Adds a sorted list into another sorted list.
93 * Note : h->len is modified, but only incremented. This assures
94 * its coherence through the function.
96 * j is an index to the element following the last one added in the
99 void lttv_hooks_add_list(LttvHooks
*h
, const LttvHooks
*list
)
103 const LttvHookClosure
*new_c
;
105 if(unlikely(list
== NULL
)) return;
107 for(i
= 0, j
= 0 ; i
< list
->len
; i
++) {
108 new_c
= &g_array_index(list
, LttvHookClosure
, i
);
109 gboolean found
=FALSE
;
111 /* Preliminary check for duplication */
112 /* only hook and hook data is checked, not priority */
113 for(k
= 0; k
< h
->len
; k
++) {
114 c
= &g_array_index(h
, LttvHookClosure
, k
);
115 if(new_c
->hook
== c
->hook
&& new_c
->hook_data
== c
->hook_data
) {
116 /* Found another identical entry : increment its ref_count and
117 * jump over the source index */
118 g_assert(new_c
->prio
== c
->prio
);
126 /* If not found, add it to the destination array */
128 c
= &g_array_index(h
, LttvHookClosure
, j
);
129 if(new_c
->prio
< c
->prio
) {
130 g_array_insert_val(h
,j
,*new_c
);
137 g_array_append_val(h
,*new_c
);
145 void *lttv_hooks_remove(LttvHooks
*h
, LttvHook f
)
153 for(i
= 0 ; i
< h
->len
; i
++) {
154 c
= &g_array_index(h
, LttvHookClosure
, i
);
156 if(c
->ref_count
== 1) {
157 hook_data
= c
->hook_data
;
158 lttv_hooks_remove_by_position(h
, i
);
161 g_assert(c
->ref_count
!= 0);
163 return NULL
; /* We do not want anyone to free a hook_data
172 void lttv_hooks_remove_data(LttvHooks
*h
, LttvHook f
, void *hook_data
)
178 for(i
= 0 ; i
< h
->len
; i
++) {
179 c
= &g_array_index(h
, LttvHookClosure
, i
);
180 if(c
->hook
== f
&& c
->hook_data
== hook_data
) {
181 if(c
->ref_count
== 1) {
182 lttv_hooks_remove_by_position(h
, i
);
185 g_assert(c
->ref_count
!= 0);
194 void lttv_hooks_remove_list(LttvHooks
*h
, const LttvHooks
*list
)
198 LttvHookClosure
*c
, *c_list
;
200 if(list
== NULL
) return;
202 /* This iteration assume that both list are in the same order */
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) {
209 lttv_hooks_remove_by_position(h
, i
);
210 g_assert((count
-1) == h
->len
);
212 g_assert(c
->ref_count
!= 0);
220 /* Normally the hooks in h are ordered as in list. If this is not the case,
223 if(unlikely(j
< list
->len
)) {
224 for(; j
< list
->len
; j
++) {
225 c_list
= &g_array_index(list
, LttvHookClosure
, j
);
226 lttv_hooks_remove_data(h
, c_list
->hook
, c_list
->hook_data
);
232 unsigned lttv_hooks_number(LttvHooks
*h
)
238 void lttv_hooks_get(LttvHooks
*h
, unsigned i
, LttvHook
*f
, void **hook_data
,
243 if(unlikely(i
>= h
->len
))
251 c
= &g_array_index(h
, LttvHookClosure
, i
);
253 *hook_data
= c
->hook_data
;
258 void lttv_hooks_remove_by_position(LttvHooks
*h
, unsigned i
)
260 g_array_remove_index(h
, i
);
263 gboolean
lttv_hooks_call(LttvHooks
*h
, void *call_data
)
265 gboolean ret
, sum_ret
= FALSE
;
271 if(likely(h
!= NULL
)) {
272 for(i
= 0 ; i
< h
->len
; i
++) {
273 c
= &g_array_index(h
, LttvHookClosure
, i
);
274 ret
= c
->hook(c
->hook_data
,call_data
);
275 sum_ret
= sum_ret
|| ret
;
282 gboolean
lttv_hooks_call_check(LttvHooks
*h
, void *call_data
)
288 for(i
= 0 ; i
< h
->len
; i
++) {
289 c
= &g_array_index(h
, LttvHookClosure
, i
);
290 if(unlikely(c
->hook(c
->hook_data
,call_data
))) return TRUE
;
295 /* Optimised for h1 == NULL, h2 != NULL. This is the case
296 * for optimised computation (with specific by id hooks, but
299 * The second case that should occur the most often is
300 * h1 != NULL , h2 == NULL.
302 gint
lttv_hooks_call_merge(LttvHooks
*h1
, void *call_data1
,
303 LttvHooks
*h2
, void *call_data2
)
305 gint ret
, sum_ret
= 0;
307 LttvHookClosure
*c1
, *c2
;
311 if(unlikely(h1
!= NULL
)) {
312 if(unlikely(h2
!= NULL
)) {
313 for(i
= 0, j
= 0 ; i
< h1
->len
&& j
< h2
->len
;) {
314 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
315 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
316 if(c1
->prio
<= c2
->prio
) {
317 ret
= c1
->hook(c1
->hook_data
,call_data1
);
318 sum_ret
= sum_ret
| ret
;
321 ret
= c2
->hook(c2
->hook_data
,call_data2
);
322 sum_ret
= sum_ret
| ret
;
326 /* Finish the last list with hooks left */
327 for(;i
< h1
->len
; i
++) {
328 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
329 ret
= c1
->hook(c1
->hook_data
,call_data1
);
330 sum_ret
= sum_ret
| ret
;
332 for(;j
< h2
->len
; j
++) {
333 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
334 ret
= c2
->hook(c2
->hook_data
,call_data2
);
335 sum_ret
= sum_ret
| ret
;
337 } else { /* h1 != NULL && h2 == NULL */
338 for(i
= 0 ; i
< h1
->len
; i
++) {
339 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
340 ret
= c1
->hook(c1
->hook_data
,call_data1
);
341 sum_ret
= sum_ret
| ret
;
344 } else if(likely(h2
!= NULL
)) { /* h1 == NULL && h2 != NULL */
345 for(j
= 0 ; j
< h2
->len
; j
++) {
346 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
347 ret
= c2
->hook(c2
->hook_data
,call_data2
);
348 sum_ret
= sum_ret
| ret
;
355 gboolean
lttv_hooks_call_check_merge(LttvHooks
*h1
, void *call_data1
,
356 LttvHooks
*h2
, void *call_data2
)
358 LttvHookClosure
*c1
, *c2
;
362 if(unlikely(h1
!= NULL
)) {
363 if(unlikely(h2
!= NULL
)) {
364 for(i
= 0, j
= 0 ; i
< h1
->len
&& j
< h2
->len
;) {
365 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
366 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
367 if(c1
->prio
<= c2
->prio
) {
368 if(c1
->hook(c1
->hook_data
,call_data1
)) return TRUE
;
371 if(c2
->hook(c2
->hook_data
,call_data2
)) return TRUE
;
375 /* Finish the last list with hooks left */
376 for(;i
< h1
->len
; i
++) {
377 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
378 if(c1
->hook(c1
->hook_data
,call_data1
)) return TRUE
;
380 for(;j
< h2
->len
; j
++) {
381 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
382 if(c2
->hook(c2
->hook_data
,call_data2
)) return TRUE
;
384 } else { /* h2 == NULL && h1 != NULL */
385 for(i
= 0 ; i
< h1
->len
; i
++) {
386 c1
= &g_array_index(h1
, LttvHookClosure
, i
);
387 if(c1
->hook(c1
->hook_data
,call_data1
)) return TRUE
;
390 } else if(likely(h2
!= NULL
)) { /* h1 == NULL && h2 != NULL */
391 for(j
= 0 ; j
< h2
->len
; j
++) {
392 c2
= &g_array_index(h2
, LttvHookClosure
, j
);
393 if(c2
->hook(c2
->hook_data
,call_data2
)) return TRUE
;
401 void lttv_hooks_print(const LttvHooks
*h
)
403 gboolean ret
, sum_ret
= FALSE
;
409 if(likely(h
!= NULL
)) {
410 for(i
= 0 ; i
< h
->len
; i
++) {
411 c
= &g_array_index(h
, LttvHookClosure
, i
);
412 printf("%p:%i:%i,", c
->hook
, c
->ref_count
, c
->prio
);
This page took 0.039772 seconds and 4 git commands to generate.