4444#include "strv.h"
4545#include "tclass.h"
4646
47+ DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR (
48+ network_hash_ops ,
49+ char , string_hash_func , string_compare_func ,
50+ Network , network_unref );
51+
52+ DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR (
53+ stacked_netdevs_hash_ops ,
54+ char , string_hash_func , string_compare_func ,
55+ NetDev , netdev_unref );
56+
4757static int network_resolve_netdev_one (Network * network , const char * name , NetDevKind kind , NetDev * * ret ) {
4858 const char * kind_string ;
4959 NetDev * netdev ;
@@ -105,14 +115,14 @@ static int network_resolve_stacked_netdevs(Network *network) {
105115 if (network_resolve_netdev_one (network , name , PTR_TO_INT (kind ), & netdev ) <= 0 )
106116 continue ;
107117
108- r = hashmap_ensure_put (& network -> stacked_netdevs , & string_hash_ops , netdev -> ifname , netdev );
118+ r = hashmap_ensure_put (& network -> stacked_netdevs , & stacked_netdevs_hash_ops , netdev -> ifname , netdev );
109119 if (r == - ENOMEM )
110120 return log_oom ();
111121 if (r < 0 )
112122 log_warning_errno (r , "%s: Failed to add NetDev '%s' to network, ignoring: %m" ,
113123 network -> filename , (const char * ) name );
114124
115- netdev = NULL ;
125+ TAKE_PTR ( netdev ) ;
116126 }
117127
118128 return 0 ;
@@ -589,7 +599,7 @@ int network_load_one(Manager *manager, OrderedHashmap **networks, const char *fi
589599 if (r < 0 )
590600 return r ; /* network_verify() logs internally. */
591601
592- r = ordered_hashmap_ensure_put (networks , & string_hash_ops , network -> name , network );
602+ r = ordered_hashmap_ensure_put (networks , & network_hash_ops , network -> name , network );
593603 if (r < 0 )
594604 return log_warning_errno (r , "%s: Failed to store configuration into hashmap: %m" , filename );
595605
@@ -640,15 +650,15 @@ static bool network_netdev_equal(Network *a, Network *b) {
640650}
641651
642652int network_reload (Manager * manager ) {
643- OrderedHashmap * new_networks = NULL ;
653+ _cleanup_ordered_hashmap_free_ OrderedHashmap * new_networks = NULL ;
644654 Network * n , * old ;
645655 int r ;
646656
647657 assert (manager );
648658
649659 r = network_load (manager , & new_networks );
650660 if (r < 0 )
651- goto failure ;
661+ return r ;
652662
653663 ORDERED_HASHMAP_FOREACH (n , new_networks ) {
654664 r = network_get_by_name (manager , n -> name , & old );
@@ -670,14 +680,13 @@ int network_reload(Manager *manager) {
670680 /* Nothing updated, use the existing Network object, and drop the new one. */
671681 r = ordered_hashmap_replace (new_networks , old -> name , old );
672682 if (r < 0 )
673- goto failure ;
683+ return r ;
674684
675685 network_ref (old );
676686 network_unref (n );
677687 }
678688
679- ordered_hashmap_free_with_destructor (manager -> networks , network_unref );
680- manager -> networks = new_networks ;
689+ ordered_hashmap_free_and_replace (manager -> networks , new_networks );
681690
682691 r = manager_build_dhcp_pd_subnet_ids (manager );
683692 if (r < 0 )
@@ -688,11 +697,6 @@ int network_reload(Manager *manager) {
688697 return r ;
689698
690699 return 0 ;
691-
692- failure :
693- ordered_hashmap_free_with_destructor (new_networks , network_unref );
694-
695- return r ;
696700}
697701
698702int manager_build_dhcp_pd_subnet_ids (Manager * manager ) {
@@ -820,7 +824,7 @@ static Network *network_free(Network *network) {
820824 netdev_unref (network -> bridge );
821825 netdev_unref (network -> bond );
822826 netdev_unref (network -> vrf );
823- hashmap_free_with_destructor (network -> stacked_netdevs , netdev_unref );
827+ hashmap_free (network -> stacked_netdevs );
824828
825829 /* static configs */
826830 set_free (network -> ipv6_proxy_ndp_addresses );
0 commit comments