[glib] Use stack-allocated GVariantBuilders



commit c7f0f2c4377c5fd242c52d30a09df74e6d6e9828
Author: Christian Persch <chpe gnome org>
Date:   Fri May 14 14:00:24 2010 +0200

    Use stack-allocated GVariantBuilders
    
    This saves a few allocations.
    Also simplify the code a bit in gdbusconnection.
    
    Bug #618616.

 gio/gdbusconnection.c |   19 ++++++-------------
 gio/gdbusmessage.c    |   30 +++++++++++++++---------------
 2 files changed, 21 insertions(+), 28 deletions(-)
---
diff --git a/gio/gdbusconnection.c b/gio/gdbusconnection.c
index 017b2d4..7bc7224 100644
--- a/gio/gdbusconnection.c
+++ b/gio/gdbusconnection.c
@@ -3380,9 +3380,7 @@ static gboolean
 invoke_get_all_properties_in_idle_cb (gpointer _data)
 {
   PropertyGetAllData *data = _data;
-  GVariantBuilder *builder;
-  GVariant *packed;
-  GVariant *result;
+  GVariantBuilder builder;
   GError *error;
   GDBusMessage *reply;
   guint n;
@@ -3395,7 +3393,8 @@ invoke_get_all_properties_in_idle_cb (gpointer _data)
    *       We could fail the whole call if just a single get_property() call
    *       returns an error. We need clarification in the D-Bus spec about this.
    */
-  builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
+  g_variant_builder_init (&builder, G_VARIANT_TYPE ("(a{sv})"));
+  g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
   for (n = 0; data->interface_info->properties != NULL && data->interface_info->properties[n] != NULL; n++)
     {
       const GDBusPropertyInfo *property_info = data->interface_info->properties[n];
@@ -3415,21 +3414,15 @@ invoke_get_all_properties_in_idle_cb (gpointer _data)
       if (value == NULL)
         continue;
 
-      g_variant_ref_sink (value);
-      g_variant_builder_add (builder,
+      g_variant_builder_add (&builder,
                              "{sv}",
                              property_info->name,
                              value);
-      g_variant_unref (value);
     }
-  result = g_variant_builder_end (builder);
-
-  builder = g_variant_builder_new (G_VARIANT_TYPE_TUPLE);
-  g_variant_builder_add_value (builder, result); /* steals result since result is floating */
-  packed = g_variant_builder_end (builder);
+  g_variant_builder_close (&builder);
 
   reply = g_dbus_message_new_method_reply (data->message);
-  g_dbus_message_set_body (reply, packed);
+  g_dbus_message_set_body (reply, g_variant_builder_end (&builder));
   g_dbus_connection_send_message (data->connection, reply, NULL, NULL);
   g_object_unref (reply);
 
diff --git a/gio/gdbusmessage.c b/gio/gdbusmessage.c
index 24d5ef6..eabe171 100644
--- a/gio/gdbusmessage.c
+++ b/gio/gdbusmessage.c
@@ -965,7 +965,7 @@ parse_value_from_blob (GMemoryInputStream    *mis,
       goffset offset;
       goffset target;
       const GVariantType *element_type;
-      GVariantBuilder *builder;
+      GVariantBuilder builder;
 
       if (!ensure_input_padding (mis, 4, &local_error))
         goto fail;
@@ -981,7 +981,7 @@ parse_value_from_blob (GMemoryInputStream    *mis,
           goto fail;
         }
 
-      builder = g_variant_builder_new (type);
+      g_variant_builder_init (&builder, type);
       element_type = g_variant_type_element (type);
 
       if (array_len == 0)
@@ -1009,21 +1009,21 @@ parse_value_from_blob (GMemoryInputStream    *mis,
                                             &local_error);
               if (item == NULL)
                 {
-                  g_variant_builder_unref (builder);
+                  g_variant_builder_clear (&builder);
                   goto fail;
                 }
-              g_variant_builder_add_value (builder, item);
+              g_variant_builder_add_value (&builder, item);
               offset = g_seekable_tell (G_SEEKABLE (mis));
             }
         }
 
       if (!just_align)
         {
-          ret = g_variant_builder_end (builder);
+          ret = g_variant_builder_end (&builder);
         }
       else
         {
-          g_variant_builder_unref (builder);
+          g_variant_builder_clear (&builder);
         }
     }
   else if (g_variant_type_is_dict_entry (type))
@@ -1069,9 +1069,9 @@ parse_value_from_blob (GMemoryInputStream    *mis,
       if (!just_align)
         {
           const GVariantType *element_type;
-          GVariantBuilder *builder;
+          GVariantBuilder builder;
 
-          builder = g_variant_builder_new (type);
+          g_variant_builder_init (&builder, type);
           element_type = g_variant_type_first (type);
           while (element_type != NULL)
             {
@@ -1083,14 +1083,14 @@ parse_value_from_blob (GMemoryInputStream    *mis,
                                             &local_error);
               if (item == NULL)
                 {
-                  g_variant_builder_unref (builder);
+                  g_variant_builder_clear (&builder);
                   goto fail;
                 }
-              g_variant_builder_add_value (builder, item);
+              g_variant_builder_add_value (&builder, item);
 
               element_type = g_variant_type_next (element_type);
             }
-          ret = g_variant_builder_end (builder);
+          ret = g_variant_builder_end (&builder);
         }
     }
   else if (g_variant_type_is_variant (type))
@@ -1774,7 +1774,7 @@ g_dbus_message_to_blob (GDBusMessage          *message,
   goffset body_start_offset;
   gsize body_size;
   GVariant *header_fields;
-  GVariantBuilder *builder;
+  GVariantBuilder builder;
   GHashTableIter hash_iter;
   gpointer key;
   GVariant *header_value;
@@ -1826,16 +1826,16 @@ g_dbus_message_to_blob (GDBusMessage          *message,
       goto out;
     }
 
-  builder = g_variant_builder_new (G_VARIANT_TYPE ("a{yv}"));//G_VARIANT_TYPE_ARRAY);
+  g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{yv}"));
   g_hash_table_iter_init (&hash_iter, message->priv->headers);
   while (g_hash_table_iter_next (&hash_iter, &key, (gpointer) &header_value))
     {
-      g_variant_builder_add (builder,
+      g_variant_builder_add (&builder,
                              "{yv}",
                              (guchar) GPOINTER_TO_UINT (key),
                              header_value);
     }
-  header_fields = g_variant_new ("a{yv}", builder);
+  header_fields = g_variant_builder_end (&builder);
 
   if (!append_value_to_blob (header_fields,
                              g_variant_get_type (header_fields),



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