From: Yu Watanabe Date: Sat, 12 Apr 2025 17:34:16 +0000 (+0900) Subject: network/radv: use hash_ops with destructor for managing prefixes X-Git-Tag: v258-rc1~790^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=10d786458c85f98a0de999167ebe4d0f8ecf8c1e;p=thirdparty%2Fsystemd.git network/radv: use hash_ops with destructor for managing prefixes --- diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c index 3b05cf170cd..3ae87e939a7 100644 --- a/src/network/networkd-network.c +++ b/src/network/networkd-network.c @@ -831,9 +831,9 @@ static Network *network_free(Network *network) { hashmap_free_with_destructor(network->bridge_mdb_entries_by_section, bridge_mdb_free); ordered_hashmap_free(network->neighbors_by_section); hashmap_free(network->address_labels_by_section); - hashmap_free_with_destructor(network->prefixes_by_section, prefix_free); - hashmap_free_with_destructor(network->route_prefixes_by_section, route_prefix_free); - hashmap_free_with_destructor(network->pref64_prefixes_by_section, prefix64_free); + hashmap_free(network->prefixes_by_section); + hashmap_free(network->route_prefixes_by_section); + hashmap_free(network->pref64_prefixes_by_section); hashmap_free(network->rules_by_section); hashmap_free_with_destructor(network->dhcp_static_leases_by_section, dhcp_static_lease_free); ordered_hashmap_free(network->sr_iov_by_section); diff --git a/src/network/networkd-radv.c b/src/network/networkd-radv.c index 570f9d60872..5e838028f24 100644 --- a/src/network/networkd-radv.c +++ b/src/network/networkd-radv.c @@ -35,7 +35,7 @@ bool link_radv_enabled(Link *link) { return link->network->router_prefix_delegation; } -Prefix* prefix_free(Prefix *prefix) { +static Prefix* prefix_free(Prefix *prefix) { if (!prefix) return NULL; @@ -52,6 +52,11 @@ Prefix* prefix_free(Prefix *prefix) { DEFINE_SECTION_CLEANUP_FUNCTIONS(Prefix, prefix_free); +DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( + prefix_hash_ops_by_section, + ConfigSection, config_section_hash_func, config_section_compare_func, + Prefix, prefix_free); + static int prefix_new_static(Network *network, const char *filename, unsigned section_line, Prefix **ret) { _cleanup_(config_section_freep) ConfigSection *n = NULL; _cleanup_(prefix_freep) Prefix *prefix = NULL; @@ -87,7 +92,7 @@ static int prefix_new_static(Network *network, const char *filename, unsigned se .prefix.preferred_until = USEC_INFINITY, }; - r = hashmap_ensure_put(&network->prefixes_by_section, &config_section_hash_ops, prefix->section, prefix); + r = hashmap_ensure_put(&network->prefixes_by_section, &prefix_hash_ops_by_section, prefix->section, prefix); if (r < 0) return r; @@ -95,7 +100,7 @@ static int prefix_new_static(Network *network, const char *filename, unsigned se return 0; } -RoutePrefix* route_prefix_free(RoutePrefix *prefix) { +static RoutePrefix* route_prefix_free(RoutePrefix *prefix) { if (!prefix) return NULL; @@ -111,6 +116,11 @@ RoutePrefix* route_prefix_free(RoutePrefix *prefix) { DEFINE_SECTION_CLEANUP_FUNCTIONS(RoutePrefix, route_prefix_free); +DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( + route_prefix_hash_ops_by_section, + ConfigSection, config_section_hash_func, config_section_compare_func, + RoutePrefix, route_prefix_free); + static int route_prefix_new_static(Network *network, const char *filename, unsigned section_line, RoutePrefix **ret) { _cleanup_(config_section_freep) ConfigSection *n = NULL; _cleanup_(route_prefix_freep) RoutePrefix *prefix = NULL; @@ -143,7 +153,7 @@ static int route_prefix_new_static(Network *network, const char *filename, unsig .route.valid_until = USEC_INFINITY, }; - r = hashmap_ensure_put(&network->route_prefixes_by_section, &config_section_hash_ops, prefix->section, prefix); + r = hashmap_ensure_put(&network->route_prefixes_by_section, &route_prefix_hash_ops_by_section, prefix->section, prefix); if (r < 0) return r; @@ -151,7 +161,7 @@ static int route_prefix_new_static(Network *network, const char *filename, unsig return 0; } -Prefix64* prefix64_free(Prefix64 *prefix) { +static Prefix64* prefix64_free(Prefix64 *prefix) { if (!prefix) return NULL; @@ -167,6 +177,11 @@ Prefix64* prefix64_free(Prefix64 *prefix) { DEFINE_SECTION_CLEANUP_FUNCTIONS(Prefix64, prefix64_free); +DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( + prefix64_hash_ops_by_section, + ConfigSection, config_section_hash_func, config_section_compare_func, + Prefix64, prefix64_free); + static int prefix64_new_static(Network *network, const char *filename, unsigned section_line, Prefix64 **ret) { _cleanup_(config_section_freep) ConfigSection *n = NULL; _cleanup_(prefix64_freep) Prefix64 *prefix = NULL; @@ -199,7 +214,7 @@ static int prefix64_new_static(Network *network, const char *filename, unsigned .prefix64.valid_until = USEC_INFINITY, }; - r = hashmap_ensure_put(&network->pref64_prefixes_by_section, &config_section_hash_ops, prefix->section, prefix); + r = hashmap_ensure_put(&network->pref64_prefixes_by_section, &prefix64_hash_ops_by_section, prefix->section, prefix); if (r < 0) return r; @@ -809,9 +824,9 @@ void network_adjust_radv(Network *network) { } if (!FLAGS_SET(network->router_prefix_delegation, RADV_PREFIX_DELEGATION_STATIC)) { - network->prefixes_by_section = hashmap_free_with_destructor(network->prefixes_by_section, prefix_free); - network->route_prefixes_by_section = hashmap_free_with_destructor(network->route_prefixes_by_section, route_prefix_free); - network->pref64_prefixes_by_section = hashmap_free_with_destructor(network->pref64_prefixes_by_section, prefix64_free); + network->prefixes_by_section = hashmap_free(network->prefixes_by_section); + network->route_prefixes_by_section = hashmap_free(network->route_prefixes_by_section); + network->pref64_prefixes_by_section = hashmap_free(network->pref64_prefixes_by_section); } if (!network->router_prefix_delegation) diff --git a/src/network/networkd-radv.h b/src/network/networkd-radv.h index 3f0a374632a..ed8ee216de8 100644 --- a/src/network/networkd-radv.h +++ b/src/network/networkd-radv.h @@ -52,10 +52,6 @@ typedef struct Prefix64 { sd_ndisc_prefix64 prefix64; } Prefix64; -Prefix* prefix_free(Prefix *prefix); -RoutePrefix* route_prefix_free(RoutePrefix *prefix); -Prefix64* prefix64_free(Prefix64 *prefix); - void network_adjust_radv(Network *network); int link_request_radv_addresses(Link *link);