Replace g_hash_table_foreach() with GHashTableIter
authorDan Winship <danw@gnome.org>
Sun, 3 Jun 2012 13:36:14 +0000 (09:36 -0400)
committerDan Winship <danw@gnome.org>
Fri, 13 Jul 2012 18:18:35 +0000 (14:18 -0400)
or g_hash_table_new_full() when the foreach was only being used for
cleanup.

libsoup/soup-auth-manager-ntlm.c
libsoup/soup-auth-manager.c
libsoup/soup-form.c
libsoup/soup-session.c
libsoup/soup-xmlrpc.c

index d21a25c..1e6ebe5 100644 (file)
@@ -74,6 +74,8 @@ typedef struct {
 #endif
 } SoupNTLMConnection;
 
+static void free_ntlm_connection (SoupNTLMConnection *conn);
+
 typedef struct {
        gboolean use_ntlm;
 
@@ -105,7 +107,8 @@ soup_auth_manager_ntlm_init (SoupAuthManagerNTLM *ntlm)
        SoupAuthManagerNTLMPrivate *priv =
                SOUP_AUTH_MANAGER_NTLM_GET_PRIVATE (ntlm);
 
-       priv->connections_by_id = g_hash_table_new (NULL, NULL);
+       priv->connections_by_id = g_hash_table_new_full (NULL, NULL, NULL,
+                                                        (GDestroyNotify)free_ntlm_connection);
        priv->connections_by_msg = g_hash_table_new (NULL, NULL);
 #ifdef USE_NTLM_AUTH
        priv->ntlm_auth_accessible = (access (NTLM_AUTH, X_OK) == 0);
@@ -128,19 +131,11 @@ free_ntlm_connection (SoupNTLMConnection *conn)
 }
 
 static void
-free_ntlm_connection_foreach (gpointer key, gpointer value, gpointer user_data)
-{
-       free_ntlm_connection (value);
-}
-
-static void
 finalize (GObject *object)
 {
        SoupAuthManagerNTLMPrivate *priv =
                SOUP_AUTH_MANAGER_NTLM_GET_PRIVATE (object);
 
-       g_hash_table_foreach (priv->connections_by_id,
-                             free_ntlm_connection_foreach, NULL);
        g_hash_table_destroy (priv->connections_by_id);
        g_hash_table_destroy (priv->connections_by_msg);
 
@@ -189,11 +184,7 @@ static void
 delete_conn (SoupSocket *socket, gpointer user_data)
 {
        SoupAuthManagerNTLMPrivate *priv = user_data;
-       SoupNTLMConnection *conn;
 
-       conn = g_hash_table_lookup (priv->connections_by_id, socket);
-       if (conn)
-               free_ntlm_connection (conn);
        g_hash_table_remove (priv->connections_by_id, socket);
        g_signal_handlers_disconnect_by_func (socket, delete_conn, priv);
 }
index 1aacf48..2711d9a 100644 (file)
@@ -63,30 +63,18 @@ typedef struct {
        GHashTable  *auths;            /* scheme:realm -> SoupAuth */
 } SoupAuthHost;
 
+static void soup_auth_host_free (SoupAuthHost *host);
+
 static void
 soup_auth_manager_init (SoupAuthManager *manager)
 {
        SoupAuthManagerPrivate *priv = SOUP_AUTH_MANAGER_GET_PRIVATE (manager);
 
        priv->auth_types = g_ptr_array_new ();
-       priv->auth_hosts = g_hash_table_new (soup_uri_host_hash,
-                                            soup_uri_host_equal);
-}
-
-static gboolean
-foreach_free_host (gpointer key, gpointer value, gpointer data)
-{
-       SoupAuthHost *host = value;
-
-       if (host->auth_realms)
-               soup_path_map_free (host->auth_realms);
-       if (host->auths)
-               g_hash_table_destroy (host->auths);
-
-       soup_uri_free (host->uri);
-       g_slice_free (SoupAuthHost, host);
-
-       return TRUE;
+       priv->auth_hosts = g_hash_table_new_full (soup_uri_host_hash,
+                                                 soup_uri_host_equal,
+                                                 NULL,
+                                                 (GDestroyNotify)soup_auth_host_free);
 }
 
 static void
@@ -99,7 +87,6 @@ finalize (GObject *object)
                g_type_class_unref (priv->auth_types->pdata[i]);
        g_ptr_array_free (priv->auth_types, TRUE);
 
-       g_hash_table_foreach_remove (priv->auth_hosts, foreach_free_host, NULL);
        g_hash_table_destroy (priv->auth_hosts);
 
        if (priv->proxy_auth)
@@ -402,6 +389,18 @@ get_auth_host_for_message (SoupAuthManagerPrivate *priv, SoupMessage *msg)
        return host;
 }
 
+static void
+soup_auth_host_free (SoupAuthHost *host)
+{
+       if (host->auth_realms)
+               soup_path_map_free (host->auth_realms);
+       if (host->auths)
+               g_hash_table_destroy (host->auths);
+
+       soup_uri_free (host->uri);
+       g_slice_free (SoupAuthHost, host);
+}
+
 static SoupAuth *
 lookup_auth (SoupAuthManagerPrivate *priv, SoupMessage *msg)
 {
index a781d3c..3fe6578 100644 (file)
@@ -239,12 +239,6 @@ encode_pair (GString *str, const char *name, const char *value)
        append_form_encoded (str, value);
 }
 
-static void
-hash_encode_foreach (gpointer name, gpointer value, gpointer str)
-{
-       encode_pair (str, name, value);
-}
-
 /**
  * soup_form_encode:
  * @first_field: name of the first form field
@@ -295,8 +289,12 @@ char *
 soup_form_encode_hash (GHashTable *form_data_set)
 {
        GString *str = g_string_new (NULL);
+       GHashTableIter iter;
+       gpointer name, value;
 
-       g_hash_table_foreach (form_data_set, hash_encode_foreach, str);
+       g_hash_table_iter_init (&iter, form_data_set);
+       while (g_hash_table_iter_next (&iter, &name, &value))
+               encode_pair (str, name, value);
        return g_string_free (str, FALSE);
 }
 
index a91eead..97a379a 100644 (file)
@@ -2338,15 +2338,6 @@ soup_session_cancel_message (SoupSession *session, SoupMessage *msg,
 }
 
 static void
-gather_conns (gpointer key, gpointer host, gpointer data)
-{
-       SoupConnection *conn = key;
-       GSList **conns = data;
-
-       *conns = g_slist_prepend (*conns, g_object_ref (conn));
-}
-
-static void
 flush_queue (SoupSession *session)
 {
        SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
@@ -2371,6 +2362,8 @@ soup_session_abort (SoupSession *session)
 {
        SoupSessionPrivate *priv;
        GSList *conns, *c;
+       GHashTableIter iter;
+       gpointer conn, host;
 
        g_return_if_fail (SOUP_IS_SESSION (session));
        priv = SOUP_SESSION_GET_PRIVATE (session);
@@ -2380,9 +2373,11 @@ soup_session_abort (SoupSession *session)
        /* Close all connections */
        g_mutex_lock (&priv->host_lock);
        conns = NULL;
-       g_hash_table_foreach (priv->conns, gather_conns, &conns);
-
+       g_hash_table_iter_init (&iter, priv->conns);
+       while (g_hash_table_iter_next (&iter, &conn, &host))
+               conns = g_slist_prepend (conns, g_object_ref (conn));
        g_mutex_unlock (&priv->host_lock);
+
        for (c = conns; c; c = c->next) {
                soup_connection_disconnect (c->data);
                g_object_unref (c->data);
index 06c9bca..49eced6 100644 (file)
@@ -31,24 +31,6 @@ static xmlNode *find_real_node (xmlNode *node);
 
 static gboolean insert_value (xmlNode *parent, GValue *value);
 
-static void
-insert_member (gpointer name, gpointer value, gpointer data)
-{
-       xmlNode *member, **struct_node = data;
-
-       if (!*struct_node)
-               return;
-
-       member = xmlNewChild (*struct_node, NULL,
-                             (const xmlChar *)"member", NULL);
-       xmlNewTextChild (member, NULL,
-                        (const xmlChar *)"name", (const xmlChar *)name);
-       if (!insert_value (member, value)) {
-               xmlFreeNode (*struct_node);
-               *struct_node = NULL;
-       }
-}
-
 static gboolean
 insert_value (xmlNode *parent, GValue *value)
 {
@@ -95,11 +77,28 @@ insert_value (xmlNode *parent, GValue *value)
                g_free (encoded);
        } else if (type == G_TYPE_HASH_TABLE) {
                GHashTable *hash = g_value_get_boxed (value);
-               xmlNode *struct_node;
+               GHashTableIter iter;
+               gpointer mname, mvalue;
+               xmlNode *struct_node, *member;
 
                struct_node = xmlNewChild (xvalue, NULL,
                                           (const xmlChar *)"struct", NULL);
-               g_hash_table_foreach (hash, insert_member, &struct_node);
+
+               g_hash_table_iter_init (&iter, hash);
+
+               while (g_hash_table_iter_next (&iter, &mname, &mvalue)) {
+                       member = xmlNewChild (struct_node, NULL,
+                                             (const xmlChar *)"member", NULL);
+                       xmlNewTextChild (member, NULL,
+                                        (const xmlChar *)"name",
+                                        (const xmlChar *)mname);
+                       if (!insert_value (member, mvalue)) {
+                               xmlFreeNode (struct_node);
+                               struct_node = NULL;
+                               break;
+                       }
+               }
+
                if (!struct_node)
                        return FALSE;
 #ifdef G_GNUC_BEGIN_IGNORE_DEPRECATIONS