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.
21 * Modified by the GLib Team and others 1997-1999. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
32 #include "gmoduleconf.h"
37 /* We maintain a list of modules, so we can reference count them.
38 * That's needed because some platforms don't support refernce counts on
39 * modules e.g. the shl_* implementation of HP-UX
40 * (http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html).
41 * Also, the module for the program itself is kept seperatedly for
42 * faster access and because it has special semantics.
46 /* --- structures --- */
52 guint is_resident : 1;
58 /* --- prototypes --- */
59 static gpointer _g_module_open (const gchar *file_name,
61 static void _g_module_close (gpointer handle,
63 static gpointer _g_module_self (void);
64 static gpointer _g_module_symbol (gpointer handle,
65 const gchar *symbol_name);
66 static gchar* _g_module_build_path (const gchar *directory,
67 const gchar *module_name);
68 static inline void g_module_set_error (const gchar *error);
69 static inline GModule* g_module_find_by_handle (gpointer handle);
70 static inline GModule* g_module_find_by_name (const gchar *name);
73 /* --- variables --- */
74 G_LOCK_DEFINE_STATIC (GModule);
75 const char *g_log_domain_gmodule = "GModule";
76 static GModule *modules = NULL;
77 static GModule *main_module = NULL;
78 static GStaticPrivate module_error_private = G_STATIC_PRIVATE_INIT;
81 /* --- inline functions --- */
82 static inline GModule*
83 g_module_find_by_handle (gpointer handle)
86 GModule *retval = NULL;
89 if (main_module && main_module->handle == handle)
92 for (module = modules; module; module = module->next)
93 if (handle == module->handle)
103 static inline GModule*
104 g_module_find_by_name (const gchar *name)
107 GModule *retval = NULL;
110 for (module = modules; module; module = module->next)
111 if (strcmp (name, module->file_name) == 0)
122 g_module_set_error (const gchar *error)
124 g_static_private_set (&module_error_private, g_strdup (error), g_free);
129 /* --- include platform specifc code --- */
130 #define CHECK_ERROR(rv) { g_module_set_error (NULL); }
131 #if (G_MODULE_IMPL == G_MODULE_IMPL_DL)
132 #include "gmodule-dl.c"
133 #elif (G_MODULE_IMPL == G_MODULE_IMPL_DLD)
134 #include "gmodule-dld.c"
135 #elif (G_MODULE_IMPL == G_MODULE_IMPL_WIN32)
136 #include "gmodule-win32.c"
137 #elif (G_MODULE_IMPL == G_MODULE_IMPL_OS2)
138 #include "gmodule-os2.c"
141 #define CHECK_ERROR(rv) { g_module_set_error ("dynamic modules are " \
142 "not supported by this system"); return rv; }
144 _g_module_open (const gchar *file_name,
150 _g_module_close (gpointer handle,
155 _g_module_self (void)
160 _g_module_symbol (gpointer handle,
161 const gchar *symbol_name)
166 _g_module_build_path (const gchar *directory,
167 const gchar *module_name)
171 #endif /* no implementation */
173 /* --- functions --- */
175 g_module_supported (void)
183 g_module_open (const gchar *file_name,
196 handle = _g_module_self ();
199 main_module = g_new (GModule, 1);
200 main_module->file_name = NULL;
201 main_module->handle = handle;
202 main_module->ref_count = 1;
203 main_module->is_resident = TRUE;
204 main_module->unload = NULL;
205 main_module->next = NULL;
213 /* we first search the module list by name */
214 module = g_module_find_by_name (file_name);
222 /* open the module */
223 handle = _g_module_open (file_name, (flags & G_MODULE_BIND_LAZY) != 0);
227 GModuleCheckInit check_init;
228 const gchar *check_failed = NULL;
230 /* search the module list by handle, since file names are not unique */
231 module = g_module_find_by_handle (handle);
234 _g_module_close (module->handle, TRUE);
236 g_module_set_error (NULL);
241 saved_error = g_strdup (g_module_error ());
242 g_module_set_error (NULL);
244 module = g_new (GModule, 1);
245 module->file_name = g_strdup (file_name);
246 module->handle = handle;
247 module->ref_count = 1;
248 module->is_resident = FALSE;
249 module->unload = NULL;
251 module->next = modules;
255 /* check initialization */
256 if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init))
257 check_failed = check_init (module);
259 /* we don't call unload() if the initialization check failed. */
261 g_module_symbol (module, "g_module_unload", (gpointer) &module->unload);
267 error = g_strconcat ("GModule initialization check failed: ", check_failed, NULL);
268 g_module_close (module);
270 g_module_set_error (error);
274 g_module_set_error (saved_error);
276 g_free (saved_error);
283 g_module_close (GModule *module)
287 g_return_val_if_fail (module != NULL, FALSE);
288 g_return_val_if_fail (module->ref_count > 0, FALSE);
292 if (!module->ref_count && !module->is_resident && module->unload)
294 GModuleUnload unload;
296 unload = module->unload;
297 module->unload = NULL;
301 if (!module->ref_count && !module->is_resident)
315 last->next = node->next;
317 modules = node->next;
326 _g_module_close (module->handle, FALSE);
327 g_free (module->file_name);
332 return g_module_error() == NULL;
336 g_module_make_resident (GModule *module)
338 g_return_if_fail (module != NULL);
340 module->is_resident = TRUE;
344 g_module_error (void)
346 return g_static_private_get (&module_error_private);
350 g_module_symbol (GModule *module,
351 const gchar *symbol_name,
359 g_return_val_if_fail (module != NULL, FALSE);
360 g_return_val_if_fail (symbol_name != NULL, FALSE);
361 g_return_val_if_fail (symbol != NULL, FALSE);
363 #ifdef G_MODULE_NEED_USCORE
367 name = g_strconcat ("_", symbol_name, NULL);
368 *symbol = _g_module_symbol (module->handle, name);
371 #else /* !G_MODULE_NEED_USCORE */
372 *symbol = _g_module_symbol (module->handle, symbol_name);
373 #endif /* !G_MODULE_NEED_USCORE */
375 if ((module_error = g_module_error()))
379 error = g_strconcat ("`", symbol_name, "': ", module_error, NULL);
380 g_module_set_error (error);
390 g_module_name (GModule *module)
392 g_return_val_if_fail (module != NULL, NULL);
394 if (module == main_module)
397 return module->file_name;
401 g_module_build_path (const gchar *directory,
402 const gchar *module_name)
404 g_return_val_if_fail (module_name != NULL, NULL);
406 return _g_module_build_path (directory, module_name);