[glib/wip/resources] Allow resources to work with unloadable GTypeModules



commit 9630d67cac4b6cf2ec6da5df1771875316ab3b1c
Author: Alexander Larsson <alexl redhat com>
Date:   Tue Jan 10 16:40:45 2012 +0100

    Allow resources to work with unloadable GTypeModules

 gio/gresource.c            |   36 +++++++++++++++++++++++++++++++++---
 gio/gresource.h            |    2 ++
 gio/tests/resourceplugin.c |    4 +++-
 gio/tests/resources.c      |   18 +++++++++++++++++-
 4 files changed, 55 insertions(+), 5 deletions(-)
---
diff --git a/gio/gresource.c b/gio/gresource.c
index d9a8f60..1dfe741 100644
--- a/gio/gresource.c
+++ b/gio/gresource.c
@@ -36,6 +36,7 @@ struct _GResource
   int ref_count;
 
   GvdbTable *table;
+  GTypeModule *module;
 };
 
 /**
@@ -78,6 +79,35 @@ g_resource_unref (GResource *resource)
     }
 }
 
+void
+g_resource_set_module (GResource *resource,
+		       GTypeModule *type_module)
+{
+  if (resource->module)
+    g_object_unref (resource->module);
+
+  resource->module = type_module;
+
+  if (resource->module)
+    g_object_ref (resource->module);
+}
+
+static GResource *
+g_resource_use (GResource *resource)
+{
+  if (resource->module)
+    g_type_module_use (resource->module);
+  return g_resource_ref (resource);
+}
+
+static void
+g_resource_unuse (GResource *resource)
+{
+  if (resource->module)
+    g_type_module_unuse (resource->module);
+  g_resource_unref (resource);
+}
+
 GResource *
 g_resource_new_from_data (GBytes *data,
 			  GError **error)
@@ -223,8 +253,8 @@ g_resource_open_stream (GResource *resource,
 
   stream = g_memory_input_stream_new_from_data (data, data_size, NULL);
   g_object_set_data_full (G_OBJECT (stream), "g-resource",
-			  g_resource_ref (resource),
-			  (GDestroyNotify)g_resource_unref);
+			  g_resource_use (resource),
+			  (GDestroyNotify)g_resource_unuse);
 
   if (flags & G_RESOURCE_FLAGS_COMPRESSED)
     {
@@ -303,7 +333,7 @@ g_resource_lookup_data (GResource *resource,
       return g_bytes_new_take (uncompressed, size);
     }
   else
-    return g_bytes_new_with_free_func (data, data_size, (GDestroyNotify)g_resource_unref, g_resource_ref (resource));
+    return g_bytes_new_with_free_func (data, data_size, (GDestroyNotify)g_resource_unuse, g_resource_use (resource));
 }
 
 gboolean
diff --git a/gio/gresource.h b/gio/gresource.h
index 31b5d32..a9a7655 100644
--- a/gio/gresource.h
+++ b/gio/gresource.h
@@ -58,6 +58,8 @@ gboolean      g_resource_get_info            (GResource             *resource,
 					      gsize                 *size,
 					      guint32               *flags,
 					      GError               **error);
+void          g_resource_set_module          (GResource             *resource,
+					      GTypeModule           *type_module);
 
 void          g_resources_register           (GResource             *resource);
 void          g_resources_unregister         (GResource             *resource);
diff --git a/gio/tests/resourceplugin.c b/gio/tests/resourceplugin.c
index b66d541..c1626ec 100644
--- a/gio/tests/resourceplugin.c
+++ b/gio/tests/resourceplugin.c
@@ -1,8 +1,11 @@
 #include <gio/gio.h>
 
+extern GResource *_g_plugin_resource;
+
 void
 g_io_module_load (GIOModule *module)
 {
+  g_resource_set_module (_g_plugin_resource, G_TYPE_MODULE (module));
 }
 
 void
@@ -15,4 +18,3 @@ g_io_module_query (void)
 {
   return NULL;
 }
-
diff --git a/gio/tests/resources.c b/gio/tests/resources.c
index 586bafa..486504e 100644
--- a/gio/tests/resources.c
+++ b/gio/tests/resources.c
@@ -410,6 +410,8 @@ test_resource_module (void)
 
       error = NULL;
 
+      /* Module is not loaded yet */
+
       found = g_resources_get_info ("/resourceplugin/test1.txt",
 				    G_RESOURCE_LOOKUP_FLAGS_NONE,
 				    &size, &flags, &error);
@@ -417,6 +419,8 @@ test_resource_module (void)
       g_assert (error != NULL);
       g_clear_error (&error);
 
+      /* Load the module */
+
       g_type_module_use (G_TYPE_MODULE (module));
 
       found = g_resources_get_info ("/resourceplugin/test1.txt",
@@ -435,13 +439,25 @@ test_resource_module (void)
       size = g_bytes_get_size (data);
       g_assert (size == 6);
       g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
-      g_bytes_unref (data);
 
+      /* Don't unref data, should keep the module in use */
+
+      /* Unuse the module */
       g_type_module_unuse (G_TYPE_MODULE (module));
 
       found = g_resources_get_info ("/resourceplugin/test1.txt",
 				    G_RESOURCE_LOOKUP_FLAGS_NONE,
 				    &size, &flags, &error);
+      g_assert (found);
+      g_assert (error == NULL);
+
+
+      /* Unref the data, should unload the module */
+      g_bytes_unref (data);
+
+      found = g_resources_get_info ("/resourceplugin/test1.txt",
+				    G_RESOURCE_LOOKUP_FLAGS_NONE,
+				    &size, &flags, &error);
       g_assert (!found);
       g_assert (error != NULL);
       g_clear_error (&error);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]