libnm: avoid deep cloning list of address attribute names for nm_utils_ip_addresses_to_variant()

It's only a temporary list. No need for cloning the names
as well.
This commit is contained in:
Thomas Haller
2017-12-11 11:37:21 +01:00
parent f299a79d5b
commit cb06a36a18
3 changed files with 19 additions and 10 deletions

View File

@@ -216,6 +216,8 @@ GHashTable *_nm_utils_copy_strdict (GHashTable *strdict);
typedef gpointer (*NMUtilsCopyFunc) (gpointer); typedef gpointer (*NMUtilsCopyFunc) (gpointer);
const char **_nm_ip_address_get_attribute_names (const NMIPAddress *addr, gboolean sorted, guint *out_length);
gboolean _nm_ip_route_attribute_validate_all (const NMIPRoute *route); gboolean _nm_ip_route_attribute_validate_all (const NMIPRoute *route);
const char **_nm_ip_route_get_attribute_names (const NMIPRoute *route, gboolean sorted, guint *out_length); const char **_nm_ip_route_get_attribute_names (const NMIPRoute *route, gboolean sorted, guint *out_length);
GHashTable *_nm_ip_route_get_attributes_direct (NMIPRoute *route); GHashTable *_nm_ip_route_get_attributes_direct (NMIPRoute *route);

View File

@@ -526,6 +526,14 @@ nm_ip_address_set_prefix (NMIPAddress *address,
address->prefix = prefix; address->prefix = prefix;
} }
const char **
_nm_ip_address_get_attribute_names (const NMIPAddress *address, gboolean sorted, guint *out_length)
{
nm_assert (address);
return nm_utils_strdict_get_keys (address->attributes, sorted, out_length);
}
/** /**
* nm_ip_address_get_attribute_names: * nm_ip_address_get_attribute_names:
* @address: the #NMIPAddress * @address: the #NMIPAddress
@@ -541,7 +549,7 @@ nm_ip_address_get_attribute_names (NMIPAddress *address)
g_return_val_if_fail (address, NULL); g_return_val_if_fail (address, NULL);
names = nm_utils_strdict_get_keys (address->attributes, TRUE, NULL); names = _nm_ip_address_get_attribute_names (address, TRUE, NULL);
return nm_utils_strv_make_deep_copied_nonnull (names); return nm_utils_strv_make_deep_copied_nonnull (names);
} }
@@ -1126,7 +1134,7 @@ _nm_ip_route_get_attributes_direct (NMIPRoute *route)
const char ** const char **
_nm_ip_route_get_attribute_names (const NMIPRoute *route, gboolean sorted, guint *out_length) _nm_ip_route_get_attribute_names (const NMIPRoute *route, gboolean sorted, guint *out_length)
{ {
g_return_val_if_fail (route != NULL, NULL); nm_assert (route);
return nm_utils_strdict_get_keys (route->attributes, sorted, out_length); return nm_utils_strdict_get_keys (route->attributes, sorted, out_length);
} }

View File

@@ -1878,7 +1878,7 @@ GVariant *
nm_utils_ip_addresses_to_variant (GPtrArray *addresses) nm_utils_ip_addresses_to_variant (GPtrArray *addresses)
{ {
GVariantBuilder builder; GVariantBuilder builder;
int i; guint i;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("aa{sv}")); g_variant_builder_init (&builder, G_VARIANT_TYPE ("aa{sv}"));
@@ -1886,8 +1886,8 @@ nm_utils_ip_addresses_to_variant (GPtrArray *addresses)
for (i = 0; i < addresses->len; i++) { for (i = 0; i < addresses->len; i++) {
NMIPAddress *addr = addresses->pdata[i]; NMIPAddress *addr = addresses->pdata[i];
GVariantBuilder addr_builder; GVariantBuilder addr_builder;
char **names; gs_free const char **names = NULL;
int n; guint j, len;
g_variant_builder_init (&addr_builder, G_VARIANT_TYPE ("a{sv}")); g_variant_builder_init (&addr_builder, G_VARIANT_TYPE ("a{sv}"));
g_variant_builder_add (&addr_builder, "{sv}", g_variant_builder_add (&addr_builder, "{sv}",
@@ -1897,13 +1897,12 @@ nm_utils_ip_addresses_to_variant (GPtrArray *addresses)
"prefix", "prefix",
g_variant_new_uint32 (nm_ip_address_get_prefix (addr))); g_variant_new_uint32 (nm_ip_address_get_prefix (addr)));
names = nm_ip_address_get_attribute_names (addr); names = _nm_ip_address_get_attribute_names (addr, TRUE, &len);
for (n = 0; names[n]; n++) { for (j = 0; j < len; j++) {
g_variant_builder_add (&addr_builder, "{sv}", g_variant_builder_add (&addr_builder, "{sv}",
names[n], names[j],
nm_ip_address_get_attribute (addr, names[n])); nm_ip_address_get_attribute (addr, names[j]));
} }
g_strfreev (names);
g_variant_builder_add (&builder, "a{sv}", &addr_builder); g_variant_builder_add (&builder, "a{sv}", &addr_builder);
} }