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.
25 #include "gmoduleconf.h"
30 /* We maintain a list of modules, so we can reference count them.
31 * That's needed because some platforms don't support refernce counts on
32 * modules e.g. the shl_* implementation of HP-UX
33 * (http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html).
34 * Also, the module for the program itself is kept seperatedly for
35 * faster access and because it has special semantics.
39 /* --- structures --- */
45 guint is_resident : 1;
51 /* --- prototypes --- */
52 static gpointer _g_module_open (const gchar *file_name,
54 static void _g_module_close (gpointer handle,
56 static gpointer _g_module_self (void);
57 static gpointer _g_module_symbol (gpointer handle,
58 const gchar *symbol_name);
59 static gchar* _g_module_build_path (const gchar *directory,
60 const gchar *module_name);
61 static inline void g_module_set_error (const gchar *error);
62 static inline GModule* g_module_find_by_handle (gpointer handle);
63 static inline GModule* g_module_find_by_name (const gchar *name);
66 /* --- variables --- */
67 G_LOCK_DEFINE_STATIC (GModule);
68 const char *g_log_domain_gmodule = "GModule";
69 static GModule *modules = NULL;
70 static GModule *main_module = NULL;
71 static GStaticPrivate module_error_private = G_STATIC_PRIVATE_INIT;
74 /* --- inline functions --- */
75 static inline GModule*
76 g_module_find_by_handle (gpointer handle)
79 GModule *retval = NULL;
82 if (main_module && main_module->handle == handle)
85 for (module = modules; module; module = module->next)
86 if (handle == module->handle)
96 static inline GModule*
97 g_module_find_by_name (const gchar *name)
100 GModule *retval = NULL;
103 for (module = modules; module; module = module->next)
104 if (strcmp (name, module->file_name) == 0)
115 g_module_set_error (const gchar *error)
117 g_static_private_set (&module_error_private, g_strdup (error), g_free);
122 /* --- include platform specifc code --- */
123 #define CHECK_ERROR(rv) { g_module_set_error (NULL); }
124 #if (G_MODULE_IMPL == G_MODULE_IMPL_DL)
125 #include "gmodule-dl.c"
126 #elif (G_MODULE_IMPL == G_MODULE_IMPL_DLD)
127 #include "gmodule-dld.c"
128 #elif (G_MODULE_IMPL == G_MODULE_IMPL_WIN32)
129 #include "gmodule-win32.c"
132 #define CHECK_ERROR(rv) { g_module_set_error ("unsupported"); return rv; }
134 _g_module_open (const gchar *file_name,
140 _g_module_close (gpointer handle,
145 _g_module_self (void)
150 _g_module_symbol (gpointer handle,
151 const gchar *symbol_name)
156 _g_module_build_path (const gchar *directory,
157 const gchar *module_name)
161 #endif /* no implementation */
163 #if defined (NATIVE_WIN32) && defined (__LCC__)
165 LibMain (void *hinstDll,
166 unsigned long dwReason,
171 #endif /* NATIVE_WIN32 && __LCC__ */
174 /* --- functions --- */
176 g_module_supported (void)
184 g_module_open (const gchar *file_name,
197 handle = _g_module_self ();
200 main_module = g_new (GModule, 1);
201 main_module->file_name = NULL;
202 main_module->handle = handle;
203 main_module->ref_count = 1;
204 main_module->is_resident = TRUE;
205 main_module->unload = NULL;
206 main_module->next = NULL;
214 /* we first search the module list by name */
215 module = g_module_find_by_name (file_name);
223 /* open the module */
224 handle = _g_module_open (file_name, (flags & G_MODULE_BIND_LAZY) != 0);
228 GModuleCheckInit check_init;
229 const gchar *check_failed = NULL;
231 /* search the module list by handle, since file names are not unique */
232 module = g_module_find_by_handle (handle);
235 _g_module_close (module->handle, TRUE);
237 g_module_set_error (NULL);
242 saved_error = g_strdup (g_module_error ());
243 g_module_set_error (NULL);
245 module = g_new (GModule, 1);
246 module->file_name = g_strdup (file_name);
247 module->handle = handle;
248 module->ref_count = 1;
249 module->is_resident = FALSE;
250 module->unload = NULL;
252 module->next = modules;
256 /* check initialization */
257 if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init))
258 check_failed = check_init (module);
260 /* we don't call unload() if the initialization check failed. */
262 g_module_symbol (module, "g_module_unload", (gpointer) &module->unload);
268 error = g_strconcat ("GModule initialization check failed: ", check_failed, NULL);
269 g_module_close (module);
271 g_module_set_error (error);
275 g_module_set_error (saved_error);
277 g_free (saved_error);
284 g_module_close (GModule *module)
288 g_return_val_if_fail (module != NULL, FALSE);
289 g_return_val_if_fail (module->ref_count > 0, FALSE);
293 if (!module->ref_count && !module->is_resident && module->unload)
295 GModuleUnload unload;
297 unload = module->unload;
298 module->unload = NULL;
302 if (!module->ref_count && !module->is_resident)
316 last->next = node->next;
318 modules = node->next;
327 _g_module_close (module->handle, FALSE);
328 g_free (module->file_name);
333 return g_module_error() == NULL;
337 g_module_make_resident (GModule *module)
339 g_return_if_fail (module != NULL);
341 module->is_resident = TRUE;
345 g_module_error (void)
347 return g_static_private_get (&module_error_private);
351 g_module_symbol (GModule *module,
352 const gchar *symbol_name,
360 g_return_val_if_fail (module != NULL, FALSE);
361 g_return_val_if_fail (symbol_name != NULL, FALSE);
362 g_return_val_if_fail (symbol != NULL, FALSE);
364 #ifdef G_MODULE_NEED_USCORE
368 name = g_strconcat ("_", symbol_name, NULL);
369 *symbol = _g_module_symbol (module->handle, name);
372 #else /* !G_MODULE_NEED_USCORE */
373 *symbol = _g_module_symbol (module->handle, symbol_name);
374 #endif /* !G_MODULE_NEED_USCORE */
376 if ((module_error = g_module_error()))
380 error = g_strconcat ("`", symbol_name, "': ", module_error, NULL);
381 g_module_set_error (error);
391 g_module_name (GModule *module)
393 g_return_val_if_fail (module != NULL, NULL);
395 if (module == main_module)
398 return module->file_name;
402 g_module_build_path (const gchar *directory,
403 const gchar *module_name)
405 g_return_val_if_fail (module_name != NULL, NULL);
407 return _g_module_build_path (directory, module_name);