util: hostmem: 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-24 21:22:35 +05:30 committed by Erik Skultety
parent 618f6a74da
commit a38abf267f

View File

@ -263,7 +263,7 @@ virHostMemGetStats(int cellNum ATTRIBUTE_UNUSED,
#ifdef __linux__ #ifdef __linux__
{ {
int ret; int ret;
char *meminfo_path = NULL; VIR_AUTOFREE(char *) meminfo_path = NULL;
FILE *meminfo; FILE *meminfo;
int max_node; int max_node;
@ -299,12 +299,10 @@ virHostMemGetStats(int cellNum ATTRIBUTE_UNUSED,
if (!meminfo) { if (!meminfo) {
virReportSystemError(errno, virReportSystemError(errno,
_("cannot open %s"), meminfo_path); _("cannot open %s"), meminfo_path);
VIR_FREE(meminfo_path);
return -1; return -1;
} }
ret = virHostMemGetStatsLinux(meminfo, cellNum, params, nparams); ret = virHostMemGetStatsLinux(meminfo, cellNum, params, nparams);
VIR_FORCE_FCLOSE(meminfo); VIR_FORCE_FCLOSE(meminfo);
VIR_FREE(meminfo_path);
return ret; return ret;
} }
@ -322,45 +320,36 @@ virHostMemGetStats(int cellNum ATTRIBUTE_UNUSED,
static int static int
virHostMemSetParameterValue(virTypedParameterPtr param) virHostMemSetParameterValue(virTypedParameterPtr param)
{ {
char *path = NULL; VIR_AUTOFREE(char *) path = NULL;
char *strval = NULL; VIR_AUTOFREE(char *) strval = NULL;
int ret = -1;
int rc = -1; int rc = -1;
char *field = strchr(param->field, '_'); char *field = strchr(param->field, '_');
sa_assert(field); sa_assert(field);
field++; field++;
if (virAsprintf(&path, "%s/%s", if (virAsprintf(&path, "%s/%s",
SYSFS_MEMORY_SHARED_PATH, field) < 0) { SYSFS_MEMORY_SHARED_PATH, field) < 0)
ret = -2; return -2;
goto cleanup;
}
if (virAsprintf(&strval, "%u", param->value.ui) == -1) { if (virAsprintf(&strval, "%u", param->value.ui) == -1)
ret = -2; return -2;
goto cleanup;
}
if ((rc = virFileWriteStr(path, strval, 0)) < 0) { if ((rc = virFileWriteStr(path, strval, 0)) < 0) {
virReportSystemError(-rc, _("failed to set %s"), param->field); virReportSystemError(-rc, _("failed to set %s"), param->field);
goto cleanup; return -1;
} }
ret = 0; return 0;
cleanup:
VIR_FREE(path);
VIR_FREE(strval);
return ret;
} }
static bool static bool
virHostMemParametersAreAllSupported(virTypedParameterPtr params, virHostMemParametersAreAllSupported(virTypedParameterPtr params,
int nparams) int nparams)
{ {
char *path = NULL;
size_t i; size_t i;
for (i = 0; i < nparams; i++) { for (i = 0; i < nparams; i++) {
VIR_AUTOFREE(char *) path = NULL;
virTypedParameterPtr param = &params[i]; virTypedParameterPtr param = &params[i];
char *field = strchr(param->field, '_'); char *field = strchr(param->field, '_');
@ -374,11 +363,8 @@ virHostMemParametersAreAllSupported(virTypedParameterPtr params,
virReportError(VIR_ERR_OPERATION_INVALID, virReportError(VIR_ERR_OPERATION_INVALID,
_("Parameter '%s' is not supported by " _("Parameter '%s' is not supported by "
"this kernel"), param->field); "this kernel"), param->field);
VIR_FREE(path);
return false; return false;
} }
VIR_FREE(path);
} }
return true; return true;
@ -430,23 +416,20 @@ static int
virHostMemGetParameterValue(const char *field, virHostMemGetParameterValue(const char *field,
void *value) void *value)
{ {
char *path = NULL; VIR_AUTOFREE(char *) path = NULL;
char *buf = NULL; VIR_AUTOFREE(char *) buf = NULL;
char *tmp = NULL; char *tmp = NULL;
int ret = -1;
int rc = -1; int rc = -1;
if (virAsprintf(&path, "%s/%s", if (virAsprintf(&path, "%s/%s",
SYSFS_MEMORY_SHARED_PATH, field) < 0) SYSFS_MEMORY_SHARED_PATH, field) < 0)
goto cleanup; return -1;
if (!virFileExists(path)) { if (!virFileExists(path))
ret = -2; return -2;
goto cleanup;
}
if (virFileReadAll(path, 1024, &buf) < 0) if (virFileReadAll(path, 1024, &buf) < 0)
goto cleanup; return -1;
if ((tmp = strchr(buf, '\n'))) if ((tmp = strchr(buf, '\n')))
*tmp = '\0'; *tmp = '\0';
@ -465,14 +448,10 @@ virHostMemGetParameterValue(const char *field,
if (rc < 0) { if (rc < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("failed to parse %s"), field); _("failed to parse %s"), field);
goto cleanup; return -1;
} }
ret = 0; return 0;
cleanup:
VIR_FREE(path);
VIR_FREE(buf);
return ret;
} }
#endif #endif