[libvirt] [PATCHv8 12/17] conf: Remove virDomainResctrlAppend and introduce virDomainResctrlNew

Wang Huaqiang huaqiang.wang at intel.com
Mon Nov 12 13:31:43 UTC 2018


Introduced virDomainResctrlNew to do the most part of virDomainResctrlAppend
and move the operation of appending resctrl to @def->resctrls out of
function.

Rather than rely on virDomainResctrlAppend to perform the allocation, move
the onus to the caller and make use of virBitmapNewCopy for @vcpus and
virObjectRef for @alloc, thus removing the need to set each to NULL after the
call.

Signed-off-by: Wang Huaqiang <huaqiang.wang at intel.com>
---
 src/conf/domain_conf.c | 56 +++++++++++++++++++++++++++++---------------------
 1 file changed, 33 insertions(+), 23 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 237540b..8433214 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -18948,26 +18948,22 @@ virDomainCachetuneDefParseCache(xmlXPathContextPtr ctxt,
 }
 
 
-static int
-virDomainResctrlAppend(virDomainDefPtr def,
-                       xmlNodePtr node,
-                       virResctrlAllocPtr alloc,
-                       virBitmapPtr vcpus,
-                       unsigned int flags)
+static virDomainResctrlDefPtr
+virDomainResctrlNew(xmlNodePtr node,
+                    virResctrlAllocPtr alloc,
+                    virBitmapPtr vcpus,
+                    unsigned int flags)
 {
     char *vcpus_str = NULL;
     char *alloc_id = NULL;
-    virDomainResctrlDefPtr tmp_resctrl = NULL;
-    int ret = -1;
-
-    if (VIR_ALLOC(tmp_resctrl) < 0)
-        goto cleanup;
+    virDomainResctrlDefPtr resctrl = NULL;
+    virDomainResctrlDefPtr ret = NULL;
 
     /* We need to format it back because we need to be consistent in the naming
      * even when users specify some "sub-optimal" string there. */
     vcpus_str = virBitmapFormat(vcpus);
     if (!vcpus_str)
-        goto cleanup;
+        return NULL;
 
     if (!(flags & VIR_DOMAIN_DEF_PARSE_INACTIVE))
         alloc_id = virXMLPropString(node, "id");
@@ -18985,15 +18981,20 @@ virDomainResctrlAppend(virDomainDefPtr def,
     if (virResctrlAllocSetID(alloc, alloc_id) < 0)
         goto cleanup;
 
-    tmp_resctrl->vcpus = vcpus;
-    tmp_resctrl->alloc = alloc;
+    if (VIR_ALLOC(resctrl) < 0)
+        goto cleanup;
 
-    if (VIR_APPEND_ELEMENT(def->resctrls, def->nresctrls, tmp_resctrl) < 0)
+    if (!(resctrl->vcpus = virBitmapNewCopy(vcpus))) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("failed to copy 'vcpus'"));
         goto cleanup;
+    }
 
-    ret = 0;
+    resctrl->alloc = virObjectRef(alloc);
+
+    VIR_STEAL_PTR(ret, resctrl);
  cleanup:
-    virDomainResctrlDefFree(tmp_resctrl);
+    virDomainResctrlDefFree(resctrl);
     VIR_FREE(alloc_id);
     VIR_FREE(vcpus_str);
     return ret;
@@ -19010,6 +19011,7 @@ virDomainCachetuneDefParse(virDomainDefPtr def,
     xmlNodePtr *nodes = NULL;
     virBitmapPtr vcpus = NULL;
     virResctrlAllocPtr alloc = NULL;
+    virDomainResctrlDefPtr resctrl = NULL;
     ssize_t i = 0;
     int n;
     int ret = -1;
@@ -19048,19 +19050,22 @@ virDomainCachetuneDefParse(virDomainDefPtr def,
             goto cleanup;
     }
 
+    resctrl = virDomainResctrlNew(node, alloc, vcpus, flags);
+    if (!resctrl)
+        goto cleanup;
+
     if (virResctrlAllocIsEmpty(alloc)) {
         ret = 0;
         goto cleanup;
     }
 
-    if (virDomainResctrlAppend(def, node, alloc, vcpus, flags) < 0)
+    if (VIR_APPEND_ELEMENT(def->resctrls, def->nresctrls, resctrl) < 0)
         goto cleanup;
-    vcpus = NULL;
-    alloc = NULL;
 
     ret = 0;
  cleanup:
     ctxt->node = oldnode;
+    virDomainResctrlDefFree(resctrl);
     virObjectUnref(alloc);
     virBitmapFree(vcpus);
     VIR_FREE(nodes);
@@ -19218,6 +19223,8 @@ virDomainMemorytuneDefParse(virDomainDefPtr def,
     xmlNodePtr *nodes = NULL;
     virBitmapPtr vcpus = NULL;
     virResctrlAllocPtr alloc = NULL;
+    virDomainResctrlDefPtr resctrl = NULL;
+
     ssize_t i = 0;
     int n;
     int ret = -1;
@@ -19262,15 +19269,18 @@ virDomainMemorytuneDefParse(virDomainDefPtr def,
      * just update the existing alloc information, which is done in above
      * virDomainMemorytuneDefParseMemory */
     if (new_alloc) {
-        if (virDomainResctrlAppend(def, node, alloc, vcpus, flags) < 0)
+        resctrl = virDomainResctrlNew(node, alloc, vcpus, flags);
+        if (!resctrl)
+            goto cleanup;
+
+        if (VIR_APPEND_ELEMENT(def->resctrls, def->nresctrls, resctrl) < 0)
             goto cleanup;
-        vcpus = NULL;
-        alloc = NULL;
     }
 
     ret = 0;
  cleanup:
     ctxt->node = oldnode;
+    virDomainResctrlDefFree(resctrl);
     virObjectUnref(alloc);
     virBitmapFree(vcpus);
     VIR_FREE(nodes);
-- 
2.7.4




More information about the libvir-list mailing list