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 LttvHooks
*lttv_hooks_new()
31 return g_array_new(FALSE
, FALSE
, sizeof(LttvHookClosure
));
35 void lttv_hooks_destroy(LttvHooks
*h
)
37 g_log(G_LOG_DOMAIN
, G_LOG_LEVEL_DEBUG
, "lttv_hooks_destroy()");
38 g_array_free(h
, TRUE
);
42 void lttv_hooks_add(LttvHooks
*h
, LttvHook f
, void *hook_data
)
46 if(h
== NULL
)g_error("Null hook added");
49 c
.hook_data
= hook_data
;
50 g_array_append_val(h
,c
);
54 void lttv_hooks_add_list(LttvHooks
*h
, LttvHooks
*list
)
58 if(list
== NULL
) return;
59 for(i
= 0 ; i
< list
->len
; i
++) {
60 g_array_append_val(h
,g_array_index(list
, LttvHookClosure
, i
));
65 void *lttv_hooks_remove(LttvHooks
*h
, LttvHook f
)
73 for(i
= 0 ; i
< h
->len
; i
++) {
74 c
= &g_array_index(h
, LttvHookClosure
, i
);
76 hook_data
= c
->hook_data
;
77 lttv_hooks_remove_by_position(h
, i
);
85 void lttv_hooks_remove_data(LttvHooks
*h
, LttvHook f
, void *hook_data
)
91 for(i
= 0 ; i
< h
->len
; i
++) {
92 c
= &g_array_index(h
, LttvHookClosure
, i
);
93 if(c
->hook
== f
&& c
->hook_data
== hook_data
) {
94 lttv_hooks_remove_by_position(h
, i
);
101 void lttv_hooks_remove_list(LttvHooks
*h
, LttvHooks
*list
)
105 LttvHookClosure
*c
, *c_list
;
107 if(list
== NULL
) return;
108 for(i
= 0, j
= 0 ; i
< h
->len
&& j
< list
->len
;) {
109 c
= &g_array_index(h
, LttvHookClosure
, i
);
110 c_list
= &g_array_index(list
, LttvHookClosure
, j
);
111 if(c
->hook
== c_list
->hook
&& c
->hook_data
== c_list
->hook_data
) {
112 lttv_hooks_remove_by_position(h
, i
);
118 /* Normally the hooks in h are ordered as in list. If this is not the case,
122 for(; j
< list
->len
; j
++) {
123 c_list
= &g_array_index(list
, LttvHookClosure
, j
);
124 lttv_hooks_remove_data(h
, c_list
->hook
, c_list
->hook_data
);
130 unsigned lttv_hooks_number(LttvHooks
*h
)
136 void lttv_hooks_get(LttvHooks
*h
, unsigned i
, LttvHook
*f
, void **hook_data
)
140 c
= &g_array_index(h
, LttvHookClosure
, i
);
142 *hook_data
= c
->hook_data
;
146 void lttv_hooks_remove_by_position(LttvHooks
*h
, unsigned i
)
148 g_array_remove_index(h
, i
);
152 gboolean
lttv_hooks_call(LttvHooks
*h
, void *call_data
)
154 gboolean ret
, sum_ret
= FALSE
;
161 for(i
= 0 ; i
< h
->len
; i
++) {
162 c
= &g_array_index(h
, LttvHookClosure
, i
);
163 ret
= c
->hook(c
->hook_data
,call_data
);
164 sum_ret
= sum_ret
|| ret
;
171 gboolean
lttv_hooks_call_check(LttvHooks
*h
, void *call_data
)
177 for(i
= 0 ; i
< h
->len
; i
++) {
178 c
= &g_array_index(h
, LttvHookClosure
, i
);
179 if(c
->hook(c
->hook_data
,call_data
)) return TRUE
;
185 LttvHooksById
*lttv_hooks_by_id_new()
187 return g_ptr_array_new();
191 void lttv_hooks_by_id_destroy(LttvHooksById
*h
)
195 for(i
= 0 ; i
< h
->len
; i
++) {
196 if(h
->pdata
[i
] != NULL
) lttv_hooks_destroy((LttvHooks
*)(h
->pdata
[i
]));
198 g_ptr_array_free(h
, TRUE
);
202 LttvHooks
*lttv_hooks_by_id_find(LttvHooksById
*h
, unsigned id
)
204 if(h
->len
<= id
) g_ptr_array_set_size(h
, id
+ 1);
205 if(h
->pdata
[id
] == NULL
) h
->pdata
[id
] = lttv_hooks_new();
210 unsigned lttv_hooks_by_id_max_id(LttvHooksById
*h
)
216 LttvHooks
*lttv_hooks_by_id_get(LttvHooksById
*h
, unsigned id
)
218 if(id
< h
->len
) return h
->pdata
[id
];
223 void lttv_hooks_by_id_remove(LttvHooksById
*h
, unsigned id
)
225 if(id
< h
->len
&& h
->pdata
[id
] != NULL
) {
226 lttv_hooks_destroy((LttvHooks
*)h
->pdata
[id
]);
This page took 0.034455 seconds and 4 git commands to generate.