99cbc7
From bd5cb5d5dfff79f092c843116876837d50b60250 Mon Sep 17 00:00:00 2001
99cbc7
Message-Id: <bd5cb5d5dfff79f092c843116876837d50b60250@dist-git>
99cbc7
From: Bing Niu <bing.niu@intel.com>
99cbc7
Date: Mon, 15 Apr 2019 17:32:46 +0200
99cbc7
Subject: [PATCH] util: Refactor virResctrlAllocFormat of virresctrl
99cbc7
MIME-Version: 1.0
99cbc7
Content-Type: text/plain; charset=UTF-8
99cbc7
Content-Transfer-Encoding: 8bit
99cbc7
99cbc7
Refactor virResctrlAllocFormat so that it is easy to support other
99cbc7
resource allocation technologies.
99cbc7
99cbc7
Signed-off-by: Bing Niu <bing.niu@intel.com>
99cbc7
Reviewed-by: John Ferlan <jferlan@redhat.com>
99cbc7
(cherry picked from commit 65bae2f18c27c4a30a8ea675b7c3767cdfbe4c70)
99cbc7
99cbc7
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1468650
99cbc7
99cbc7
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
99cbc7
Message-Id: <18dd3a283af009213d718a81f66fe625f6335ece.1555342313.git.phrdina@redhat.com>
99cbc7
Reviewed-by: Ján Tomko <jtomko@redhat.com>
99cbc7
---
99cbc7
 src/util/virresctrl.c | 41 ++++++++++++++++++++++++++---------------
99cbc7
 1 file changed, 26 insertions(+), 15 deletions(-)
99cbc7
99cbc7
diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
99cbc7
index 313f964f21..a38c9261b6 100644
99cbc7
--- a/src/util/virresctrl.c
99cbc7
+++ b/src/util/virresctrl.c
99cbc7
@@ -849,17 +849,14 @@ virResctrlAllocGetID(virResctrlAllocPtr alloc)
99cbc7
 }
99cbc7
 
99cbc7
 
99cbc7
-char *
99cbc7
-virResctrlAllocFormat(virResctrlAllocPtr alloc)
99cbc7
+static int
99cbc7
+virResctrlAllocFormatCache(virResctrlAllocPtr alloc,
99cbc7
+                           virBufferPtr buf)
99cbc7
 {
99cbc7
-    virBuffer buf = VIR_BUFFER_INITIALIZER;
99cbc7
     unsigned int level = 0;
99cbc7
     unsigned int type = 0;
99cbc7
     unsigned int cache = 0;
99cbc7
 
99cbc7
-    if (!alloc)
99cbc7
-        return NULL;
99cbc7
-
99cbc7
     for (level = 0; level < alloc->nlevels; level++) {
99cbc7
         virResctrlAllocPerLevelPtr a_level = alloc->levels[level];
99cbc7
 
99cbc7
@@ -872,7 +869,7 @@ virResctrlAllocFormat(virResctrlAllocPtr alloc)
99cbc7
             if (!a_type)
99cbc7
                 continue;
99cbc7
 
99cbc7
-            virBufferAsprintf(&buf, "L%u%s:", level, virResctrlTypeToString(type));
99cbc7
+            virBufferAsprintf(buf, "L%u%s:", level, virResctrlTypeToString(type));
99cbc7
 
99cbc7
             for (cache = 0; cache < a_type->nmasks; cache++) {
99cbc7
                 virBitmapPtr mask = a_type->masks[cache];
99cbc7
@@ -882,21 +879,35 @@ virResctrlAllocFormat(virResctrlAllocPtr alloc)
99cbc7
                     continue;
99cbc7
 
99cbc7
                 mask_str = virBitmapToString(mask, false, true);
99cbc7
-                if (!mask_str) {
99cbc7
-                    virBufferFreeAndReset(&buf;;
99cbc7
-                    return NULL;
99cbc7
-                }
99cbc7
+                if (!mask_str)
99cbc7
+                    return -1;
99cbc7
 
99cbc7
-                virBufferAsprintf(&buf, "%u=%s;", cache, mask_str);
99cbc7
+                virBufferAsprintf(buf, "%u=%s;", cache, mask_str);
99cbc7
                 VIR_FREE(mask_str);
99cbc7
             }
99cbc7
 
99cbc7
-            virBufferTrim(&buf, ";", 1);
99cbc7
-            virBufferAddChar(&buf, '\n');
99cbc7
+            virBufferTrim(buf, ";", 1);
99cbc7
+            virBufferAddChar(buf, '\n');
99cbc7
         }
99cbc7
     }
99cbc7
 
99cbc7
-    virBufferCheckError(&buf;;
99cbc7
+    return virBufferCheckError(buf);
99cbc7
+}
99cbc7
+
99cbc7
+
99cbc7
+char *
99cbc7
+virResctrlAllocFormat(virResctrlAllocPtr alloc)
99cbc7
+{
99cbc7
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
99cbc7
+
99cbc7
+    if (!alloc)
99cbc7
+        return NULL;
99cbc7
+
99cbc7
+    if (virResctrlAllocFormatCache(alloc, &buf) < 0) {
99cbc7
+        virBufferFreeAndReset(&buf;;
99cbc7
+        return NULL;
99cbc7
+    }
99cbc7
+
99cbc7
     return virBufferContentAndReset(&buf;;
99cbc7
 }
99cbc7
 
99cbc7
-- 
99cbc7
2.21.0
99cbc7