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 (http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html).
28 * Also, the module for the program itself is kept seperatedly for
33 /* --- structures --- */
39 GModuleDeInit de_init;
44 /* --- prototypes --- */
45 static gpointer _g_module_open (const gchar *file_name,
47 static void _g_module_close (gpointer *handle_p,
49 static gpointer _g_module_self (void);
50 static gpointer _g_module_symbol (gpointer *handle_p,
51 const gchar *symbol_name);
52 static inline void g_module_set_error (const gchar *error);
53 static inline GModule* g_module_find_by_handle (gpointer handle);
54 static inline GModule* g_module_find_by_name (const gchar *name);
57 /* --- variables --- */
58 static GModule *modules = NULL;
59 static GModule *main_module = NULL;
60 static gchar *module_error = NULL;
63 /* --- inline functions --- */
64 static inline GModule*
65 g_module_find_by_handle (gpointer handle)
69 if (main_module && main_module->handle == handle)
72 for (module = modules; module; module = module->next)
73 if (handle == module->handle)
78 static inline GModule*
79 g_module_find_by_name (const gchar *name)
83 for (module = modules; module; module = module->next)
84 if (strcmp (name, module->file_name) == 0)
90 g_module_set_error (const gchar *error)
93 g_free (module_error);
95 module_error = g_strdup (error);
102 /* --- include platform specifc code --- */
103 #define CHECK_ERROR(rv) { g_module_set_error (NULL); }
104 #if (G_MODULE_IMPL == G_MODULE_IMPL_DL)
105 #include "gmodule-dl.c"
106 #elif (G_MODULE_IMPL == G_MODULE_IMPL_DLD)
107 #include "gmodule-dld.c"
110 #define CHECK_ERROR(rv) { g_module_set_error ("unsupported"); return rv; }
111 #endif /* no implementation */
114 /* --- functions --- */
116 g_module_supported (void)
124 g_module_open (const gchar *file_name,
136 handle = _g_module_self ();
139 main_module = g_new (GModule, 1);
140 main_module->file_name = NULL;
141 main_module->handle = handle;
142 main_module->ref_count = 1;
143 main_module->de_init = NULL;
144 main_module->next = NULL;
151 /* we first search the module list by name */
152 module = g_module_find_by_name (file_name);
160 /* open the module */
161 handle = _g_module_open (file_name, (flags & G_MODULE_BIND_LAZY) != 0);
165 GModuleCheckInit check_init;
166 gboolean check_failed = FALSE;
168 /* search the module list by handle, since file names are not unique */
169 module = g_module_find_by_handle (handle);
172 _g_module_close (&module->handle, TRUE);
174 g_module_set_error (NULL);
179 saved_error = module_error;
181 g_module_set_error (NULL);
183 module = g_new (GModule, 1);
184 module->file_name = g_strdup (file_name);
185 module->handle = handle;
186 module->ref_count = 1;
187 module->de_init = NULL;
188 module->next = modules;
191 /* check initialization */
192 if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init))
193 check_failed = check_init (module);
195 /* we don't call de_init() if the initialization check failed. */
197 g_module_symbol (module, "g_module_de_init", (gpointer) &module->de_init);
201 g_module_close (module);
203 g_module_set_error ("GModule initialization check failed");
206 g_module_set_error (saved_error);
207 g_free (saved_error);
214 g_module_close (GModule *module)
218 g_return_val_if_fail (module != NULL, FALSE);
219 g_return_val_if_fail (module->ref_count > 0, FALSE);
221 if (module != main_module)
224 if (!module->ref_count && module->de_init)
225 module->de_init (module);
226 if (!module->ref_count)
238 last->next = node->next;
240 modules = node->next;
248 _g_module_close (&module->handle, FALSE);
249 g_free (module->file_name);
254 return module_error == NULL;
258 g_module_error (void)
264 g_module_symbol (GModule *module,
265 const gchar *symbol_name,
266 gconstpointer *symbol)
272 g_return_val_if_fail (module != NULL, FALSE);
273 g_return_val_if_fail (symbol_name != NULL, FALSE);
274 g_return_val_if_fail (symbol != NULL, FALSE);
276 #ifdef G_MODULE_NEED_USCORE
277 symbol_name = g_strconcat ("_", symbol_name, NULL);
278 *symbol = _g_module_symbol (&module->handle, symbol_name);
279 g_free (symbol_name);
280 #else /* !G_MODULE_NEED_USCORE */
281 *symbol = _g_module_symbol (&module->handle, symbol_name);
282 #endif /* !G_MODULE_NEED_USCORE */
294 g_module_name (GModule *module)
296 g_return_val_if_fail (module != NULL, NULL);
298 if (module == main_module)
301 return module->file_name;