Commit | Line | Data |
---|---|---|
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> |
eccb5352 | 24 | |
dc877563 | 25 | typedef struct _LttvHookClosure { |
90e19f82 AM |
26 | LttvHook hook; |
27 | void *hook_data; | |
28 | LttvHookPrio prio; | |
29 | guint ref_count; | |
dc877563 | 30 | } LttvHookClosure; |
eccb5352 | 31 | |
47e76340 | 32 | gint lttv_hooks_prio_compare(LttvHookClosure *a, LttvHookClosure *b) |
33 | { | |
90e19f82 AM |
34 | gint ret=0; |
35 | if(a->prio < b->prio) ret = -1; | |
36 | else if(a->prio > b->prio) ret = 1; | |
37 | return ret; | |
47e76340 | 38 | } |
39 | ||
eccb5352 | 40 | |
7a4bdb54 | 41 | LttvHooks *lttv_hooks_new(void) |
dc877563 | 42 | { |
90e19f82 | 43 | return g_array_new(FALSE, FALSE, sizeof(LttvHookClosure)); |
eccb5352 | 44 | } |
45 | ||
dc877563 | 46 | |
47 | void lttv_hooks_destroy(LttvHooks *h) | |
48 | { | |
90e19f82 AM |
49 | g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "lttv_hooks_destroy()"); |
50 | g_array_free(h, TRUE); | |
eccb5352 | 51 | } |
52 | ||
eccb5352 | 53 | |
47e76340 | 54 | void lttv_hooks_add(LttvHooks *h, LttvHook f, void *hook_data, LttvHookPrio p) |
dc877563 | 55 | { |
90e19f82 AM |
56 | LttvHookClosure *c, new_c; |
57 | guint i; | |
58 | ||
59 | if(unlikely(h == NULL))g_error("Null hook added"); | |
60 | ||
61 | new_c.hook = f; | |
62 | new_c.hook_data = hook_data; | |
63 | new_c.prio = p; | |
64 | new_c.ref_count = 1; | |
65 | ||
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); | |
72 | c->ref_count++; | |
73 | return; | |
74 | } | |
75 | } | |
76 | ||
77 | ||
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); | |
82 | return; | |
83 | } | |
84 | } | |
85 | if(i == h->len) | |
86 | g_array_append_val(h,new_c); | |
eccb5352 | 87 | } |
88 | ||
bb545b3c | 89 | /* lttv_hooks_add_list |
90 | * | |
91 | * Adds a sorted list into another sorted list. | |
92 | * | |
93 | * Note : h->len is modified, but only incremented. This assures | |
94 | * its coherence through the function. | |
95 | * | |
96 | * j is an index to the element following the last one added in the | |
97 | * destination array. | |
98 | */ | |
99 | void lttv_hooks_add_list(LttvHooks *h, const LttvHooks *list) | |
dc877563 | 100 | { |
90e19f82 AM |
101 | guint i,j,k; |
102 | LttvHookClosure *c; | |
103 | const LttvHookClosure *new_c; | |
104 | ||
105 | if(unlikely(list == NULL)) return; | |
106 | ||
107 | for(i = 0, j = 0 ; i < list->len; i++) { | |
108 | new_c = &g_array_index(list, LttvHookClosure, i); | |
109 | gboolean found=FALSE; | |
110 | ||
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); | |
119 | found=TRUE; | |
120 | c->ref_count++; | |
121 | break; | |
122 | } | |
123 | } | |
124 | ||
125 | if(!found) { | |
126 | /* If not found, add it to the destination array */ | |
127 | while(j < h->len) { | |
128 | c = &g_array_index(h, LttvHookClosure, j); | |
129 | if(new_c->prio < c->prio) { | |
130 | g_array_insert_val(h,j,*new_c); | |
131 | j++; | |
132 | break; | |
133 | } | |
134 | else j++; | |
135 | } | |
136 | if(j == h->len) { | |
137 | g_array_append_val(h,*new_c); | |
138 | j++; | |
139 | } | |
140 | } | |
141 | } | |
dc877563 | 142 | } |
143 | ||
144 | ||
145 | void *lttv_hooks_remove(LttvHooks *h, LttvHook f) | |
146 | { | |
90e19f82 AM |
147 | unsigned i; |
148 | ||
149 | void *hook_data; | |
150 | ||
151 | LttvHookClosure *c; | |
152 | ||
153 | for(i = 0 ; i < h->len ; i++) { | |
154 | c = &g_array_index(h, LttvHookClosure, i); | |
155 | if(c->hook == f) { | |
156 | if(c->ref_count == 1) { | |
157 | hook_data = c->hook_data; | |
158 | lttv_hooks_remove_by_position(h, i); | |
159 | return hook_data; | |
160 | } else { | |
161 | g_assert(c->ref_count != 0); | |
162 | c->ref_count--; | |
163 | return NULL; /* We do not want anyone to free a hook_data | |
164 | still referenced */ | |
165 | } | |
166 | } | |
167 | } | |
168 | return NULL; | |
dc877563 | 169 | } |
170 | ||
171 | ||
172 | void lttv_hooks_remove_data(LttvHooks *h, LttvHook f, void *hook_data) | |
eccb5352 | 173 | { |
90e19f82 AM |
174 | unsigned i; |
175 | ||
176 | LttvHookClosure *c; | |
177 | ||
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); | |
183 | return; | |
184 | } else { | |
185 | g_assert(c->ref_count != 0); | |
186 | c->ref_count--; | |
187 | return; | |
188 | } | |
189 | } | |
190 | } | |
dc877563 | 191 | } |
192 | ||
193 | ||
27fb4fd2 | 194 | void lttv_hooks_remove_list(LttvHooks *h, const LttvHooks *list) |
dc877563 | 195 | { |
90e19f82 AM |
196 | guint i, j; |
197 | ||
198 | LttvHookClosure *c, *c_list; | |
199 | ||
200 | if(list == NULL) return; | |
27fb4fd2 YB |
201 | g_assert(h != list); |
202 | /* This iteration assume that both list are in the same order */ | |
90e19f82 AM |
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) { | |
27fb4fd2 | 208 | int count = h->len; |
90e19f82 | 209 | lttv_hooks_remove_by_position(h, i); |
27fb4fd2 | 210 | g_assert((count-1) == h->len); |
90e19f82 AM |
211 | } else { |
212 | g_assert(c->ref_count != 0); | |
213 | c->ref_count--; | |
214 | } | |
215 | j++; | |
216 | } | |
217 | else i++; | |
218 | } | |
219 | ||
220 | /* Normally the hooks in h are ordered as in list. If this is not the case, | |
221 | try harder here. */ | |
222 | ||
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); | |
227 | } | |
228 | } | |
eccb5352 | 229 | } |
230 | ||
231 | ||
dc877563 | 232 | unsigned lttv_hooks_number(LttvHooks *h) |
233 | { | |
90e19f82 | 234 | return h->len; |
dc877563 | 235 | } |
eccb5352 | 236 | |
dc877563 | 237 | |
47e76340 | 238 | void lttv_hooks_get(LttvHooks *h, unsigned i, LttvHook *f, void **hook_data, |
90e19f82 | 239 | LttvHookPrio *p) |
dc877563 | 240 | { |
90e19f82 AM |
241 | LttvHookClosure *c; |
242 | ||
243 | if(unlikely(i >= h->len)) | |
244 | { | |
245 | *f = NULL; | |
246 | *hook_data = NULL; | |
247 | *p = 0; | |
248 | return; | |
249 | } | |
250 | ||
251 | c = &g_array_index(h, LttvHookClosure, i); | |
252 | *f = c->hook; | |
253 | *hook_data = c->hook_data; | |
254 | *p = c->prio; | |
dc877563 | 255 | } |
256 | ||
257 | ||
ffd54a90 | 258 | void lttv_hooks_remove_by_position(LttvHooks *h, unsigned i) |
dc877563 | 259 | { |
90e19f82 | 260 | g_array_remove_index(h, i); |
dc877563 | 261 | } |
262 | ||
dc877563 | 263 | gboolean lttv_hooks_call(LttvHooks *h, void *call_data) |
264 | { | |
90e19f82 | 265 | gboolean ret, sum_ret = FALSE; |
dc877563 | 266 | |
90e19f82 | 267 | LttvHookClosure *c; |
dc877563 | 268 | |
90e19f82 | 269 | guint i; |
ffd54a90 | 270 | |
90e19f82 AM |
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; | |
276 | } | |
277 | } | |
278 | return sum_ret; | |
dc877563 | 279 | } |
280 | ||
281 | ||
282 | gboolean lttv_hooks_call_check(LttvHooks *h, void *call_data) | |
283 | { | |
90e19f82 | 284 | LttvHookClosure *c; |
dc877563 | 285 | |
90e19f82 | 286 | guint i; |
ffd54a90 | 287 | |
90e19f82 AM |
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; | |
291 | } | |
292 | return FALSE; | |
dc877563 | 293 | } |
294 | ||
1d1df11d | 295 | /* Optimised for h1 == NULL, h2 != NULL. This is the case |
296 | * for optimised computation (with specific by id hooks, but | |
297 | * no main hooks). | |
298 | * | |
299 | * The second case that should occur the most often is | |
300 | * h1 != NULL , h2 == NULL. | |
301 | */ | |
78b82ded | 302 | gint lttv_hooks_call_merge(LttvHooks *h1, void *call_data1, |
90e19f82 | 303 | LttvHooks *h2, void *call_data2) |
47e76340 | 304 | { |
90e19f82 AM |
305 | gint ret, sum_ret = 0; |
306 | ||
307 | LttvHookClosure *c1, *c2; | |
308 | ||
309 | guint i, j; | |
310 | ||
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; | |
319 | i++; | |
320 | } else { | |
321 | ret = c2->hook(c2->hook_data,call_data2); | |
322 | sum_ret = sum_ret | ret; | |
323 | j++; | |
324 | } | |
325 | } | |
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; | |
331 | } | |
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; | |
336 | } | |
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; | |
342 | } | |
343 | } | |
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; | |
349 | } | |
350 | } | |
351 | ||
352 | return sum_ret; | |
47e76340 | 353 | } |
354 | ||
355 | gboolean lttv_hooks_call_check_merge(LttvHooks *h1, void *call_data1, | |
90e19f82 | 356 | LttvHooks *h2, void *call_data2) |
47e76340 | 357 | { |
90e19f82 AM |
358 | LttvHookClosure *c1, *c2; |
359 | ||
360 | guint i, j; | |
361 | ||
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; | |
369 | i++; | |
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); | |
378 | if(c1->hook(c1->hook_data,call_data1)) return TRUE; | |
379 | } | |
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; | |
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; | |
388 | } | |
389 | } | |
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; | |
394 | } | |
395 | } | |
396 | ||
397 | return FALSE; | |
750eb11a | 398 | } |
27fb4fd2 YB |
399 | |
400 | ||
401 | void lttv_hooks_print(const LttvHooks *h) | |
402 | { | |
403 | gboolean ret, sum_ret = FALSE; | |
404 | ||
405 | LttvHookClosure *c; | |
406 | ||
407 | guint i; | |
408 | ||
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); | |
413 | } | |
414 | printf("\n"); | |
415 | } | |
416 | ||
417 | } |