minor add
[lttv.git] / ltt / branches / poly / lttv / lttv / hook.c
CommitLineData
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 23typedef struct _LttvHookClosure {
ffd54a90 24 LttvHook hook;
eccb5352 25 void *hook_data;
dc877563 26} LttvHookClosure;
eccb5352 27
28
dc877563 29LttvHooks *lttv_hooks_new()
30{
31 return g_array_new(FALSE, FALSE, sizeof(LttvHookClosure));
eccb5352 32}
33
dc877563 34
35void lttv_hooks_destroy(LttvHooks *h)
36{
2a2fa4f0 37 g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "lttv_hooks_destroy()");
eccb5352 38 g_array_free(h, TRUE);
39}
40
eccb5352 41
dc877563 42void lttv_hooks_add(LttvHooks *h, LttvHook f, void *hook_data)
43{
44 LttvHookClosure c;
45
46 if(h == NULL)g_error("Null hook added");
eccb5352 47
48 c.hook = f;
49 c.hook_data = hook_data;
50 g_array_append_val(h,c);
51}
52
53
dc877563 54void lttv_hooks_add_list(LttvHooks *h, LttvHooks *list)
55{
56 guint i;
57
ffd54a90 58 if(list == NULL) return;
dc877563 59 for(i = 0 ; i < list->len; i++) {
60 g_array_append_val(h,g_array_index(list, LttvHookClosure, i));
61 }
62}
63
64
65void *lttv_hooks_remove(LttvHooks *h, LttvHook f)
66{
67 unsigned i;
68
69 void *hook_data;
70
71 LttvHookClosure *c;
72
73 for(i = 0 ; i < h->len ; i++) {
74 c = &g_array_index(h, LttvHookClosure, i);
75 if(c->hook == f) {
76 hook_data = c->hook_data;
77 lttv_hooks_remove_by_position(h, i);
78 return hook_data;
79 }
80 }
81 return NULL;
82}
83
84
85void lttv_hooks_remove_data(LttvHooks *h, LttvHook f, void *hook_data)
eccb5352 86{
dc877563 87 unsigned i;
88
89 LttvHookClosure *c;
eccb5352 90
91 for(i = 0 ; i < h->len ; i++) {
dc877563 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);
95 return;
96 }
97 }
98}
99
100
101void lttv_hooks_remove_list(LttvHooks *h, LttvHooks *list)
102{
103 guint i, j;
104
105 LttvHookClosure *c, *c_list;
106
ffd54a90 107 if(list == NULL) return;
dc877563 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);
113 j++;
114 }
115 else i++;
116 }
117
118 /* Normally the hooks in h are ordered as in list. If this is not the case,
119 try harder here. */
120
121 if(j < list->len) {
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);
125 }
eccb5352 126 }
127}
128
129
dc877563 130unsigned lttv_hooks_number(LttvHooks *h)
131{
132 return h->len;
133}
eccb5352 134
dc877563 135
136void lttv_hooks_get(LttvHooks *h, unsigned i, LttvHook *f, void **hook_data)
137{
138 LttvHookClosure *c;
139
140 c = &g_array_index(h, LttvHookClosure, i);
141 *f = c->hook;
142 *hook_data = c->hook_data;
143}
144
145
ffd54a90 146void lttv_hooks_remove_by_position(LttvHooks *h, unsigned i)
dc877563 147{
ffd54a90 148 g_array_remove_index(h, i);
dc877563 149}
150
151
152gboolean lttv_hooks_call(LttvHooks *h, void *call_data)
153{
b445142a 154 gboolean ret, sum_ret = FALSE;
dc877563 155
156 LttvHookClosure *c;
157
ffd54a90 158 guint i;
159
dc877563 160 if(h != NULL) {
161 for(i = 0 ; i < h->len ; i++) {
162 c = &g_array_index(h, LttvHookClosure, i);
b445142a 163 ret = c->hook(c->hook_data,call_data);
164 sum_ret = sum_ret || ret;
dc877563 165 }
166 }
b445142a 167 return sum_ret;
dc877563 168}
169
170
171gboolean lttv_hooks_call_check(LttvHooks *h, void *call_data)
172{
173 LttvHookClosure *c;
174
ffd54a90 175 guint i;
176
dc877563 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;
180 }
181 return FALSE;
182}
183
184
185LttvHooksById *lttv_hooks_by_id_new()
186{
eccb5352 187 return g_ptr_array_new();
188}
189
190
dc877563 191void lttv_hooks_by_id_destroy(LttvHooksById *h)
192{
193 guint i;
194
195 for(i = 0 ; i < h->len ; i++) {
196 if(h->pdata[i] != NULL) lttv_hooks_destroy((LttvHooks *)(h->pdata[i]));
197 }
eccb5352 198 g_ptr_array_free(h, TRUE);
199}
200
201
ffd54a90 202LttvHooks *lttv_hooks_by_id_find(LttvHooksById *h, unsigned id)
eccb5352 203{
dc877563 204 if(h->len <= id) g_ptr_array_set_size(h, id + 1);
eccb5352 205 if(h->pdata[id] == NULL) h->pdata[id] = lttv_hooks_new();
dc877563 206 return h->pdata[id];
eccb5352 207}
208
dc877563 209
210unsigned lttv_hooks_by_id_max_id(LttvHooksById *h)
eccb5352 211{
dc877563 212 return h->len;
213}
214
215
216LttvHooks *lttv_hooks_by_id_get(LttvHooksById *h, unsigned id)
217{
218 if(id < h->len) return h->pdata[id];
219 return NULL;
220}
221
222
223void lttv_hooks_by_id_remove(LttvHooksById *h, unsigned id)
224{
225 if(id < h->len && h->pdata[id] != NULL) {
226 lttv_hooks_destroy((LttvHooks *)h->pdata[id]);
227 h->pdata[id] = NULL;
228 }
eccb5352 229}
230
This page took 0.036999 seconds and 4 git commands to generate.