diff options
author | Thomas Haller <thaller@redhat.com> | 2018-10-22 14:43:48 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2018-10-23 11:07:23 +0200 |
commit | 4047633225b098a44670ff23ac0ff491b91fb2ae (patch) | |
tree | a0895b3538cc3d83740762d8834e77ef00f4c24f /libnm-core | |
parent | 4f27164148d0c796851b2f82553915f9296ac7cd (diff) |
libnm: hash settings in NMConnection by gtype
NMConnection keeps a list (hash table) of all settings.
There are two lookup methods to find a setting in a connection:
- nm_connection_get_setting() by GType
- nm_connection_get_setting_by_name() by name
Note, that nm_connection_get_setting_by_name() first converts the
name to a GType, and then looks up the setting by GType.
But then, nm_connection_get_setting() would again convert the GType to
the type name, and hash the name. That is pointless, just index by GType
directly.
Maybe, using a hash table is anyway overkill because commonly there are
only a handful of settings in a connection. Regardless of that, change
the hashing.
Diffstat (limited to 'libnm-core')
-rw-r--r-- | libnm-core/nm-connection.c | 44 |
1 files changed, 30 insertions, 14 deletions
diff --git a/libnm-core/nm-connection.c b/libnm-core/nm-connection.c index a19a09dcd..eb42bc07a 100644 --- a/libnm-core/nm-connection.c +++ b/libnm-core/nm-connection.c @@ -75,6 +75,22 @@ static guint signals[LAST_SIGNAL] = { 0 }; /*****************************************************************************/ +static gpointer +_gtype_to_hash_key (GType gtype) +{ +#if NM_MORE_ASSERTS + _nm_unused const gsize *const test_gtype_typedef = >ype; + + nm_assert ((GType) (GPOINTER_TO_SIZE (GSIZE_TO_POINTER (gtype))) == gtype); + G_STATIC_ASSERT_EXPR (sizeof (gpointer) >= sizeof (gsize)); + G_STATIC_ASSERT_EXPR (sizeof (gsize) == sizeof (GType)); +#endif + + return GSIZE_TO_POINTER (gtype); +} + +/*****************************************************************************/ + static void setting_changed_cb (NMSetting *setting, GParamSpec *pspec, @@ -94,18 +110,18 @@ static void _nm_connection_add_setting (NMConnection *connection, NMSetting *setting) { NMConnectionPrivate *priv; - const char *name; + GType setting_type; NMSetting *s_old; nm_assert (NM_IS_CONNECTION (connection)); nm_assert (NM_IS_SETTING (setting)); priv = NM_CONNECTION_GET_PRIVATE (connection); - name = G_OBJECT_TYPE_NAME (setting); + setting_type = G_OBJECT_TYPE (setting); - if ((s_old = g_hash_table_lookup (priv->settings, (gpointer) name))) + if ((s_old = g_hash_table_lookup (priv->settings, _gtype_to_hash_key (setting_type)))) g_signal_handlers_disconnect_by_func (s_old, setting_changed_cb, connection); - g_hash_table_insert (priv->settings, (gpointer) name, setting); + g_hash_table_insert (priv->settings, _gtype_to_hash_key (setting_type), setting); /* Listen for property changes so we can emit the 'changed' signal */ g_signal_connect (setting, "notify", (GCallback) setting_changed_cb, connection); } @@ -135,17 +151,15 @@ _nm_connection_remove_setting (NMConnection *connection, GType setting_type) { NMConnectionPrivate *priv; NMSetting *setting; - const char *setting_name; g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE); g_return_val_if_fail (g_type_is_a (setting_type, NM_TYPE_SETTING), FALSE); priv = NM_CONNECTION_GET_PRIVATE (connection); - setting_name = g_type_name (setting_type); - setting = g_hash_table_lookup (priv->settings, setting_name); + setting = g_hash_table_lookup (priv->settings, _gtype_to_hash_key (setting_type)); if (setting) { g_signal_handlers_disconnect_by_func (setting, setting_changed_cb, connection); - g_hash_table_remove (priv->settings, setting_name); + g_hash_table_remove (priv->settings, _gtype_to_hash_key (setting_type)); g_signal_emit (connection, signals[CHANGED], 0); return TRUE; } @@ -175,7 +189,7 @@ _connection_get_setting (NMConnection *connection, GType setting_type) nm_assert (g_type_is_a (setting_type, NM_TYPE_SETTING)); setting = g_hash_table_lookup (NM_CONNECTION_GET_PRIVATE (connection)->settings, - g_type_name (setting_type)); + _gtype_to_hash_key (setting_type)); nm_assert (!setting || G_TYPE_CHECK_INSTANCE_TYPE (setting, setting_type)); return setting; } @@ -1878,7 +1892,7 @@ nm_connection_to_dbus (NMConnection *connection, NMConnectionPrivate *priv; GVariantBuilder builder; GHashTableIter iter; - gpointer key, data; + gpointer data; GVariant *setting_dict, *ret; g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL); @@ -1888,7 +1902,7 @@ nm_connection_to_dbus (NMConnection *connection, /* Add each setting's hash to the main hash */ g_hash_table_iter_init (&iter, priv->settings); - while (g_hash_table_iter_next (&iter, &key, &data)) { + while (g_hash_table_iter_next (&iter, NULL, &data)) { NMSetting *setting = NM_SETTING (data); setting_dict = _nm_setting_to_dbus (setting, connection, flags); @@ -2033,14 +2047,13 @@ nm_connection_dump (NMConnection *connection) { GHashTableIter iter; NMSetting *setting; - const char *setting_name; char *str; if (!connection) return; g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings); - while (g_hash_table_iter_next (&iter, (gpointer) &setting_name, (gpointer) &setting)) { + while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting)) { str = nm_setting_to_string (setting); g_print ("%s\n", str); g_free (str); @@ -2914,7 +2927,10 @@ nm_connection_get_private (NMConnection *connection) priv, (GDestroyNotify) nm_connection_private_free); priv->self = connection; - priv->settings = g_hash_table_new_full (nm_str_hash, g_str_equal, NULL, g_object_unref); + priv->settings = g_hash_table_new_full (nm_direct_hash, + NULL, + NULL, + g_object_unref); } return priv; |