| 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 | |
| 19 | |
| 20 | #include <lttv/hook.h> |
| 21 | |
| 22 | |
| 23 | typedef struct _LttvHookClosure { |
| 24 | LttvHook hook; |
| 25 | void *hook_data; |
| 26 | } LttvHookClosure; |
| 27 | |
| 28 | |
| 29 | LttvHooks *lttv_hooks_new() |
| 30 | { |
| 31 | return g_array_new(FALSE, FALSE, sizeof(LttvHookClosure)); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | void lttv_hooks_destroy(LttvHooks *h) |
| 36 | { |
| 37 | g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "lttv_hooks_destroy()"); |
| 38 | g_array_free(h, TRUE); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | void lttv_hooks_add(LttvHooks *h, LttvHook f, void *hook_data) |
| 43 | { |
| 44 | LttvHookClosure c; |
| 45 | |
| 46 | if(h == NULL)g_error("Null hook added"); |
| 47 | |
| 48 | c.hook = f; |
| 49 | c.hook_data = hook_data; |
| 50 | g_array_append_val(h,c); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | void lttv_hooks_add_list(LttvHooks *h, LttvHooks *list) |
| 55 | { |
| 56 | guint i; |
| 57 | |
| 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)); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | |
| 65 | void *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 | |
| 85 | void lttv_hooks_remove_data(LttvHooks *h, LttvHook f, void *hook_data) |
| 86 | { |
| 87 | unsigned i; |
| 88 | |
| 89 | LttvHookClosure *c; |
| 90 | |
| 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); |
| 95 | return; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | |
| 101 | void lttv_hooks_remove_list(LttvHooks *h, LttvHooks *list) |
| 102 | { |
| 103 | guint i, j; |
| 104 | |
| 105 | LttvHookClosure *c, *c_list; |
| 106 | |
| 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); |
| 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 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | |
| 130 | unsigned lttv_hooks_number(LttvHooks *h) |
| 131 | { |
| 132 | return h->len; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | void 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 | |
| 146 | void lttv_hooks_remove_by_position(LttvHooks *h, unsigned i) |
| 147 | { |
| 148 | g_array_remove_index(h, i); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | gboolean lttv_hooks_call(LttvHooks *h, void *call_data) |
| 153 | { |
| 154 | gboolean ret, sum_ret = FALSE; |
| 155 | |
| 156 | LttvHookClosure *c; |
| 157 | |
| 158 | guint i; |
| 159 | |
| 160 | if(h != NULL) { |
| 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; |
| 165 | } |
| 166 | } |
| 167 | return sum_ret; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | gboolean lttv_hooks_call_check(LttvHooks *h, void *call_data) |
| 172 | { |
| 173 | LttvHookClosure *c; |
| 174 | |
| 175 | guint i; |
| 176 | |
| 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 | |
| 185 | LttvHooksById *lttv_hooks_by_id_new() |
| 186 | { |
| 187 | return g_ptr_array_new(); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | void 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 | } |
| 198 | g_ptr_array_free(h, TRUE); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | LttvHooks *lttv_hooks_by_id_find(LttvHooksById *h, unsigned id) |
| 203 | { |
| 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(); |
| 206 | return h->pdata[id]; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | unsigned lttv_hooks_by_id_max_id(LttvHooksById *h) |
| 211 | { |
| 212 | return h->len; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | LttvHooks *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 | |
| 223 | void 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 | } |
| 229 | } |
| 230 | |