2 /* module.c : Implementation of the module loading/unloading mechanism.
6 #include <lttv/module.h>
8 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
9 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
16 GPtrArray
*dependents
;
20 /* Table of loaded modules and paths where to search for modules */
22 static GHashTable
*modules
= NULL
;
24 static GPtrArray
*modulesPaths
= NULL
;
26 static void lttv_module_unload_all();
29 void lttv_module_init(int argc
, char **argv
)
31 g_info("Init module.c");
32 modules
= g_hash_table_new(g_str_hash
, g_str_equal
);
33 modulesPaths
= g_ptr_array_new();
37 void lttv_module_destroy()
41 g_info("Destroy module.c");
43 /* Unload all modules */
44 lttv_module_unload_all();
46 /* Free the modules paths pointer array as well as the elements */
47 for(i
= 0; i
< modulesPaths
->len
; i
++) {
48 g_free(modulesPaths
->pdata
[i
]);
50 g_ptr_array_free(modulesPaths
,TRUE
) ;
53 /* destroy the hash table */
54 g_hash_table_destroy(modules
) ;
59 /* Add a new pathname to the modules loading search path */
61 void lttv_module_path_add(const char *name
)
63 g_info("Add module path %s", name
);
64 g_ptr_array_add(modulesPaths
,(char*)g_strdup(name
));
69 module_load(const char *name
, int argc
, char **argv
)
79 const char *module_name
;
81 LttvModuleInit init_function
;
83 g_info("Load module %s", name
);
85 /* Try to find the module along all the user specified paths */
87 for(i
= 0 ; i
< modulesPaths
->len
; i
++) {
88 pathname
= g_module_build_path(modulesPaths
->pdata
[i
],name
);
89 g_info("Try path %s", pathname
);
90 gm
= g_module_open(pathname
,0);
94 g_info("Trial failed, %s", g_module_error());
97 /* Try the default system path */
100 pathname
= g_module_build_path(NULL
,name
);
101 g_info("Try default path");
102 gm
= g_module_open(pathname
,0);
106 /* Module cannot be found */
108 g_info("Trial failed, %s", g_module_error());
109 g_info("Failed to load %s", name
);
113 /* Check if the module was already opened using the hopefully canonical name
114 returned by g_module_name. */
116 module_name
= g_module_name(gm
);
118 m
= g_hash_table_lookup(modules
, module_name
);
121 g_info("Module %s (%s) loaded, call its init function", name
, module_name
);
123 /* Module loaded for the first time. Insert it in the table and call the
124 init function if any. */
126 m
= g_new(LttvModule
, 1);
130 m
->dependents
= g_ptr_array_new();
131 g_hash_table_insert(modules
, (gpointer
)module_name
, m
);
133 if(!g_module_symbol(gm
, "init", (gpointer
)&init_function
)) {
134 g_warning("module %s (%s) has no init function", name
, pathname
);
136 else init_function(m
, argc
, argv
);
140 /* Module was already opened, check that it really is the same and
141 undo the extra g_module_open */
143 g_info("Module %s (%s) was already loaded, no need to call init function",
145 if(m
->module
!= gm
) g_error("Two gmodules with the same pathname");
155 lttv_module_load(const char *name
, int argc
, char **argv
)
157 g_info("Load module %s explicitly", name
);
158 LttvModule
*m
= module_load(name
, argc
, argv
);
159 if(m
!= NULL
) m
->load_count
++;
165 lttv_module_require(LttvModule
*m
, const char *name
, int argc
, char **argv
)
169 g_info("Load module %s, as %s is a dependent requiring it", name
,
170 g_module_name(m
->module
));
171 module
= module_load(name
, argc
, argv
);
172 if(module
!= NULL
) g_ptr_array_add(module
->dependents
, m
);
177 static void module_unload(LttvModule
*m
)
179 LttvModuleDestroy destroy_function
;
185 /* Decrement the reference count */
187 g_info("Unload module %s", g_module_name(m
->module
));
189 if(m
->ref_count
> 0) {
190 g_info("Module usage count decremented to %d", m
->ref_count
);
193 /* We really have to unload the module, first unload its dependents */
195 len
= m
->dependents
->len
;
196 g_info("Unload dependent modules");
198 for(i
= 0 ; i
< len
; i
++) {
199 module_unload(m
->dependents
->pdata
[i
]);
202 if(len
!= m
->dependents
->len
) g_error("dependents list modified");
204 /* Unload the module itself */
206 g_info("Call the destroy function and unload the module");
207 if(!g_module_symbol(m
->module
, "destroy", (gpointer
)&destroy_function
)) {
208 g_warning("module (%s) has no destroy function", pathname
);
210 else destroy_function();
212 g_hash_table_remove(modules
, g_module_name(m
->module
));
213 g_ptr_array_free(m
->dependents
, TRUE
);
214 g_module_close(m
->module
);
219 void lttv_module_unload(LttvModule
*m
)
221 g_info("Explicitly unload module %s", g_module_name(m
->module
));
222 if(m
->load_count
<= 0) {
223 g_error("more unload than load (%s)", g_module_name(m
->module
));
232 list_modules(gpointer key
, gpointer value
, gpointer user_data
)
234 g_ptr_array_add((GPtrArray
*)user_data
, value
);
239 lttv_module_list(guint
*nb
)
241 GPtrArray
*list
= g_ptr_array_new();
245 g_hash_table_foreach(modules
, list_modules
, list
);
247 array
= (LttvModule
**)list
->pdata
;
248 g_ptr_array_free(list
, FALSE
);
254 lttv_module_info(LttvModule
*m
, const char **name
,
255 guint
*ref_count
, guint
*load_count
, guint
*nb_dependents
)
257 guint i
, len
= m
->dependents
->len
;
259 LttvModule
**array
= g_new(LttvModule
*, len
);
261 *name
= g_module_name(m
->module
);
262 *ref_count
= m
->ref_count
;
263 *load_count
= m
->load_count
;
264 *nb_dependents
= len
;
265 for(i
= 0 ; i
< len
; i
++) array
[i
] = m
->dependents
->pdata
[i
];
270 lttv_module_name(LttvModule
*m
)
272 return g_module_name(m
->module
);
276 list_independent(gpointer key
, gpointer value
, gpointer user_data
)
278 LttvModule
*m
= (LttvModule
*)value
;
280 if(m
->load_count
> 0) g_ptr_array_add((GPtrArray
*)user_data
, m
);
285 lttv_module_unload_all()
291 GPtrArray
*independent_modules
= g_ptr_array_new();
293 g_hash_table_foreach(modules
, list_independent
, independent_modules
);
295 for(i
= 0 ; i
< independent_modules
->len
; i
++) {
296 m
= (LttvModule
*)independent_modules
->pdata
[i
];
297 while(m
->load_count
> 0) lttv_module_unload(m
);
300 g_ptr_array_free(independent_modules
, TRUE
);
301 if(g_hash_table_size(modules
) != 0) g_warning("cannot unload all modules");