Commit | Line | Data |
---|---|---|
9c312311 | 1 | /* This file is part of the Linux Trace Toolkit viewer |
2 | * Copyright (C) 2003-2004 Michel Dagenais | |
3 | * | |
47e76340 | 4 | * 25/05/2004 Mathieu Desnoyers : Hook priorities |
5 | * | |
9c312311 | 6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License Version 2 as | |
8 | * published by the Free Software Foundation; | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
18 | * MA 02111-1307, USA. | |
19 | */ | |
20 | ||
c5d77517 | 21 | #ifndef HOOK_H |
22 | #define HOOK_H | |
23 | ||
858bd80a | 24 | #include <glib.h> |
2eef04b5 | 25 | #include <ltt/compiler.h> |
c5d77517 | 26 | |
27 | /* A hook is a function to call with the supplied hook data, and with | |
28 | call site specific data (e.g., hooks for events are called with a | |
29 | pointer to the current event). */ | |
dc877563 | 30 | |
31 | typedef gboolean (*LttvHook)(void *hook_data, void *call_data); | |
c5d77517 | 32 | |
33 | ||
34 | /* A list of hooks allows registering hooks to be called later. */ | |
35 | ||
996acd92 | 36 | typedef GArray LttvHooks; |
dc877563 | 37 | |
47e76340 | 38 | /* A priority associated with each hook, from -19 (high prio) to 20 (low prio) |
39 | * 0 being the default priority. | |
40 | * | |
41 | * Priority ordering is done in the lttv_hooks_add and lttv_hooks_add_list | |
42 | * functions. Hook removal does not change list order. | |
43 | */ | |
44 | ||
bb545b3c | 45 | #define LTTV_PRIO_DEFAULT 50 |
46 | #define LTTV_PRIO_HIGH 0 | |
47 | #define LTTV_PRIO_LOW 99 | |
48 | ||
47e76340 | 49 | typedef gint LttvHookPrio; |
dc877563 | 50 | |
51 | /* Create and destroy a list of hooks */ | |
52 | ||
53 | LttvHooks *lttv_hooks_new(); | |
54 | ||
55 | void lttv_hooks_destroy(LttvHooks *h); | |
56 | ||
57 | ||
58 | /* Add a hook and its hook data to the list */ | |
59 | ||
47e76340 | 60 | void lttv_hooks_add(LttvHooks *h, LttvHook f, void *hook_data, LttvHookPrio p); |
dc877563 | 61 | |
62 | ||
63 | /* Add a list of hooks to the list h */ | |
64 | ||
bb545b3c | 65 | void lttv_hooks_add_list(LttvHooks *h, const LttvHooks *list); |
dc877563 | 66 | |
67 | ||
68 | /* Remove a hook from the list. Return the hook data. */ | |
69 | ||
70 | void *lttv_hooks_remove(LttvHooks *h, LttvHook f); | |
71 | ||
72 | ||
73 | /* Remove a hook from the list checking that the hook data match. */ | |
74 | ||
75 | void lttv_hooks_remove_data(LttvHooks *h, LttvHook f, void *hook_data); | |
76 | ||
77 | ||
78 | /* Remove a list of hooks from the hooks list in h. */ | |
c5d77517 | 79 | |
3f328da1 | 80 | void lttv_hooks_remove_list(LttvHooks *h, LttvHooks *list); |
c5d77517 | 81 | |
c5d77517 | 82 | |
dc877563 | 83 | /* Return the number of hooks in the list */ |
c5d77517 | 84 | |
dc877563 | 85 | unsigned lttv_hooks_number(LttvHooks *h); |
1b82f325 | 86 | |
1b82f325 | 87 | |
47e76340 | 88 | /* Return the hook at the specified position in the list. |
89 | * *f and *hook_data are NULL if no hook exists at that position. */ | |
1b82f325 | 90 | |
47e76340 | 91 | void lttv_hooks_get(LttvHooks *h, unsigned i, LttvHook *f, void **hook_data, |
90e19f82 | 92 | LttvHookPrio *p); |
1b82f325 | 93 | |
1b82f325 | 94 | |
dc877563 | 95 | /* Remove the specified hook. The position of the following hooks may change */ |
8436038a | 96 | /* The hook is removed from the list event if its ref_count is higher than 1 */ |
dc877563 | 97 | |
98 | void lttv_hooks_remove_by_position(LttvHooks *h, unsigned i); | |
99 | ||
100 | ||
101 | /* Call all the hooks in the list, each with its hook data, | |
47e76340 | 102 | with the specified call data, in priority order. Return TRUE if one hook |
103 | returned TRUE. */ | |
dc877563 | 104 | |
105 | gboolean lttv_hooks_call(LttvHooks *h, void *call_data); | |
106 | ||
107 | ||
47e76340 | 108 | /* Call the hooks in the list in priority order until one returns true, |
109 | * in which case TRUE is returned. */ | |
dc877563 | 110 | |
111 | gboolean lttv_hooks_call_check(LttvHooks *h, void *call_data); | |
c5d77517 | 112 | |
113 | ||
47e76340 | 114 | /* Call hooks from two lists in priority order. If priority is the same, |
115 | * hooks from h1 are called first. */ | |
116 | ||
117 | gboolean lttv_hooks_call_merge(LttvHooks *h1, void *call_data1, | |
90e19f82 | 118 | LttvHooks *h2, void *call_data2); |
47e76340 | 119 | |
120 | gboolean lttv_hooks_call_check_merge(LttvHooks *h1, void *call_data1, | |
90e19f82 | 121 | LttvHooks *h2, void *call_data2); |
47e76340 | 122 | |
c5d77517 | 123 | /* Sometimes different hooks need to be called based on the case. The |
dc877563 | 124 | case is represented by an unsigned integer id */ |
125 | ||
9d239bd9 | 126 | typedef struct _LttvHooksById { |
90e19f82 AM |
127 | GPtrArray *index; |
128 | GArray *array; | |
9d239bd9 | 129 | } LttvHooksById; |
dc877563 | 130 | |
dc877563 | 131 | /* Create and destroy a hooks by id list */ |
132 | ||
750eb11a | 133 | LttvHooksById *lttv_hooks_by_id_new(void); |
dc877563 | 134 | |
135 | void lttv_hooks_by_id_destroy(LttvHooksById *h); | |
136 | ||
137 | ||
138 | /* Obtain the hooks for a given id, creating a list if needed */ | |
139 | ||
140 | LttvHooks *lttv_hooks_by_id_find(LttvHooksById *h, unsigned id); | |
141 | ||
142 | ||
143 | /* Return an id larger than any for which a list exists. */ | |
144 | ||
145 | unsigned lttv_hooks_by_id_max_id(LttvHooksById *h); | |
146 | ||
c5d77517 | 147 | |
dc877563 | 148 | /* Get the list of hooks for an id, NULL if none exists */ |
c5d77517 | 149 | |
04cd9a1e | 150 | static inline LttvHooks *lttv_hooks_by_id_get(LttvHooksById *h, unsigned id) |
88237536 | 151 | { |
90e19f82 AM |
152 | LttvHooks *ret; |
153 | if(likely(id < h->index->len)) ret = h->index->pdata[id]; | |
154 | else ret = NULL; | |
88237536 | 155 | |
90e19f82 | 156 | return ret; |
88237536 | 157 | } |
c5d77517 | 158 | |
dc877563 | 159 | /* Remove the list of hooks associated with an id */ |
c5d77517 | 160 | |
dc877563 | 161 | void lttv_hooks_by_id_remove(LttvHooksById *h, unsigned id); |
c5d77517 | 162 | |
750eb11a | 163 | void lttv_hooks_by_id_copy(LttvHooksById *dest, LttvHooksById *src); |
164 | ||
165 | /* | |
166 | * Hooks per channel per id. Useful for GUI to save/restore hooks | |
167 | * on a per trace basis (rather than per tracefile). | |
168 | */ | |
169 | ||
170 | /* Internal structure, contained in by the LttvHooksByIdChannelArray */ | |
171 | typedef struct _LttvHooksByIdChannel { | |
90e19f82 AM |
172 | LttvHooksById *hooks_by_id; |
173 | GQuark channel; | |
750eb11a | 174 | } LttvHooksByIdChannel; |
175 | ||
176 | typedef struct _LttvHooksByIdChannelArray { | |
90e19f82 | 177 | GArray *array; /* Array of LttvHooksByIdChannel */ |
750eb11a | 178 | } LttvHooksByIdChannelArray; |
179 | ||
180 | LttvHooksByIdChannelArray *lttv_hooks_by_id_channel_new(void); | |
181 | ||
182 | void lttv_hooks_by_id_channel_destroy(LttvHooksByIdChannelArray *h); | |
183 | ||
184 | LttvHooks *lttv_hooks_by_id_channel_find(LttvHooksByIdChannelArray *h, | |
90e19f82 | 185 | GQuark channel, guint16 id); |
750eb11a | 186 | |
c5d77517 | 187 | #endif // HOOK_H |