[PATCH 1/2] daemon: Rework remoteClientFreeFunc cleanup loops into C macro

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Rather than 'n' repetitive code segments, let's create a single macro
which will make the code easier to read.

Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx>
---
 daemon/remote.c | 120 +++++++++++++++-----------------------------------------
 1 file changed, 31 insertions(+), 89 deletions(-)

diff --git a/daemon/remote.c b/daemon/remote.c
index 1c9708c..6fdafce 100644
--- a/daemon/remote.c
+++ b/daemon/remote.c
@@ -1623,6 +1623,24 @@ void remoteRelayConnectionClosedEvent(virConnectPtr conn ATTRIBUTE_UNUSED, int r
                                   &msg);
 }
 
+#define CLEAN_EVENT_CALLBACK(conn, eventCallbacks, neventCallbacks, name)   \
+    do {                                                                    \
+        size_t i;                                                           \
+        for (i = 0; i < neventCallbacks; i++) {                             \
+            int callbackID = eventCallbacks[i]->callbackID;                 \
+            if (callbackID < 0) {                                           \
+                VIR_WARN("unexpected incomplete %s callback %zu", name, i); \
+                continue;                                                   \
+            }                                                               \
+            VIR_DEBUG("Deregistering remote %s event relay %d",             \
+                      name, callbackID);                                    \
+            eventCallbacks[i]->callbackID = -1;                             \
+            if (virConnectDomainEventDeregisterAny(conn, callbackID) < 0)   \
+                VIR_WARN("unexpected %s event deregister failure", name);   \
+        }                                                                   \
+        VIR_FREE(eventCallbacks);                                           \
+    } while (0);
+
 /*
  * You must hold lock for at least the client
  * We don't free stuff here, merely disconnect the client's
@@ -1637,98 +1655,21 @@ void remoteClientFreeFunc(void *data)
     /* Deregister event delivery callback */
     if (priv->conn) {
         virIdentityPtr sysident = virIdentityGetSystem();
-        size_t i;
 
         virIdentitySetCurrent(sysident);
 
-        for (i = 0; i < priv->ndomainEventCallbacks; i++) {
-            int callbackID = priv->domainEventCallbacks[i]->callbackID;
-            if (callbackID < 0) {
-                VIR_WARN("unexpected incomplete domain callback %zu", i);
-                continue;
-            }
-            VIR_DEBUG("Deregistering remote domain event relay %d",
-                      callbackID);
-            priv->domainEventCallbacks[i]->callbackID = -1;
-            if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
-                VIR_WARN("unexpected domain event deregister failure");
-        }
-        VIR_FREE(priv->domainEventCallbacks);
-
-        for (i = 0; i < priv->nnetworkEventCallbacks; i++) {
-            int callbackID = priv->networkEventCallbacks[i]->callbackID;
-            if (callbackID < 0) {
-                VIR_WARN("unexpected incomplete network callback %zu", i);
-                continue;
-            }
-            VIR_DEBUG("Deregistering remote network event relay %d",
-                      callbackID);
-            priv->networkEventCallbacks[i]->callbackID = -1;
-            if (virConnectNetworkEventDeregisterAny(priv->conn,
-                                                    callbackID) < 0)
-                VIR_WARN("unexpected network event deregister failure");
-        }
-        VIR_FREE(priv->networkEventCallbacks);
-
-        for (i = 0; i < priv->nstorageEventCallbacks; i++) {
-            int callbackID = priv->storageEventCallbacks[i]->callbackID;
-            if (callbackID < 0) {
-                VIR_WARN("unexpected incomplete storage pool callback %zu", i);
-                continue;
-            }
-            VIR_DEBUG("Deregistering remote storage pool event relay %d",
-                      callbackID);
-            priv->storageEventCallbacks[i]->callbackID = -1;
-            if (virConnectStoragePoolEventDeregisterAny(priv->conn,
-                                                        callbackID) < 0)
-                VIR_WARN("unexpected storage pool event deregister failure");
-        }
-        VIR_FREE(priv->storageEventCallbacks);
-
-        for (i = 0; i < priv->nnodeDeviceEventCallbacks; i++) {
-            int callbackID = priv->nodeDeviceEventCallbacks[i]->callbackID;
-            if (callbackID < 0) {
-                VIR_WARN("unexpected incomplete node device callback %zu", i);
-                continue;
-            }
-            VIR_DEBUG("Deregistering remote node device event relay %d",
-                      callbackID);
-            priv->nodeDeviceEventCallbacks[i]->callbackID = -1;
-            if (virConnectNodeDeviceEventDeregisterAny(priv->conn,
-                                                       callbackID) < 0)
-                VIR_WARN("unexpected node device event deregister failure");
-        }
-        VIR_FREE(priv->nodeDeviceEventCallbacks);
-
-        for (i = 0; i < priv->nsecretEventCallbacks; i++) {
-            int callbackID = priv->secretEventCallbacks[i]->callbackID;
-            if (callbackID < 0) {
-                VIR_WARN("unexpected incomplete secret callback %zu", i);
-                continue;
-            }
-            VIR_DEBUG("Deregistering remote secret event relay %d",
-                      callbackID);
-            priv->secretEventCallbacks[i]->callbackID = -1;
-            if (virConnectSecretEventDeregisterAny(priv->conn,
-                                                   callbackID) < 0)
-                VIR_WARN("unexpected secret event deregister failure");
-        }
-        VIR_FREE(priv->secretEventCallbacks);
-
-        for (i = 0; i < priv->nqemuEventCallbacks; i++) {
-            int callbackID = priv->qemuEventCallbacks[i]->callbackID;
-            if (callbackID < 0) {
-                VIR_WARN("unexpected incomplete qemu monitor callback %zu", i);
-                continue;
-            }
-            VIR_DEBUG("Deregistering remote qemu monitor event relay %d",
-                      callbackID);
-            priv->qemuEventCallbacks[i]->callbackID = -1;
-            if (virConnectDomainQemuMonitorEventDeregister(priv->conn,
-                                                           callbackID) < 0)
-                VIR_WARN("unexpected qemu monitor event deregister failure");
-        }
-        VIR_FREE(priv->qemuEventCallbacks);
+        CLEAN_EVENT_CALLBACK(priv->conn, priv->domainEventCallbacks,
+                             priv->ndomainEventCallbacks, "domain");
+        CLEAN_EVENT_CALLBACK(priv->conn, priv->networkEventCallbacks,
+                             priv->nnetworkEventCallbacks, "network");
+        CLEAN_EVENT_CALLBACK(priv->conn, priv->storageEventCallbacks,
+                             priv->nstorageEventCallbacks, "storage");
+        CLEAN_EVENT_CALLBACK(priv->conn, priv->nodeDeviceEventCallbacks,
+                             priv->nnodeDeviceEventCallbacks, "node device");
+        CLEAN_EVENT_CALLBACK(priv->conn, priv->secretEventCallbacks,
+                             priv->nsecretEventCallbacks, "secret");
+        CLEAN_EVENT_CALLBACK(priv->conn, priv->qemuEventCallbacks,
+                             priv->nqemuEventCallbacks, "qemu monitor");
 
         if (priv->closeRegistered) {
             if (virConnectUnregisterCloseCallback(priv->conn,
@@ -1744,6 +1685,7 @@ void remoteClientFreeFunc(void *data)
 
     VIR_FREE(priv);
 }
+#undef CLEAN_EVENT_CALLBACK
 
 
 static void remoteClientCloseFunc(virNetServerClientPtr client)
-- 
2.9.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]
  Powered by Linux