glib/gmodule/gmodule.c
Tim Janik d5803865b4 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 21:14:11 +00:00

305 lines
7.1 KiB
C

/* GMODULE - GLIB wrapper code for dynamic module loading
* Copyright (C) 1998 Tim Janik
*
* 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
* 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).
* Also, the module for the program itself is kept seperatedly for
* faster access and because it has special semantics.
*/
/* --- structures --- */
struct _GModule
{
gchar *file_name;
gpointer handle;
guint ref_count;
GModuleDeInit de_init;
GModule *next;
};
/* --- prototypes --- */
static gpointer _g_module_open (const gchar *file_name,
gboolean bind_lazy);
static void _g_module_close (gpointer handle,
gboolean is_unref);
static gpointer _g_module_self (void);
static gpointer _g_module_symbol (gpointer handle,
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 --- */
const char *g_log_domain_gmodule = "GModule";
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;
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;
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; }
#endif /* no implementation */
/* --- functions --- */
gboolean
g_module_supported (void)
{
CHECK_ERROR (FALSE);
return TRUE;
}
GModule*
g_module_open (const gchar *file_name,
GModuleFlags flags)
{
GModule *module;
gpointer handle;
CHECK_ERROR (NULL);
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;
main_module->ref_count = 1;
main_module->de_init = NULL;
main_module->next = NULL;
}
}
return main_module;
}
/* we first search the module list by name */
module = g_module_find_by_name (file_name);
if (module)
{
module->ref_count++;
return module;
}
/* open the module */
handle = _g_module_open (file_name, (flags & G_MODULE_BIND_LAZY) != 0);
if (handle)
{
gchar *saved_error;
GModuleCheckInit check_init;
gboolean check_failed = FALSE;
/* 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);
module->ref_count++;
g_module_set_error (NULL);
return module;
}
saved_error = module_error;
module_error = NULL;
g_module_set_error (NULL);
module = g_new (GModule, 1);
module->file_name = g_strdup (file_name);
module->handle = handle;
module->ref_count = 1;
module->de_init = 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 de_init() if the initialization check failed. */
if (!check_failed)
g_module_symbol (module, "g_module_de_init", (gpointer) &module->de_init);
if (check_failed)
{
g_module_close (module);
module = NULL;
g_module_set_error ("GModule initialization check failed");
}
else
g_module_set_error (saved_error);
g_free (saved_error);
}
return module;
}
gboolean
g_module_close (GModule *module)
{
CHECK_ERROR (FALSE);
g_return_val_if_fail (module != NULL, FALSE);
g_return_val_if_fail (module->ref_count > 0, FALSE);
if (module != main_module)
module->ref_count--;
if (!module->ref_count && module->de_init)
module->de_init (module);
if (!module->ref_count)
{
GModule *last;
GModule *node;
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);
g_free (module);
}
return module_error == NULL;
}
gchar*
g_module_error (void)
{
return module_error;
}
gboolean
g_module_symbol (GModule *module,
const gchar *symbol_name,
gpointer *symbol)
{
if (symbol)
*symbol = NULL;
CHECK_ERROR (FALSE);
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);
#ifdef G_MODULE_NEED_USCORE
symbol_name = g_strconcat ("_", symbol_name, NULL);
*symbol = _g_module_symbol (module->handle, symbol_name);
g_free (symbol_name);
#else /* !G_MODULE_NEED_USCORE */
*symbol = _g_module_symbol (module->handle, symbol_name);
#endif /* !G_MODULE_NEED_USCORE */
if (module_error)
{
*symbol = NULL;
return FALSE;
}
return TRUE;
}
gchar*
g_module_name (GModule *module)
{
g_return_val_if_fail (module != NULL, NULL);
if (module == main_module)
return "main";
return module->file_name;
}