1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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 for (module = modules; module; module = module->next)
70 if (handle == module->handle)
75 static inline GModule*
76 g_module_find_by_name (const gchar *name)
80 for (module = modules; module; module = module->next)
81 if (strcmp (name, module->file_name) == 0)
87 g_module_set_error (const gchar *error)
90 g_free (module_error);
92 module_error = g_strdup (error);
99 /* --- include platform specifc code --- */
100 #define CHECK_ERROR(rv) { g_module_set_error (NULL); }
101 #if (G_MODULE_IMPL == G_MODULE_IMPL_DL)
102 #include "gmodule-dl.c"
103 #elif (G_MODULE_IMPL == G_MODULE_IMPL_DLD)
104 #include "gmodule-dld.c"
107 #define CHECK_ERROR(rv) { g_module_set_error ("unsupported"); return rv; }
108 #endif /* no implementation */
111 /* --- functions --- */
113 g_module_supported (void)
121 g_module_open (const gchar *file_name,
133 handle = _g_module_self ();
136 main_module = g_new (GModule, 1);
137 main_module->file_name = NULL;
138 main_module->handle = handle;
139 main_module->ref_count = 1;
140 main_module->de_init = NULL;
141 main_module->next = NULL;
148 /* we first search the module list by name */
149 module = g_module_find_by_name (file_name);
157 /* open the module */
158 handle = _g_module_open (file_name, (flags & G_MODULE_BIND_LAZY) != 0);
162 GModuleCheckInit check_init;
163 gboolean check_failed = FALSE;
165 /* search the module list by handle, since file names are not unique */
166 module = g_module_find_by_handle (handle);
169 _g_module_close (&module->handle, TRUE);
171 g_module_set_error (NULL);
176 saved_error = module_error;
178 g_module_set_error (NULL);
180 module = g_new (GModule, 1);
181 module->file_name = g_strdup (file_name);
182 module->handle = handle;
183 module->ref_count = 0;
184 module->de_init = NULL;
187 /* check initialization */
188 if (g_module_symbol (module, "g_module_check_init", &check_init))
189 check_failed = check_init (module);
191 /* should call de_init() on failed initializations also? */
193 g_module_symbol (module, "g_module_de_init", &module->de_init);
195 module->ref_count += 1;
196 module->next = modules;
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)
236 last->next = node->next;
238 modules = node->next;
247 module->de_init (module);
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;