fix cmopiler warnings. check (de)initialization code.

This commit is contained in:
Tim Janik 1998-08-09 11:39:50 +00:00
parent 6050cbef0a
commit 1718cd8c8d
5 changed files with 71 additions and 20 deletions

View File

@ -36,6 +36,7 @@ struct _GModule
gchar *file_name; gchar *file_name;
gpointer handle; gpointer handle;
guint ref_count; guint ref_count;
GModuleDeInit de_init;
GModule *next; GModule *next;
}; };
@ -134,9 +135,10 @@ g_module_open (const gchar *file_name,
{ {
main_module = g_new (GModule, 1); main_module = g_new (GModule, 1);
main_module->file_name = NULL; main_module->file_name = NULL;
main_module->ref_count = 1;
main_module->next = NULL;
main_module->handle = handle; main_module->handle = handle;
main_module->ref_count = 1;
main_module->de_init = NULL;
main_module->next = NULL;
} }
} }
@ -156,7 +158,9 @@ g_module_open (const gchar *file_name,
handle = _g_module_open (file_name, (flags & G_MODULE_BIND_LAZY) != 0); handle = _g_module_open (file_name, (flags & G_MODULE_BIND_LAZY) != 0);
if (handle) if (handle)
{ {
GModule *module; gchar *saved_error;
GModuleCheckInit check_init;
gboolean check_failed = FALSE;
/* search the module list by handle, since file names are not unique */ /* search the module list by handle, since file names are not unique */
module = g_module_find_by_handle (handle); module = g_module_find_by_handle (handle);
@ -169,17 +173,41 @@ g_module_open (const gchar *file_name,
return module; return module;
} }
saved_error = module_error;
module_error = NULL;
g_module_set_error (NULL);
module = g_new (GModule, 1); module = g_new (GModule, 1);
module->file_name = g_strdup (file_name); module->file_name = g_strdup (file_name);
module->handle = handle; module->handle = handle;
module->ref_count = 1; module->ref_count = 0;
module->de_init = NULL;
module->next = NULL;
/* check initialization */
if (g_module_symbol (module, "g_module_check_init", &check_init))
check_failed = check_init (module);
/* should call de_init() on failed initializations also? */
if (!check_failed)
g_module_symbol (module, "g_module_de_init", &module->de_init);
module->ref_count += 1;
module->next = modules; module->next = modules;
modules = module; modules = module;
return module; 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 NULL; return module;
} }
gboolean gboolean
@ -198,9 +226,6 @@ g_module_close (GModule *module)
GModule *last; GModule *last;
GModule *node; GModule *node;
_g_module_close (&module->handle, FALSE);
g_free (module->file_name);
last = NULL; last = NULL;
node = modules; node = modules;
while (node) while (node)
@ -216,6 +241,14 @@ g_module_close (GModule *module)
last = node; last = node;
node = last->next; node = last->next;
} }
module->next = NULL;
if (module->de_init)
module->de_init (module);
_g_module_close (&module->handle, FALSE);
g_free (module->file_name);
g_free (module); g_free (module);
} }

View File

@ -28,7 +28,9 @@ extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
/* exporting and importing functions */ /* exporting and importing functions,
* we need autoconf support here for supporting windows.
*/
#define G_MODULE_EXPORT #define G_MODULE_EXPORT
#define G_MODULE_IMPORT extern #define G_MODULE_IMPORT extern
@ -40,6 +42,8 @@ typedef enum
} GModuleFlags; } GModuleFlags;
typedef struct _GModule GModule; typedef struct _GModule GModule;
typedef gboolean (*GModuleCheckInit) (GModule *module);
typedef void (*GModuleDeInit) (GModule *module);
/* return TRUE if dynamic module loading is supported */ /* return TRUE if dynamic module loading is supported */
gboolean g_module_supported (void); gboolean g_module_supported (void);

View File

@ -52,7 +52,7 @@ gplugin_a_module_func (GModule *module)
g_print ("GPluginA: retrive symbol `%s' from \"%s\"\n", g_print ("GPluginA: retrive symbol `%s' from \"%s\"\n",
string, string,
g_basename (g_module_name (module))); g_basename (g_module_name (module)));
if (!g_module_symbol (module, string, &f)) if (!g_module_symbol (module, string, (gpointer) &f))
{ {
g_print ("error: %s\n", g_module_error ()); g_print ("error: %s\n", g_module_error ());
exit (1); exit (1);

View File

@ -18,6 +18,20 @@
*/ */
#include <gmodule.h> #include <gmodule.h>
G_MODULE_EXPORT gboolean
g_module_check_init (GModule *module)
{
g_print ("GPluginB: check-init\n");
return 0;
}
G_MODULE_EXPORT void
g_module_de_init (GModule *module)
{
g_print ("GPluginB: de-init\n");
}
G_MODULE_EXPORT void G_MODULE_EXPORT void
gplugin_b_func (void) gplugin_b_func (void)
{ {

View File

@ -76,14 +76,14 @@ main (int arg,
*/ */
string = "gplugin_a_func"; string = "gplugin_a_func";
g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a))); g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a)));
if (!g_module_symbol (module_a, string, &f_a)) if (!g_module_symbol (module_a, string, (gpointer) &f_a))
{ {
g_print ("error: %s\n", g_module_error ()); g_print ("error: %s\n", g_module_error ());
return 1; return 1;
} }
string = "gplugin_b_func"; string = "gplugin_b_func";
g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_b))); g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_b)));
if (!g_module_symbol (module_b, string, &f_b)) if (!g_module_symbol (module_b, string, (gpointer) &f_b))
{ {
g_print ("error: %s\n", g_module_error ()); g_print ("error: %s\n", g_module_error ());
return 1; return 1;
@ -97,19 +97,19 @@ main (int arg,
*/ */
string = "g_clash_func"; string = "g_clash_func";
g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_self))); g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_self)));
if (!g_module_symbol (module_self, string, &f_self)) if (!g_module_symbol (module_self, string, (gpointer) &f_self))
{ {
g_print ("error: %s\n", g_module_error ()); g_print ("error: %s\n", g_module_error ());
return 1; return 1;
} }
g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a))); g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a)));
if (!g_module_symbol (module_a, string, &f_a)) if (!g_module_symbol (module_a, string, (gpointer) &f_a))
{ {
g_print ("error: %s\n", g_module_error ()); g_print ("error: %s\n", g_module_error ());
return 1; return 1;
} }
g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_b))); g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_b)));
if (!g_module_symbol (module_b, string, &f_b)) if (!g_module_symbol (module_b, string, (gpointer) &f_b))
{ {
g_print ("error: %s\n", g_module_error ()); g_print ("error: %s\n", g_module_error ());
return 1; return 1;
@ -125,13 +125,13 @@ main (int arg,
*/ */
string = "gplugin_clash_func"; string = "gplugin_clash_func";
g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a))); g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a)));
if (!g_module_symbol (module_a, string, &f_a)) if (!g_module_symbol (module_a, string, (gpointer) &f_a))
{ {
g_print ("error: %s\n", g_module_error ()); g_print ("error: %s\n", g_module_error ());
return 1; return 1;
} }
g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_b))); g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_b)));
if (!g_module_symbol (module_b, string, &f_b)) if (!g_module_symbol (module_b, string, (gpointer) &f_b))
{ {
g_print ("error: %s\n", g_module_error ()); g_print ("error: %s\n", g_module_error ());
return 1; return 1;
@ -147,7 +147,7 @@ main (int arg,
*/ */
string = "gplugin_a_module_func"; string = "gplugin_a_module_func";
g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a))); g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a)));
if (!g_module_symbol (module_a, string, &gmod_f)) if (!g_module_symbol (module_a, string, (gpointer) &gmod_f))
{ {
g_print ("error: %s\n", g_module_error ()); g_print ("error: %s\n", g_module_error ());
return 1; return 1;