[PATCHv3 05/17] conf: use VIR_(APPEND|DELETE)_ELEMENT for domain event lists

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

 



---
 src/conf/domain_event.c | 51 ++++++++-----------------------------------------
 src/qemu/qemu_domain.c  |  6 ++----
 2 files changed, 10 insertions(+), 47 deletions(-)

diff --git a/src/conf/domain_event.c b/src/conf/domain_event.c
index cf37308..5e18fa5 100644
--- a/src/conf/domain_event.c
+++ b/src/conf/domain_event.c
@@ -41,12 +41,12 @@ typedef virDomainMeta *virDomainMetaPtr;
 
 struct _virDomainEventCallbackList {
     unsigned int nextID;
-    unsigned int count;
+    size_t count;
     virDomainEventCallbackPtr *callbacks;
 };
 
 struct _virDomainEventQueue {
-    unsigned int count;
+    size_t count;
     virDomainEventPtr *events;
 };
 
@@ -171,18 +171,7 @@ virDomainEventCallbackListRemove(virConnectPtr conn,
                 (*freecb)(cbList->callbacks[i]->opaque);
             virObjectUnref(cbList->callbacks[i]->conn);
             VIR_FREE(cbList->callbacks[i]);
-
-            if (i < (cbList->count - 1))
-                memmove(cbList->callbacks + i,
-                        cbList->callbacks + i + 1,
-                        sizeof(*(cbList->callbacks)) *
-                                (cbList->count - (i + 1)));
-
-            if (VIR_REALLOC_N(cbList->callbacks,
-                              cbList->count - 1) < 0) {
-                ; /* Failure to reduce memory allocation isn't fatal */
-            }
-            cbList->count--;
+            VIR_DELETE_ELEMENT(cbList->callbacks, i, cbList->count);
 
             for (i = 0 ; i < cbList->count ; i++) {
                 if (!cbList->callbacks[i]->deleted)
@@ -221,18 +210,7 @@ virDomainEventCallbackListRemoveID(virConnectPtr conn,
                 (*freecb)(cbList->callbacks[i]->opaque);
             virObjectUnref(cbList->callbacks[i]->conn);
             VIR_FREE(cbList->callbacks[i]);
-
-            if (i < (cbList->count - 1))
-                memmove(cbList->callbacks + i,
-                        cbList->callbacks + i + 1,
-                        sizeof(*(cbList->callbacks)) *
-                                (cbList->count - (i + 1)));
-
-            if (VIR_REALLOC_N(cbList->callbacks,
-                              cbList->count - 1) < 0) {
-                ; /* Failure to reduce memory allocation isn't fatal */
-            }
-            cbList->count--;
+            VIR_DELETE_ELEMENT(cbList->callbacks, i, cbList->count);
 
             for (i = 0 ; i < cbList->count ; i++) {
                 if (!cbList->callbacks[i]->deleted)
@@ -311,13 +289,7 @@ virDomainEventCallbackListPurgeMarked(virDomainEventCallbackListPtr cbList)
                 (*freecb)(cbList->callbacks[i]->opaque);
             virObjectUnref(cbList->callbacks[i]->conn);
             VIR_FREE(cbList->callbacks[i]);
-
-            if (i < (cbList->count - 1))
-                memmove(cbList->callbacks + i,
-                        cbList->callbacks + i + 1,
-                        sizeof(*(cbList->callbacks)) *
-                                (cbList->count - (i + 1)));
-            cbList->count--;
+            VIR_DELETE_ELEMENT_INPLACE(cbList->callbacks, i, cbList->count);
             i--;
         }
     }
@@ -391,15 +363,11 @@ virDomainEventCallbackListAddID(virConnectPtr conn,
         event->dom->id = dom->id;
     }
 
-    /* Make space on list */
-    if (VIR_REALLOC_N(cbList->callbacks, cbList->count + 1) < 0)
+    /* add event to end of list */
+    if (VIR_APPEND_ELEMENT_COPY(cbList->callbacks, cbList->count, &event) < 0)
         goto no_memory;
 
     virObjectRef(event->conn);
-
-    cbList->callbacks[cbList->count] = event;
-    cbList->count++;
-
     event->callbackID = cbList->nextID++;
 
     for (i = 0 ; i < cbList->count ; i++) {
@@ -1192,14 +1160,11 @@ virDomainEventQueuePush(virDomainEventQueuePtr evtQueue,
     }
 
     /* Make space on queue */
-    if (VIR_REALLOC_N(evtQueue->events,
-                      evtQueue->count + 1) < 0) {
+    if (VIR_APPEND_ELEMENT(evtQueue->events, evtQueue->count, &event) < 0) {
         virReportOOMError();
         return -1;
     }
 
-    evtQueue->events[evtQueue->count] = event;
-    evtQueue->count++;
     return 0;
 }
 
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 8d8cf02..189511d 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -1989,10 +1989,8 @@ qemuDomainCleanupRemove(virDomainObjPtr vm,
 
     for (i = 0; i < priv->ncleanupCallbacks; i++) {
         if (priv->cleanupCallbacks[i] == cb) {
-            memmove(priv->cleanupCallbacks + i,
-                    priv->cleanupCallbacks + i + 1,
-                    priv->ncleanupCallbacks - i - 1);
-            priv->ncleanupCallbacks--;
+            VIR_DELETE_ELEMENT_INPLACE(priv->cleanupCallbacks,
+                                       i, priv->ncleanupCallbacks);
         }
     }
 
-- 
1.7.11.7

--
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]