diff options
author | Thomas Haller <thaller@redhat.com> | 2020-06-19 18:07:16 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2020-06-19 18:07:16 +0200 |
commit | 8ba4537b24617680b6f0e94d69b0b552a7ee4bd3 (patch) | |
tree | 19d02b4e5023c0bd3f150c5df4fd79bcbbe28bc7 | |
parent | d64eb7201ec1ac095827f8a8643b71d7f673495d (diff) | |
parent | bb6c2d73713f69ceb6c827dcc7fb7dac91595ece (diff) | |
download | NetworkManager-8ba4537b24617680b6f0e94d69b0b552a7ee4bd3.tar.gz |
all: merge branch 'th/stdict-values'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/531
-rw-r--r-- | libnm-core/nm-setting-bond.c | 2 | ||||
-rw-r--r-- | libnm-core/nm-setting-ip-config.c | 19 | ||||
-rw-r--r-- | libnm-core/nm-setting-tc-config.c | 52 | ||||
-rw-r--r-- | shared/nm-glib-aux/nm-shared-utils.c | 117 | ||||
-rw-r--r-- | shared/nm-glib-aux/nm-shared-utils.h | 23 | ||||
-rw-r--r-- | src/nm-core-utils.c | 22 | ||||
-rw-r--r-- | src/nm-core-utils.h | 2 | ||||
-rw-r--r-- | src/nm-dhcp-config.c | 2 | ||||
-rw-r--r-- | src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c | 18 |
9 files changed, 146 insertions, 111 deletions
diff --git a/libnm-core/nm-setting-bond.c b/libnm-core/nm-setting-bond.c index 8986e434e5..da759efcaa 100644 --- a/libnm-core/nm-setting-bond.c +++ b/libnm-core/nm-setting-bond.c @@ -368,7 +368,7 @@ static void _ensure_options_idx_cache (NMSettingBondPrivate *priv) { if (!G_UNLIKELY (priv->options_idx_cache)) - priv->options_idx_cache = nm_utils_named_values_from_str_dict_with_sort (priv->options, NULL, _get_option_sort, NULL); + priv->options_idx_cache = nm_utils_named_values_from_strdict_full (priv->options, NULL, _get_option_sort, NULL, NULL, 0, NULL); } /** diff --git a/libnm-core/nm-setting-ip-config.c b/libnm-core/nm-setting-ip-config.c index bc81129177..f7a022b6cb 100644 --- a/libnm-core/nm-setting-ip-config.c +++ b/libnm-core/nm-setting-ip-config.c @@ -1363,9 +1363,12 @@ nm_ip_route_attribute_validate (const char *name, gboolean _nm_ip_route_attribute_validate_all (const NMIPRoute *route, GError **error) { - GHashTableIter iter; - const char *key; + NMUtilsNamedValue attrs_static[G_N_ELEMENTS (ip_route_attribute_spec)]; + gs_free NMUtilsNamedValue *attrs_free = NULL; + const NMUtilsNamedValue *attrs; + guint attrs_len; GVariant *val; + guint i; guint8 u8; g_return_val_if_fail (route, FALSE); @@ -1374,9 +1377,15 @@ _nm_ip_route_attribute_validate_all (const NMIPRoute *route, GError **error) if (!route->attributes) return TRUE; - g_hash_table_iter_init (&iter, route->attributes); - while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &val)) { - if (!nm_ip_route_attribute_validate (key, val, route->family, NULL, error)) + attrs = nm_utils_named_values_from_strdict (route->attributes, + &attrs_len, + attrs_static, + &attrs_free); + for (i = 0; i < attrs_len; i++) { + const char *key = attrs[i].name; + GVariant *val2 = attrs[i].value_ptr; + + if (!nm_ip_route_attribute_validate (key, val2, route->family, NULL, NULL)) return FALSE; } diff --git a/libnm-core/nm-setting-tc-config.c b/libnm-core/nm-setting-tc-config.c index 8658231562..2ece266976 100644 --- a/libnm-core/nm-setting-tc-config.c +++ b/libnm-core/nm-setting-tc-config.c @@ -174,13 +174,14 @@ nm_tc_qdisc_equal (NMTCQdisc *qdisc, NMTCQdisc *other) static guint _nm_tc_qdisc_hash (NMTCQdisc *qdisc) { - gs_free const char **names = NULL; - GVariant *variant; + NMUtilsNamedValue attrs_static[30]; + gs_free NMUtilsNamedValue *attrs_free = NULL; + const NMUtilsNamedValue *attrs; NMHashState h; guint length; guint i; - names = nm_utils_strdict_get_keys (qdisc->attributes, TRUE, &length); + attrs = nm_utils_named_values_from_strdict (qdisc->attributes, &length, attrs_static, &attrs_free); nm_hash_init (&h, 43869703); nm_hash_update_vals (&h, @@ -189,13 +190,13 @@ _nm_tc_qdisc_hash (NMTCQdisc *qdisc) length); nm_hash_update_str0 (&h, qdisc->kind); for (i = 0; i < length; i++) { + const char *key = attrs[i].name; + GVariant *variant = attrs[i].value_ptr; const GVariantType *vtype; - variant = g_hash_table_lookup (qdisc->attributes, names[i]); - vtype = g_variant_get_type (variant); - nm_hash_update_str (&h, names[i]); + nm_hash_update_str (&h, key); nm_hash_update_str (&h, (const char *) vtype); if (g_variant_type_is_basic (vtype)) nm_hash_update_val (&h, g_variant_hash (variant)); @@ -585,6 +586,8 @@ nm_tc_action_get_kind (NMTCAction *action) * Gets an array of attribute names defined on @action. * * Returns: (transfer full): a %NULL-terminated array of attribute names, + * + * Since: 1.12 **/ char ** nm_tc_action_get_attribute_names (NMTCAction *action) @@ -614,6 +617,8 @@ _nm_tc_action_get_attributes (NMTCAction *action) * * Returns: (transfer none): the value of the attribute with name @name on * @action, or %NULL if @action has no such attribute. + * + * Since: 1.12 **/ GVariant * nm_tc_action_get_attribute (NMTCAction *action, const char *name) @@ -634,6 +639,8 @@ nm_tc_action_get_attribute (NMTCAction *action, const char *name) * @value: (transfer none) (allow-none): the value * * Sets or clears the named attribute on @action to the given value. + * + * Since: 1.12 **/ void nm_tc_action_set_attribute (NMTCAction *action, const char *name, GVariant *value) @@ -792,24 +799,31 @@ nm_tc_tfilter_equal (NMTCTfilter *tfilter, NMTCTfilter *other) static guint _nm_tc_tfilter_hash (NMTCTfilter *tfilter) { - gs_free const char **names = NULL; - guint i, attr_hash; - GVariant *variant; NMHashState h; - guint length; nm_hash_init (&h, 63624437); nm_hash_update_vals (&h, tfilter->handle, tfilter->parent); nm_hash_update_str0 (&h, tfilter->kind); + if (tfilter->action) { + gs_free NMUtilsNamedValue *attrs_free = NULL; + NMUtilsNamedValue attrs_static[30]; + const NMUtilsNamedValue *attrs; + guint length; + guint i; + nm_hash_update_str0 (&h, tfilter->action->kind); - names = nm_utils_strdict_get_keys (tfilter->action->attributes, TRUE, &length); + + attrs = nm_utils_named_values_from_strdict (tfilter->action->attributes, &length, attrs_static, &attrs_free); for (i = 0; i < length; i++) { - nm_hash_update_str (&h, names[i]); - variant = g_hash_table_lookup (tfilter->action->attributes, names[i]); + GVariant *variant = attrs[i].value_ptr; + + nm_hash_update_str (&h, attrs[i].name); if (g_variant_type_is_basic (g_variant_get_type (variant))) { + guint attr_hash; + /* g_variant_hash() works only for basic types, thus * we ignore any non-basic attribute. Actions differing * only for non-basic attributes will collide. */ @@ -818,6 +832,7 @@ _nm_tc_tfilter_hash (NMTCTfilter *tfilter) } } } + return nm_hash_complete (&h); } @@ -1374,9 +1389,11 @@ _qdiscs_to_variant (GPtrArray *qdiscs) if (qdiscs) { for (i = 0; i < qdiscs->len; i++) { + NMUtilsNamedValue attrs_static[30]; + gs_free NMUtilsNamedValue *attrs_free = NULL; + const NMUtilsNamedValue *attrs; NMTCQdisc *qdisc = qdiscs->pdata[i]; guint length; - gs_free const char **attrs = nm_utils_strdict_get_keys (qdisc->attributes, TRUE, &length); GVariantBuilder qdisc_builder; guint y; @@ -1391,9 +1408,12 @@ _qdiscs_to_variant (GPtrArray *qdiscs) g_variant_builder_add (&qdisc_builder, "{sv}", "parent", g_variant_new_uint32 (nm_tc_qdisc_get_parent (qdisc))); + attrs = nm_utils_named_values_from_strdict (qdisc->attributes, &length, attrs_static, &attrs_free); for (y = 0; y < length; y++) { - g_variant_builder_add (&qdisc_builder, "{sv}", attrs[y], - g_hash_table_lookup (qdisc->attributes, attrs[y])); + g_variant_builder_add (&qdisc_builder, + "{sv}", + attrs[y].name, + attrs[y].value_ptr); } g_variant_builder_add (&builder, "a{sv}", &qdisc_builder); diff --git a/shared/nm-glib-aux/nm-shared-utils.c b/shared/nm-glib-aux/nm-shared-utils.c index 39e98a31df..68e6ee5ba1 100644 --- a/shared/nm-glib-aux/nm-shared-utils.c +++ b/shared/nm-glib-aux/nm-shared-utils.c @@ -478,50 +478,52 @@ nm_utils_gbytes_to_variant_ay (GBytes *bytes) GVariant * nm_utils_strdict_to_variant_ass (GHashTable *strdict) { - GHashTableIter iter; - const char *key, *value; + gs_free NMUtilsNamedValue *values_free = NULL; + NMUtilsNamedValue values_prepared[20]; + const NMUtilsNamedValue *values; GVariantBuilder builder; - guint i, len; - - g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}")); - - if (!strdict) - goto out; - len = g_hash_table_size (strdict); - if (!len) - goto out; + guint i; + guint n; - g_hash_table_iter_init (&iter, strdict); - if (!g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)) - nm_assert_not_reached (); + values = nm_utils_named_values_from_strdict (strdict, + &n, + values_prepared, + &values_free); - if (len == 1) - g_variant_builder_add (&builder, "{ss}", key, value); - else { - gs_free NMUtilsNamedValue *idx_free = NULL; - NMUtilsNamedValue *idx; + g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}")); + for (i = 0; i < n; i++) { + g_variant_builder_add (&builder, + "{ss}", + values[i].name, + values[i].value_str); + } + return g_variant_builder_end (&builder); +} - if (len > 300 / sizeof (NMUtilsNamedValue)) { - idx_free = g_new (NMUtilsNamedValue, len); - idx = idx_free; - } else - idx = g_alloca (sizeof (NMUtilsNamedValue) * len); +/*****************************************************************************/ - i = 0; - do { - idx[i].name = key; - idx[i].value_str = value; - i++; - } while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)); - nm_assert (i == len); +GVariant * +nm_utils_strdict_to_variant_asv (GHashTable *strdict) +{ + gs_free NMUtilsNamedValue *values_free = NULL; + NMUtilsNamedValue values_prepared[20]; + const NMUtilsNamedValue *values; + GVariantBuilder builder; + guint i; + guint n; - nm_utils_named_value_list_sort (idx, len, NULL, NULL); + values = nm_utils_named_values_from_strdict (strdict, + &n, + values_prepared, + &values_free); - for (i = 0; i < len; i++) - g_variant_builder_add (&builder, "{ss}", idx[i].name, idx[i].value_str); + g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}")); + for (i = 0; i < n; i++) { + g_variant_builder_add (&builder, + "{sv}", + values[i].name, + g_variant_new_string (values[i].value_str)); } - -out: return g_variant_builder_end (&builder); } @@ -2909,28 +2911,44 @@ nm_utils_fd_read_loop_exact (int fd, void *buf, size_t nbytes, bool do_poll) /*****************************************************************************/ +G_STATIC_ASSERT (G_STRUCT_OFFSET (NMUtilsNamedValue, name) == 0); + NMUtilsNamedValue * -nm_utils_named_values_from_str_dict_with_sort (GHashTable *hash, - guint *out_len, - GCompareDataFunc compare_func, - gpointer user_data) +nm_utils_named_values_from_strdict_full (GHashTable *hash, + guint *out_len, + GCompareDataFunc compare_func, + gpointer user_data, + NMUtilsNamedValue *provided_buffer, + guint provided_buffer_len, + NMUtilsNamedValue **out_allocated_buffer) { GHashTableIter iter; NMUtilsNamedValue *values; guint i, len; + nm_assert (provided_buffer_len == 0 || provided_buffer); + nm_assert (!out_allocated_buffer || !*out_allocated_buffer); + if ( !hash || !(len = g_hash_table_size (hash))) { NM_SET_OUT (out_len, 0); return NULL; } + if (provided_buffer_len >= len + 1) { + /* the buffer provided by the caller is large enough. Use it. */ + values = provided_buffer; + } else { + /* allocate a new buffer. */ + values = g_new (NMUtilsNamedValue, len + 1); + NM_SET_OUT (out_allocated_buffer, values); + } + i = 0; - values = g_new (NMUtilsNamedValue, len + 1); g_hash_table_iter_init (&iter, hash); while (g_hash_table_iter_next (&iter, (gpointer *) &values[i].name, - (gpointer *) &values[i].value_ptr)) + &values[i].value_ptr)) i++; nm_assert (i == len); values[i].name = NULL; @@ -4925,7 +4943,7 @@ _nm_utils_format_variant_attributes_full (GString *str, for (i = 0; i < num_values; i++) { name = values[i].name; - variant = (GVariant *) values[i].value_ptr; + variant = values[i].value_ptr; value = NULL; if (g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT32)) @@ -4969,17 +4987,24 @@ _nm_utils_format_variant_attributes (GHashTable *attributes, char attr_separator, char key_value_separator) { + gs_free NMUtilsNamedValue *values_free = NULL; + NMUtilsNamedValue values_prepared[20]; + const NMUtilsNamedValue *values; GString *str = NULL; - gs_free NMUtilsNamedValue *values = NULL; guint len; g_return_val_if_fail (attr_separator, NULL); g_return_val_if_fail (key_value_separator, NULL); - if (!attributes || !g_hash_table_size (attributes)) + if (!attributes) return NULL; - values = nm_utils_named_values_from_str_dict (attributes, &len); + values = nm_utils_named_values_from_strdict (attributes, + &len, + values_prepared, + &values_free); + if (len == 0) + return NULL; str = g_string_new (""); _nm_utils_format_variant_attributes_full (str, diff --git a/shared/nm-glib-aux/nm-shared-utils.h b/shared/nm-glib-aux/nm-shared-utils.h index eeb8be5b7d..67997e6b58 100644 --- a/shared/nm-glib-aux/nm-shared-utils.h +++ b/shared/nm-glib-aux/nm-shared-utils.h @@ -422,6 +422,7 @@ gboolean nm_utils_gbytes_equal_mem (GBytes *bytes, GVariant *nm_utils_gbytes_to_variant_ay (GBytes *bytes); GVariant *nm_utils_strdict_to_variant_ass (GHashTable *strdict); +GVariant *nm_utils_strdict_to_variant_asv (GHashTable *strdict); /*****************************************************************************/ @@ -1446,24 +1447,22 @@ typedef struct { }; union { const char *value_str; - gconstpointer value_ptr; + gpointer value_ptr; }; } NMUtilsNamedValue; #define NM_UTILS_NAMED_VALUE_INIT(n, v) { .name = (n), .value_ptr = (v) } -NMUtilsNamedValue *nm_utils_named_values_from_str_dict_with_sort (GHashTable *hash, - guint *out_len, - GCompareDataFunc compare_func, - gpointer user_data); +NMUtilsNamedValue *nm_utils_named_values_from_strdict_full (GHashTable *hash, + guint *out_len, + GCompareDataFunc compare_func, + gpointer user_data, + NMUtilsNamedValue *provided_buffer, + guint provided_buffer_len, + NMUtilsNamedValue **out_allocated_buffer); -static inline NMUtilsNamedValue * -nm_utils_named_values_from_str_dict (GHashTable *hash, guint *out_len) -{ - G_STATIC_ASSERT (G_STRUCT_OFFSET (NMUtilsNamedValue, name) == 0); - - return nm_utils_named_values_from_str_dict_with_sort (hash, out_len, nm_strcmp_p_with_data, NULL); -} +#define nm_utils_named_values_from_strdict(hash, out_len, array, out_allocated_buffer) \ + nm_utils_named_values_from_strdict_full ((hash), (out_len), nm_strcmp_p_with_data, NULL, (array), G_N_ELEMENTS (array), (out_allocated_buffer)) gssize nm_utils_named_value_list_find (const NMUtilsNamedValue *arr, gsize len, diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index 019d1e600f..bc80dabf35 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -4108,28 +4108,6 @@ nm_utils_parse_dns_domain (const char *domain, gboolean *is_routing) /*****************************************************************************/ -GVariant * -nm_utils_strdict_to_variant (GHashTable *options) -{ - GVariantBuilder builder; - gs_free NMUtilsNamedValue *values = NULL; - guint i; - guint n; - - values = nm_utils_named_values_from_str_dict (options, &n); - - g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}")); - for (i = 0; i < n; i++) { - g_variant_builder_add (&builder, - "{sv}", - values[i].name, - g_variant_new_string (values[i].value_str)); - } - return g_variant_builder_end (&builder); -} - -/*****************************************************************************/ - static guint32 get_max_rate_ht_20 (int mcs) { diff --git a/src/nm-core-utils.h b/src/nm-core-utils.h index e30d7b3651..b36c9b581f 100644 --- a/src/nm-core-utils.h +++ b/src/nm-core-utils.h @@ -449,8 +449,6 @@ gboolean nm_utils_validate_plugin (const char *path, struct stat *stat, GError * char **nm_utils_read_plugin_paths (const char *dirname, const char *prefix); char *nm_utils_format_con_diff_for_audit (GHashTable *diff); -GVariant *nm_utils_strdict_to_variant (GHashTable *options); - /*****************************************************************************/ /* this enum is compatible with ICMPV6_ROUTER_PREF_* (from <linux/icmpv6.h>, diff --git a/src/nm-dhcp-config.c b/src/nm-dhcp-config.c index 3b9211df99..fd55c3ff43 100644 --- a/src/nm-dhcp-config.c +++ b/src/nm-dhcp-config.c @@ -75,7 +75,7 @@ nm_dhcp_config_set_options (NMDhcpConfig *self, priv = NM_DHCP_CONFIG_GET_PRIVATE (self); nm_g_variant_unref (priv->options); - priv->options = g_variant_ref_sink (nm_utils_strdict_to_variant (options)); + priv->options = g_variant_ref_sink (nm_utils_strdict_to_variant_asv (options)); _notify (self, PROP_OPTIONS); } 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 e6526944c7..2eb3cf0ce2 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c @@ -114,10 +114,13 @@ write_secrets (shvarFile *ifcfg, GError **error) { nm_auto_shvar_file_close shvarFile *keyfile = NULL; - gs_free const char **secrets_keys = NULL; - guint i, secrets_keys_n; + gs_free NMUtilsNamedValue *secrets_arr_free = NULL; + NMUtilsNamedValue secrets_arr_static[30]; + const NMUtilsNamedValue *secrets_arr; + guint secrets_len; GError *local = NULL; gboolean any_secrets = FALSE; + guint i; keyfile = utils_get_keys_ifcfg (svFileGetName (ifcfg), TRUE); if (!keyfile) { @@ -126,10 +129,13 @@ write_secrets (shvarFile *ifcfg, return FALSE; } - 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); + secrets_arr = nm_utils_named_values_from_strdict (secrets, + &secrets_len, + secrets_arr_static, + &secrets_arr_free); + for (i = 0; i < secrets_len; i++) { + const char *k = secrets_arr[i].name; + const char *v = secrets_arr[i].value_str; if (v) { svSetValueStr (keyfile, k, v); |