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