glib/gmodule/gmodule.c

353 lines
8.0 KiB
C
Raw Normal View History

1998-08-09 13:42:02 +02:00
/* GMODULE - GLIB wrapper code for dynamic module loading
* Copyright (C) 1998 Tim Janik
1998-08-09 10:32:18 +02:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1998-08-09 10:32:18 +02:00
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "gmodule.h"
#include "gmoduleconf.h"
#include <errno.h>
#include <string.h>
/* We maintain a list of modules, so we can reference count them.
* That's needed because some platforms don't support refernce counts on
* modules e.g. the shl_* implementation of HP-UX
* (http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html).
1998-08-09 10:32:18 +02:00
* Also, the module for the program itself is kept seperatedly for
* faster access and because it has special semantics.
1998-08-09 10:32:18 +02:00
*/
/* --- structures --- */
struct _GModule
{
gchar *file_name;
gpointer handle;
guint ref_count : 31;
guint is_resident : 1;
GModuleUnload unload;
1998-08-09 10:32:18 +02:00
GModule *next;
};
/* --- prototypes --- */
static gpointer _g_module_open (const gchar *file_name,
gboolean bind_lazy);
static void _g_module_close (gpointer handle,
1998-08-09 10:32:18 +02:00
gboolean is_unref);
static gpointer _g_module_self (void);
static gpointer _g_module_symbol (gpointer handle,
1998-08-09 10:32:18 +02:00
const gchar *symbol_name);
static inline void g_module_set_error (const gchar *error);
static inline GModule* g_module_find_by_handle (gpointer handle);
static inline GModule* g_module_find_by_name (const gchar *name);
/* --- variables --- */
version bump to 1.1.3, binary age 0, interface age 0. Sun Aug 16 20:28:27 1998 Tim Janik <timj@gtk.org> * version bump to 1.1.3, binary age 0, interface age 0. * glib.h: be nice to platforms that don't have gint64 and don't issue #warning on every compilation. since glib doesn't require gint64 itself, packages that need gint64 should test for this themselves. * glib.h: * gutils.c: added a new function g_vsnprintf(). Fri Aug 14 16:41:53 1998 Tim Janik <timj@gtk.org> * glib.h: added static inline functions for bit mask tests: g_bit_nth_lsf, g_bit_nth_msf and g_bit_storage. Fri Aug 13 14:23:37 1998 Tim Janik <timj@gtk.org> * glib.h: * gmessages.c: revised the message handling system, which is now based on a new mechanism g_log*. most of the assertment macros got adapted to feature the new g_log() call with an additional specification of the log level in a preprocessor macro G_LOG_DOMAIN. if G_LOG_DOMAIN is undefined upon the includion of glib.h, it'll be defined with a value of (NULL) and thus preserves the original bahaviour for warning and error messages. the message handler setting functions for g_warning, g_error and g_message are only provided for backwards compatibility and might get removed somewhen. * Makefile.am: feature the G_LOG_DOMAIN macro to set the log domain to "GLib" upon compilation. we currently have to add this definition to the DEFS variable. * testglib.c: we need an ugly #undef G_LOG_DOMAIN at the start of this file currently, since automake doesn't support per target _CFLAGS yet. * glib.h: changed some gints to gbooleans, made a few const corrections, removed some superfluous G_STMT_START{}G_STMT_END wrappers, added some in other required places. * gnode.c: (g_node_prepend): (g_node_insert_before): (g_node_insert): (g_node_append_data): (g_node_prepend_data): (g_node_insert_data_before): (g_node_insert_data): (g_node_append): return (node), so these macros/functions can be usefully chained with g_node_new(). [GModule] Fri Aug 14 02:24:39 1998 Tim Janik <timj@gtk.org> * Makefile.am: feature the G_LOG_DOMAIN macro to set the log domain to "GModule" upon compilation. we currently have to add this definition to the DEFS variable. * testgmodule.c: we need an ugly #undef G_LOG_DOMAIN at the start of this file currently, since automake doesn't support per target _CFLAGS yet.
1998-08-16 23:14:11 +02:00
const char *g_log_domain_gmodule = "GModule";
1998-08-09 10:32:18 +02:00
static GModule *modules = NULL;
static GModule *main_module = NULL;
static gchar *module_error = NULL;
/* --- inline functions --- */
static inline GModule*
g_module_find_by_handle (gpointer handle)
{
GModule *module;
if (main_module && main_module->handle == handle)
return main_module;
1998-08-09 10:32:18 +02:00
for (module = modules; module; module = module->next)
if (handle == module->handle)
return module;
return NULL;
}
static inline GModule*
g_module_find_by_name (const gchar *name)
{
GModule *module;
1998-08-09 10:32:18 +02:00
for (module = modules; module; module = module->next)
if (strcmp (name, module->file_name) == 0)
return module;
return NULL;
}
static inline void
g_module_set_error (const gchar *error)
{
if (module_error)
g_free (module_error);
if (error)
module_error = g_strdup (error);
else
module_error = NULL;
errno = 0;
}
/* --- include platform specifc code --- */
#define CHECK_ERROR(rv) { g_module_set_error (NULL); }
#if (G_MODULE_IMPL == G_MODULE_IMPL_DL)
#include "gmodule-dl.c"
#elif (G_MODULE_IMPL == G_MODULE_IMPL_DLD)
#include "gmodule-dld.c"
#else
#undef CHECK_ERROR
#define CHECK_ERROR(rv) { g_module_set_error ("unsupported"); return rv; }
static gpointer
_g_module_open (const gchar *file_name,
gboolean bind_lazy)
{
return NULL;
}
static void
_g_module_close (gpointer handle,
gboolean is_unref)
{
}
static gpointer
_g_module_self (void)
{
return NULL;
}
static gpointer
_g_module_symbol (gpointer handle,
const gchar *symbol_name)
{
return NULL;
}
1998-08-09 10:32:18 +02:00
#endif /* no implementation */
/* --- functions --- */
gboolean
g_module_supported (void)
{
CHECK_ERROR (FALSE);
1998-08-09 10:32:18 +02:00
return TRUE;
}
GModule*
g_module_open (const gchar *file_name,
GModuleFlags flags)
{
GModule *module;
gpointer handle;
1998-08-09 10:32:18 +02:00
CHECK_ERROR (NULL);
1998-08-09 10:32:18 +02:00
if (!file_name)
{
if (!main_module)
{
handle = _g_module_self ();
if (handle)
{
main_module = g_new (GModule, 1);
main_module->file_name = NULL;
main_module->handle = handle;
1998-08-09 10:32:18 +02:00
main_module->ref_count = 1;
main_module->is_resident = TRUE;
main_module->unload = NULL;
1998-08-09 10:32:18 +02:00
main_module->next = NULL;
}
}
1998-08-09 10:32:18 +02:00
return main_module;
}
1998-08-09 10:32:18 +02:00
/* we first search the module list by name */
module = g_module_find_by_name (file_name);
if (module)
{
module->ref_count++;
1998-08-09 10:32:18 +02:00
return module;
}
1998-08-09 10:32:18 +02:00
/* open the module */
handle = _g_module_open (file_name, (flags & G_MODULE_BIND_LAZY) != 0);
if (handle)
{
gchar *saved_error;
GModuleCheckInit check_init;
const gchar *check_failed = NULL;
1998-08-09 10:32:18 +02:00
/* search the module list by handle, since file names are not unique */
module = g_module_find_by_handle (handle);
if (module)
{
_g_module_close (module->handle, TRUE);
1998-08-09 10:32:18 +02:00
module->ref_count++;
g_module_set_error (NULL);
1998-08-09 10:32:18 +02:00
return module;
}
saved_error = module_error;
module_error = NULL;
g_module_set_error (NULL);
1998-08-09 10:32:18 +02:00
module = g_new (GModule, 1);
module->file_name = g_strdup (file_name);
module->handle = handle;
module->ref_count = 1;
module->is_resident = FALSE;
module->unload = NULL;
module->next = modules;
modules = module;
/* check initialization */
if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init))
check_failed = check_init (module);
/* we don't call unload() if the initialization check failed. */
if (!check_failed)
g_module_symbol (module, "g_module_unload", (gpointer) &module->unload);
if (check_failed)
{
gchar *error;
error = g_strconcat ("GModule initialization check failed: ", check_failed, NULL);
g_module_close (module);
module = NULL;
g_module_set_error (error);
g_free (error);
}
else
g_module_set_error (saved_error);
g_free (saved_error);
1998-08-09 10:32:18 +02:00
}
return module;
1998-08-09 10:32:18 +02:00
}
gboolean
g_module_close (GModule *module)
1998-08-09 10:32:18 +02:00
{
CHECK_ERROR (FALSE);
1998-08-09 10:32:18 +02:00
g_return_val_if_fail (module != NULL, FALSE);
g_return_val_if_fail (module->ref_count > 0, FALSE);
module->ref_count--;
if (!module->ref_count && !module->is_resident && module->unload)
{
GModuleUnload unload;
unload = module->unload;
module->unload = NULL;
unload (module);
}
if (!module->ref_count && !module->is_resident)
1998-08-09 10:32:18 +02:00
{
GModule *last;
GModule *node;
1998-08-09 10:32:18 +02:00
last = NULL;
node = modules;
while (node)
{
if (node == module)
{
if (last)
last->next = node->next;
else
modules = node->next;
break;
}
last = node;
node = last->next;
}
module->next = NULL;
_g_module_close (module->handle, FALSE);
g_free (module->file_name);
1998-08-09 10:32:18 +02:00
g_free (module);
}
1998-08-09 10:32:18 +02:00
return module_error == NULL;
}
void
g_module_make_resident (GModule *module)
{
g_return_if_fail (module != NULL);
module->is_resident = TRUE;
}
1998-08-09 10:32:18 +02:00
gchar*
g_module_error (void)
{
return module_error;
}
gboolean
g_module_symbol (GModule *module,
const gchar *symbol_name,
version bump to 1.1.3, binary age 0, interface age 0. Sun Aug 16 20:28:27 1998 Tim Janik <timj@gtk.org> * version bump to 1.1.3, binary age 0, interface age 0. * glib.h: be nice to platforms that don't have gint64 and don't issue #warning on every compilation. since glib doesn't require gint64 itself, packages that need gint64 should test for this themselves. * glib.h: * gutils.c: added a new function g_vsnprintf(). Fri Aug 14 16:41:53 1998 Tim Janik <timj@gtk.org> * glib.h: added static inline functions for bit mask tests: g_bit_nth_lsf, g_bit_nth_msf and g_bit_storage. Fri Aug 13 14:23:37 1998 Tim Janik <timj@gtk.org> * glib.h: * gmessages.c: revised the message handling system, which is now based on a new mechanism g_log*. most of the assertment macros got adapted to feature the new g_log() call with an additional specification of the log level in a preprocessor macro G_LOG_DOMAIN. if G_LOG_DOMAIN is undefined upon the includion of glib.h, it'll be defined with a value of (NULL) and thus preserves the original bahaviour for warning and error messages. the message handler setting functions for g_warning, g_error and g_message are only provided for backwards compatibility and might get removed somewhen. * Makefile.am: feature the G_LOG_DOMAIN macro to set the log domain to "GLib" upon compilation. we currently have to add this definition to the DEFS variable. * testglib.c: we need an ugly #undef G_LOG_DOMAIN at the start of this file currently, since automake doesn't support per target _CFLAGS yet. * glib.h: changed some gints to gbooleans, made a few const corrections, removed some superfluous G_STMT_START{}G_STMT_END wrappers, added some in other required places. * gnode.c: (g_node_prepend): (g_node_insert_before): (g_node_insert): (g_node_append_data): (g_node_prepend_data): (g_node_insert_data_before): (g_node_insert_data): (g_node_append): return (node), so these macros/functions can be usefully chained with g_node_new(). [GModule] Fri Aug 14 02:24:39 1998 Tim Janik <timj@gtk.org> * Makefile.am: feature the G_LOG_DOMAIN macro to set the log domain to "GModule" upon compilation. we currently have to add this definition to the DEFS variable. * testgmodule.c: we need an ugly #undef G_LOG_DOMAIN at the start of this file currently, since automake doesn't support per target _CFLAGS yet.
1998-08-16 23:14:11 +02:00
gpointer *symbol)
1998-08-09 10:32:18 +02:00
{
if (symbol)
*symbol = NULL;
CHECK_ERROR (FALSE);
1998-08-09 10:32:18 +02:00
g_return_val_if_fail (module != NULL, FALSE);
g_return_val_if_fail (symbol_name != NULL, FALSE);
g_return_val_if_fail (symbol != NULL, FALSE);
1998-08-09 10:32:18 +02:00
#ifdef G_MODULE_NEED_USCORE
symbol_name = g_strconcat ("_", symbol_name, NULL);
*symbol = _g_module_symbol (module->handle, symbol_name);
1998-08-09 10:32:18 +02:00
g_free (symbol_name);
#else /* !G_MODULE_NEED_USCORE */
*symbol = _g_module_symbol (module->handle, symbol_name);
1998-08-09 10:32:18 +02:00
#endif /* !G_MODULE_NEED_USCORE */
1998-08-09 10:32:18 +02:00
if (module_error)
{
gchar *error;
error = g_strconcat ("`", symbol_name, "': ", module_error, NULL);
g_module_set_error (error);
g_free (error);
1998-08-09 10:32:18 +02:00
*symbol = NULL;
return FALSE;
}
1998-08-09 10:32:18 +02:00
return TRUE;
}
gchar*
g_module_name (GModule *module)
{
g_return_val_if_fail (module != NULL, NULL);
1998-08-09 10:32:18 +02:00
if (module == main_module)
return "main";
1998-08-09 10:32:18 +02:00
return module->file_name;
}