[libvirt] [PATCHv2 07/11] conf: use VIR_DELETE_ELEMENTS_N for conf object lists

Laine Stump laine at laine.org
Tue Oct 23 17:46:53 UTC 2012


The volume object list is also directly manipulated in the storage
driver, test storage driver, and parallels storage driver (this should
probably be consolidated).
---
 src/conf/interface_conf.c         | 14 +++-----------
 src/conf/interface_conf.h         |  4 ++--
 src/conf/network_conf.c           | 11 +----------
 src/conf/network_conf.h           |  2 +-
 src/conf/node_device_conf.c       | 11 +----------
 src/conf/node_device_conf.h       |  4 ++--
 src/conf/nwfilter_conf.c          | 12 ++----------
 src/conf/nwfilter_conf.h          |  2 +-
 src/conf/storage_conf.c           | 11 +----------
 src/conf/storage_conf.h           |  4 ++--
 src/parallels/parallels_storage.c | 15 ++-------------
 src/storage/storage_backend_rbd.c |  2 +-
 src/storage/storage_driver.c      | 13 ++-----------
 src/test/test_driver.c            | 15 ++-------------
 14 files changed, 23 insertions(+), 97 deletions(-)

diff --git a/src/conf/interface_conf.c b/src/conf/interface_conf.c
index 31b9219..73a95e9 100644
--- a/src/conf/interface_conf.c
+++ b/src/conf/interface_conf.c
@@ -1,7 +1,7 @@
 /*
  * interface_conf.c: interfaces XML handling
  *
- * Copyright (C) 2006-2010 Red Hat, Inc.
+ * Copyright (C) 2006-2012 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -1313,16 +1313,8 @@ void virInterfaceRemove(virInterfaceObjListPtr interfaces,
         if (interfaces->objs[i] == iface) {
             virInterfaceObjUnlock(interfaces->objs[i]);
             virInterfaceObjFree(interfaces->objs[i]);
-
-            if (i < (interfaces->count - 1))
-                memmove(interfaces->objs + i, interfaces->objs + i + 1,
-                        sizeof(*(interfaces->objs)) * (interfaces->count - (i + 1)));
-
-            if (VIR_REALLOC_N(interfaces->objs, interfaces->count - 1) < 0) {
-                ; /* Failure to reduce memory allocation isn't fatal */
-            }
-            interfaces->count--;
-
+            ignore_value(VIR_DELETE_ELEMENTS_N(interfaces->objs, i,
+                                               interfaces->count, 1));
             break;
         }
         virInterfaceObjUnlock(interfaces->objs[i]);
diff --git a/src/conf/interface_conf.h b/src/conf/interface_conf.h
index 1749629..bcfca62 100644
--- a/src/conf/interface_conf.h
+++ b/src/conf/interface_conf.h
@@ -1,7 +1,7 @@
 /*
  * interface_conf.h: interface XML handling entry points
  *
- * Copyright (C) 2006-2009 Red Hat, Inc.
+ * Copyright (C) 2006-2012 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -171,7 +171,7 @@ struct _virInterfaceObj {
 typedef struct _virInterfaceObjList virInterfaceObjList;
 typedef virInterfaceObjList *virInterfaceObjListPtr;
 struct _virInterfaceObjList {
-    unsigned int count;
+    size_t count;
     virInterfaceObjPtr *objs;
 };
 
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index a8c42a0..b07b82d 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -494,16 +494,7 @@ void virNetworkRemoveInactive(virNetworkObjListPtr nets,
         if (nets->objs[i] == net) {
             virNetworkObjUnlock(nets->objs[i]);
             virNetworkObjFree(nets->objs[i]);
-
-            if (i < (nets->count - 1))
-                memmove(nets->objs + i, nets->objs + i + 1,
-                        sizeof(*(nets->objs)) * (nets->count - (i + 1)));
-
-            if (VIR_REALLOC_N(nets->objs, nets->count - 1) < 0) {
-                ; /* Failure to reduce memory allocation isn't fatal */
-            }
-            nets->count--;
-
+            ignore_value(VIR_DELETE_ELEMENTS_N(nets->objs, i, nets->count, 1));
             break;
         }
         virNetworkObjUnlock(nets->objs[i]);
diff --git a/src/conf/network_conf.h b/src/conf/network_conf.h
index 7d90a19..01c975d 100644
--- a/src/conf/network_conf.h
+++ b/src/conf/network_conf.h
@@ -228,7 +228,7 @@ struct _virNetworkObj {
 typedef struct _virNetworkObjList virNetworkObjList;
 typedef virNetworkObjList *virNetworkObjListPtr;
 struct _virNetworkObjList {
-    unsigned int count;
+    size_t count;
     virNetworkObjPtr *objs;
 };
 
diff --git a/src/conf/node_device_conf.c b/src/conf/node_device_conf.c
index 41fa8e4..ab2b263 100644
--- a/src/conf/node_device_conf.c
+++ b/src/conf/node_device_conf.c
@@ -218,16 +218,7 @@ void virNodeDeviceObjRemove(virNodeDeviceObjListPtr devs,
         if (devs->objs[i] == dev) {
             virNodeDeviceObjUnlock(dev);
             virNodeDeviceObjFree(devs->objs[i]);
-
-            if (i < (devs->count - 1))
-                memmove(devs->objs + i, devs->objs + i + 1,
-                        sizeof(*(devs->objs)) * (devs->count - (i + 1)));
-
-            if (VIR_REALLOC_N(devs->objs, devs->count - 1) < 0) {
-                ; /* Failure to reduce memory allocation isn't fatal */
-            }
-            devs->count--;
-
+            ignore_value(VIR_DELETE_ELEMENTS_N(devs->objs, i, devs->count, 1));
             break;
         }
         virNodeDeviceObjUnlock(dev);
diff --git a/src/conf/node_device_conf.h b/src/conf/node_device_conf.h
index e394042..ee02991 100644
--- a/src/conf/node_device_conf.h
+++ b/src/conf/node_device_conf.h
@@ -1,7 +1,7 @@
 /*
  * node_device_conf.h: config handling for node devices
  *
- * Copyright (C) 2010-2011 Red Hat, Inc.
+ * Copyright (C) 2010-2012 Red Hat, Inc.
  * Copyright (C) 2008 Virtual Iron Software, Inc.
  * Copyright (C) 2008 David F. Lively
  *
@@ -199,7 +199,7 @@ struct _virNodeDeviceObj {
 typedef struct _virNodeDeviceObjList virNodeDeviceObjList;
 typedef virNodeDeviceObjList *virNodeDeviceObjListPtr;
 struct _virNodeDeviceObjList {
-    unsigned int count;
+    size_t count;
     virNodeDeviceObjPtr *objs;
 };
 
diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c
index 27dbee8..3f7c49e 100644
--- a/src/conf/nwfilter_conf.c
+++ b/src/conf/nwfilter_conf.c
@@ -426,16 +426,8 @@ virNWFilterObjRemove(virNWFilterObjListPtr nwfilters,
         if (nwfilters->objs[i] == nwfilter) {
             virNWFilterObjUnlock(nwfilters->objs[i]);
             virNWFilterObjFree(nwfilters->objs[i]);
-
-            if (i < (nwfilters->count - 1))
-                memmove(nwfilters->objs + i, nwfilters->objs + i + 1,
-                        sizeof(*(nwfilters->objs)) * (nwfilters->count - (i + 1)));
-
-            if (VIR_REALLOC_N(nwfilters->objs, nwfilters->count - 1) < 0) {
-                ; /* Failure to reduce memory allocation isn't fatal */
-            }
-            nwfilters->count--;
-
+            ignore_value(VIR_DELETE_ELEMENTS_N(nwfilters->objs, i,
+                                               nwfilters->count, 1));
             break;
         }
         virNWFilterObjUnlock(nwfilters->objs[i]);
diff --git a/src/conf/nwfilter_conf.h b/src/conf/nwfilter_conf.h
index 805fbe7..4a2a9f3 100644
--- a/src/conf/nwfilter_conf.h
+++ b/src/conf/nwfilter_conf.h
@@ -547,7 +547,7 @@ struct _virNWFilterObj {
 typedef struct _virNWFilterObjList virNWFilterObjList;
 typedef virNWFilterObjList *virNWFilterObjListPtr;
 struct _virNWFilterObjList {
-    unsigned int count;
+    size_t count;
     virNWFilterObjPtr *objs;
 };
 
diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c
index 5d9e61a..4076720 100644
--- a/src/conf/storage_conf.c
+++ b/src/conf/storage_conf.c
@@ -402,16 +402,7 @@ virStoragePoolObjRemove(virStoragePoolObjListPtr pools,
         if (pools->objs[i] == pool) {
             virStoragePoolObjUnlock(pools->objs[i]);
             virStoragePoolObjFree(pools->objs[i]);
-
-            if (i < (pools->count - 1))
-                memmove(pools->objs + i, pools->objs + i + 1,
-                        sizeof(*(pools->objs)) * (pools->count - (i + 1)));
-
-            if (VIR_REALLOC_N(pools->objs, pools->count - 1) < 0) {
-                ; /* Failure to reduce memory allocation isn't fatal */
-            }
-            pools->count--;
-
+            ignore_value(VIR_DELETE_ELEMENTS_N(pools->objs, i, pools->count, 1));
             break;
         }
         virStoragePoolObjUnlock(pools->objs[i]);
diff --git a/src/conf/storage_conf.h b/src/conf/storage_conf.h
index d509b13..24ece24 100644
--- a/src/conf/storage_conf.h
+++ b/src/conf/storage_conf.h
@@ -116,7 +116,7 @@ struct _virStorageVolDef {
 typedef struct _virStorageVolDefList virStorageVolDefList;
 typedef virStorageVolDefList *virStorageVolDefListPtr;
 struct _virStorageVolDefList {
-    unsigned int count;
+    size_t count;
     virStorageVolDefPtr *objs;
 };
 
@@ -318,7 +318,7 @@ struct _virStoragePoolObj {
 typedef struct _virStoragePoolObjList virStoragePoolObjList;
 typedef virStoragePoolObjList *virStoragePoolObjListPtr;
 struct _virStoragePoolObjList {
-    unsigned int count;
+    size_t count;
     virStoragePoolObjPtr *objs;
 };
 
diff --git a/src/parallels/parallels_storage.c b/src/parallels/parallels_storage.c
index 112e288..c542345 100644
--- a/src/parallels/parallels_storage.c
+++ b/src/parallels/parallels_storage.c
@@ -1178,19 +1178,8 @@ parallelsStorageVolumeDelete(virStorageVolPtr vol, unsigned int flags)
             }
 
             virStorageVolDefFree(privvol);
-
-            if (i < (privpool->volumes.count - 1))
-                memmove(privpool->volumes.objs + i,
-                        privpool->volumes.objs + i + 1,
-                        sizeof(*(privpool->volumes.objs)) *
-                        (privpool->volumes.count - (i + 1)));
-
-            if (VIR_REALLOC_N(privpool->volumes.objs,
-                              privpool->volumes.count - 1) < 0) {
-                ;   /* Failure to reduce memory allocation isn't fatal */
-            }
-            privpool->volumes.count--;
-
+            ignore_value(VIR_DELETE_ELEMENTS_N(privpool->volumes.objs, i,
+                                               privpool->volumes.count, 1));
             break;
         }
     }
diff --git a/src/storage/storage_backend_rbd.c b/src/storage/storage_backend_rbd.c
index 0c9bdcc..935cb74 100644
--- a/src/storage/storage_backend_rbd.c
+++ b/src/storage/storage_backend_rbd.c
@@ -345,7 +345,7 @@ static int virStorageBackendRBDRefreshPool(virConnectPtr conn ATTRIBUTE_UNUSED,
         pool->volumes.objs[pool->volumes.count++] = vol;
     }
 
-    VIR_DEBUG("Found %d images in RBD pool %s",
+    VIR_DEBUG("Found %zu images in RBD pool %s",
               pool->volumes.count, pool->def->source.name);
 
     ret = 0;
diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c
index 28829d3..890a1f8 100644
--- a/src/storage/storage_driver.c
+++ b/src/storage/storage_driver.c
@@ -2193,17 +2193,8 @@ storageVolumeDelete(virStorageVolPtr obj,
             VIR_INFO("Deleting volume '%s' from storage pool '%s'",
                      vol->name, pool->def->name);
             virStorageVolDefFree(vol);
-            vol = NULL;
-
-            if (i < (pool->volumes.count - 1))
-                memmove(pool->volumes.objs + i, pool->volumes.objs + i + 1,
-                        sizeof(*(pool->volumes.objs)) * (pool->volumes.count - (i + 1)));
-
-            if (VIR_REALLOC_N(pool->volumes.objs, pool->volumes.count - 1) < 0) {
-                ; /* Failure to reduce memory allocation isn't fatal */
-            }
-            pool->volumes.count--;
-
+            ignore_value(VIR_DELETE_ELEMENTS_N(pool->volumes.objs, i,
+                                               pool->volumes.count, 1));
             break;
         }
     }
diff --git a/src/test/test_driver.c b/src/test/test_driver.c
index c9f9115..025e1e9 100644
--- a/src/test/test_driver.c
+++ b/src/test/test_driver.c
@@ -5024,19 +5024,8 @@ testStorageVolumeDelete(virStorageVolPtr vol,
     for (i = 0 ; i < privpool->volumes.count ; i++) {
         if (privpool->volumes.objs[i] == privvol) {
             virStorageVolDefFree(privvol);
-
-            if (i < (privpool->volumes.count - 1))
-                memmove(privpool->volumes.objs + i,
-                        privpool->volumes.objs + i + 1,
-                        sizeof(*(privpool->volumes.objs)) *
-                                (privpool->volumes.count - (i + 1)));
-
-            if (VIR_REALLOC_N(privpool->volumes.objs,
-                              privpool->volumes.count - 1) < 0) {
-                ; /* Failure to reduce memory allocation isn't fatal */
-            }
-            privpool->volumes.count--;
-
+            ignore_value(VIR_DELETE_ELEMENTS_N(privpool->volumes.objs, i,
+                                               privpool->volumes.count, 1));
             break;
         }
     }
-- 
1.7.11.7




More information about the libvir-list mailing list