]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network-generator: use hash_ops with destructor for Network, NetDev, and Link
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 12 Apr 2025 16:36:56 +0000 (01:36 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 13 Apr 2025 01:03:40 +0000 (10:03 +0900)
src/network/generator/network-generator.c

index 1c3d9a7e050916205c62827319967841d2d30c00..ab3d2d8860a9af3f7ada0cc1e749afbb44518362 100644 (file)
@@ -209,6 +209,11 @@ static Network* network_free(Network *network) {
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_free);
 
+DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
+                network_hash_ops,
+                char, string_hash_func, string_compare_func,
+                Network, network_free);
+
 static int network_new(Context *context, const char *name, Network **ret) {
         _cleanup_(network_freep) Network *network = NULL;
         _cleanup_free_ char *ifname = NULL;
@@ -234,7 +239,7 @@ static int network_new(Context *context, const char *name, Network **ret) {
                 .dhcp_use_dns = -1,
         };
 
-        r = hashmap_ensure_put(&context->networks_by_name, &string_hash_ops, network->ifname, network);
+        r = hashmap_ensure_put(&context->networks_by_name, &network_hash_ops, network->ifname, network);
         if (r < 0)
                 return r;
 
@@ -277,6 +282,11 @@ static NetDev* netdev_free(NetDev *netdev) {
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(NetDev*, netdev_free);
 
+DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
+                netdev_hash_ops,
+                char, string_hash_func, string_compare_func,
+                NetDev, netdev_free);
+
 static int netdev_new(Context *context, const char *_kind, const char *_ifname, NetDev **ret) {
         _cleanup_(netdev_freep) NetDev *netdev = NULL;
         _cleanup_free_ char *kind = NULL, *ifname = NULL;
@@ -306,7 +316,7 @@ static int netdev_new(Context *context, const char *_kind, const char *_ifname,
                 .ifname = TAKE_PTR(ifname),
         };
 
-        r = hashmap_ensure_put(&context->netdevs_by_name, &string_hash_ops, netdev->ifname, netdev);
+        r = hashmap_ensure_put(&context->netdevs_by_name, &netdev_hash_ops, netdev->ifname, netdev);
         if (r < 0)
                 return r;
 
@@ -355,6 +365,11 @@ static Link* link_free(Link *link) {
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_free);
 
+DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
+                link_hash_ops,
+                char, string_hash_func, string_compare_func,
+                Link, link_free);
+
 static int link_new(
                 Context *context,
                 const char *name,
@@ -398,7 +413,7 @@ static int link_new(
                 .mac = *mac,
         };
 
-        r = hashmap_ensure_put(&context->links_by_filename, &string_hash_ops, link->filename, link);
+        r = hashmap_ensure_put(&context->links_by_filename, &link_hash_ops, link->filename, link);
         if (r < 0)
                 return r;
 
@@ -1272,9 +1287,9 @@ void context_clear(Context *context) {
         if (!context)
                 return;
 
-        hashmap_free_with_destructor(context->networks_by_name, network_free);
-        hashmap_free_with_destructor(context->netdevs_by_name, netdev_free);
-        hashmap_free_with_destructor(context->links_by_filename, link_free);
+        hashmap_free(context->networks_by_name);
+        hashmap_free(context->netdevs_by_name);
+        hashmap_free(context->links_by_filename);
 }
 
 static int address_dump(Address *address, FILE *f) {