Refine the interactions between the hooks provided by the different modules.
[lttv.git] / ltt / branches / poly / lttv / module.c
1
2 /* module.c : Implementation of the module loading/unloading mechanism.
3 *
4 */
5
6 #include <lttv/module.h>
7
8
9 struct _LttvModule
10 {
11 GModule *module;
12 guint ref_count;
13 guint load_count;
14 GPtrArray *dependents;
15 };
16
17
18 /* Table of loaded modules and paths where to search for modules */
19
20 static GHashTable *modules = NULL;
21
22 static GPtrArray *modulesPaths = NULL;
23
24 static void lttv_module_unload_all();
25
26
27 void lttv_module_init(int argc, char **argv)
28 {
29 modules = g_hash_table_new(g_str_hash, g_str_equal);
30 modulesPaths = g_ptr_array_new();
31 }
32
33
34 void lttv_module_destroy()
35 {
36 int i;
37
38 /* Unload all modules */
39 lttv_module_unload_all();
40
41 /* Free the modules paths pointer array as well as the elements */
42 for(i = 0; i < modulesPaths->len ; i++) {
43 g_free(modulesPaths->pdata[i]);
44 }
45 g_ptr_array_free(modulesPaths,TRUE) ;
46 modulesPaths = NULL;
47
48 /* destroy the hash table */
49 g_hash_table_destroy(modules) ;
50 modules = NULL;
51 }
52
53
54 /* Add a new pathname to the modules loading search path */
55
56 void lttv_module_path_add(const char *name)
57 {
58 g_ptr_array_add(modulesPaths,(char*)g_strdup(name));
59 }
60
61
62 static LttvModuleInfo *
63 module_load(const char *name, int argc, char **argv)
64 {
65 GModule *gm;
66
67 LttvModule *m;
68
69 int i;
70
71 char *pathname;
72
73 LttvModuleInit init_function;
74
75 /* Try to find the module along all the user specified paths */
76
77 for(i = 0 ; i < modulesPaths->len ; i++) {
78 pathname = g_module_build_path(modulesPaths->pdata[i],name);
79 gm = g_module_open(pathname,0);
80 g_free(pathname);
81
82 if(gm != NULL) break;
83 }
84
85 /* Try the default system path */
86
87 if(gm == NULL) {
88 pathname = g_module_build_path(NULL,name);
89 gm = g_module_open(pathname,0);
90 g_free(pathname);
91 }
92
93 /* Module cannot be found */
94
95 if(gm == NULL) return NULL;
96
97 /* Check if the module was already opened using the hopefully canonical name
98 returned by g_module_name. */
99
100 pathname = g_module_name(gm);
101
102 m = g_hash_table_lookup(modules, pathname);
103
104 if(m == NULL) {
105
106 /* Module loaded for the first time. Insert it in the table and call the
107 init function if any. */
108
109 m = g_new(LttvModule);
110 m->module = gm;
111 m->ref_count = 0;
112 m->load_count = 0;
113 m->dependents = g_ptr_array_new();
114 g_hash_table_insert(modules, pathname, m);
115
116 if(!g_module_symbol(gm, "init", (gpointer)&init_function)) {
117 g_warning("module %s (%s) has no init function", name, pathname);
118 }
119 else init_Function(argc,argv);
120 }
121 else {
122
123 /* Module was already opened, check that it really is the same and
124 undo the extra g_module_open */
125
126 if(m->module != gm) g_error("Two gmodules with the same pathname");
127 g_module_close(gm);
128 }
129
130 m->ref_count++;
131 return m;
132 }
133
134
135 LttvModuleInfo *
136 lttv_module_load(const char *name, int argc, char **argv)
137 {
138 LttvModule *m = module_load(name, argc, argv);
139
140 if(m != NULL) m->load_count++;
141 return m;
142 }
143
144
145 LttvModule *
146 lttv_module_require(LttvModule *m, const char *name, int argc, char **argv)
147 {
148 LttvModule *module;
149
150 module = module_load(name, argc, argv);
151 if(module != NULL) g_ptr_array_add(m->dependents, module);
152 return module;
153 }
154
155
156 static void module_unload(LttvModule *m)
157 {
158 LttvModuleDestroy destroy_function;
159
160 char *pathname;
161
162 guint len;
163
164 /* Decrement the reference count */
165
166 m->ref_count--;
167 if(m->ref_count > 0) return;
168
169 /* We really have to unload the module, first unload its dependents */
170
171 len = m->dependents->len;
172
173 for(i = 0 ; i < len ; i++) {
174 module_unload(m->dependents->pdata[i]);
175 }
176
177 if(len != m->dependents->len) g_error("dependents list modified");
178
179 /* Unload the module itself */
180
181 if(!g_module_symbol(m->module, "destroy", (gpointer)&destroy_function)) {
182 g_warning("module (%s) has no destroy function", pathname);
183 }
184 else destroy_function();
185
186 g_hash_table_remove(modules, g_module_name(m->module));
187 g_ptr_array_free(m->dependents, TRUE);
188 g_module_close(m->module);
189 g_free(m);
190 }
191
192
193 void lttv_module_unload(LttvModule *m)
194 {
195 if(m->load_count <= 0) {
196 g_error("more unload than load (%s)", g_module_name(m->module));
197 return;
198 }
199 m->load_count--;
200 module_unload(m);
201 }
202
203
204 static void
205 list_modules(gpointer key, gpointer value, gpointer user_data)
206 {
207 g_ptr_array_add((GPtrArray *)user_data, value);
208 }
209
210
211 LttvModule **
212 lttv_module_list(guint *nb)
213 {
214 GPtrArray *list = g_ptr_array_new();
215
216 LttvModule **array;
217
218 g_hash_table_foreach(modules, list_modules, list);
219 *nb = list->len;
220 array = (LttvModule **)list->pdata;
221 g_ptr_array_free(list, FALSE);
222 return array;
223 }
224
225
226 LttvModule **
227 lttv_module_info(LttvModule *m, const char **name,
228 guint *ref_count, guint *load_count, guint *nb_dependents)
229 {
230 guint i, len = m->dependents->len;
231
232 LttvModule **array = g_new(LttvModule *, len);
233
234 *name = g_module_name(m->module);
235 *ref_count = m->ref_count;
236 *load_count = m->load_count;
237 *nb_dependents = len;
238 for(i = 0 ; i < len ; i++) array[i] = m->dependents->pdata[i];
239 return array;
240 }
241
242
243 static void
244 list_independent(gpointer key, gpointer value, gpointer user_data)
245 {
246 LttvModule *m = (LttvModule *)value;
247
248 if(m->load_count > 0) g_ptr_array_add((GPtrArray *)user_data, m);
249 }
250
251
252 void
253 lttv_module_unload_all()
254 {
255 guint i;
256
257 LttvModule *m;
258
259 GPtrArray *independent_modules = g_ptr_array_new();
260
261 g_hash_table_foreach(modules, list_independent, independent_modules);
262
263 for(i = 0 ; i < independent_modules->len ; i++) {
264 m = (LttvModule)independent_modules->pdata[i];
265 while(m->load_count > 0) lttv_module_unload(m);
266 }
267
268 g_ptr_array_free(independent_modules, TRUE);
269 if(g_hash_table_size(modules) != 0) g_warning("cannot unload all modules");
270 }
This page took 0.034227 seconds and 4 git commands to generate.