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 const char *g_log_domain_gmodule = "GModule";
60 static GModule *modules = NULL;
61 static GModule *main_module = NULL;
62 static gchar *module_error = NULL;
65 /* --- inline functions --- */
66 static inline GModule*
67 g_module_find_by_handle (gpointer handle)
71 if (main_module && main_module->handle == handle)
74 for (module = modules; module; module = module->next)
75 if (handle == module->handle)
80 static inline GModule*
81 g_module_find_by_name (const gchar *name)
85 for (module = modules; module; module = module->next)
86 if (strcmp (name, module->file_name) == 0)
92 g_module_set_error (const gchar *error)
95 g_free (module_error);
97 module_error = g_strdup (error);
104 /* --- include platform specifc code --- */
105 #define CHECK_ERROR(rv) { g_module_set_error (NULL); }
106 #if (G_MODULE_IMPL == G_MODULE_IMPL_DL)
107 #include "gmodule-dl.c"
108 #elif (G_MODULE_IMPL == G_MODULE_IMPL_DLD)
109 #include "gmodule-dld.c"
112 #define CHECK_ERROR(rv) { g_module_set_error ("unsupported"); return rv; }
114 _g_module_open (const gchar *file_name,
120 _g_module_close (gpointer handle,
125 _g_module_self (void)
130 _g_module_symbol (gpointer handle,
131 const gchar *symbol_name)
135 #endif /* no implementation */
138 /* --- functions --- */
140 g_module_supported (void)
148 g_module_open (const gchar *file_name,
160 handle = _g_module_self ();
163 main_module = g_new (GModule, 1);
164 main_module->file_name = NULL;
165 main_module->handle = handle;
166 main_module->ref_count = 1;
167 main_module->de_init = NULL;
168 main_module->next = NULL;
175 /* we first search the module list by name */
176 module = g_module_find_by_name (file_name);
184 /* open the module */
185 handle = _g_module_open (file_name, (flags & G_MODULE_BIND_LAZY) != 0);
189 GModuleCheckInit check_init;
190 const gchar *check_failed = NULL;
192 /* search the module list by handle, since file names are not unique */
193 module = g_module_find_by_handle (handle);
196 _g_module_close (module->handle, TRUE);
198 g_module_set_error (NULL);
203 saved_error = module_error;
205 g_module_set_error (NULL);
207 module = g_new (GModule, 1);
208 module->file_name = g_strdup (file_name);
209 module->handle = handle;
210 module->ref_count = 1;
211 module->de_init = NULL;
212 module->next = modules;
215 /* check initialization */
216 if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init))
217 check_failed = check_init (module);
219 /* we don't call de_init() if the initialization check failed. */
221 g_module_symbol (module, "g_module_de_init", (gpointer) &module->de_init);
227 error = g_strconcat ("GModule initialization check failed: ", check_failed, NULL);
228 g_module_close (module);
230 g_module_set_error (error);
234 g_module_set_error (saved_error);
235 g_free (saved_error);
242 g_module_close (GModule *module)
246 g_return_val_if_fail (module != NULL, FALSE);
247 g_return_val_if_fail (module->ref_count > 0, FALSE);
249 if (module != main_module)
252 if (!module->ref_count && module->de_init)
253 module->de_init (module);
254 if (!module->ref_count)
266 last->next = node->next;
268 modules = node->next;
276 _g_module_close (module->handle, FALSE);
277 g_free (module->file_name);
282 return module_error == NULL;
286 g_module_error (void)
292 g_module_symbol (GModule *module,
293 const gchar *symbol_name,
300 g_return_val_if_fail (module != NULL, FALSE);
301 g_return_val_if_fail (symbol_name != NULL, FALSE);
302 g_return_val_if_fail (symbol != NULL, FALSE);
304 #ifdef G_MODULE_NEED_USCORE
305 symbol_name = g_strconcat ("_", symbol_name, NULL);
306 *symbol = _g_module_symbol (module->handle, symbol_name);
307 g_free (symbol_name);
308 #else /* !G_MODULE_NEED_USCORE */
309 *symbol = _g_module_symbol (module->handle, symbol_name);
310 #endif /* !G_MODULE_NEED_USCORE */
316 error = g_strconcat ("`", symbol_name, "': ", module_error, NULL);
317 g_module_set_error (error);
327 g_module_name (GModule *module)
329 g_return_val_if_fail (module != NULL, NULL);
331 if (module == main_module)
334 return module->file_name;