[PATCH v3 34/35] util: filecache: use VIR_AUTOFREE instead of VIR_FREE for scalar types

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

 



By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@xxxxxxxxx>
---
 src/util/virfilecache.c | 35 +++++++++++------------------------
 1 file changed, 11 insertions(+), 24 deletions(-)

diff --git a/src/util/virfilecache.c b/src/util/virfilecache.c
index 96ae96d..2927c68 100644
--- a/src/util/virfilecache.c
+++ b/src/util/virfilecache.c
@@ -100,18 +100,17 @@ static char *
 virFileCacheGetFileName(virFileCachePtr cache,
                         const char *name)
 {
-    char *file = NULL;
-    char *namehash = NULL;
+    VIR_AUTOFREE(char *) namehash = NULL;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
 
     if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, name, &namehash) < 0)
-        goto cleanup;
+        return NULL;
 
     if (virFileMakePath(cache->dir) < 0) {
         virReportSystemError(errno,
                              _("Unable to create directory '%s'"),
                              cache->dir);
-        goto cleanup;
+        return NULL;
     }
 
     virBufferAsprintf(&buf, "%s/%s", cache->dir, namehash);
@@ -120,13 +119,9 @@ virFileCacheGetFileName(virFileCachePtr cache,
         virBufferAsprintf(&buf, ".%s", cache->suffix);
 
     if (virBufferCheckError(&buf) < 0)
-        goto cleanup;
-
-    file = virBufferContentAndReset(&buf);
+        return NULL;
 
- cleanup:
-    VIR_FREE(namehash);
-    return file;
+    return virBufferContentAndReset(&buf);
 }
 
 
@@ -135,7 +130,7 @@ virFileCacheLoad(virFileCachePtr cache,
                  const char *name,
                  void **data)
 {
-    char *file = NULL;
+    VIR_AUTOFREE(char *) file = NULL;
     int ret = -1;
     void *loadData = NULL;
 
@@ -178,7 +173,6 @@ virFileCacheLoad(virFileCachePtr cache,
 
  cleanup:
     virObjectUnref(loadData);
-    VIR_FREE(file);
     return ret;
 }
 
@@ -188,20 +182,15 @@ virFileCacheSave(virFileCachePtr cache,
                  const char *name,
                  void *data)
 {
-    char *file = NULL;
-    int ret = -1;
+    VIR_AUTOFREE(char *) file = NULL;
 
     if (!(file = virFileCacheGetFileName(cache, name)))
-        return ret;
+        return -1;
 
     if (cache->handlers.saveFile(data, file, cache->priv) < 0)
-        goto cleanup;
-
-    ret = 0;
+        return -1;
 
- cleanup:
-    VIR_FREE(file);
-    return ret;
+    return 0;
 }
 
 
@@ -346,7 +335,7 @@ virFileCacheLookupByFunc(virFileCachePtr cache,
                          const void *iterData)
 {
     void *data = NULL;
-    char *name = NULL;
+    VIR_AUTOFREE(char *) name = NULL;
 
     virObjectLock(cache);
 
@@ -356,8 +345,6 @@ virFileCacheLookupByFunc(virFileCachePtr cache,
     virObjectRef(data);
     virObjectUnlock(cache);
 
-    VIR_FREE(name);
-
     return data;
 }
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux