1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-22 17:34:18 +03:00

util: filecache: use VIR_AUTOFREE instead of VIR_FREE for scalar types

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@gmail.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
Sukrit Bhatnagar 2018-07-13 23:25:08 +05:30 committed by Erik Skultety
parent d2576d3bbe
commit c10ffc37a7

View File

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