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 guint is_resident : 1;
41 GModuleDeInit de_init;
46 /* --- prototypes --- */
47 static gpointer _g_module_open (const gchar *file_name,
49 static void _g_module_close (gpointer handle,
51 static gpointer _g_module_self (void);
52 static gpointer _g_module_symbol (gpointer handle,
53 const gchar *symbol_name);
54 static inline void g_module_set_error (const gchar *error);
55 static inline GModule* g_module_find_by_handle (gpointer handle);
56 static inline GModule* g_module_find_by_name (const gchar *name);
59 /* --- variables --- */
60 const char *g_log_domain_gmodule = "GModule";
61 static GModule *modules = NULL;
62 static GModule *main_module = NULL;
63 static gchar *module_error = NULL;
66 /* --- inline functions --- */
67 static inline GModule*
68 g_module_find_by_handle (gpointer handle)
72 if (main_module && main_module->handle == handle)
75 for (module = modules; module; module = module->next)
76 if (handle == module->handle)
81 static inline GModule*
82 g_module_find_by_name (const gchar *name)
86 for (module = modules; module; module = module->next)
87 if (strcmp (name, module->file_name) == 0)
93 g_module_set_error (const gchar *error)
96 g_free (module_error);
98 module_error = g_strdup (error);
105 /* --- include platform specifc code --- */
106 #define CHECK_ERROR(rv) { g_module_set_error (NULL); }
107 #if (G_MODULE_IMPL == G_MODULE_IMPL_DL)
108 #include "gmodule-dl.c"
109 #elif (G_MODULE_IMPL == G_MODULE_IMPL_DLD)
110 #include "gmodule-dld.c"
113 #define CHECK_ERROR(rv) { g_module_set_error ("unsupported"); return rv; }
115 _g_module_open (const gchar *file_name,
121 _g_module_close (gpointer handle,
126 _g_module_self (void)
131 _g_module_symbol (gpointer handle,
132 const gchar *symbol_name)
136 #endif /* no implementation */
139 /* --- functions --- */
141 g_module_supported (void)
149 g_module_open (const gchar *file_name,
161 handle = _g_module_self ();
164 main_module = g_new (GModule, 1);
165 main_module->file_name = NULL;
166 main_module->handle = handle;
167 main_module->ref_count = 1;
168 main_module->is_resident = TRUE;
169 main_module->de_init = NULL;
170 main_module->next = NULL;
177 /* we first search the module list by name */
178 module = g_module_find_by_name (file_name);
186 /* open the module */
187 handle = _g_module_open (file_name, (flags & G_MODULE_BIND_LAZY) != 0);
191 GModuleCheckInit check_init;
192 const gchar *check_failed = NULL;
194 /* search the module list by handle, since file names are not unique */
195 module = g_module_find_by_handle (handle);
198 _g_module_close (module->handle, TRUE);
200 g_module_set_error (NULL);
205 saved_error = module_error;
207 g_module_set_error (NULL);
209 module = g_new (GModule, 1);
210 module->file_name = g_strdup (file_name);
211 module->handle = handle;
212 module->ref_count = 1;
213 module->is_resident = FALSE;
214 module->de_init = NULL;
215 module->next = modules;
218 /* check initialization */
219 if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init))
220 check_failed = check_init (module);
222 /* we don't call de_init() if the initialization check failed. */
224 g_module_symbol (module, "g_module_de_init", (gpointer) &module->de_init);
230 error = g_strconcat ("GModule initialization check failed: ", check_failed, NULL);
231 g_module_close (module);
233 g_module_set_error (error);
237 g_module_set_error (saved_error);
238 g_free (saved_error);
245 g_module_close (GModule *module)
249 g_return_val_if_fail (module != NULL, FALSE);
250 g_return_val_if_fail (module->ref_count > 0, FALSE);
254 if (!module->ref_count && !module->is_resident && module->de_init)
256 GModuleDeInit de_init;
258 de_init = module->de_init;
259 module->de_init = NULL;
263 if (!module->ref_count && !module->is_resident)
275 last->next = node->next;
277 modules = node->next;
285 _g_module_close (module->handle, FALSE);
286 g_free (module->file_name);
291 return module_error == NULL;
295 g_module_make_resident (GModule *module)
297 g_return_if_fail (module != NULL);
299 module->is_resident = TRUE;
303 g_module_error (void)
309 g_module_symbol (GModule *module,
310 const gchar *symbol_name,
317 g_return_val_if_fail (module != NULL, FALSE);
318 g_return_val_if_fail (symbol_name != NULL, FALSE);
319 g_return_val_if_fail (symbol != NULL, FALSE);
321 #ifdef G_MODULE_NEED_USCORE
322 symbol_name = g_strconcat ("_", symbol_name, NULL);
323 *symbol = _g_module_symbol (module->handle, symbol_name);
324 g_free (symbol_name);
325 #else /* !G_MODULE_NEED_USCORE */
326 *symbol = _g_module_symbol (module->handle, symbol_name);
327 #endif /* !G_MODULE_NEED_USCORE */
333 error = g_strconcat ("`", symbol_name, "': ", module_error, NULL);
334 g_module_set_error (error);
344 g_module_name (GModule *module)
346 g_return_val_if_fail (module != NULL, NULL);
348 if (module == main_module)
351 return module->file_name;