[glib/resources] Use GBytes in g_resource_new_from_data



commit 8cbff4365f208808ecc9dc1d7608a8c5348ef654
Author: Alexander Larsson <alexl redhat com>
Date:   Thu Dec 22 13:03:35 2011 +0100

    Use GBytes in g_resource_new_from_data
    
    This means we're using the standard refcounting container
    instead of a separate destroy notify.

 gio/glib-compile-resources.c |    4 +++-
 gio/gresource.c              |   18 ++++++++----------
 gio/gresource.h              |    4 +---
 gio/tests/resources.c        |    4 +++-
 4 files changed, 15 insertions(+), 15 deletions(-)
---
diff --git a/gio/glib-compile-resources.c b/gio/glib-compile-resources.c
index a463dc0..5c269ce 100644
--- a/gio/glib-compile-resources.c
+++ b/gio/glib-compile-resources.c
@@ -618,7 +618,9 @@ main (int argc, char **argv)
 	       "\n"
 	       "static void register_resource (void)\n"
 	       "{\n"
-	       "  resource = g_resource_new_from_data (%s_resource_data, sizeof (%s_resource_data), NULL, NULL);\n"
+	       "  GBytes *bytes = g_bytes_new_static (%s_resource_data, sizeof (%s_resource_data));\n"
+	       "  resource = g_resource_new_from_data (bytes, NULL);\n"
+	       "  g_bytes_unref (bytes);\n"
 	       "  if (resource)\n"
 	       "    {\n"
 	       "      g_resources_register (resource);\n"
diff --git a/gio/gresource.c b/gio/gresource.c
index 9540acf..b5f5474 100644
--- a/gio/gresource.c
+++ b/gio/gresource.c
@@ -36,8 +36,7 @@ struct _GResource
   int ref_count;
 
   GvdbTable *table;
-  void *data;
-  GDestroyNotify free_data;
+  GBytes *data;
 };
 
 /**
@@ -76,21 +75,21 @@ g_resource_unref (GResource *resource)
   if (resource->ref_count-- == 0)
     {
       gvdb_table_unref (resource->table);
-      if (resource->free_data)
-	resource->free_data (resource->data);
+      if (resource->data)
+	g_bytes_unref (resource->data);
       g_free (resource);
     }
 }
 
 GResource *
-g_resource_new_from_data (const void *data, gsize length,
-			  GDestroyNotify free_data, GError **error)
+g_resource_new_from_data (GBytes *data,
+			  GError **error)
 {
   GResource *resource;
   GvdbTable *table;
 
-  table = gvdb_table_new_from_data ((void *)data,
-				    length,
+  table = gvdb_table_new_from_data (g_bytes_get_data (data, NULL),
+				    g_bytes_get_size (data),
 				    TRUE,
 				    NULL,
 				    NULL,
@@ -102,8 +101,7 @@ g_resource_new_from_data (const void *data, gsize length,
 
   resource = g_new0 (GResource, 1);
   resource->table = table;
-  resource->data = (void *)data;
-  resource->free_data = free_data;
+  resource->data = g_bytes_ref (data);
 
   return resource;
 }
diff --git a/gio/gresource.h b/gio/gresource.h
index 02567db..03b638e 100644
--- a/gio/gresource.h
+++ b/gio/gresource.h
@@ -34,9 +34,7 @@ G_BEGIN_DECLS
 #define G_RESOURCE_ERROR (g_resource_error_quark ())
 GQuark g_resource_error_quark (void);
 
-GResource *   g_resource_new_from_data       (const void   *data,
-					      gsize         length,
-					      GDestroyNotify free,
+GResource *   g_resource_new_from_data       (GBytes       *data,
 					      GError      **error);
 GResource *   g_resource_ref                 (GResource    *resource);
 void          g_resource_unref               (GResource    *resource);
diff --git a/gio/tests/resources.c b/gio/tests/resources.c
index 466a12c..5eae960 100644
--- a/gio/tests/resources.c
+++ b/gio/tests/resources.c
@@ -161,12 +161,14 @@ test_resource_data (void)
   gboolean loaded_file;
   char *content;
   gsize content_size;
+  GBytes *data;
 
   loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
 				     NULL);
   g_assert (loaded_file);
 
-  resource = g_resource_new_from_data (content, content_size, g_free, &error);
+  data = g_bytes_new_take (content, content_size);
+  resource = g_resource_new_from_data (data, &error);
   g_assert (resource != NULL);
   g_assert (error == NULL);
 



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