9c312311 |
1 | /* This file is part of the Linux Trace Toolkit viewer |
2 | * Copyright (C) 2003-2004 Michel Dagenais |
3 | * |
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; |
7 | * |
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. |
12 | * |
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, |
16 | * MA 02111-1307, USA. |
17 | */ |
18 | |
ffd54a90 |
19 | |
3d218f2a |
20 | #include <lttv/hook.h> |
eccb5352 |
21 | |
22 | |
dc877563 |
23 | typedef struct _LttvHookClosure { |
47e76340 |
24 | LttvHook hook; |
25 | void *hook_data; |
26 | LttvHookPrio prio; |
8436038a |
27 | guint ref_count; |
dc877563 |
28 | } LttvHookClosure; |
eccb5352 |
29 | |
47e76340 |
30 | gint lttv_hooks_prio_compare(LttvHookClosure *a, LttvHookClosure *b) |
31 | { |
32 | if(a->prio < b->prio) return -1; |
33 | if(a->prio > b->prio) return 1; |
34 | return 0; |
35 | } |
36 | |
eccb5352 |
37 | |
dc877563 |
38 | LttvHooks *lttv_hooks_new() |
39 | { |
40 | return g_array_new(FALSE, FALSE, sizeof(LttvHookClosure)); |
eccb5352 |
41 | } |
42 | |
dc877563 |
43 | |
44 | void lttv_hooks_destroy(LttvHooks *h) |
45 | { |
2a2fa4f0 |
46 | g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "lttv_hooks_destroy()"); |
eccb5352 |
47 | g_array_free(h, TRUE); |
48 | } |
49 | |
eccb5352 |
50 | |
47e76340 |
51 | void lttv_hooks_add(LttvHooks *h, LttvHook f, void *hook_data, LttvHookPrio p) |
dc877563 |
52 | { |
bb545b3c |
53 | LttvHookClosure *c, new_c; |
54 | guint i; |
55 | |
dc877563 |
56 | if(h == NULL)g_error("Null hook added"); |
eccb5352 |
57 | |
bb545b3c |
58 | new_c.hook = f; |
59 | new_c.hook_data = hook_data; |
60 | new_c.prio = p; |
5a6db8b4 |
61 | new_c.ref_count = 1; |
8436038a |
62 | |
63 | /* Preliminary check for duplication */ |
64 | /* only hook and hook data is checked */ |
65 | for(i = 0; i < h->len; i++) { |
66 | c = &g_array_index(h, LttvHookClosure, i); |
67 | if(new_c.hook == c->hook && new_c.hook_data == c->hook_data) { |
68 | g_assert(new_c.prio == c->prio); |
69 | c->ref_count++; |
70 | return; |
71 | } |
72 | } |
73 | |
74 | |
bb545b3c |
75 | for(i = 0; i < h->len; i++) { |
bb545b3c |
76 | c = &g_array_index(h, LttvHookClosure, i); |
77 | if(new_c.prio < c->prio) { |
78 | g_array_insert_val(h,i,new_c); |
79 | return; |
80 | } |
81 | } |
82 | if(i == h->len) |
83 | g_array_append_val(h,new_c); |
eccb5352 |
84 | } |
85 | |
bb545b3c |
86 | /* lttv_hooks_add_list |
87 | * |
88 | * Adds a sorted list into another sorted list. |
89 | * |
90 | * Note : h->len is modified, but only incremented. This assures |
91 | * its coherence through the function. |
92 | * |
93 | * j is an index to the element following the last one added in the |
94 | * destination array. |
95 | */ |
96 | void lttv_hooks_add_list(LttvHooks *h, const LttvHooks *list) |
dc877563 |
97 | { |
8436038a |
98 | guint i,j,k; |
bb545b3c |
99 | LttvHookClosure *c; |
100 | const LttvHookClosure *new_c; |
dc877563 |
101 | |
ffd54a90 |
102 | if(list == NULL) return; |
bb545b3c |
103 | for(i = 0, j = 0 ; i < list->len; i++) { |
104 | new_c = &g_array_index(list, LttvHookClosure, i); |
8436038a |
105 | gboolean found=FALSE; |
106 | |
107 | /* Preliminary check for duplication */ |
108 | /* only hook and hook data is checked, not priority */ |
109 | for(k = 0; k < h->len; k++) { |
110 | c = &g_array_index(h, LttvHookClosure, k); |
111 | if(new_c->hook == c->hook && new_c->hook_data == c->hook_data) { |
112 | /* Found another identical entry : increment its ref_count and |
113 | * jump over the source index */ |
114 | g_assert(new_c->prio == c->prio); |
115 | found=TRUE; |
116 | c->ref_count++; |
bb545b3c |
117 | break; |
118 | } |
bb545b3c |
119 | } |
8436038a |
120 | |
121 | if(!found) { |
122 | /* If not found, add it to the destination array */ |
123 | while(j < h->len) { |
124 | c = &g_array_index(h, LttvHookClosure, j); |
125 | if(new_c->prio < c->prio) { |
126 | g_array_insert_val(h,j,*new_c); |
127 | j++; |
128 | break; |
129 | } |
130 | else j++; |
131 | } |
132 | if(j == h->len) { |
133 | g_array_append_val(h,*new_c); |
134 | j++; |
135 | } |
bb545b3c |
136 | } |
dc877563 |
137 | } |
138 | } |
139 | |
140 | |
141 | void *lttv_hooks_remove(LttvHooks *h, LttvHook f) |
142 | { |
143 | unsigned i; |
144 | |
145 | void *hook_data; |
146 | |
147 | LttvHookClosure *c; |
148 | |
149 | for(i = 0 ; i < h->len ; i++) { |
150 | c = &g_array_index(h, LttvHookClosure, i); |
151 | if(c->hook == f) { |
8436038a |
152 | if(c->ref_count == 1) { |
153 | hook_data = c->hook_data; |
154 | lttv_hooks_remove_by_position(h, i); |
155 | return hook_data; |
156 | } else { |
157 | g_assert(c->ref_count != 0); |
158 | c->ref_count--; |
159 | return NULL; /* We do not want anyone to free a hook_data |
160 | still referenced */ |
161 | } |
dc877563 |
162 | } |
163 | } |
164 | return NULL; |
165 | } |
166 | |
167 | |
168 | void lttv_hooks_remove_data(LttvHooks *h, LttvHook f, void *hook_data) |
eccb5352 |
169 | { |
dc877563 |
170 | unsigned i; |
171 | |
172 | LttvHookClosure *c; |
eccb5352 |
173 | |
174 | for(i = 0 ; i < h->len ; i++) { |
dc877563 |
175 | c = &g_array_index(h, LttvHookClosure, i); |
176 | if(c->hook == f && c->hook_data == hook_data) { |
8436038a |
177 | if(c->ref_count == 1) { |
178 | lttv_hooks_remove_by_position(h, i); |
179 | return; |
180 | } else { |
181 | g_assert(c->ref_count != 0); |
182 | c->ref_count--; |
183 | return; |
184 | } |
dc877563 |
185 | } |
186 | } |
187 | } |
188 | |
189 | |
190 | void lttv_hooks_remove_list(LttvHooks *h, LttvHooks *list) |
191 | { |
192 | guint i, j; |
193 | |
194 | LttvHookClosure *c, *c_list; |
195 | |
ffd54a90 |
196 | if(list == NULL) return; |
dc877563 |
197 | for(i = 0, j = 0 ; i < h->len && j < list->len ;) { |
198 | c = &g_array_index(h, LttvHookClosure, i); |
199 | c_list = &g_array_index(list, LttvHookClosure, j); |
200 | if(c->hook == c_list->hook && c->hook_data == c_list->hook_data) { |
8436038a |
201 | if(c->ref_count == 1) { |
202 | lttv_hooks_remove_by_position(h, i); |
203 | } else { |
204 | g_assert(c->ref_count != 0); |
205 | c->ref_count--; |
206 | } |
dc877563 |
207 | j++; |
208 | } |
209 | else i++; |
210 | } |
211 | |
212 | /* Normally the hooks in h are ordered as in list. If this is not the case, |
213 | try harder here. */ |
214 | |
215 | if(j < list->len) { |
216 | for(; j < list->len ; j++) { |
217 | c_list = &g_array_index(list, LttvHookClosure, j); |
218 | lttv_hooks_remove_data(h, c_list->hook, c_list->hook_data); |
219 | } |
eccb5352 |
220 | } |
221 | } |
222 | |
223 | |
dc877563 |
224 | unsigned lttv_hooks_number(LttvHooks *h) |
225 | { |
226 | return h->len; |
227 | } |
eccb5352 |
228 | |
dc877563 |
229 | |
47e76340 |
230 | void lttv_hooks_get(LttvHooks *h, unsigned i, LttvHook *f, void **hook_data, |
231 | LttvHookPrio *p) |
dc877563 |
232 | { |
233 | LttvHookClosure *c; |
234 | |
47e76340 |
235 | if(i >= h->len) |
236 | { |
237 | *f = NULL; |
238 | *hook_data = NULL; |
239 | *p = 0; |
240 | return; |
241 | } |
242 | |
dc877563 |
243 | c = &g_array_index(h, LttvHookClosure, i); |
244 | *f = c->hook; |
245 | *hook_data = c->hook_data; |
47e76340 |
246 | *p = c->prio; |
dc877563 |
247 | } |
248 | |
249 | |
ffd54a90 |
250 | void lttv_hooks_remove_by_position(LttvHooks *h, unsigned i) |
dc877563 |
251 | { |
ffd54a90 |
252 | g_array_remove_index(h, i); |
dc877563 |
253 | } |
254 | |
dc877563 |
255 | gboolean lttv_hooks_call(LttvHooks *h, void *call_data) |
256 | { |
b445142a |
257 | gboolean ret, sum_ret = FALSE; |
dc877563 |
258 | |
259 | LttvHookClosure *c; |
260 | |
ffd54a90 |
261 | guint i; |
262 | |
dc877563 |
263 | if(h != NULL) { |
264 | for(i = 0 ; i < h->len ; i++) { |
265 | c = &g_array_index(h, LttvHookClosure, i); |
b445142a |
266 | ret = c->hook(c->hook_data,call_data); |
267 | sum_ret = sum_ret || ret; |
dc877563 |
268 | } |
269 | } |
b445142a |
270 | return sum_ret; |
dc877563 |
271 | } |
272 | |
273 | |
274 | gboolean lttv_hooks_call_check(LttvHooks *h, void *call_data) |
275 | { |
276 | LttvHookClosure *c; |
277 | |
ffd54a90 |
278 | guint i; |
279 | |
dc877563 |
280 | for(i = 0 ; i < h->len ; i++) { |
281 | c = &g_array_index(h, LttvHookClosure, i); |
282 | if(c->hook(c->hook_data,call_data)) return TRUE; |
283 | } |
284 | return FALSE; |
285 | } |
286 | |
47e76340 |
287 | gboolean lttv_hooks_call_merge(LttvHooks *h1, void *call_data1, |
288 | LttvHooks *h2, void *call_data2) |
289 | { |
290 | gboolean ret, sum_ret = FALSE; |
291 | |
292 | LttvHookClosure *c1, *c2; |
293 | |
294 | guint i, j; |
295 | |
296 | if(h1 != NULL && h2 != NULL) { |
297 | for(i = 0, j = 0 ; i < h1->len && j < h2->len ;) { |
298 | c1 = &g_array_index(h1, LttvHookClosure, i); |
299 | c2 = &g_array_index(h2, LttvHookClosure, j); |
300 | if(c1->prio <= c2->prio) { |
301 | ret = c1->hook(c1->hook_data,call_data1); |
302 | sum_ret = sum_ret || ret; |
303 | i++; |
304 | } |
305 | else { |
306 | ret = c2->hook(c2->hook_data,call_data2); |
307 | sum_ret = sum_ret || ret; |
308 | j++; |
309 | } |
310 | } |
311 | /* Finish the last list with hooks left */ |
312 | for(;i < h1->len; i++) { |
313 | c1 = &g_array_index(h1, LttvHookClosure, i); |
314 | ret = c1->hook(c1->hook_data,call_data1); |
315 | sum_ret = sum_ret || ret; |
316 | } |
317 | for(;j < h2->len; j++) { |
318 | c2 = &g_array_index(h2, LttvHookClosure, j); |
319 | ret = c2->hook(c2->hook_data,call_data2); |
320 | sum_ret = sum_ret || ret; |
321 | } |
322 | } |
323 | else if(h1 != NULL && h2 == NULL) { |
324 | for(i = 0 ; i < h1->len ; i++) { |
325 | c1 = &g_array_index(h1, LttvHookClosure, i); |
326 | ret = c1->hook(c1->hook_data,call_data1); |
327 | sum_ret = sum_ret || ret; |
328 | } |
329 | } |
330 | else if(h1 == NULL && h2 != NULL) { |
331 | for(j = 0 ; j < h2->len ; j++) { |
332 | c2 = &g_array_index(h2, LttvHookClosure, j); |
333 | ret = c2->hook(c2->hook_data,call_data2); |
334 | sum_ret = sum_ret || ret; |
335 | } |
336 | } |
337 | |
338 | return sum_ret; |
339 | } |
340 | |
341 | gboolean lttv_hooks_call_check_merge(LttvHooks *h1, void *call_data1, |
342 | LttvHooks *h2, void *call_data2) |
343 | { |
344 | LttvHookClosure *c1, *c2; |
345 | |
346 | guint i, j; |
347 | |
348 | if(h1 != NULL && h2 != NULL) { |
349 | for(i = 0, j = 0 ; i < h1->len && j < h2->len ;) { |
350 | c1 = &g_array_index(h1, LttvHookClosure, i); |
351 | c2 = &g_array_index(h2, LttvHookClosure, j); |
352 | if(c1->prio <= c2->prio) { |
353 | if(c1->hook(c1->hook_data,call_data1)) return TRUE; |
354 | i++; |
355 | } |
356 | else { |
357 | if(c2->hook(c2->hook_data,call_data2)) return TRUE; |
358 | j++; |
359 | } |
360 | } |
361 | /* Finish the last list with hooks left */ |
362 | for(;i < h1->len; i++) { |
363 | c1 = &g_array_index(h1, LttvHookClosure, i); |
364 | if(c1->hook(c1->hook_data,call_data1)) return TRUE; |
365 | } |
366 | for(;j < h2->len; j++) { |
367 | c2 = &g_array_index(h2, LttvHookClosure, j); |
368 | if(c2->hook(c2->hook_data,call_data2)) return TRUE; |
369 | } |
370 | } |
371 | else if(h1 != NULL && h2 == NULL) { |
372 | for(i = 0 ; i < h1->len ; i++) { |
373 | c1 = &g_array_index(h1, LttvHookClosure, i); |
374 | if(c1->hook(c1->hook_data,call_data1)) return TRUE; |
375 | } |
376 | } |
377 | else if(h1 == NULL && h2 != NULL) { |
378 | for(j = 0 ; j < h2->len ; j++) { |
379 | c2 = &g_array_index(h2, LttvHookClosure, j); |
380 | if(c2->hook(c2->hook_data,call_data2)) return TRUE; |
381 | } |
382 | } |
383 | |
384 | return FALSE; |
385 | |
386 | } |
387 | |
dc877563 |
388 | |
389 | LttvHooksById *lttv_hooks_by_id_new() |
390 | { |
eccb5352 |
391 | return g_ptr_array_new(); |
392 | } |
393 | |
394 | |
dc877563 |
395 | void lttv_hooks_by_id_destroy(LttvHooksById *h) |
396 | { |
397 | guint i; |
398 | |
399 | for(i = 0 ; i < h->len ; i++) { |
400 | if(h->pdata[i] != NULL) lttv_hooks_destroy((LttvHooks *)(h->pdata[i])); |
401 | } |
eccb5352 |
402 | g_ptr_array_free(h, TRUE); |
403 | } |
404 | |
405 | |
ffd54a90 |
406 | LttvHooks *lttv_hooks_by_id_find(LttvHooksById *h, unsigned id) |
eccb5352 |
407 | { |
dc877563 |
408 | if(h->len <= id) g_ptr_array_set_size(h, id + 1); |
eccb5352 |
409 | if(h->pdata[id] == NULL) h->pdata[id] = lttv_hooks_new(); |
dc877563 |
410 | return h->pdata[id]; |
eccb5352 |
411 | } |
412 | |
dc877563 |
413 | |
414 | unsigned lttv_hooks_by_id_max_id(LttvHooksById *h) |
eccb5352 |
415 | { |
dc877563 |
416 | return h->len; |
417 | } |
418 | |
419 | |
420 | LttvHooks *lttv_hooks_by_id_get(LttvHooksById *h, unsigned id) |
421 | { |
422 | if(id < h->len) return h->pdata[id]; |
423 | return NULL; |
424 | } |
425 | |
426 | |
427 | void lttv_hooks_by_id_remove(LttvHooksById *h, unsigned id) |
428 | { |
429 | if(id < h->len && h->pdata[id] != NULL) { |
430 | lttv_hooks_destroy((LttvHooks *)h->pdata[id]); |
431 | h->pdata[id] = NULL; |
432 | } |
eccb5352 |
433 | } |
434 | |