diff options
author | Thomas Haller <thaller@redhat.com> | 2018-03-25 14:16:17 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2018-03-27 09:58:00 +0200 |
commit | e49a32936c6b7544750e60c6ec0816049f8fdfb9 (patch) | |
tree | 886a18399ff81a192301e0252d978a80f4bba51e | |
parent | cd48bc74b69794a1f60c2f08cc503f8ca43322ae (diff) | |
download | NetworkManager-e49a32936c6b7544750e60c6ec0816049f8fdfb9.tar.gz |
all: use nm_utils_hash_keys_to_array()
-rw-r--r-- | libnm-core/nm-keyfile-writer.c | 8 | ||||
-rw-r--r-- | libnm-core/nm-setting-ip-config.c | 2 | ||||
-rw-r--r-- | libnm-core/nm-setting-user.c | 19 | ||||
-rw-r--r-- | src/devices/nm-lldp-listener.c | 45 | ||||
-rw-r--r-- | src/dns/nm-dns-systemd-resolved.c | 12 | ||||
-rw-r--r-- | src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c | 11 |
6 files changed, 39 insertions, 58 deletions
diff --git a/libnm-core/nm-keyfile-writer.c b/libnm-core/nm-keyfile-writer.c index 8af7c086e7..f92faef5bb 100644 --- a/libnm-core/nm-keyfile-writer.c +++ b/libnm-core/nm-keyfile-writer.c @@ -338,13 +338,9 @@ write_hash_of_string (GKeyFile *file, } hash = g_value_get_boxed (value); - keys = (const char **) g_hash_table_get_keys_as_array (hash, &l); - if (!keys) - return; - - g_qsort_with_data (keys, l, sizeof (const char *), nm_strcmp_p_with_data, NULL); - for (i = 0; keys[i]; i++) { + keys = nm_utils_strdict_get_keys (hash, TRUE, &l); + for (i = 0; i < l; i++) { const char *property, *data; gboolean write_item = TRUE; diff --git a/libnm-core/nm-setting-ip-config.c b/libnm-core/nm-setting-ip-config.c index e62e8c63ea..e581aee557 100644 --- a/libnm-core/nm-setting-ip-config.c +++ b/libnm-core/nm-setting-ip-config.c @@ -1129,7 +1129,7 @@ _nm_ip_route_get_attributes_direct (NMIPRoute *route) * * Returns: (array length=out_length) (transfer container): a %NULL-terminated array * of attribute names or %NULL if there are no attributes. The order of the returned - * names is undefined. + * names depends on @sorted. **/ const char ** _nm_ip_route_get_attribute_names (const NMIPRoute *route, gboolean sorted, guint *out_length) diff --git a/libnm-core/nm-setting-user.c b/libnm-core/nm-setting-user.c index 8fc96b278f..ccc030aacd 100644 --- a/libnm-core/nm-setting-user.c +++ b/libnm-core/nm-setting-user.c @@ -226,7 +226,6 @@ nm_setting_user_get_keys (NMSettingUser *setting, guint *out_len) { NMSettingUser *self = setting; NMSettingUserPrivate *priv; - guint len; g_return_val_if_fail (NM_IS_SETTING_USER (self), NULL); @@ -237,19 +236,13 @@ nm_setting_user_get_keys (NMSettingUser *setting, guint *out_len) return priv->keys; } - if (!priv->data || !g_hash_table_size (priv->data)) { - NM_SET_OUT (out_len, 0); - return (const char **) &priv->keys; - } + priv->keys = nm_utils_strdict_get_keys (priv->data, + TRUE, + out_len); - priv->keys = (const char **) g_hash_table_get_keys_as_array (priv->data, &len); - g_qsort_with_data (priv->keys, - len, - sizeof (const char *), - nm_strcmp_p_with_data, - NULL); - NM_SET_OUT (out_len, len); - return priv->keys; + /* don't return %NULL, but hijack the @keys fields as a pseudo + * empty strv array. */ + return priv->keys ?: ((const char **) &priv->keys); } /*****************************************************************************/ diff --git a/src/devices/nm-lldp-listener.c b/src/devices/nm-lldp-listener.c index 2ed2a7d9fa..f637825b5a 100644 --- a/src/devices/nm-lldp-listener.c +++ b/src/devices/nm-lldp-listener.c @@ -286,19 +286,21 @@ lldp_neighbor_id_hash (gconstpointer ptr) } static int -lldp_neighbor_id_cmp (gconstpointer a, gconstpointer b) +lldp_neighbor_id_cmp (const LldpNeighbor *x, const LldpNeighbor *y) { - const LldpNeighbor *x = a, *y = b; - int c; - - if (x->chassis_id_type != y->chassis_id_type) - return x->chassis_id_type < y->chassis_id_type ? -1 : 1; - if (x->port_id_type != y->port_id_type) - return x->port_id_type < y->port_id_type ? -1 : 1; - c = g_strcmp0 (x->chassis_id, y->chassis_id); - if (c == 0) - c = g_strcmp0 (x->port_id, y->port_id); - return c < 0 ? -1 : (c > 0 ? 1 : 0); + NM_CMP_SELF (x, y); + NM_CMP_FIELD (x, y, chassis_id_type); + NM_CMP_FIELD (x, y, port_id_type); + NM_CMP_FIELD_STR0 (x, y, chassis_id); + NM_CMP_FIELD_STR0 (x, y, port_id); + return 0; +} + +static int +lldp_neighbor_id_cmp_p (gconstpointer a, gconstpointer b, gpointer user_data) +{ + return lldp_neighbor_id_cmp (*((const LldpNeighbor *const*) a), + *((const LldpNeighbor *const*) b)); } static gboolean @@ -838,20 +840,23 @@ GVariant * nm_lldp_listener_get_neighbors (NMLldpListener *self) { NMLldpListenerPrivate *priv; - GVariantBuilder array_builder; - GList *neighbors, *iter; g_return_val_if_fail (NM_IS_LLDP_LISTENER (self), FALSE); priv = NM_LLDP_LISTENER_GET_PRIVATE (self); - if (!priv->variant) { + if (G_UNLIKELY (!priv->variant)) { + GVariantBuilder array_builder; + gs_free LldpNeighbor **neighbors = NULL; + guint i, n; + g_variant_builder_init (&array_builder, G_VARIANT_TYPE ("aa{sv}")); - neighbors = g_hash_table_get_keys (priv->lldp_neighbors); - neighbors = g_list_sort (neighbors, lldp_neighbor_id_cmp); - for (iter = neighbors; iter; iter = iter->next) - g_variant_builder_add_value (&array_builder, lldp_neighbor_to_variant (iter->data)); - g_list_free (neighbors); + neighbors = (LldpNeighbor **) nm_utils_hash_keys_to_array (priv->lldp_neighbors, + lldp_neighbor_id_cmp_p, + NULL, + &n); + for (i = 0; i < n; i++) + g_variant_builder_add_value (&array_builder, lldp_neighbor_to_variant (neighbors[i])); priv->variant = g_variant_ref_sink (g_variant_builder_end (&array_builder)); } return priv->variant; diff --git a/src/dns/nm-dns-systemd-resolved.c b/src/dns/nm-dns-systemd-resolved.c index 2e790d0b14..7da27e5f7f 100644 --- a/src/dns/nm-dns-systemd-resolved.c +++ b/src/dns/nm-dns-systemd-resolved.c @@ -330,14 +330,10 @@ update (NMDnsPlugin *plugin, free_pending_updates (self); - interfaces_keys = g_hash_table_get_keys_as_array (interfaces, &interfaces_len); - if (interfaces_len > 1) { - g_qsort_with_data (interfaces_keys, - interfaces_len, - sizeof (gpointer), - nm_cmp_int2ptr_p_with_data, - NULL); - } + interfaces_keys = nm_utils_hash_keys_to_array (interfaces, + nm_cmp_int2ptr_p_with_data, + NULL, + &interfaces_len); for (i = 0; i < interfaces_len; i++) { InterfaceConfig *ic = g_hash_table_lookup (interfaces, GINT_TO_POINTER (interfaces_keys[i])); diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c index 1484dd43c8..3c97a43e6f 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c @@ -148,16 +148,7 @@ write_secrets (shvarFile *ifcfg, /* we purge all existing secrets. */ svUnsetAll (keyfile, SV_KEY_TYPE_ANY); - /* sort the keys. */ - secrets_keys = (const char **) g_hash_table_get_keys_as_array (secrets, &secrets_keys_n); - if (secrets_keys_n > 1) { - g_qsort_with_data (secrets_keys, - secrets_keys_n, - sizeof (const char *), - nm_strcmp_p_with_data, - NULL); - } - + secrets_keys = nm_utils_strdict_get_keys (secrets, TRUE, &secrets_keys_n); for (i = 0; i < secrets_keys_n; i++) { const char *k = secrets_keys[i]; const char *v = g_hash_table_lookup (secrets, k); |