network/wiphy: use hash_ops with destructor for managing Wiphy objects

This commit is contained in:
Yu Watanabe
2025-04-13 03:38:50 +09:00
parent 4cf443e644
commit 52278e0634
3 changed files with 10 additions and 6 deletions

View File

@@ -692,7 +692,7 @@ Manager* manager_free(Manager *m) {
m->tuntap_fds_by_name = hashmap_free(m->tuntap_fds_by_name);
m->wiphy_by_name = hashmap_free(m->wiphy_by_name);
m->wiphy_by_index = hashmap_free_with_destructor(m->wiphy_by_index, wiphy_free);
m->wiphy_by_index = hashmap_free(m->wiphy_by_index);
ordered_set_free(m->address_pools);

View File

@@ -12,7 +12,7 @@
#include "udev-util.h"
#include "wifi-util.h"
Wiphy *wiphy_free(Wiphy *w) {
static Wiphy* wiphy_free(Wiphy *w) {
if (!w)
return NULL;
@@ -29,6 +29,13 @@ Wiphy *wiphy_free(Wiphy *w) {
return mfree(w);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(Wiphy*, wiphy_free);
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
wiphy_hash_ops,
void, trivial_hash_func, trivial_compare_func,
Wiphy, wiphy_free);
static int wiphy_new(Manager *manager, sd_netlink_message *message, Wiphy **ret) {
_cleanup_(wiphy_freep) Wiphy *w = NULL;
_cleanup_free_ char *name = NULL;
@@ -56,7 +63,7 @@ static int wiphy_new(Manager *manager, sd_netlink_message *message, Wiphy **ret)
.name = TAKE_PTR(name),
};
r = hashmap_ensure_put(&manager->wiphy_by_index, NULL, UINT32_TO_PTR(w->index), w);
r = hashmap_ensure_put(&manager->wiphy_by_index, &wiphy_hash_ops, UINT32_TO_PTR(w->index), w);
if (r < 0)
return r;

View File

@@ -31,9 +31,6 @@ typedef struct Wiphy {
RFKillState rfkill_state;
} Wiphy;
Wiphy *wiphy_free(Wiphy *w);
DEFINE_TRIVIAL_CLEANUP_FUNC(Wiphy*, wiphy_free);
int wiphy_get_by_index(Manager *manager, uint32_t index, Wiphy **ret);
int wiphy_get_by_name(Manager *manager, const char *name, Wiphy **ret);