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

[libvirt] [PATCH 12/14] Remove pointless strdup in node suspend code



From: "Daniel P. Berrange" <berrange redhat com>

The command name for the suspend action does not need to be
strdup'd. The constant string can be used directly. This
also means the code can be trivially rearranged to make the
switch clearer

* src/util/virnodesuspend.c: Remove strdup of cmdString
---
 src/util/virnodesuspend.c |   49 +++++++++++++++-----------------------------
 1 files changed, 17 insertions(+), 32 deletions(-)

diff --git a/src/util/virnodesuspend.c b/src/util/virnodesuspend.c
index 75a8c2f..3805e9c 100644
--- a/src/util/virnodesuspend.c
+++ b/src/util/virnodesuspend.c
@@ -127,9 +127,7 @@ cleanup:
  */
 static void virNodeSuspend(void *cmdString)
 {
-    virCommandPtr suspendCmd = virCommandNew((char *)cmdString);
-
-    VIR_FREE(cmdString);
+    virCommandPtr suspendCmd = virCommandNew((const char *)cmdString);
 
     /*
      * Delay for sometime so that the function nodeSuspendForDuration()
@@ -183,7 +181,7 @@ int nodeSuspendForDuration(virConnectPtr conn ATTRIBUTE_UNUSED,
                            unsigned int flags)
 {
     static virThread thread;
-    char *cmdString = NULL;
+    const char *cmdString = NULL;
     int ret = -1;
     unsigned int supported;
 
@@ -209,40 +207,28 @@ int nodeSuspendForDuration(virConnectPtr conn ATTRIBUTE_UNUSED,
     /* Check if the host supports the requested suspend target */
     switch (target) {
     case VIR_NODE_SUSPEND_TARGET_MEM:
-        if (supported & (1 << VIR_NODE_SUSPEND_TARGET_MEM)) {
-            cmdString = strdup("pm-suspend");
-            if (cmdString == NULL) {
-                virReportOOMError();
-                goto cleanup;
-            }
-            break;
+        if (!(supported & (1 << VIR_NODE_SUSPEND_TARGET_MEM))) {
+            virNodeSuspendError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("Suspend-to-RAM"));
+            goto cleanup;
         }
-        virNodeSuspendError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("Suspend-to-RAM"));
-        goto cleanup;
+        cmdString = "pm-suspend";
+        break;
 
     case VIR_NODE_SUSPEND_TARGET_DISK:
-        if (supported & (1 << VIR_NODE_SUSPEND_TARGET_DISK)) {
-            cmdString = strdup("pm-hibernate");
-            if (cmdString == NULL) {
-                virReportOOMError();
-                goto cleanup;
-            }
-            break;
+        if (!(supported & (1 << VIR_NODE_SUSPEND_TARGET_DISK))) {
+            virNodeSuspendError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("Suspend-to-Disk"));
+            goto cleanup;
         }
-        virNodeSuspendError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("Suspend-to-Disk"));
-        goto cleanup;
+        cmdString = "pm-hibernate";
+        break;
 
     case VIR_NODE_SUSPEND_TARGET_HYBRID:
-        if (supported & (1 << VIR_NODE_SUSPEND_TARGET_HYBRID)) {
-            cmdString = strdup("pm-suspend-hybrid");
-            if (cmdString == NULL) {
-                virReportOOMError();
-                goto cleanup;
-            }
-            break;
+        if (!(supported & (1 << VIR_NODE_SUSPEND_TARGET_HYBRID))) {
+            virNodeSuspendError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("Hybrid-Suspend"));
+            goto cleanup;
         }
-        virNodeSuspendError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("Hybrid-Suspend"));
-        goto cleanup;
+        cmdString = "pm-suspend-hybrid";
+        break;
 
     default:
         virNodeSuspendError(VIR_ERR_INVALID_ARG, "%s", _("Invalid suspend target"));
@@ -262,7 +248,6 @@ int nodeSuspendForDuration(virConnectPtr conn ATTRIBUTE_UNUSED,
     ret = 0;
 cleanup:
     virNodeSuspendUnlock();
-    VIR_FREE(cmdString);
     return ret;
 }
 
-- 
1.7.6.4


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