Use hash tables as sets in various places

Where we were already treating GHashTables as sets, modify them to use
the set-specific APIs g_hash_table_add() and g_hash_table_contains(), to
make that usage more obvious and less prone to being broken.

Heavily based on patches by Garrett Regier <garrettregier@gmail.com>.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=749371
This commit is contained in:
Philip Withnall
2017-10-25 10:53:14 +01:00
parent e130d2e511
commit 3eacec1587
9 changed files with 25 additions and 22 deletions

View File

@@ -309,7 +309,7 @@ print_names (GDBusConnection *c,
} }
g_variant_get (result, "(as)", &iter); g_variant_get (result, "(as)", &iter);
while (g_variant_iter_loop (iter, "s", &str)) while (g_variant_iter_loop (iter, "s", &str))
g_hash_table_insert (name_set, g_strdup (str), NULL); g_hash_table_add (name_set, g_strdup (str));
g_variant_iter_free (iter); g_variant_iter_free (iter);
g_variant_unref (result); g_variant_unref (result);
@@ -333,7 +333,7 @@ print_names (GDBusConnection *c,
} }
g_variant_get (result, "(as)", &iter); g_variant_get (result, "(as)", &iter);
while (g_variant_iter_loop (iter, "s", &str)) while (g_variant_iter_loop (iter, "s", &str))
g_hash_table_insert (name_set, g_strdup (str), NULL); g_hash_table_add (name_set, g_strdup (str));
g_variant_iter_free (iter); g_variant_iter_free (iter);
g_variant_unref (result); g_variant_unref (result);

View File

@@ -635,7 +635,7 @@ g_dbus_connection_dispose (GObject *object)
else else
{ {
if (alive_connections != NULL) if (alive_connections != NULL)
g_warn_if_fail (g_hash_table_lookup (alive_connections, connection) == NULL); g_warn_if_fail (!g_hash_table_contains (alive_connections, connection));
} }
CONNECTION_UNLOCK (connection); CONNECTION_UNLOCK (connection);
G_UNLOCK (message_bus_lock); G_UNLOCK (message_bus_lock);
@@ -2226,7 +2226,7 @@ on_worker_message_received (GDBusWorker *worker,
gboolean alive; gboolean alive;
G_LOCK (message_bus_lock); G_LOCK (message_bus_lock);
alive = (g_hash_table_lookup (alive_connections, user_data) != NULL); alive = g_hash_table_contains (alive_connections, user_data);
if (!alive) if (!alive)
{ {
G_UNLOCK (message_bus_lock); G_UNLOCK (message_bus_lock);
@@ -2324,7 +2324,7 @@ on_worker_message_about_to_be_sent (GDBusWorker *worker,
gboolean alive; gboolean alive;
G_LOCK (message_bus_lock); G_LOCK (message_bus_lock);
alive = (g_hash_table_lookup (alive_connections, user_data) != NULL); alive = g_hash_table_contains (alive_connections, user_data);
if (!alive) if (!alive)
{ {
G_UNLOCK (message_bus_lock); G_UNLOCK (message_bus_lock);
@@ -2397,7 +2397,7 @@ on_worker_closed (GDBusWorker *worker,
guint old_atomic_flags; guint old_atomic_flags;
G_LOCK (message_bus_lock); G_LOCK (message_bus_lock);
alive = (g_hash_table_lookup (alive_connections, user_data) != NULL); alive = g_hash_table_contains (alive_connections, user_data);
if (!alive) if (!alive)
{ {
G_UNLOCK (message_bus_lock); G_UNLOCK (message_bus_lock);
@@ -2572,7 +2572,7 @@ initable_init (GInitable *initable,
G_LOCK (message_bus_lock); G_LOCK (message_bus_lock);
if (alive_connections == NULL) if (alive_connections == NULL)
alive_connections = g_hash_table_new (g_direct_hash, g_direct_equal); alive_connections = g_hash_table_new (g_direct_hash, g_direct_equal);
g_hash_table_insert (alive_connections, connection, connection); g_hash_table_add (alive_connections, connection);
G_UNLOCK (message_bus_lock); G_UNLOCK (message_bus_lock);
connection->worker = _g_dbus_worker_new (connection->stream, connection->worker = _g_dbus_worker_new (connection->stream,
@@ -4708,8 +4708,8 @@ maybe_add_path (const gchar *path, gsize path_len, const gchar *object_path, GHa
else else
s = g_strdup (begin); s = g_strdup (begin);
if (g_hash_table_lookup (set, s) == NULL) if (!g_hash_table_contains (set, s))
g_hash_table_insert (set, s, GUINT_TO_POINTER (1)); g_hash_table_add (set, s);
else else
g_free (s); g_free (s);
} }

View File

@@ -202,14 +202,14 @@ g_io_module_scope_block (GIOModuleScope *scope,
g_return_if_fail (basename != NULL); g_return_if_fail (basename != NULL);
key = g_strdup (basename); key = g_strdup (basename);
g_hash_table_insert (scope->basenames, key, key); g_hash_table_add (scope->basenames, key);
} }
static gboolean static gboolean
_g_io_module_scope_contains (GIOModuleScope *scope, _g_io_module_scope_contains (GIOModuleScope *scope,
const gchar *basename) const gchar *basename)
{ {
return g_hash_table_lookup (scope->basenames, basename) ? TRUE : FALSE; return g_hash_table_contains (scope->basenames, basename);
} }
struct _GIOModule { struct _GIOModule {

View File

@@ -785,18 +785,21 @@ g_settings_schema_source_list_schemas (GSettingsSchemaSource *source,
for (i = 0; list[i]; i++) for (i = 0; list[i]; i++)
{ {
if (!g_hash_table_lookup (single, list[i]) && if (!g_hash_table_contains (single, list[i]) &&
!g_hash_table_lookup (reloc, list[i])) !g_hash_table_contains (reloc, list[i]))
{ {
gchar *schema;
GvdbTable *table; GvdbTable *table;
schema = g_strdup (list[i]);
table = gvdb_table_get_table (s->table, list[i]); table = gvdb_table_get_table (s->table, list[i]);
g_assert (table != NULL); g_assert (table != NULL);
if (gvdb_table_has_value (table, ".path")) if (gvdb_table_has_value (table, ".path"))
g_hash_table_insert (single, g_strdup (list[i]), NULL); g_hash_table_add (single, schema);
else else
g_hash_table_insert (reloc, g_strdup (list[i]), NULL); g_hash_table_add (reloc, schema);
gvdb_table_unref (table); gvdb_table_unref (table);
} }

View File

@@ -712,7 +712,7 @@ g_scanner_scope_add_symbol (GScanner *scanner,
c++; c++;
} }
} }
g_hash_table_insert (scanner->symbol_table, key, key); g_hash_table_add (scanner->symbol_table, key);
} }
else else
key->value = value; key->value = value;

View File

@@ -248,7 +248,7 @@ g_string_chunk_insert_const (GStringChunk *chunk,
if (!lookup) if (!lookup)
{ {
lookup = g_string_chunk_insert (chunk, string); lookup = g_string_chunk_insert (chunk, string);
g_hash_table_insert (chunk->const_table, lookup, lookup); g_hash_table_add (chunk->const_table, lookup);
} }
return lookup; return lookup;

View File

@@ -343,7 +343,7 @@ test_GDateTime_hash (void)
h = g_hash_table_new_full (g_date_time_hash, g_date_time_equal, h = g_hash_table_new_full (g_date_time_hash, g_date_time_equal,
(GDestroyNotify)g_date_time_unref, (GDestroyNotify)g_date_time_unref,
NULL); NULL);
g_hash_table_insert (h, g_date_time_new_now_local (), NULL); g_hash_table_add (h, g_date_time_new_now_local ());
g_hash_table_remove_all (h); g_hash_table_remove_all (h);
g_hash_table_destroy (h); g_hash_table_destroy (h);
} }

View File

@@ -1007,7 +1007,7 @@ g_object_init (GObject *object,
{ {
G_LOCK (debug_objects); G_LOCK (debug_objects);
debug_objects_count++; debug_objects_count++;
g_hash_table_insert (debug_objects_ht, object, object); g_hash_table_add (debug_objects_ht, object);
G_UNLOCK (debug_objects); G_UNLOCK (debug_objects);
}); });
} }
@@ -1062,7 +1062,7 @@ g_object_finalize (GObject *object)
GOBJECT_IF_DEBUG (OBJECTS, GOBJECT_IF_DEBUG (OBJECTS,
{ {
G_LOCK (debug_objects); G_LOCK (debug_objects);
g_assert (g_hash_table_lookup (debug_objects_ht, object) == object); g_assert (g_hash_table_contains (debug_objects_ht, object));
g_hash_table_remove (debug_objects_ht, object); g_hash_table_remove (debug_objects_ht, object);
debug_objects_count--; debug_objects_count--;
G_UNLOCK (debug_objects); G_UNLOCK (debug_objects);
@@ -3335,7 +3335,7 @@ g_object_unref (gpointer _object)
{ {
/* catch objects not chaining finalize handlers */ /* catch objects not chaining finalize handlers */
G_LOCK (debug_objects); G_LOCK (debug_objects);
g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL); g_assert (!g_hash_table_contains (debug_objects_ht, object));
G_UNLOCK (debug_objects); G_UNLOCK (debug_objects);
}); });
g_type_free_instance ((GTypeInstance*) object); g_type_free_instance ((GTypeInstance*) object);

View File

@@ -948,7 +948,7 @@ g_param_spec_pool_insert (GParamSpecPool *pool,
g_mutex_lock (&pool->mutex); g_mutex_lock (&pool->mutex);
pspec->owner_type = owner_type; pspec->owner_type = owner_type;
g_param_spec_ref (pspec); g_param_spec_ref (pspec);
g_hash_table_insert (pool->hash_table, pspec, pspec); g_hash_table_add (pool->hash_table, pspec);
g_mutex_unlock (&pool->mutex); g_mutex_unlock (&pool->mutex);
} }
else else