git-svn-id: http://ltt.polymtl.ca/svn@289 04897980-b3bd-0310-b5e0-8ef037075253
[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 LttvModule *
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 const char *module_name;
74
75 LttvModuleInit init_function;
76
77 /* Try to find the module along all the user specified paths */
78
79 for(i = 0 ; i < modulesPaths->len ; i++) {
80 pathname = g_module_build_path(modulesPaths->pdata[i],name);
81 gm = g_module_open(pathname,G_MODULE_BIND_LAZY);
82 g_critical("loading module : %s", pathname);
83 g_critical("module error : %s", g_module_error());
84 g_free(pathname);
85
86 if(gm != NULL) break;
87 }
88
89 /* Try the default system path */
90
91 if(gm == NULL) {
92 pathname = g_module_build_path(NULL,name);
93 gm = g_module_open(pathname,G_MODULE_BIND_LAZY);
94 g_critical("loading module : %s", pathname);
95 g_free(pathname);
96 }
97
98 /* Module cannot be found */
99 if(gm == NULL) return NULL;
100
101 /* Check if the module was already opened using the hopefully canonical name
102 returned by g_module_name. */
103
104 module_name = g_module_name(gm);
105
106 m = g_hash_table_lookup(modules, module_name);
107
108 if(m == NULL) {
109
110 /* Module loaded for the first time. Insert it in the table and call the
111 init function if any. */
112
113 m = g_new(LttvModule, 1);
114 m->module = gm;
115 m->ref_count = 0;
116 m->load_count = 0;
117 m->dependents = g_ptr_array_new();
118 g_hash_table_insert(modules, (gpointer)module_name, m);
119
120 if(!g_module_symbol(gm, "init", (gpointer)&init_function)) {
121 g_warning("module %s (%s) has no init function", name, pathname);
122 }
123 else init_function(m, argc, argv);
124 }
125 else {
126
127 /* Module was already opened, check that it really is the same and
128 undo the extra g_module_open */
129
130 if(m->module != gm) g_error("Two gmodules with the same pathname");
131 g_module_close(gm);
132 }
133
134 m->ref_count++;
135 return m;
136 }
137
138
139 LttvModule *
140 lttv_module_load(const char *name, int argc, char **argv)
141 {
142 LttvModule *m = module_load(name, argc, argv);
143 if(m != NULL) m->load_count++;
144 return m;
145 }
146
147
148 LttvModule *
149 lttv_module_require(LttvModule *m, const char *name, int argc, char **argv)
150 {
151 LttvModule *module;
152
153 module = module_load(name, argc, argv);
154 if(module != NULL) g_ptr_array_add(m->dependents, module);
155 return module;
156 }
157
158
159 static void module_unload(LttvModule *m)
160 {
161 LttvModuleDestroy destroy_function;
162
163 char *pathname;
164
165 guint i, len;
166
167 /* Decrement the reference count */
168
169 m->ref_count--;
170 if(m->ref_count > 0) return;
171
172 /* We really have to unload the module, first unload its dependents */
173
174 len = m->dependents->len;
175
176 for(i = 0 ; i < len ; i++) {
177 module_unload(m->dependents->pdata[i]);
178 }
179
180 if(len != m->dependents->len) g_error("dependents list modified");
181
182 /* Unload the module itself */
183
184 if(!g_module_symbol(m->module, "destroy", (gpointer)&destroy_function)) {
185 g_warning("module (%s) has no destroy function", pathname);
186 }
187 else destroy_function();
188
189 g_hash_table_remove(modules, g_module_name(m->module));
190 g_ptr_array_free(m->dependents, TRUE);
191 g_module_close(m->module);
192 g_free(m);
193 }
194
195
196 void lttv_module_unload(LttvModule *m)
197 {
198 if(m->load_count <= 0) {
199 g_error("more unload than load (%s)", g_module_name(m->module));
200 return;
201 }
202 m->load_count--;
203 module_unload(m);
204 }
205
206
207 static void
208 list_modules(gpointer key, gpointer value, gpointer user_data)
209 {
210 g_ptr_array_add((GPtrArray *)user_data, value);
211 }
212
213
214 LttvModule **
215 lttv_module_list(guint *nb)
216 {
217 GPtrArray *list = g_ptr_array_new();
218
219 LttvModule **array;
220
221 g_hash_table_foreach(modules, list_modules, list);
222 *nb = list->len;
223 array = (LttvModule **)list->pdata;
224 g_ptr_array_free(list, FALSE);
225 return array;
226 }
227
228
229 LttvModule **
230 lttv_module_info(LttvModule *m, const char **name,
231 guint *ref_count, guint *load_count, guint *nb_dependents)
232 {
233 guint i, len = m->dependents->len;
234
235 LttvModule **array = g_new(LttvModule *, len);
236
237 *name = g_module_name(m->module);
238 *ref_count = m->ref_count;
239 *load_count = m->load_count;
240 *nb_dependents = len;
241 for(i = 0 ; i < len ; i++) array[i] = m->dependents->pdata[i];
242 return array;
243 }
244
245
246 static void
247 list_independent(gpointer key, gpointer value, gpointer user_data)
248 {
249 LttvModule *m = (LttvModule *)value;
250
251 if(m->load_count > 0) g_ptr_array_add((GPtrArray *)user_data, m);
252 }
253
254
255 void
256 lttv_module_unload_all()
257 {
258 guint i;
259
260 LttvModule *m;
261
262 GPtrArray *independent_modules = g_ptr_array_new();
263
264 g_hash_table_foreach(modules, list_independent, independent_modules);
265
266 for(i = 0 ; i < independent_modules->len ; i++) {
267 m = (LttvModule *)independent_modules->pdata[i];
268 while(m->load_count > 0) lttv_module_unload(m);
269 }
270
271 g_ptr_array_free(independent_modules, TRUE);
272 if(g_hash_table_size(modules) != 0) g_warning("cannot unload all modules");
273 }
This page took 0.039913 seconds and 5 git commands to generate.