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

Re: [libvirt] PATCH: 4 of 5: Remove virStoragePoolObjPtr linked list



This patch removes the linked list in the virStoragePoolObjPtr and
and virStorageVolDefPtr objects, and adds new virStoragePoolObjList
and virStorageVolDefList structs to track pools & vols as arrays. The
storage driver driver gets updated to account for this API change.

 storage_backend_disk.c    |   10 +
 storage_backend_fs.c      |   75 +++++--------
 storage_backend_iscsi.c   |    9 +
 storage_backend_logical.c |   11 +-
 storage_conf.c            |  165 ++++++++++++++++--------------
 storage_conf.h            |   37 ++++--
 storage_driver.c          |  253 ++++++++++++++++++++++------------------------
 7 files changed, 290 insertions(+), 270 deletions(-)

Daniel

diff -r 03f1715be403 src/storage_backend_disk.c
--- a/src/storage_backend_disk.c	Fri Oct 03 12:58:28 2008 +0100
+++ b/src/storage_backend_disk.c	Fri Oct 03 13:07:54 2008 +0100
@@ -185,9 +185,13 @@
             return -1;
         }
 
-        vol->next = pool->volumes;
-        pool->volumes = vol;
-        pool->nvolumes++;
+        if (VIR_REALLOC_N(pool->volumes.objs,
+                          pool->volumes.count+1) < 0) {
+            virStorageReportError(conn, VIR_ERR_NO_MEMORY, _("volume"));
+            virStorageVolDefFree(vol);
+            return -1;
+        }
+        pool->volumes.objs[pool->volumes.count++] = vol;
 
         /* Prepended path will be same for all partitions, so we can
          * strip the path to form a reasonable pool-unique name
diff -r 03f1715be403 src/storage_backend_fs.c
--- a/src/storage_backend_fs.c	Fri Oct 03 12:58:28 2008 +0100
+++ b/src/storage_backend_fs.c	Fri Oct 03 13:07:54 2008 +0100
@@ -822,6 +822,7 @@
     DIR *dir;
     struct dirent *ent;
     struct statvfs sb;
+    virStorageVolDefPtr vol = NULL;
 
     if (!(dir = opendir(pool->def->target.path))) {
         virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
@@ -831,61 +832,42 @@
     }
 
     while ((ent = readdir(dir)) != NULL) {
-        virStorageVolDefPtr vol;
         int ret;
 
-        if (VIR_ALLOC(vol) < 0) {
-            virStorageReportError(conn, VIR_ERR_NO_MEMORY,
-                                  "%s", _("volume"));
-            goto cleanup;
-        }
+        if (VIR_ALLOC(vol) < 0)
+            goto no_memory;
 
-        vol->name = strdup(ent->d_name);
-        if (vol->name == NULL) {
-            VIR_FREE(vol);
-            virStorageReportError(conn, VIR_ERR_NO_MEMORY,
-                                  "%s", _("volume name"));
-            goto cleanup;
-        }
+        if ((vol->name = strdup(ent->d_name)) == NULL)
+            goto no_memory;
 
         vol->target.format = VIR_STORAGE_VOL_RAW; /* Real value is filled in during probe */
         if (VIR_ALLOC_N(vol->target.path, strlen(pool->def->target.path) +
-                        1 + strlen(vol->name) + 1) < 0) {
-            VIR_FREE(vol->target.path);
-            VIR_FREE(vol);
-            virStorageReportError(conn, VIR_ERR_NO_MEMORY,
-                                  "%s", _("volume name"));
-            goto cleanup;
-        }
+                        1 + strlen(vol->name) + 1) < 0)
+            goto no_memory;
+
         strcpy(vol->target.path, pool->def->target.path);
         strcat(vol->target.path, "/");
         strcat(vol->target.path, vol->name);
-        if ((vol->key = strdup(vol->target.path)) == NULL) {
-            VIR_FREE(vol->name);
-            VIR_FREE(vol->target.path);
-            VIR_FREE(vol);
-            virStorageReportError(conn, VIR_ERR_NO_MEMORY,
-                                  "%s", _("volume key"));
-            goto cleanup;
+        if ((vol->key = strdup(vol->target.path)) == NULL)
+            goto no_memory;
+
+        if ((ret = virStorageBackendProbeFile(conn, vol) < 0)) {
+            if (ret == -1)
+                goto no_memory;
+            else {
+                /* Silently ignore non-regular files,
+                 * eg '.' '..', 'lost+found' */
+                virStorageVolDefFree(vol);
+                vol = NULL;
+                continue;
+            }
         }
 
-        if ((ret = virStorageBackendProbeFile(conn, vol) < 0)) {
-            VIR_FREE(vol->key);
-            VIR_FREE(vol->name);
-            VIR_FREE(vol->target.path);
-            VIR_FREE(vol);
-            if (ret == -1)
-                goto cleanup;
-            else
-                /* Silently ignore non-regular files,
-                 * eg '.' '..', 'lost+found' */
-                continue;
-        }
-
-        vol->next = pool->volumes;
-        pool->volumes = vol;
-        pool->nvolumes++;
-        continue;
+        if (VIR_REALLOC_N(pool->volumes.objs,
+                          pool->volumes.count+1) < 0)
+            goto no_memory;
+        pool->volumes.objs[pool->volumes.count++] = vol;
+        vol = NULL;
     }
     closedir(dir);
 
@@ -904,8 +886,13 @@
 
     return 0;
 
+no_memory:
+    virStorageReportError(conn, VIR_ERR_NO_MEMORY, NULL);
+    /* fallthrough */
+
  cleanup:
     closedir(dir);
+    virStorageVolDefFree(vol);
     virStoragePoolObjClearVols(pool);
     return -1;
 }
diff -r 03f1715be403 src/storage_backend_iscsi.c
--- a/src/storage_backend_iscsi.c	Fri Oct 03 12:58:28 2008 +0100
+++ b/src/storage_backend_iscsi.c	Fri Oct 03 13:07:54 2008 +0100
@@ -236,9 +236,12 @@
     pool->def->capacity += vol->capacity;
     pool->def->allocation += vol->allocation;
 
-    vol->next = pool->volumes;
-    pool->volumes = vol;
-    pool->nvolumes++;
+    if (VIR_REALLOC_N(pool->volumes.objs,
+                      pool->volumes.count+1) < 0) {
+        virStorageReportError(conn, VIR_ERR_NO_MEMORY, NULL);
+        goto cleanup;
+    }
+    pool->volumes.objs[pool->volumes.count++] = vol;
 
     close(fd);
 
diff -r 03f1715be403 src/storage_backend_logical.c
--- a/src/storage_backend_logical.c	Fri Oct 03 12:58:28 2008 +0100
+++ b/src/storage_backend_logical.c	Fri Oct 03 13:07:54 2008 +0100
@@ -119,12 +119,17 @@
 
         if ((vol->name = strdup(groups[0])) == NULL) {
             virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("volume"));
+            virStorageVolDefFree(vol);
             return -1;
         }
 
-        vol->next = pool->volumes;
-        pool->volumes = vol;
-        pool->nvolumes++;
+        if (VIR_REALLOC_N(pool->volumes.objs,
+                          pool->volumes.count + 1)) {
+            virStorageReportError(conn, VIR_ERR_NO_MEMORY, NULL);
+            virStorageVolDefFree(vol);
+            return -1;
+        }
+        pool->volumes.objs[pool->volumes.count++] = vol;
     }
 
     if (vol->target.path == NULL) {
diff -r 03f1715be403 src/storage_conf.c
--- a/src/storage_conf.c	Fri Oct 03 12:58:28 2008 +0100
+++ b/src/storage_conf.c	Fri Oct 03 13:07:54 2008 +0100
@@ -71,6 +71,10 @@
 void
 virStorageVolDefFree(virStorageVolDefPtr def) {
     int i;
+
+    if (!def)
+        return;
+
     VIR_FREE(def->name);
     VIR_FREE(def->key);
 
@@ -87,6 +91,9 @@
 void
 virStoragePoolDefFree(virStoragePoolDefPtr def) {
     int i;
+
+    if (!def)
+        return;
 
     VIR_FREE(def->name);
     VIR_FREE(def->source.host.name);
@@ -111,38 +118,48 @@
 
 void
 virStoragePoolObjFree(virStoragePoolObjPtr obj) {
-    if (obj->def)
-        virStoragePoolDefFree(obj->def);
-    if (obj->newDef)
-        virStoragePoolDefFree(obj->newDef);
+    if (!obj)
+        return;
+
+    virStoragePoolDefFree(obj->def);
+    virStoragePoolDefFree(obj->newDef);
 
     VIR_FREE(obj->configFile);
     VIR_FREE(obj->autostartLink);
     VIR_FREE(obj);
 }
 
+void virStoragePoolObjListFree(virStoragePoolObjListPtr pools)
+{
+    unsigned int i;
+    for (i = 0 ; i < pools->count ; i++)
+        virStoragePoolObjFree(pools->objs[i]);
+    VIR_FREE(pools->objs);
+    pools->count = 0;
+}
+
 void
-virStoragePoolObjRemove(virStorageDriverStatePtr driver,
+virStoragePoolObjRemove(virStoragePoolObjListPtr pools,
                         virStoragePoolObjPtr pool)
 {
-    virStoragePoolObjPtr prev = NULL, curr;
+    unsigned int i;
 
-    curr = driver->pools;
-    while (curr != pool) {
-        prev = curr;
-        curr = curr->next;
+    for (i = 0 ; i < pools->count ; i++) {
+        if (pools->objs[i] == pool) {
+            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--;
+
+            break;
+        }
     }
-
-    if (curr) {
-        if (prev)
-            prev->next = curr->next;
-        else
-            driver->pools = curr->next;
-
-        driver->ninactivePools--;
-    }
-
-    virStoragePoolObjFree(pool);
 }
 
 
@@ -925,29 +942,25 @@
 
 
 virStoragePoolObjPtr
-virStoragePoolObjFindByUUID(virStorageDriverStatePtr driver,
+virStoragePoolObjFindByUUID(virStoragePoolObjListPtr pools,
                             const unsigned char *uuid) {
-    virStoragePoolObjPtr pool = driver->pools;
+    unsigned int i;
 
-    while (pool) {
-        if (!memcmp(pool->def->uuid, uuid, VIR_UUID_BUFLEN))
-            return pool;
-        pool = pool->next;
-    }
+    for (i = 0 ; i < pools->count ; i++)
+        if (!memcmp(pools->objs[i]->def->uuid, uuid, VIR_UUID_BUFLEN))
+            return pools->objs[i];
 
     return NULL;
 }
 
 virStoragePoolObjPtr
-virStoragePoolObjFindByName(virStorageDriverStatePtr driver,
+virStoragePoolObjFindByName(virStoragePoolObjListPtr pools,
                             const char *name) {
-    virStoragePoolObjPtr pool = driver->pools;
+    unsigned int i;
 
-    while (pool) {
-        if (STREQ(pool->def->name, name))
-            return pool;
-        pool = pool->next;
-    }
+    for (i = 0 ; i < pools->count ; i++)
+        if (STREQ(pools->objs[i]->def->name, name))
+            return pools->objs[i];
 
     return NULL;
 }
@@ -955,26 +968,22 @@
 void
 virStoragePoolObjClearVols(virStoragePoolObjPtr pool)
 {
-    virStorageVolDefPtr vol = pool->volumes;
-    while (vol) {
-        virStorageVolDefPtr next = vol->next;
-        virStorageVolDefFree(vol);
-        vol = next;
-    }
-    pool->volumes = NULL;
-    pool->nvolumes = 0;
+    unsigned int i;
+    for (i = 0 ; i < pool->volumes.count ; i++)
+        virStorageVolDefFree(pool->volumes.objs[i]);
+
+    VIR_FREE(pool->volumes.objs);
+    pool->volumes.count = 0;
 }
 
 virStorageVolDefPtr
 virStorageVolDefFindByKey(virStoragePoolObjPtr pool,
                           const char *key) {
-    virStorageVolDefPtr vol = pool->volumes;
+    unsigned int i;
 
-    while (vol) {
-        if (STREQ(vol->key, key))
-            return vol;
-        vol = vol->next;
-    }
+    for (i = 0 ; i < pool->volumes.count ; i++)
+        if (STREQ(pool->volumes.objs[i]->key, key))
+            return pool->volumes.objs[i];
 
     return NULL;
 }
@@ -982,13 +991,11 @@
 virStorageVolDefPtr
 virStorageVolDefFindByPath(virStoragePoolObjPtr pool,
                            const char *path) {
-    virStorageVolDefPtr vol = pool->volumes;
+    unsigned int i;
 
-    while (vol) {
-        if (STREQ(vol->target.path, path))
-            return vol;
-        vol = vol->next;
-    }
+    for (i = 0 ; i < pool->volumes.count ; i++)
+        if (STREQ(pool->volumes.objs[i]->target.path, path))
+            return pool->volumes.objs[i];
 
     return NULL;
 }
@@ -996,24 +1003,22 @@
 virStorageVolDefPtr
 virStorageVolDefFindByName(virStoragePoolObjPtr pool,
                            const char *name) {
-    virStorageVolDefPtr vol = pool->volumes;
+    unsigned int i;
 
-    while (vol) {
-        if (STREQ(vol->name, name))
-            return vol;
-        vol = vol->next;
-    }
+    for (i = 0 ; i < pool->volumes.count ; i++)
+        if (STREQ(pool->volumes.objs[i]->name, name))
+            return pool->volumes.objs[i];
 
     return NULL;
 }
 
 virStoragePoolObjPtr
 virStoragePoolObjAssignDef(virConnectPtr conn,
-                           virStorageDriverStatePtr driver,
+                           virStoragePoolObjListPtr pools,
                            virStoragePoolDefPtr def) {
     virStoragePoolObjPtr pool;
 
-    if ((pool = virStoragePoolObjFindByName(driver, def->name))) {
+    if ((pool = virStoragePoolObjFindByName(pools, def->name))) {
         if (!virStoragePoolObjIsActive(pool)) {
             virStoragePoolDefFree(pool->def);
             pool->def = def;
@@ -1033,16 +1038,21 @@
 
     pool->active = 0;
     pool->def = def;
-    pool->next = driver->pools;
 
-    driver->pools = pool;
-    driver->ninactivePools++;
+    if (VIR_REALLOC_N(pools->objs, pools->count+1) < 0) {
+        pool->def = NULL;
+        virStoragePoolObjFree(pool);
+        virStorageReportError(conn, VIR_ERR_NO_MEMORY, NULL);
+        return NULL;
+    }
+    pools->objs[pools->count++] = pool;
 
     return pool;
 }
 
 static virStoragePoolObjPtr
-virStoragePoolObjLoad(virStorageDriverStatePtr driver,
+virStoragePoolObjLoad(virConnectPtr conn,
+                      virStoragePoolObjListPtr pools,
                       const char *file,
                       const char *path,
                       const char *xml,
@@ -1064,7 +1074,7 @@
         return NULL;
     }
 
-    if (!(pool = virStoragePoolObjAssignDef(NULL, driver, def))) {
+    if (!(pool = virStoragePoolObjAssignDef(conn, pools, def))) {
         virStorageLog("Failed to load storage pool config '%s': out of memory", path);
         virStoragePoolDefFree(def);
         return NULL;
@@ -1091,15 +1101,18 @@
 
 
 int
-virStoragePoolObjScanConfigs(virStorageDriverStatePtr driver) {
+virStoragePoolLoadAllConfigs(virConnectPtr conn,
+                             virStoragePoolObjListPtr pools,
+                             const char *configDir,
+                             const char *autostartDir) {
     DIR *dir;
     struct dirent *entry;
 
-    if (!(dir = opendir(driver->configDir))) {
+    if (!(dir = opendir(configDir))) {
         if (errno == ENOENT)
             return 0;
         virStorageLog("Failed to open dir '%s': %s",
-                      driver->configDir, strerror(errno));
+                      configDir, strerror(errno));
         return -1;
     }
 
@@ -1114,24 +1127,24 @@
         if (!virFileHasSuffix(entry->d_name, ".xml"))
             continue;
 
-        if (virFileBuildPath(driver->configDir, entry->d_name,
+        if (virFileBuildPath(configDir, entry->d_name,
                              NULL, path, PATH_MAX) < 0) {
             virStorageLog("Config filename '%s/%s' is too long",
-                          driver->configDir, entry->d_name);
+                          configDir, entry->d_name);
             continue;
         }
 
-        if (virFileBuildPath(driver->autostartDir, entry->d_name,
+        if (virFileBuildPath(autostartDir, entry->d_name,
                              NULL, autostartLink, PATH_MAX) < 0) {
             virStorageLog("Autostart link path '%s/%s' is too long",
-                          driver->autostartDir, entry->d_name);
+                          autostartDir, entry->d_name);
             continue;
         }
 
         if (virFileReadAll(path, 8192, &xml) < 0)
             continue;
 
-        virStoragePoolObjLoad(driver, entry->d_name, path, xml, autostartLink);
+        virStoragePoolObjLoad(conn, pools, entry->d_name, path, xml, autostartLink);
 
         VIR_FREE(xml);
     }
diff -r 03f1715be403 src/storage_conf.h
--- a/src/storage_conf.h	Fri Oct 03 12:58:28 2008 +0100
+++ b/src/storage_conf.h	Fri Oct 03 13:07:54 2008 +0100
@@ -87,10 +87,14 @@
 
     virStorageVolSource source;
     virStorageVolTarget target;
-
-    virStorageVolDefPtr next;
 };
 
+typedef struct _virStorageVolDefList virStorageVolDefList;
+typedef virStorageVolDefList *virStorageVolDefListPtr;
+struct _virStorageVolDefList {
+    unsigned int count;
+    virStorageVolDefPtr *objs;
+};
 
 
 
@@ -222,10 +226,14 @@
     virStoragePoolDefPtr def;
     virStoragePoolDefPtr newDef;
 
-    int nvolumes;
-    virStorageVolDefPtr volumes;
+    virStorageVolDefList volumes;
+};
 
-    virStoragePoolObjPtr next;
+typedef struct _virStoragePoolObjList virStoragePoolObjList;
+typedef virStoragePoolObjList *virStoragePoolObjListPtr;
+struct _virStoragePoolObjList {
+    unsigned int count;
+    virStoragePoolObjPtr *objs;
 };
 
 
@@ -235,9 +243,8 @@
 typedef virStorageDriverState *virStorageDriverStatePtr;
 
 struct _virStorageDriverState {
-    int nactivePools;
-    int ninactivePools;
-    virStoragePoolObjPtr pools;
+    virStoragePoolObjList pools;
+
     char *configDir;
     char *autostartDir;
 };
@@ -252,11 +259,14 @@
                            const char *fmt, ...)
     ATTRIBUTE_FORMAT(printf, 3, 4);
 
-int virStoragePoolObjScanConfigs(virStorageDriverStatePtr driver);
+int virStoragePoolLoadAllConfigs(virConnectPtr conn,
+                                 virStoragePoolObjListPtr pools,
+                                 const char *configDir,
+                                 const char *autostartDir);
 
-virStoragePoolObjPtr virStoragePoolObjFindByUUID(virStorageDriverStatePtr driver,
+virStoragePoolObjPtr virStoragePoolObjFindByUUID(virStoragePoolObjListPtr pools,
                                                  const unsigned char *uuid);
-virStoragePoolObjPtr virStoragePoolObjFindByName(virStorageDriverStatePtr driver,
+virStoragePoolObjPtr virStoragePoolObjFindByName(virStoragePoolObjListPtr pools,
                                                  const char *name);
 
 virStorageVolDefPtr virStorageVolDefFindByKey(virStoragePoolObjPtr pool,
@@ -283,7 +293,7 @@
                              virStorageVolDefPtr def);
 
 virStoragePoolObjPtr virStoragePoolObjAssignDef(virConnectPtr conn,
-                                                virStorageDriverStatePtr driver,
+                                                virStoragePoolObjListPtr pools,
                                                 virStoragePoolDefPtr def);
 
 int virStoragePoolObjSaveDef(virConnectPtr conn,
@@ -296,7 +306,8 @@
 void virStorageVolDefFree(virStorageVolDefPtr def);
 void virStoragePoolDefFree(virStoragePoolDefPtr def);
 void virStoragePoolObjFree(virStoragePoolObjPtr pool);
-void virStoragePoolObjRemove(virStorageDriverStatePtr driver,
+void virStoragePoolObjListFree(virStoragePoolObjListPtr pools);
+void virStoragePoolObjRemove(virStoragePoolObjListPtr pools,
                              virStoragePoolObjPtr pool);
 
 #endif /* __VIR_STORAGE_DRIVER_H__ */
diff -r 03f1715be403 src/storage_driver.c
--- a/src/storage_driver.c	Fri Oct 03 12:58:28 2008 +0100
+++ b/src/storage_driver.c	Fri Oct 03 13:07:54 2008 +0100
@@ -49,11 +49,10 @@
 
 static void
 storageDriverAutostart(virStorageDriverStatePtr driver) {
-    virStoragePoolObjPtr pool;
+    unsigned int i;
 
-    pool = driver->pools;
-    while (pool != NULL) {
-        virStoragePoolObjPtr next = pool->next;
+    for (i = 0 ; i < driver->pools.count ; i++) {
+        virStoragePoolObjPtr pool = driver->pools.objs[i];
 
         if (pool->autostart &&
             !virStoragePoolObjIsActive(pool)) {
@@ -61,7 +60,6 @@
             if ((backend = virStorageBackendForType(pool->def->type)) == NULL) {
                 storageLog("Missing backend %d",
                            pool->def->type);
-                pool = next;
                 continue;
             }
 
@@ -70,7 +68,6 @@
                 virErrorPtr err = virGetLastError();
                 storageLog("Failed to autostart storage pool '%s': %s",
                            pool->def->name, err ? err->message : NULL);
-                pool = next;
                 continue;
             }
 
@@ -80,15 +77,10 @@
                     backend->stopPool(NULL, pool);
                 storageLog("Failed to autostart storage pool '%s': %s",
                            pool->def->name, err ? err->message : NULL);
-                pool = next;
                 continue;
             }
             pool->active = 1;
-            driver->nactivePools++;
-            driver->ninactivePools--;
         }
-
-        pool = next;
     }
 }
 
@@ -149,7 +141,10 @@
     }
     */
 
-    if (virStoragePoolObjScanConfigs(driverState) < 0) {
+    if (virStoragePoolLoadAllConfigs(NULL,
+                                     &driverState->pools,
+                                     driverState->configDir,
+                                     driverState->autostartDir) < 0) {
         storageDriverShutdown();
         return -1;
     }
@@ -173,7 +168,13 @@
  */
 static int
 storageDriverReload(void) {
-    virStoragePoolObjScanConfigs(driverState);
+    if (!driverState)
+        return -1;
+
+    virStoragePoolLoadAllConfigs(NULL,
+                                 &driverState->pools,
+                                 driverState->configDir,
+                                 driverState->autostartDir);
     storageDriverAutostart(driverState);
 
     return 0;
@@ -188,11 +189,17 @@
  */
 static int
 storageDriverActive(void) {
+    unsigned int i;
+
+    if (!driverState)
+        return 0;
+
     /* If we've any active networks or guests, then we
      * mark this driver as active
      */
-    if (driverState->nactivePools)
-        return 1;
+    for (i = 0 ; i < driverState->pools.count ; i++)
+        if (virStoragePoolObjIsActive(driverState->pools.objs[i]))
+            return 1;
 
     /* Otherwise we're happy to deal with a shutdown */
     return 0;
@@ -205,15 +212,15 @@
  */
 static int
 storageDriverShutdown(void) {
-    virStoragePoolObjPtr pool;
+    unsigned int i;
 
     if (!driverState)
         return -1;
 
     /* shutdown active pools */
-    pool = driverState->pools;
-    while (pool) {
-        virStoragePoolObjPtr next = pool->next;
+    for (i = 0 ; i < driverState->pools.count ; i++) {
+        virStoragePoolObjPtr pool = driverState->pools.objs[i];
+
         if (virStoragePoolObjIsActive(pool)) {
             virStorageBackendPtr backend;
             if ((backend = virStorageBackendForType(pool->def->type)) == NULL) {
@@ -229,24 +236,14 @@
             }
             virStoragePoolObjClearVols(pool);
         }
-        pool = next;
     }
 
     /* free inactive pools */
-    pool = driverState->pools;
-    while (pool) {
-        virStoragePoolObjPtr next = pool->next;
-        virStoragePoolObjFree(pool);
-        pool = next;
-    }
-    driverState->pools = NULL;
-    driverState->nactivePools = 0;
-    driverState->ninactivePools = 0;
+    virStoragePoolObjListFree(&driverState->pools);
 
-    free(driverState->configDir);
-    free(driverState->autostartDir);
-    free(driverState);
-    driverState = NULL;
+    VIR_FREE(driverState->configDir);
+    VIR_FREE(driverState->autostartDir);
+    VIR_FREE(driverState);
 
     return 0;
 }
@@ -258,7 +255,7 @@
                         const unsigned char *uuid) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, uuid);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, uuid);
     virStoragePoolPtr ret;
 
     if (!pool) {
@@ -276,7 +273,7 @@
                         const char *name) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByName(driver, name);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByName(&driver->pools, name);
     virStoragePoolPtr ret;
 
     if (!pool) {
@@ -316,7 +313,13 @@
 storageNumPools(virConnectPtr conn) {
     virStorageDriverStatePtr driver
         = (virStorageDriverStatePtr)conn->storagePrivateData;
-    return driver->nactivePools;
+    unsigned int i, nactive = 0;
+
+    for (i = 0 ; i < driver->pools.count ; i++)
+        if (virStoragePoolObjIsActive(driver->pools.objs[i]))
+            nactive++;
+
+    return nactive;
 }
 
 static int
@@ -325,18 +328,17 @@
                  int nnames) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)conn->storagePrivateData;
-    virStoragePoolObjPtr pool = driver->pools;
     int got = 0, i;
-    while (pool && got < nnames) {
-        if (virStoragePoolObjIsActive(pool)) {
-            if (!(names[got] = strdup(pool->def->name))) {
+
+    for (i = 0 ; i < driver->pools.count && got < nnames ; i++) {
+        if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
+            if (!(names[got] = strdup(driver->pools.objs[i]->def->name))) {
                 virStorageReportError(conn, VIR_ERR_NO_MEMORY,
                                       "%s", _("names"));
                 goto cleanup;
             }
             got++;
         }
-        pool = pool->next;
     }
     return got;
 
@@ -353,7 +355,13 @@
 storageNumDefinedPools(virConnectPtr conn) {
     virStorageDriverStatePtr driver
         = (virStorageDriverStatePtr)conn->storagePrivateData;
-    return driver->ninactivePools;
+    unsigned int i, nactive = 0;
+
+    for (i = 0 ; i < driver->pools.count ; i++)
+        if (!virStoragePoolObjIsActive(driver->pools.objs[i]))
+            nactive++;
+
+    return nactive;
 }
 
 static int
@@ -362,18 +370,17 @@
                         int nnames) {
     virStorageDriverStatePtr driver
         = (virStorageDriverStatePtr)conn->storagePrivateData;
-    virStoragePoolObjPtr pool = driver->pools;
     int got = 0, i;
-    while (pool && got < nnames) {
-        if (!virStoragePoolObjIsActive(pool)) {
-            if (!(names[got] = strdup(pool->def->name))) {
+
+    for (i = 0 ; i < driver->pools.count && got < nnames ; i++) {
+        if (!virStoragePoolObjIsActive(driver->pools.objs[i])) {
+            if (!(names[got] = strdup(driver->pools.objs[i]->def->name))) {
                 virStorageReportError(conn, VIR_ERR_NO_MEMORY,
                                       "%s", _("names"));
                 goto cleanup;
             }
             got++;
         }
-        pool = pool->next;
     }
     return got;
 
@@ -424,8 +431,8 @@
     if (!(def = virStoragePoolDefParse(conn, xml, NULL)))
         return NULL;
 
-    if (virStoragePoolObjFindByUUID(driver, def->uuid) ||
-        virStoragePoolObjFindByName(driver, def->name)) {
+    if (virStoragePoolObjFindByUUID(&driver->pools, def->uuid) ||
+        virStoragePoolObjFindByName(&driver->pools, def->name)) {
         virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                               "%s", _("storage pool already exists"));
         virStoragePoolDefFree(def);
@@ -437,7 +444,7 @@
         return NULL;
     }
 
-    if (!(pool = virStoragePoolObjAssignDef(conn, driver, def))) {
+    if (!(pool = virStoragePoolObjAssignDef(conn, &driver->pools, def))) {
         virStoragePoolDefFree(def);
         return NULL;
     }
@@ -451,8 +458,6 @@
         return NULL;
     }
     pool->active = 1;
-    driver->nactivePools++;
-    driver->ninactivePools--;
 
     ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);
 
@@ -478,13 +483,13 @@
         return NULL;
     }
 
-    if (!(pool = virStoragePoolObjAssignDef(conn, driver, def))) {
+    if (!(pool = virStoragePoolObjAssignDef(conn, &driver->pools, def))) {
         virStoragePoolDefFree(def);
         return NULL;
     }
 
     if (virStoragePoolObjSaveDef(conn, driver, pool, def) < 0) {
-        virStoragePoolObjRemove(driver, pool);
+        virStoragePoolObjRemove(&driver->pools, pool);
         return NULL;
     }
 
@@ -496,7 +501,7 @@
 storagePoolUndefine(virStoragePoolPtr obj) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
 
     if (!pool) {
         virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
@@ -517,12 +522,10 @@
         storageLog("Failed to delete autostart link '%s': %s",
                    pool->autostartLink, strerror(errno));
 
-    free(pool->configFile);
-    pool->configFile = NULL;
-    free(pool->autostartLink);
-    pool->autostartLink = NULL;
+    VIR_FREE(pool->configFile);
+    VIR_FREE(pool->autostartLink);
 
-    virStoragePoolObjRemove(driver, pool);
+    virStoragePoolObjRemove(&driver->pools, pool);
 
     return 0;
 }
@@ -532,7 +535,7 @@
                  unsigned int flags ATTRIBUTE_UNUSED) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
     virStorageBackendPtr backend;
 
     if (!pool) {
@@ -560,8 +563,6 @@
     }
 
     pool->active = 1;
-    driver->nactivePools++;
-    driver->ninactivePools--;
 
     return 0;
 }
@@ -571,7 +572,7 @@
                  unsigned int flags) {
     virStorageDriverStatePtr driver
         = (virStorageDriverStatePtr)obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
     virStorageBackendPtr backend;
 
     if (!pool) {
@@ -602,7 +603,7 @@
 storagePoolDestroy(virStoragePoolPtr obj) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
     virStorageBackendPtr backend;
 
     if (!pool) {
@@ -628,11 +629,9 @@
     virStoragePoolObjClearVols(pool);
 
     pool->active = 0;
-    driver->nactivePools--;
-    driver->ninactivePools++;
 
     if (pool->configFile == NULL)
-        virStoragePoolObjRemove(driver, pool);
+        virStoragePoolObjRemove(&driver->pools, pool);
 
     return 0;
 }
@@ -643,7 +642,7 @@
                   unsigned int flags) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
     virStorageBackendPtr backend;
 
     if (!pool) {
@@ -679,7 +678,7 @@
                    unsigned int flags ATTRIBUTE_UNUSED) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
     virStorageBackendPtr backend;
     int ret = 0;
 
@@ -705,11 +704,9 @@
             backend->stopPool(obj->conn, pool);
 
         pool->active = 0;
-        driver->nactivePools--;
-        driver->ninactivePools++;
 
         if (pool->configFile == NULL)
-            virStoragePoolObjRemove(driver, pool);
+            virStoragePoolObjRemove(&driver->pools, pool);
     }
 
     return ret;
@@ -721,7 +718,7 @@
                    virStoragePoolInfoPtr info) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
     virStorageBackendPtr backend;
 
     if (!pool) {
@@ -751,7 +748,7 @@
                    unsigned int flags ATTRIBUTE_UNUSED) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
 
     if (!pool) {
         virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
@@ -767,7 +764,7 @@
                         int *autostart) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
 
     if (!pool) {
         virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
@@ -789,7 +786,7 @@
                         int autostart) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
 
     if (!pool) {
         virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
@@ -845,7 +842,7 @@
 storagePoolNumVolumes(virStoragePoolPtr obj) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
 
     if (!pool) {
         virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
@@ -859,7 +856,7 @@
         return -1;
     }
 
-    return pool->nvolumes;
+    return pool->volumes.count;
 }
 
 static int
@@ -868,9 +865,8 @@
                        int maxnames) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid);
-    int i = 0;
-    virStorageVolDefPtr vol;
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
+    int i, n = 0;
 
     if (!pool) {
         virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
@@ -885,25 +881,20 @@
     }
 
     memset(names, 0, maxnames);
-    vol = pool->volumes;
-    while (vol && i < maxnames) {
-        names[i] = strdup(vol->name);
-        if (names[i] == NULL) {
+    for (i = 0 ; i < pool->volumes.count && n < maxnames ; i++) {
+        if ((names[n++] = strdup(pool->volumes.objs[i]->name)) == NULL) {
             virStorageReportError(obj->conn, VIR_ERR_NO_MEMORY,
                                   "%s", _("name"));
             goto cleanup;
         }
-        vol = vol->next;
-        i++;
     }
 
-    return i;
+    return n;
 
  cleanup:
-    for (i = 0 ; i < maxnames ; i++) {
-        free(names[i]);
-        names[i] = NULL;
-    }
+    for (n = 0 ; n < maxnames ; n++)
+        VIR_FREE(names[i]);
+
     memset(names, 0, maxnames);
     return -1;
 }
@@ -914,7 +905,7 @@
                           const char *name) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
     virStorageVolDefPtr vol;
 
     if (!pool) {
@@ -946,19 +937,19 @@
                          const char *key) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)conn->storagePrivateData;
-    virStoragePoolObjPtr pool = driver->pools;
+    unsigned int i;
 
-    while (pool) {
-        if (virStoragePoolObjIsActive(pool)) {
-            virStorageVolDefPtr vol = virStorageVolDefFindByKey(pool, key);
+    for (i = 0 ; i < driver->pools.count ; i++) {
+        if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
+            virStorageVolDefPtr vol =
+                virStorageVolDefFindByKey(driver->pools.objs[i], key);
 
             if (vol)
                 return virGetStorageVol(conn,
-                                        pool->def->name,
+                                        driver->pools.objs[i]->def->name,
                                         vol->name,
                                         vol->key);
         }
-        pool = pool->next;
     }
 
     virStorageReportError(conn, VIR_ERR_INVALID_STORAGE_VOL,
@@ -971,19 +962,19 @@
                           const char *path) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)conn->storagePrivateData;
-    virStoragePoolObjPtr pool = driver->pools;
+    unsigned int i;
 
-    while (pool) {
-        if (virStoragePoolObjIsActive(pool)) {
-            virStorageVolDefPtr vol = virStorageVolDefFindByPath(pool, path);
+    for (i = 0 ; i < driver->pools.count ; i++) {
+        if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
+            virStorageVolDefPtr vol =
+                virStorageVolDefFindByPath(driver->pools.objs[i], path);
 
             if (vol)
                 return virGetStorageVol(conn,
-                                        pool->def->name,
+                                        driver->pools.objs[i]->def->name,
                                         vol->name,
                                         vol->key);
         }
-        pool = pool->next;
     }
 
     virStorageReportError(conn, VIR_ERR_INVALID_STORAGE_VOL,
@@ -997,7 +988,7 @@
                        unsigned int flags ATTRIBUTE_UNUSED) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(driver, obj->uuid);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
     virStorageBackendPtr backend;
     virStorageVolDefPtr vol;
 
@@ -1027,6 +1018,13 @@
         return NULL;
     }
 
+    if (VIR_REALLOC_N(pool->volumes.objs,
+                      pool->volumes.count+1) < 0) {
+        virStorageReportError(obj->conn, VIR_ERR_NO_MEMORY, NULL);
+        virStorageVolDefFree(vol);
+        return NULL;
+    }
+
     if (!backend->createVol) {
         virStorageReportError(obj->conn, VIR_ERR_NO_SUPPORT,
                               "%s", _("storage pool does not support volume creation"));
@@ -1039,9 +1037,7 @@
         return NULL;
     }
 
-    vol->next = pool->volumes;
-    pool->volumes = vol;
-    pool->nvolumes++;
+    pool->volumes.objs[pool->volumes.count++] = vol;
 
     return virGetStorageVol(obj->conn, pool->def->name, vol->name, vol->key);
 }
@@ -1051,9 +1047,10 @@
                     unsigned int flags) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByName(driver, obj->pool);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
     virStorageBackendPtr backend;
-    virStorageVolDefPtr vol, tmp, prev;
+    virStorageVolDefPtr vol;
+    unsigned int i;
 
     if (!pool) {
         virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
@@ -1089,22 +1086,22 @@
         return -1;
     }
 
-    prev = NULL;
-    tmp = pool->volumes;
-    while (tmp) {
-        if (tmp == vol) {
+    for (i = 0 ; i < pool->volumes.count ; i++) {
+        if (pool->volumes.objs[i] == vol) {
+            virStorageVolDefFree(vol);
+
+            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--;
+
             break;
         }
-            prev = tmp;
-            tmp = tmp->next;
     }
-    if (prev) {
-        prev->next = vol->next;
-    } else {
-        pool->volumes = vol->next;
-    }
-    pool->nvolumes--;
-    virStorageVolDefFree(vol);
 
     return 0;
 }
@@ -1114,7 +1111,7 @@
                      virStorageVolInfoPtr info) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByName(driver, obj->pool);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
     virStorageBackendPtr backend;
     virStorageVolDefPtr vol;
 
@@ -1158,7 +1155,7 @@
                         unsigned int flags ATTRIBUTE_UNUSED) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByName(driver, obj->pool);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
     virStorageBackendPtr backend;
     virStorageVolDefPtr vol;
 
@@ -1192,7 +1189,7 @@
 storageVolumeGetPath(virStorageVolPtr obj) {
     virStorageDriverStatePtr driver =
         (virStorageDriverStatePtr)obj->conn->storagePrivateData;
-    virStoragePoolObjPtr pool = virStoragePoolObjFindByName(driver, obj->pool);
+    virStoragePoolObjPtr pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
     virStorageVolDefPtr vol;
     char *ret;
 

-- 
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|


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