1 /* GMODULE - GLIB wrapper code for dynamic module loading
2 * Copyright (C) 1998 Tim Janik
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "gmoduleconf.h"
25 /* We maintain a list of modules, so we can reference count them.
26 * That's needed because some platforms don't support refernce counts on
27 * modules e.g. the shl_* implementation of HP-UX
28 * (http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html).
29 * Also, the module for the program itself is kept seperatedly for
30 * faster access and because it has special semantics.
34 /* --- structures --- */
40 guint is_resident : 1;
46 /* --- prototypes --- */
47 static gpointer _g_module_open (const gchar *file_name,
49 static void _g_module_close (gpointer handle,
51 static gpointer _g_module_self (void);
52 static gpointer _g_module_symbol (gpointer handle,
53 const gchar *symbol_name);
54 static gchar* _g_module_build_path (const gchar *directory,
55 const gchar *module_name);
56 static inline void g_module_set_error (const gchar *error);
57 static inline GModule* g_module_find_by_handle (gpointer handle);
58 static inline GModule* g_module_find_by_name (const gchar *name);
61 /* --- variables --- */
62 const char *g_log_domain_gmodule = "GModule";
63 static GModule *modules = NULL;
64 static GModule *main_module = NULL;
65 static gchar *module_error = NULL;
68 /* --- inline functions --- */
69 static inline GModule*
70 g_module_find_by_handle (gpointer handle)
74 if (main_module && main_module->handle == handle)
77 for (module = modules; module; module = module->next)
78 if (handle == module->handle)
83 static inline GModule*
84 g_module_find_by_name (const gchar *name)
88 for (module = modules; module; module = module->next)
89 if (strcmp (name, module->file_name) == 0)
95 g_module_set_error (const gchar *error)
98 g_free (module_error);
100 module_error = g_strdup (error);
107 /* --- include platform specifc code --- */
108 #define CHECK_ERROR(rv) { g_module_set_error (NULL); }
109 #if (G_MODULE_IMPL == G_MODULE_IMPL_DL)
110 #include "gmodule-dl.c"
111 #elif (G_MODULE_IMPL == G_MODULE_IMPL_DLD)
112 #include "gmodule-dld.c"
113 #elif (G_MODULE_IMPL == G_MODULE_IMPL_WIN32)
114 #include "gmodule-win32.c"
117 #define CHECK_ERROR(rv) { g_module_set_error ("unsupported"); return rv; }
119 _g_module_open (const gchar *file_name,
125 _g_module_close (gpointer handle,
130 _g_module_self (void)
135 _g_module_symbol (gpointer handle,
136 const gchar *symbol_name)
140 #endif /* no implementation */
142 #if defined (NATIVE_WIN32) && defined (__LCC__)
144 LibMain (void *hinstDll,
145 unsigned long dwReason,
150 #endif /* NATIVE_WIN32 && __LCC__ */
153 /* --- functions --- */
155 g_module_supported (void)
163 g_module_open (const gchar *file_name,
175 handle = _g_module_self ();
178 main_module = g_new (GModule, 1);
179 main_module->file_name = NULL;
180 main_module->handle = handle;
181 main_module->ref_count = 1;
182 main_module->is_resident = TRUE;
183 main_module->unload = NULL;
184 main_module->next = NULL;
191 /* we first search the module list by name */
192 module = g_module_find_by_name (file_name);
200 /* open the module */
201 handle = _g_module_open (file_name, (flags & G_MODULE_BIND_LAZY) != 0);
205 GModuleCheckInit check_init;
206 const gchar *check_failed = NULL;
208 /* search the module list by handle, since file names are not unique */
209 module = g_module_find_by_handle (handle);
212 _g_module_close (module->handle, TRUE);
214 g_module_set_error (NULL);
219 saved_error = module_error;
221 g_module_set_error (NULL);
223 module = g_new (GModule, 1);
224 module->file_name = g_strdup (file_name);
225 module->handle = handle;
226 module->ref_count = 1;
227 module->is_resident = FALSE;
228 module->unload = NULL;
229 module->next = modules;
232 /* check initialization */
233 if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init))
234 check_failed = check_init (module);
236 /* we don't call unload() if the initialization check failed. */
238 g_module_symbol (module, "g_module_unload", (gpointer) &module->unload);
244 error = g_strconcat ("GModule initialization check failed: ", check_failed, NULL);
245 g_module_close (module);
247 g_module_set_error (error);
251 g_module_set_error (saved_error);
252 g_free (saved_error);
259 g_module_close (GModule *module)
263 g_return_val_if_fail (module != NULL, FALSE);
264 g_return_val_if_fail (module->ref_count > 0, FALSE);
268 if (!module->ref_count && !module->is_resident && module->unload)
270 GModuleUnload unload;
272 unload = module->unload;
273 module->unload = NULL;
277 if (!module->ref_count && !module->is_resident)
289 last->next = node->next;
291 modules = node->next;
299 _g_module_close (module->handle, FALSE);
300 g_free (module->file_name);
305 return module_error == NULL;
309 g_module_make_resident (GModule *module)
311 g_return_if_fail (module != NULL);
313 module->is_resident = TRUE;
317 g_module_error (void)
323 g_module_symbol (GModule *module,
324 const gchar *symbol_name,
331 g_return_val_if_fail (module != NULL, FALSE);
332 g_return_val_if_fail (symbol_name != NULL, FALSE);
333 g_return_val_if_fail (symbol != NULL, FALSE);
335 #ifdef G_MODULE_NEED_USCORE
339 name = g_strconcat ("_", symbol_name, NULL);
340 *symbol = _g_module_symbol (module->handle, name);
343 #else /* !G_MODULE_NEED_USCORE */
344 *symbol = _g_module_symbol (module->handle, symbol_name);
345 #endif /* !G_MODULE_NEED_USCORE */
351 error = g_strconcat ("`", symbol_name, "': ", module_error, NULL);
352 g_module_set_error (error);
362 g_module_name (GModule *module)
364 g_return_val_if_fail (module != NULL, NULL);
366 if (module == main_module)
369 return module->file_name;
373 g_module_build_path (const gchar *directory,
374 const gchar *module_name)
376 g_return_val_if_fail (module_name != NULL, NULL);
378 return _g_module_build_path (directory, module_name);