2 /* module.c : Implementation of the module loading/unloading mechanism.
6 #include <lttv/module.h>
14 GPtrArray
*dependents
;
18 /* Table of loaded modules and paths where to search for modules */
20 static GHashTable
*modules
= NULL
;
22 static GPtrArray
*modulesPaths
= NULL
;
24 static void lttv_module_unload_all();
27 void lttv_module_init(int argc
, char **argv
)
29 modules
= g_hash_table_new(g_str_hash
, g_str_equal
);
30 modulesPaths
= g_ptr_array_new();
34 void lttv_module_destroy()
38 /* Unload all modules */
39 lttv_module_unload_all();
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
]);
45 g_ptr_array_free(modulesPaths
,TRUE
) ;
48 /* destroy the hash table */
49 g_hash_table_destroy(modules
) ;
54 /* Add a new pathname to the modules loading search path */
56 void lttv_module_path_add(const char *name
)
58 g_ptr_array_add(modulesPaths
,(char*)g_strdup(name
));
62 static LttvModuleInfo
*
63 module_load(const char *name
, int argc
, char **argv
)
73 LttvModuleInit init_function
;
75 /* Try to find the module along all the user specified paths */
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);
85 /* Try the default system path */
88 pathname
= g_module_build_path(NULL
,name
);
89 gm
= g_module_open(pathname
,0);
93 /* Module cannot be found */
95 if(gm
== NULL
) return NULL
;
97 /* Check if the module was already opened using the hopefully canonical name
98 returned by g_module_name. */
100 pathname
= g_module_name(gm
);
102 m
= g_hash_table_lookup(modules
, pathname
);
106 /* Module loaded for the first time. Insert it in the table and call the
107 init function if any. */
109 m
= g_new(LttvModule
);
113 m
->dependents
= g_ptr_array_new();
114 g_hash_table_insert(modules
, pathname
, m
);
116 if(!g_module_symbol(gm
, "init", (gpointer
)&init_function
)) {
117 g_warning("module %s (%s) has no init function", name
, pathname
);
119 else init_Function(m
, argc
, argv
);
123 /* Module was already opened, check that it really is the same and
124 undo the extra g_module_open */
126 if(m
->module
!= gm
) g_error("Two gmodules with the same pathname");
136 lttv_module_load(const char *name
, int argc
, char **argv
)
138 LttvModule
*m
= module_load(name
, argc
, argv
);
140 if(m
!= NULL
) m
->load_count
++;
146 lttv_module_require(LttvModule
*m
, const char *name
, int argc
, char **argv
)
150 module
= module_load(name
, argc
, argv
);
151 if(module
!= NULL
) g_ptr_array_add(m
->dependents
, module
);
156 static void module_unload(LttvModule
*m
)
158 LttvModuleDestroy destroy_function
;
164 /* Decrement the reference count */
167 if(m
->ref_count
> 0) return;
169 /* We really have to unload the module, first unload its dependents */
171 len
= m
->dependents
->len
;
173 for(i
= 0 ; i
< len
; i
++) {
174 module_unload(m
->dependents
->pdata
[i
]);
177 if(len
!= m
->dependents
->len
) g_error("dependents list modified");
179 /* Unload the module itself */
181 if(!g_module_symbol(m
->module
, "destroy", (gpointer
)&destroy_function
)) {
182 g_warning("module (%s) has no destroy function", pathname
);
184 else destroy_function();
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
);
193 void lttv_module_unload(LttvModule
*m
)
195 if(m
->load_count
<= 0) {
196 g_error("more unload than load (%s)", g_module_name(m
->module
));
205 list_modules(gpointer key
, gpointer value
, gpointer user_data
)
207 g_ptr_array_add((GPtrArray
*)user_data
, value
);
212 lttv_module_list(guint
*nb
)
214 GPtrArray
*list
= g_ptr_array_new();
218 g_hash_table_foreach(modules
, list_modules
, list
);
220 array
= (LttvModule
**)list
->pdata
;
221 g_ptr_array_free(list
, FALSE
);
227 lttv_module_info(LttvModule
*m
, const char **name
,
228 guint
*ref_count
, guint
*load_count
, guint
*nb_dependents
)
230 guint i
, len
= m
->dependents
->len
;
232 LttvModule
**array
= g_new(LttvModule
*, len
);
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
];
244 list_independent(gpointer key
, gpointer value
, gpointer user_data
)
246 LttvModule
*m
= (LttvModule
*)value
;
248 if(m
->load_count
> 0) g_ptr_array_add((GPtrArray
*)user_data
, m
);
253 lttv_module_unload_all()
259 GPtrArray
*independent_modules
= g_ptr_array_new();
261 g_hash_table_foreach(modules
, list_independent
, independent_modules
);
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
);
268 g_ptr_array_free(independent_modules
, TRUE
);
269 if(g_hash_table_size(modules
) != 0) g_warning("cannot unload all modules");