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 GModuleDeInit de_init;
45 /* --- prototypes --- */
46 static gpointer _g_module_open (const gchar *file_name,
48 static void _g_module_close (gpointer handle,
50 static gpointer _g_module_self (void);
51 static gpointer _g_module_symbol (gpointer handle,
52 const gchar *symbol_name);
53 static inline void g_module_set_error (const gchar *error);
54 static inline GModule* g_module_find_by_handle (gpointer handle);
55 static inline GModule* g_module_find_by_name (const gchar *name);
58 /* --- variables --- */
59 static GModule *modules = NULL;
60 static GModule *main_module = NULL;
61 static gchar *module_error = NULL;
64 /* --- inline functions --- */
65 static inline GModule*
66 g_module_find_by_handle (gpointer handle)
70 if (main_module && main_module->handle == handle)
73 for (module = modules; module; module = module->next)
74 if (handle == module->handle)
79 static inline GModule*
80 g_module_find_by_name (const gchar *name)
84 for (module = modules; module; module = module->next)
85 if (strcmp (name, module->file_name) == 0)
91 g_module_set_error (const gchar *error)
94 g_free (module_error);
96 module_error = g_strdup (error);
103 /* --- include platform specifc code --- */
104 #define CHECK_ERROR(rv) { g_module_set_error (NULL); }
105 #if (G_MODULE_IMPL == G_MODULE_IMPL_DL)
106 #include "gmodule-dl.c"
107 #elif (G_MODULE_IMPL == G_MODULE_IMPL_DLD)
108 #include "gmodule-dld.c"
111 #define CHECK_ERROR(rv) { g_module_set_error ("unsupported"); return rv; }
112 #endif /* no implementation */
115 /* --- functions --- */
117 g_module_supported (void)
125 g_module_open (const gchar *file_name,
137 handle = _g_module_self ();
140 main_module = g_new (GModule, 1);
141 main_module->file_name = NULL;
142 main_module->handle = handle;
143 main_module->ref_count = 1;
144 main_module->de_init = NULL;
145 main_module->next = NULL;
152 /* we first search the module list by name */
153 module = g_module_find_by_name (file_name);
161 /* open the module */
162 handle = _g_module_open (file_name, (flags & G_MODULE_BIND_LAZY) != 0);
166 GModuleCheckInit check_init;
167 gboolean check_failed = FALSE;
169 /* search the module list by handle, since file names are not unique */
170 module = g_module_find_by_handle (handle);
173 _g_module_close (module->handle, TRUE);
175 g_module_set_error (NULL);
180 saved_error = module_error;
182 g_module_set_error (NULL);
184 module = g_new (GModule, 1);
185 module->file_name = g_strdup (file_name);
186 module->handle = handle;
187 module->ref_count = 1;
188 module->de_init = NULL;
189 module->next = modules;
192 /* check initialization */
193 if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init))
194 check_failed = check_init (module);
196 /* we don't call de_init() if the initialization check failed. */
198 g_module_symbol (module, "g_module_de_init", (gpointer) &module->de_init);
202 g_module_close (module);
204 g_module_set_error ("GModule initialization check failed");
207 g_module_set_error (saved_error);
208 g_free (saved_error);
215 g_module_close (GModule *module)
219 g_return_val_if_fail (module != NULL, FALSE);
220 g_return_val_if_fail (module->ref_count > 0, FALSE);
222 if (module != main_module)
225 if (!module->ref_count && module->de_init)
226 module->de_init (module);
227 if (!module->ref_count)
239 last->next = node->next;
241 modules = node->next;
249 _g_module_close (module->handle, FALSE);
250 g_free (module->file_name);
255 return module_error == NULL;
259 g_module_error (void)
265 g_module_symbol (GModule *module,
266 const gchar *symbol_name,
267 gconstpointer *symbol)
273 g_return_val_if_fail (module != NULL, FALSE);
274 g_return_val_if_fail (symbol_name != NULL, FALSE);
275 g_return_val_if_fail (symbol != NULL, FALSE);
277 #ifdef G_MODULE_NEED_USCORE
278 symbol_name = g_strconcat ("_", symbol_name, NULL);
279 *symbol = _g_module_symbol (module->handle, symbol_name);
280 g_free (symbol_name);
281 #else /* !G_MODULE_NEED_USCORE */
282 *symbol = _g_module_symbol (module->handle, symbol_name);
283 #endif /* !G_MODULE_NEED_USCORE */
295 g_module_name (GModule *module)
297 g_return_val_if_fail (module != NULL, NULL);
299 if (module == main_module)
302 return module->file_name;