[libpeas] Lazy load the plugin loaders
- From: Steve Frécinaux <sfre src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libpeas] Lazy load the plugin loaders
- Date: Sun, 13 Feb 2011 21:41:31 +0000 (UTC)
commit 88e85c78d368c5175b39bad1d14e979a5b019e30
Author: Garrett Regier <alias301 gmail com>
Date: Sun Feb 13 00:33:41 2011 -0800
Lazy load the plugin loaders
libpeas/peas-engine.c | 110 +++++++++++++++++++-----------------------------
1 files changed, 44 insertions(+), 66 deletions(-)
---
diff --git a/libpeas/peas-engine.c b/libpeas/peas-engine.c
index 10776f7..475cdfa 100644
--- a/libpeas/peas-engine.c
+++ b/libpeas/peas-engine.c
@@ -279,22 +279,6 @@ loader_destroy (LoaderInfo *info)
g_free (info);
}
-static LoaderInfo *
-add_loader (PeasEngine *engine,
- const gchar *loader_id,
- PeasObjectModule *module,
- PeasPluginLoader *loader)
-{
- LoaderInfo *info;
-
- info = g_new (LoaderInfo, 1);
- info->loader = loader;
- info->module = module;
-
- g_hash_table_insert (loaders, g_strdup (loader_id), info);
- return info;
-}
-
static void
peas_engine_init (PeasEngine *engine)
{
@@ -620,62 +604,58 @@ try_to_open_loader_module (const gchar *loader_id,
return module;
}
-static LoaderInfo *
-load_plugin_loader (PeasEngine *engine,
- const gchar *loader_id)
+static PeasPluginLoader *
+get_plugin_loader (PeasEngine *engine,
+ PeasPluginInfo *info)
{
- guint i;
- gchar *lc_loader_id;
- PeasObjectModule *module;
- PeasPluginLoader *loader;
+ LoaderInfo *loader_info;
+ gchar *loader_id;
- /* We need to ensure we use the lowercase loader_id */
- lc_loader_id = g_strdup (loader_id);
- for (i = 0; lc_loader_id[i] != '\0'; ++i)
- lc_loader_id[i] = g_ascii_tolower (lc_loader_id[i]);
+ loader_info = (LoaderInfo *) g_hash_table_lookup (loaders, info->loader);
- module = try_to_open_loader_module (lc_loader_id, FALSE);
- if (module == NULL)
- module = try_to_open_loader_module (lc_loader_id, TRUE);
+ /* The loader has not been enabled. */
+ if (loader_info == NULL)
+ return NULL;
- g_free (lc_loader_id);
+ /* The loader has already been loaded. */
+ if (loader_info->loader != NULL)
+ return loader_info->loader;
- if (module == NULL)
- {
- g_warning ("Loader '%s' could not be loaded", loader_id);
- return add_loader (engine, loader_id, NULL, NULL);
- }
+ /* We need to ensure we use the lowercase loader_id */
+ loader_id = g_ascii_strdown (info->loader, -1);
- loader = (PeasPluginLoader *) peas_object_module_create_object (module, PEAS_TYPE_PLUGIN_LOADER, 0, NULL);
+ loader_info->module = try_to_open_loader_module (loader_id, FALSE);
+ if (loader_info->module == NULL)
+ loader_info->module = try_to_open_loader_module (loader_id, TRUE);
- if (loader == NULL || !PEAS_IS_PLUGIN_LOADER (loader))
+ g_free (loader_id);
+
+ if (loader_info->module == NULL)
{
- g_warning ("Loader '%s' is not a valid PeasPluginLoader instance", loader_id);
- if (loader != NULL && G_IS_OBJECT (loader))
- g_object_unref (loader);
- module = NULL;
- loader = NULL;
+ g_hash_table_insert (loaders, loader_id, NULL);
+ return NULL;
}
- g_type_module_unuse (G_TYPE_MODULE (module));
+ loader_info->loader = PEAS_PLUGIN_LOADER (
+ peas_object_module_create_object (loader_info->module,
+ PEAS_TYPE_PLUGIN_LOADER,
+ 0, NULL));
- return add_loader (engine, loader_id, module, loader);
-}
-
-static PeasPluginLoader *
-get_plugin_loader (PeasEngine *engine,
- PeasPluginInfo *info)
-{
- const gchar *loader_id;
- LoaderInfo *loader_info;
+ g_type_module_unuse (G_TYPE_MODULE (loader_info->module));
- loader_id = info->loader;
+ if (loader_info->loader == NULL ||
+ !PEAS_IS_PLUGIN_LOADER (loader_info->loader))
+ {
+ g_warning ("Loader '%s' is not a valid PeasPluginLoader instance",
+ info->loader);
- loader_info = (LoaderInfo *) g_hash_table_lookup (loaders,
- loader_id);
+ if (G_IS_OBJECT (loader_info->loader))
+ g_object_unref (loader_info->loader);
- if (loader_info == NULL)
- return NULL;
+ g_object_unref (loader_info->module);
+ g_hash_table_insert (loaders, info->loader, NULL);
+ return NULL;
+ }
return loader_info->loader;
}
@@ -694,23 +674,21 @@ get_plugin_loader (PeasEngine *engine,
* peas_engine_enable_loader (engine, "python");
* ]|
*
- * Note: plugin loaders are shared across #PeasEngines so enabeling
+ * Note: plugin loaders are shared across #PeasEngines so enabling
* a loader on one #PeasEngine will enable it on all #PeasEngines.
**/
void
peas_engine_enable_loader (PeasEngine *engine,
const gchar *loader_id)
{
- LoaderInfo *loader_info;
-
g_return_if_fail (PEAS_IS_ENGINE (engine));
- g_return_if_fail (loader_id != NULL);
+ g_return_if_fail (loader_id != NULL && *loader_id != '\0');
- loader_info = (LoaderInfo *) g_hash_table_lookup (loaders,
- loader_id);
+ if (g_hash_table_lookup_extended (loaders, loader_id, NULL, NULL))
+ return;
- if (loader_info == NULL)
- load_plugin_loader (engine, loader_id);
+ /* The loader is loaded in get_plugin_loader() */
+ g_hash_table_insert (loaders, g_strdup (loader_id), g_new0 (LoaderInfo, 1));
}
/**
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]