[libvirt] [PATCH 3/5] conf: add virDomainDefAddController()

Laine Stump laine at laine.org
Thu Nov 19 18:25:00 UTC 2015


We need a virDomainDefAddController() that doesn't check for an
existing controller at the same index (since USB2 controllers must be
added in sets of 4 that are all at the same index), so rather than
duplicating the code in virDomainDefMaybeAddController(), split it
into two functions, in the process eliminating existing duplicated
code that loops through the controller list by calling
virDomainControllerFind(), which does the same thing).
---
 src/conf/domain_conf.c | 56 +++++++++++++++++++++++++++++++++++---------------
 1 file changed, 40 insertions(+), 16 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 0ac7dbf..ab05e7f 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -13456,6 +13456,18 @@ virDomainControllerFind(virDomainDefPtr def,
 }
 
 
+static int
+virDomainControllerUnusedIndex(virDomainDefPtr def, int type)
+{
+    int idx = 0;
+
+    while (virDomainControllerFind(def, type, idx) >= 0)
+        idx++;
+
+    return idx;
+}
+
+
 const char *
 virDomainControllerAliasFind(virDomainDefPtr def,
                              int type, int idx)
@@ -14375,33 +14387,45 @@ virDomainEmulatorPinDefParseXML(xmlNodePtr node)
 }
 
 
-int
-virDomainDefMaybeAddController(virDomainDefPtr def,
-                               int type,
-                               int idx,
-                               int model)
+static virDomainControllerDefPtr
+virDomainDefAddController(virDomainDefPtr def, int type, int idx, int model)
 {
-    size_t i;
     virDomainControllerDefPtr cont;
 
-    for (i = 0; i < def->ncontrollers; i++) {
-        if (def->controllers[i]->type == type &&
-            def->controllers[i]->idx == idx)
-            return 0;
-    }
-
     if (!(cont = virDomainControllerDefNew(type)))
-        return -1;
+        return NULL;
+
+    if (idx < 0)
+        idx = virDomainControllerUnusedIndex(def, type);
 
     cont->idx = idx;
     cont->model = model;
 
-    if (VIR_APPEND_ELEMENT(def->controllers, def->ncontrollers, cont) < 0) {
+    if (VIR_APPEND_ELEMENT_COPY(def->controllers, def->ncontrollers, cont) < 0) {
         VIR_FREE(cont);
-        return -1;
+        return NULL;
     }
 
-    return 1;
+    return cont;
+}
+
+
+int
+virDomainDefMaybeAddController(virDomainDefPtr def,
+                               int type,
+                               int idx,
+                               int model)
+{
+    /* skip if a specific index was given and it is already
+     * in use for that type of controller
+     */
+    if (idx >= 0 && virDomainControllerFind(def, type, idx) >= 0)
+        return 0;
+
+    if (virDomainDefAddController(def, type, idx, model))
+        return 1;
+    else
+        return -1;
 }
 
 
-- 
2.4.3




More information about the libvir-list mailing list