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