Skip to content

Commit 4cf443e

Browse files
committed
network/link: use hash_ops with destructor for managing Link objects
1 parent a85f73f commit 4cf443e

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

src/network/networkd-link.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ static void link_free_engines(Link *link) {
261261
link->radv = sd_radv_unref(link->radv);
262262
}
263263

264-
static Link *link_free(Link *link) {
264+
static Link* link_free(Link *link) {
265265
assert(link);
266266

267267
(void) link_clear_sysctl_shadows(link);
@@ -295,7 +295,7 @@ static Link *link_free(Link *link) {
295295
hashmap_free(link->bound_to_links);
296296
hashmap_free(link->bound_by_links);
297297

298-
set_free_with_destructor(link->slaves, link_unref);
298+
set_free(link->slaves);
299299

300300
network_unref(link->network);
301301

@@ -307,6 +307,11 @@ static Link *link_free(Link *link) {
307307

308308
DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free);
309309

310+
DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
311+
link_hash_ops,
312+
void, trivial_hash_func, trivial_compare_func,
313+
Link, link_unref);
314+
310315
int link_get_by_index(Manager *m, int ifindex, Link **ret) {
311316
Link *link;
312317

@@ -985,7 +990,7 @@ static int link_append_to_master(Link *link) {
985990
if (link_get_master(link, &master) < 0)
986991
return 0;
987992

988-
r = set_ensure_put(&master->slaves, NULL, link);
993+
r = set_ensure_put(&master->slaves, &link_hash_ops, link);
989994
if (r <= 0)
990995
return r;
991996

@@ -2746,7 +2751,7 @@ static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
27462751
.dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID,
27472752
};
27482753

2749-
r = hashmap_ensure_put(&manager->links_by_index, NULL, INT_TO_PTR(link->ifindex), link);
2754+
r = hashmap_ensure_put(&manager->links_by_index, &link_hash_ops, INT_TO_PTR(link->ifindex), link);
27502755
if (r < 0)
27512756
return log_link_debug_errno(link, r, "Failed to store link into manager: %m");
27522757

src/network/networkd-link.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ typedef struct Link {
222222
char **ntp;
223223
} Link;
224224

225+
extern const struct hash_ops link_hash_ops;
226+
225227
typedef int (*link_netlink_message_handler_t)(sd_netlink*, sd_netlink_message*, Link*);
226228

227229
bool link_is_ready_to_configure(Link *link, bool allow_unmanaged);

src/network/networkd-manager.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,12 +672,13 @@ Manager* manager_free(Manager *m) {
672672
m->request_queue = ordered_set_free(m->request_queue);
673673
m->remove_request_queue = ordered_set_free(m->remove_request_queue);
674674

675-
m->dirty_links = set_free_with_destructor(m->dirty_links, link_unref);
676675
m->new_wlan_ifindices = set_free(m->new_wlan_ifindices);
676+
677+
m->dirty_links = set_free(m->dirty_links);
677678
m->links_by_name = hashmap_free(m->links_by_name);
678679
m->links_by_hw_addr = hashmap_free(m->links_by_hw_addr);
679680
m->links_by_dhcp_pd_subnet_prefix = hashmap_free(m->links_by_dhcp_pd_subnet_prefix);
680-
m->links_by_index = hashmap_free_with_destructor(m->links_by_index, link_unref);
681+
m->links_by_index = hashmap_free(m->links_by_index);
681682

682683
m->dhcp_pd_subnet_ids = set_free(m->dhcp_pd_subnet_ids);
683684
m->networks = ordered_hashmap_free(m->networks);

src/network/networkd-state-file.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -947,8 +947,6 @@ static int link_save(Link *link) {
947947
}
948948

949949
void link_dirty(Link *link) {
950-
int r;
951-
952950
assert(link);
953951
assert(link->manager);
954952

@@ -962,10 +960,9 @@ void link_dirty(Link *link) {
962960
/* Also mark manager dirty as link is dirty */
963961
link->manager->dirty = true;
964962

965-
r = set_ensure_put(&link->manager->dirty_links, NULL, link);
966-
if (r <= 0)
967-
/* Ignore allocation errors and don't take another ref if the link was already dirty */
968-
return;
963+
if (set_ensure_put(&link->manager->dirty_links, &link_hash_ops, link) <= 0)
964+
return; /* Ignore allocation errors and don't take another ref if the link was already dirty */
965+
969966
link_ref(link);
970967
}
971968

0 commit comments

Comments
 (0)