[PATCH 12/13] src/xen: Utilize more of VIR_(APPEND|INSERT|DELETE)_ELEMENT

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

 



Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 src/xen/xen_driver.c  | 19 ++-----------------
 src/xen/xen_driver.h  |  2 +-
 src/xen/xen_inotify.c | 13 ++-----------
 src/xen/xm_internal.c | 18 +++---------------
 src/xen/xs_internal.c | 19 ++-----------------
 src/xen/xs_internal.h |  2 +-
 6 files changed, 11 insertions(+), 62 deletions(-)

diff --git a/src/xen/xen_driver.c b/src/xen/xen_driver.c
index ee05fb4..1880b22 100644
--- a/src/xen/xen_driver.c
+++ b/src/xen/xen_driver.c
@@ -2879,13 +2879,9 @@ xenUnifiedAddDomainInfo(xenUnifiedDomainInfoListPtr list,
     info->id = id;
 
     /* Make space on list */
-    n = list->count;
-    if (VIR_REALLOC_N(list->doms, n + 1) < 0) {
+    if (VIR_APPEND_ELEMENT(list->doms, list->count, info) < 0)
         goto error;
-    }
 
-    list->doms[n] = info;
-    list->count++;
     return 0;
 error:
     if (info)
@@ -2915,18 +2911,7 @@ xenUnifiedRemoveDomainInfo(xenUnifiedDomainInfoListPtr list,
             VIR_FREE(list->doms[i]->name);
             VIR_FREE(list->doms[i]);
 
-            if (i < (list->count - 1))
-                memmove(list->doms + i,
-                        list->doms + i + 1,
-                        sizeof(*(list->doms)) *
-                                (list->count - (i + 1)));
-
-            if (VIR_REALLOC_N(list->doms,
-                              list->count - 1) < 0) {
-                ; /* Failure to reduce memory allocation isn't fatal */
-            }
-            list->count--;
-
+            VIR_DELETE_ELEMENT(list->doms, i, list->count);
             return 0;
         }
     }
diff --git a/src/xen/xen_driver.h b/src/xen/xen_driver.h
index b8c1c27..54a1124 100644
--- a/src/xen/xen_driver.h
+++ b/src/xen/xen_driver.h
@@ -104,7 +104,7 @@ typedef struct _xenUnifiedDomainInfo xenUnifiedDomainInfo;
 typedef xenUnifiedDomainInfo *xenUnifiedDomainInfoPtr;
 
 struct _xenUnifiedDomainInfoList {
-    unsigned int count;
+    size_t count;
     xenUnifiedDomainInfoPtr *doms;
 };
 typedef struct _xenUnifiedDomainInfoList xenUnifiedDomainInfoList;
diff --git a/src/xen/xen_inotify.c b/src/xen/xen_inotify.c
index 2e9787f..a58509b 100644
--- a/src/xen/xen_inotify.c
+++ b/src/xen/xen_inotify.c
@@ -173,17 +173,8 @@ xenInotifyXendDomainsDirRemoveEntry(virConnectPtr conn, const char *fname)
             VIR_FREE(priv->configInfoList->doms[i]->name);
             VIR_FREE(priv->configInfoList->doms[i]);
 
-            if (i < (priv->configInfoList->count - 1))
-                memmove(priv->configInfoList->doms + i,
-                        priv->configInfoList->doms + i + 1,
-                        sizeof(*(priv->configInfoList->doms)) *
-                                (priv->configInfoList->count - (i + 1)));
-
-            if (VIR_REALLOC_N(priv->configInfoList->doms,
-                              priv->configInfoList->count - 1) < 0) {
-                ; /* Failure to reduce memory allocation isn't fatal */
-            }
-            priv->configInfoList->count--;
+            VIR_DELETE_ELEMENT(priv->configInfoList->doms, i,
+                               priv->configInfoList->count);
             return 0;
         }
     }
diff --git a/src/xen/xm_internal.c b/src/xen/xm_internal.c
index d9a3502..8037b2e 100644
--- a/src/xen/xm_internal.c
+++ b/src/xen/xm_internal.c
@@ -1262,10 +1262,8 @@ xenXMDomainAttachDeviceFlags(virConnectPtr conn,
 
     case VIR_DOMAIN_DEVICE_NET:
     {
-        if (VIR_REALLOC_N(def->nets, def->nnets+1) < 0)
+        if (VIR_APPEND_ELEMENT(def->nets, def->nnets, dev->data.net) < 0)
             goto cleanup;
-        def->nets[def->nnets++] = dev->data.net;
-        dev->data.net = NULL;
         break;
     }
 
@@ -1348,12 +1346,7 @@ xenXMDomainDetachDeviceFlags(virConnectPtr conn,
                 dev->data.disk->dst &&
                 STREQ(def->disks[i]->dst, dev->data.disk->dst)) {
                 virDomainDiskDefFree(def->disks[i]);
-                if (i < (def->ndisks - 1))
-                    memmove(def->disks + i,
-                            def->disks + i + 1,
-                            sizeof(*def->disks) *
-                            (def->ndisks - (i + 1)));
-                def->ndisks--;
+                VIR_DELETE_ELEMENT(def->disks, i, def->ndisks);
                 break;
             }
         }
@@ -1365,12 +1358,7 @@ xenXMDomainDetachDeviceFlags(virConnectPtr conn,
         for (i = 0; i < def->nnets; i++) {
             if (!virMacAddrCmp(&def->nets[i]->mac, &dev->data.net->mac)) {
                 virDomainNetDefFree(def->nets[i]);
-                if (i < (def->nnets - 1))
-                    memmove(def->nets + i,
-                            def->nets + i + 1,
-                            sizeof(*def->nets) *
-                            (def->nnets - (i + 1)));
-                def->nnets--;
+                VIR_DELETE_ELEMENT(def->nets, i, def->nnets);
                 break;
             }
         }
diff --git a/src/xen/xs_internal.c b/src/xen/xs_internal.c
index ccdb736..2d02e6e 100644
--- a/src/xen/xs_internal.c
+++ b/src/xen/xs_internal.c
@@ -666,14 +666,9 @@ xenStoreAddWatch(virConnectPtr conn,
         VIR_STRDUP(watch->token, token) < 0)
         goto error;
 
-    /* Make space on list */
-    n = list->count;
-    if (VIR_REALLOC_N(list->watches, n + 1) < 0)
+    if (VIR_APPEND_ELEMENT_COPY(list->watches, list->count, watch) < 0)
         goto error;
 
-    list->watches[n] = watch;
-    list->count++;
-
     return xs_watch(priv->xshandle, watch->path, watch->token);
 
   error:
@@ -719,17 +714,7 @@ xenStoreRemoveWatch(virConnectPtr conn, const char *path, const char *token)
             VIR_FREE(list->watches[i]->token);
             VIR_FREE(list->watches[i]);
 
-            if (i < (list->count - 1))
-                memmove(list->watches + i,
-                        list->watches + i + 1,
-                        sizeof(*(list->watches)) *
-                                (list->count - (i + 1)));
-
-            if (VIR_REALLOC_N(list->watches,
-                              list->count - 1) < 0) {
-                ; /* Failure to reduce memory allocation isn't fatal */
-            }
-            list->count--;
+            VIR_DELETE_ELEMENT(list->watches, i, list->count);
             return 0;
         }
     }
diff --git a/src/xen/xs_internal.h b/src/xen/xs_internal.h
index bc5bd8c..24e2627 100644
--- a/src/xen/xs_internal.h
+++ b/src/xen/xs_internal.h
@@ -71,7 +71,7 @@ typedef struct _xenStoreWatch xenStoreWatch;
 typedef xenStoreWatch *xenStoreWatchPtr;
 
 struct _xenStoreWatchList {
-    unsigned int count;
+    size_t count;
     xenStoreWatchPtr *watches;
 };
 typedef struct _xenStoreWatchList xenStoreWatchList;
-- 
1.9.0

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