libnm: for boxed type NMLldpNeighbor only ref the instance instead of deep cloning

There is no public API to modify a NMLldpNeighbor instance. They are only
created by NMDevice and afterwards immutable. There is no point to duplicate
them, when we can just take a ref.

Note that this means we must be careful if we ever add API to mutate a
NMLldpNeighbor. But immutable types are awesome, so we probably should
never do that anyway.
This commit is contained in:
Thomas Haller 2021-03-11 08:35:33 +01:00
parent ae881b7b5b
commit be40c8fdf7
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -129,7 +129,7 @@ G_DEFINE_ABSTRACT_TYPE(NMDevice, nm_device, NM_TYPE_OBJECT);
/*****************************************************************************/
static gboolean connection_compatible(NMDevice *device, NMConnection *connection, GError **error);
static NMLldpNeighbor *nm_lldp_neighbor_dup(NMLldpNeighbor *neighbor);
static NMLldpNeighbor *_nm_lldp_neighbor_dup(NMLldpNeighbor *neighbor);
/*****************************************************************************/
@ -138,7 +138,7 @@ struct _NMLldpNeighbor {
GHashTable *attrs;
};
G_DEFINE_BOXED_TYPE(NMLldpNeighbor, nm_lldp_neighbor, nm_lldp_neighbor_dup, nm_lldp_neighbor_unref)
G_DEFINE_BOXED_TYPE(NMLldpNeighbor, nm_lldp_neighbor, _nm_lldp_neighbor_dup, nm_lldp_neighbor_unref)
/*****************************************************************************/
@ -244,9 +244,12 @@ _notify_update_prop_lldp_neighbors(NMClient * client,
const char * attr_name;
NMLldpNeighbor *neigh;
/* Note that there is no public API to mutate a NMLldpNeighbor instance.
* This is the only place where we actually mutate it. */
neigh = nm_lldp_neighbor_new();
while (g_variant_iter_next(attrs_iter, "{&sv}", &attr_name, &attr_variant))
g_hash_table_insert(neigh->attrs, g_strdup(attr_name), attr_variant);
g_ptr_array_add(new, neigh);
g_variant_iter_free(attrs_iter);
@ -2872,6 +2875,14 @@ NM_IS_LLDP_NEIGHBOR(const NMLldpNeighbor *self)
*
* Creates a new #NMLldpNeighbor object.
*
* Note that #NMLldpNeighbor has no public API for mutating
* an instance. Also, libnm will not internally mutate a
* once exposed object. They are guaranteed to be immutable.
* Since 1.32, ref-counting is thread-safe.
*
* This function is not useful, as there is no public API to
* actually modify the (empty) instance.
*
* Returns: (transfer full): the new #NMLldpNeighbor object.
*
* Since: 1.2
@ -2894,20 +2905,15 @@ nm_lldp_neighbor_new(void)
}
static NMLldpNeighbor *
nm_lldp_neighbor_dup(NMLldpNeighbor *neighbor)
_nm_lldp_neighbor_dup(NMLldpNeighbor *neighbor)
{
NMLldpNeighbor *copy;
GHashTableIter iter;
const char * key;
GVariant * value;
copy = nm_lldp_neighbor_new();
g_hash_table_iter_init(&iter, neighbor->attrs);
while (g_hash_table_iter_next(&iter, (gpointer *) &key, (gpointer *) &value))
g_hash_table_insert(copy->attrs, g_strdup(key), g_variant_ref(value));
return copy;
/* There is no public API for mutating a NMLldpNeighbor. Nor should
* we ever add one, because immutable types (or at least, sealable types)
* are great.
*
* As such, dup is merely taking another ref. */
nm_lldp_neighbor_ref(neighbor);
return neighbor;
}
/**