network/link: use hash_ops with destructor for managing Link objects
This commit is contained in:
@@ -261,7 +261,7 @@ static void link_free_engines(Link *link) {
|
||||
link->radv = sd_radv_unref(link->radv);
|
||||
}
|
||||
|
||||
static Link *link_free(Link *link) {
|
||||
static Link* link_free(Link *link) {
|
||||
assert(link);
|
||||
|
||||
(void) link_clear_sysctl_shadows(link);
|
||||
@@ -295,7 +295,7 @@ static Link *link_free(Link *link) {
|
||||
hashmap_free(link->bound_to_links);
|
||||
hashmap_free(link->bound_by_links);
|
||||
|
||||
set_free_with_destructor(link->slaves, link_unref);
|
||||
set_free(link->slaves);
|
||||
|
||||
network_unref(link->network);
|
||||
|
||||
@@ -307,6 +307,11 @@ static Link *link_free(Link *link) {
|
||||
|
||||
DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free);
|
||||
|
||||
DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
|
||||
link_hash_ops,
|
||||
void, trivial_hash_func, trivial_compare_func,
|
||||
Link, link_unref);
|
||||
|
||||
int link_get_by_index(Manager *m, int ifindex, Link **ret) {
|
||||
Link *link;
|
||||
|
||||
@@ -985,7 +990,7 @@ static int link_append_to_master(Link *link) {
|
||||
if (link_get_master(link, &master) < 0)
|
||||
return 0;
|
||||
|
||||
r = set_ensure_put(&master->slaves, NULL, link);
|
||||
r = set_ensure_put(&master->slaves, &link_hash_ops, link);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
|
||||
@@ -2746,7 +2751,7 @@ static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
|
||||
.dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID,
|
||||
};
|
||||
|
||||
r = hashmap_ensure_put(&manager->links_by_index, NULL, INT_TO_PTR(link->ifindex), link);
|
||||
r = hashmap_ensure_put(&manager->links_by_index, &link_hash_ops, INT_TO_PTR(link->ifindex), link);
|
||||
if (r < 0)
|
||||
return log_link_debug_errno(link, r, "Failed to store link into manager: %m");
|
||||
|
||||
|
||||
@@ -222,6 +222,8 @@ typedef struct Link {
|
||||
char **ntp;
|
||||
} Link;
|
||||
|
||||
extern const struct hash_ops link_hash_ops;
|
||||
|
||||
typedef int (*link_netlink_message_handler_t)(sd_netlink*, sd_netlink_message*, Link*);
|
||||
|
||||
bool link_is_ready_to_configure(Link *link, bool allow_unmanaged);
|
||||
|
||||
@@ -672,12 +672,13 @@ Manager* manager_free(Manager *m) {
|
||||
m->request_queue = ordered_set_free(m->request_queue);
|
||||
m->remove_request_queue = ordered_set_free(m->remove_request_queue);
|
||||
|
||||
m->dirty_links = set_free_with_destructor(m->dirty_links, link_unref);
|
||||
m->new_wlan_ifindices = set_free(m->new_wlan_ifindices);
|
||||
|
||||
m->dirty_links = set_free(m->dirty_links);
|
||||
m->links_by_name = hashmap_free(m->links_by_name);
|
||||
m->links_by_hw_addr = hashmap_free(m->links_by_hw_addr);
|
||||
m->links_by_dhcp_pd_subnet_prefix = hashmap_free(m->links_by_dhcp_pd_subnet_prefix);
|
||||
m->links_by_index = hashmap_free_with_destructor(m->links_by_index, link_unref);
|
||||
m->links_by_index = hashmap_free(m->links_by_index);
|
||||
|
||||
m->dhcp_pd_subnet_ids = set_free(m->dhcp_pd_subnet_ids);
|
||||
m->networks = ordered_hashmap_free(m->networks);
|
||||
|
||||
@@ -947,8 +947,6 @@ static int link_save(Link *link) {
|
||||
}
|
||||
|
||||
void link_dirty(Link *link) {
|
||||
int r;
|
||||
|
||||
assert(link);
|
||||
assert(link->manager);
|
||||
|
||||
@@ -962,10 +960,9 @@ void link_dirty(Link *link) {
|
||||
/* Also mark manager dirty as link is dirty */
|
||||
link->manager->dirty = true;
|
||||
|
||||
r = set_ensure_put(&link->manager->dirty_links, NULL, link);
|
||||
if (r <= 0)
|
||||
/* Ignore allocation errors and don't take another ref if the link was already dirty */
|
||||
return;
|
||||
if (set_ensure_put(&link->manager->dirty_links, &link_hash_ops, link) <= 0)
|
||||
return; /* Ignore allocation errors and don't take another ref if the link was already dirty */
|
||||
|
||||
link_ref(link);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user