dbus/glib dbus-gtype-specialized.c, 1.3, 1.4 dbus-gtype-specialized.h, 1.3, 1.4 dbus-gvalue-utils.c, 1.7, 1.8

Robert McQueen robot101 at freedesktop.org
Fri Jan 27 07:40:38 PST 2006


Update of /cvs/dbus/dbus/glib
In directory gabe:/tmp/cvs-serv29370/glib

Modified Files:
	dbus-gtype-specialized.c dbus-gtype-specialized.h 
	dbus-gvalue-utils.c 
Log Message:
2006-01-27  Robert McQueen  <robot101 at debian.org>

	* glib/dbus-gtype-specialized.[ch], glib/dbus-gvalue-utils.c: Patch
	by me and Rob Taylor to add a simple_free function to D-Bus map
	and collection types, which allows those types which can be freed
	with a GDestroyNotify (such as GHashTables and GArrays, but not
	GPtrArrays) to be stored as the values in hashtables.

	* test/glib/test-dbus-glib.c, test/glib/test-service-glib.{c,xml}:
	Patch by Rob Taylor to add nested dicts to the glib tests to check
	the above code works, and appears not to leak when called repeatedly.

Index: dbus-gtype-specialized.c
===================================================================
RCS file: /cvs/dbus/dbus/glib/dbus-gtype-specialized.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- dbus-gtype-specialized.c	9 Jul 2005 01:46:51 -0000	1.3
+++ dbus-gtype-specialized.c	27 Jan 2006 15:40:36 -0000	1.4
@@ -91,7 +91,15 @@
       data = lookup_specialization_data (type);
       g_assert (data != NULL);
 
-      data->klass->vtable->free_func (type, value->data[0].v_pointer);
+      if (data->klass->vtable->free_func)
+        {
+          data->klass->vtable->free_func (type, value->data[0].v_pointer);
+        }
+      else
+        {
+          g_assert (data->klass->vtable->simple_free_func != NULL);
+          data->klass->vtable->simple_free_func (value->data[0].v_pointer);
+        }
     }
 }
 
@@ -227,6 +235,29 @@
   register_container (name, DBUS_G_SPECTYPE_MAP, (const DBusGTypeSpecializedVtable*) vtable);
 }
 
+const DBusGTypeSpecializedMapVtable* dbus_g_type_map_peek_vtable (GType map_type)
+{
+  DBusGTypeSpecializedData *data;
+  g_return_val_if_fail (dbus_g_type_is_map(map_type), NULL);
+
+  data = lookup_specialization_data (map_type);
+  g_assert (data != NULL);
+
+  return (DBusGTypeSpecializedMapVtable *)(data->klass->vtable);
+}
+
+const DBusGTypeSpecializedCollectionVtable* dbus_g_type_collection_peek_vtable (GType collection_type)
+{
+  DBusGTypeSpecializedData *data;
+  g_return_val_if_fail (dbus_g_type_is_collection(collection_type), NULL);
+
+  data = lookup_specialization_data (collection_type);
+  g_assert (data != NULL);
+
+  return (DBusGTypeSpecializedCollectionVtable *)(data->klass->vtable);
+}
+
+
 static GType
 register_specialized_instance (const DBusGTypeSpecializedContainer   *klass,
 			       char                                  *name,

Index: dbus-gtype-specialized.h
===================================================================
RCS file: /cvs/dbus/dbus/glib/dbus-gtype-specialized.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- dbus-gtype-specialized.h	9 Jul 2005 01:46:51 -0000	1.3
+++ dbus-gtype-specialized.h	27 Jan 2006 15:40:36 -0000	1.4
@@ -89,7 +89,7 @@
   DBusGTypeSpecializedConstructor    constructor;
   DBusGTypeSpecializedFreeFunc       free_func;
   DBusGTypeSpecializedCopyFunc       copy_func;
-  gpointer                           padding1;
+  GDestroyNotify                     simple_free_func; /* for type-independent freeing if possible */
   gpointer                           padding2;
   gpointer                           padding3;
 } DBusGTypeSpecializedVtable;
@@ -125,6 +125,8 @@
 void           dbus_g_type_register_map               (const char                                   *name,
 						       const DBusGTypeSpecializedMapVtable          *vtable,
 						       guint                                         flags);
+const DBusGTypeSpecializedMapVtable* dbus_g_type_map_peek_vtable (GType map_type);
+const DBusGTypeSpecializedCollectionVtable* dbus_g_type_collection_peek_vtable (GType collection_type);
 
 G_END_DECLS
 

Index: dbus-gvalue-utils.c
===================================================================
RCS file: /cvs/dbus/dbus/glib/dbus-gvalue-utils.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- dbus-gvalue-utils.c	18 Oct 2005 04:38:04 -0000	1.7
+++ dbus-gvalue-utils.c	27 Jan 2006 15:40:36 -0000	1.8
@@ -311,10 +311,29 @@
 	}
       else if (gtype == G_TYPE_VALUE_ARRAY)
         {
-          *func = g_value_array_free;
-	  return TRUE;
+          *func = (GDestroyNotify) g_value_array_free;
+          return TRUE;
+        }
+      else if (dbus_g_type_is_collection (gtype))
+        {
+          const DBusGTypeSpecializedCollectionVtable* vtable;
+          vtable = dbus_g_type_collection_peek_vtable (gtype);
+          if (vtable->base_vtable.simple_free_func)
+            {
+              *func = vtable->base_vtable.simple_free_func;
+              return TRUE;
+            }
+        }
+      else if (dbus_g_type_is_map (gtype))
+        {
+          const DBusGTypeSpecializedMapVtable* vtable;
+          vtable = dbus_g_type_map_peek_vtable (gtype);
+          if (vtable->base_vtable.simple_free_func)
+            {
+              *func = vtable->base_vtable.simple_free_func;
+              return TRUE;
+            }
         }
-
       return FALSE;
     }
 }
@@ -587,7 +606,7 @@
 }
 
 static void
-hashtable_free (GType type, gpointer val)
+hashtable_simple_free (gpointer val)
 {
   g_hash_table_destroy (val);
 }
@@ -629,7 +648,7 @@
 }
 
 static void
-array_free (GType type, gpointer val)
+array_simple_free (gpointer val)
 {
   GArray *array;
   array = val;
@@ -768,6 +787,7 @@
 static void
 ptrarray_free (GType type, gpointer val)
 {
+  /* XXX: this function appears to leak the contents of the array */
   GPtrArray *array;
   array = val;
   g_ptr_array_free (array, TRUE);
@@ -852,6 +872,7 @@
 static void
 slist_free (GType type, gpointer val)
 {
+  /* XXX: this function appears to leak the contents of the list */
   GSList *list;
   list = val;
   g_slist_free (list);
@@ -860,11 +881,18 @@
 void
 _dbus_g_type_specialized_builtins_init (void)
 {
+  /* types with a simple_free function can be freed at run-time without
+   * the destroy function needing to know the type, so they can be
+   * stored in hash tables */
+
   static const DBusGTypeSpecializedCollectionVtable array_vtable = {
     {
       array_constructor,
-      array_free,
+      NULL,
       array_copy,
+      array_simple_free,
+      NULL,
+      NULL,
     },
     array_fixed_accessor,
     NULL,
@@ -878,6 +906,9 @@
       ptrarray_constructor,
       ptrarray_free,
       ptrarray_copy,
+      NULL,
+      NULL,
+      NULL,
     },
     NULL,
     ptrarray_iterator,
@@ -891,6 +922,9 @@
       slist_constructor,
       slist_free,
       slist_copy,
+      NULL,
+      NULL,
+      NULL,
     },
     NULL,
     slist_iterator,
@@ -901,9 +935,9 @@
   static const DBusGTypeSpecializedMapVtable hashtable_vtable = {
     {
       hashtable_constructor,
-      hashtable_free,
-      hashtable_copy,
       NULL,
+      hashtable_copy,
+      hashtable_simple_free,
       NULL,
       NULL
     },



More information about the dbus-commit mailing list