network/network: use hash_ops with destructor for managing Network objects
This commit is contained in:
@@ -680,7 +680,7 @@ Manager* manager_free(Manager *m) {
|
||||
m->links_by_index = hashmap_free_with_destructor(m->links_by_index, link_unref);
|
||||
|
||||
m->dhcp_pd_subnet_ids = set_free(m->dhcp_pd_subnet_ids);
|
||||
m->networks = ordered_hashmap_free_with_destructor(m->networks, network_unref);
|
||||
m->networks = ordered_hashmap_free(m->networks);
|
||||
|
||||
/* The same object may be registered with multiple names, and netdev_detach() may drop multiple
|
||||
* entries. Hence, hashmap_free_with_destructor() cannot be used. */
|
||||
|
||||
@@ -44,6 +44,11 @@
|
||||
#include "strv.h"
|
||||
#include "tclass.h"
|
||||
|
||||
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
|
||||
network_hash_ops,
|
||||
char, string_hash_func, string_compare_func,
|
||||
Network, network_unref);
|
||||
|
||||
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
|
||||
stacked_netdevs_hash_ops,
|
||||
char, string_hash_func, string_compare_func,
|
||||
@@ -599,7 +604,7 @@ int network_load_one(Manager *manager, OrderedHashmap **networks, const char *fi
|
||||
if (r < 0)
|
||||
return r; /* network_verify() logs internally. */
|
||||
|
||||
r = ordered_hashmap_ensure_put(networks, &string_hash_ops, network->name, network);
|
||||
r = ordered_hashmap_ensure_put(networks, &network_hash_ops, network->name, network);
|
||||
if (r < 0)
|
||||
return log_warning_errno(r, "%s: Failed to store configuration into hashmap: %m", filename);
|
||||
|
||||
@@ -650,7 +655,7 @@ static bool network_netdev_equal(Network *a, Network *b) {
|
||||
}
|
||||
|
||||
int network_reload(Manager *manager) {
|
||||
OrderedHashmap *new_networks = NULL;
|
||||
_cleanup_ordered_hashmap_free_ OrderedHashmap *new_networks = NULL;
|
||||
Network *n, *old;
|
||||
int r;
|
||||
|
||||
@@ -658,7 +663,7 @@ int network_reload(Manager *manager) {
|
||||
|
||||
r = network_load(manager, &new_networks);
|
||||
if (r < 0)
|
||||
goto failure;
|
||||
return r;
|
||||
|
||||
ORDERED_HASHMAP_FOREACH(n, new_networks) {
|
||||
r = network_get_by_name(manager, n->name, &old);
|
||||
@@ -680,14 +685,13 @@ int network_reload(Manager *manager) {
|
||||
/* Nothing updated, use the existing Network object, and drop the new one. */
|
||||
r = ordered_hashmap_replace(new_networks, old->name, old);
|
||||
if (r < 0)
|
||||
goto failure;
|
||||
return r;
|
||||
|
||||
network_ref(old);
|
||||
network_unref(n);
|
||||
}
|
||||
|
||||
ordered_hashmap_free_with_destructor(manager->networks, network_unref);
|
||||
manager->networks = new_networks;
|
||||
ordered_hashmap_free_and_replace(manager->networks, new_networks);
|
||||
|
||||
r = manager_build_dhcp_pd_subnet_ids(manager);
|
||||
if (r < 0)
|
||||
@@ -698,11 +702,6 @@ int network_reload(Manager *manager) {
|
||||
return r;
|
||||
|
||||
return 0;
|
||||
|
||||
failure:
|
||||
ordered_hashmap_free_with_destructor(new_networks, network_unref);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int manager_build_dhcp_pd_subnet_ids(Manager *manager) {
|
||||
|
||||
Reference in New Issue
Block a user