[libvirt] [PATCH v1 09/40] util: cgroup: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-21 Thread Sukrit Bhatnagar
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 
---
 src/util/vircgroup.c | 528 ++-
 1 file changed, 181 insertions(+), 347 deletions(-)

diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index bc5f774..4bb4408 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -162,7 +162,7 @@ virCgroupPartitionNeedsEscaping(const char *path)
 {
 FILE *fp = NULL;
 int ret = 0;
-char *line = NULL;
+VIR_AUTOFREE(char *) line = NULL;
 size_t buflen;
 
 /* If it starts with 'cgroup.' or a '_' of any
@@ -222,7 +222,6 @@ virCgroupPartitionNeedsEscaping(const char *path)
 }
 
  cleanup:
-VIR_FREE(line);
 VIR_FORCE_FCLOSE(fp);
 return ret;
 }
@@ -255,41 +254,40 @@ virCgroupValidateMachineGroup(virCgroupPtr group,
   const char *machinename)
 {
 size_t i;
-bool valid = false;
-char *partname = NULL;
-char *scopename_old = NULL;
-char *scopename_new = NULL;
-char *partmachinename = NULL;
+VIR_AUTOFREE(char *) partname = NULL;
+VIR_AUTOFREE(char *) scopename_old = NULL;
+VIR_AUTOFREE(char *) scopename_new = NULL;
+VIR_AUTOFREE(char *) partmachinename = NULL;
 
 if (virAsprintf(&partname, "%s.libvirt-%s",
 name, drivername) < 0)
-goto cleanup;
+return false;
 
 if (virCgroupPartitionEscape(&partname) < 0)
-goto cleanup;
+return false;
 
 if (machinename &&
 (virAsprintf(&partmachinename, "%s.libvirt-%s",
  machinename, drivername) < 0 ||
  virCgroupPartitionEscape(&partmachinename) < 0))
-goto cleanup;
+return false;
 
 if (!(scopename_old = virSystemdMakeScopeName(name, drivername, true)))
-goto cleanup;
+return false;
 
 /* We should keep trying even if this failed */
 if (!machinename)
 virResetLastError();
 else if (!(scopename_new = virSystemdMakeScopeName(machinename,
drivername, false)))
-goto cleanup;
+return false;
 
 if (virCgroupPartitionEscape(&scopename_old) < 0)
-goto cleanup;
+return false;
 
 if (scopename_new &&
 virCgroupPartitionEscape(&scopename_new) < 0)
-goto cleanup;
+return false;
 
 for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
 char *tmp;
@@ -302,7 +300,7 @@ virCgroupValidateMachineGroup(virCgroupPtr group,
 
 tmp = strrchr(group->controllers[i].placement, '/');
 if (!tmp)
-goto cleanup;
+return false;
 
 if (stripEmulatorSuffix &&
 (i == VIR_CGROUP_CONTROLLER_CPU ||
@@ -312,7 +310,7 @@ virCgroupValidateMachineGroup(virCgroupPtr group,
 *tmp = '\0';
 tmp = strrchr(group->controllers[i].placement, '/');
 if (!tmp)
-goto cleanup;
+return false;
 }
 
 tmp++;
@@ -328,18 +326,11 @@ virCgroupValidateMachineGroup(virCgroupPtr group,
   tmp, virCgroupControllerTypeToString(i),
   name, NULLSTR(machinename), partname,
   scopename_old, NULLSTR(scopename_new));
-goto cleanup;
+return false;
 }
 }
 
-valid = true;
-
- cleanup:
-VIR_FREE(partmachinename);
-VIR_FREE(partname);
-VIR_FREE(scopename_old);
-VIR_FREE(scopename_new);
-return valid;
+return true;
 }
 
 
@@ -377,7 +368,6 @@ virCgroupDetectMountsFromFile(virCgroupPtr group,
 FILE *mounts = NULL;
 struct mntent entry;
 char buf[CGROUP_MAX_VAL];
-char *linksrc = NULL;
 int ret = -1;
 
 mounts = fopen(path, "r");
@@ -432,8 +422,9 @@ virCgroupDetectMountsFromFile(virCgroupPtr group,
 /* If it is a co-mount it has a filename like "cpu,cpuacct"
  * and we must identify the symlink path */
 if (checkLinks && strchr(tmp2 + 1, ',')) {
+VIR_AUTOFREE(char *) linksrc = NULL;
+
 *tmp2 = '\0';
-VIR_FREE(linksrc);
 if (virAsprintf(&linksrc, "%s/%s",
 entry.mnt_dir, typestr) < 0)
 goto cleanup;
@@ -467,7 +458,6 @@ virCgroupDetectMountsFromFile(virCgroupPtr group,
 
 ret = 0;
  cleanup:
-VIR_FREE(linksrc);
 VIR_FORCE_FCLOSE(mounts);
 return ret;
 }
@@ -546,7 +536,7 @@ virCgroupDetectPlacement(virCgroupPtr group,
 FILE *mapping  = NULL;
 char line[1024];
 int ret = -1;
-char *procfile;
+VIR_AUTOFREE(char *) procfile = NULL;
 
 VIR_DEBUG("Detecting placement for pid 

Re: [libvirt] [PATCH v1 09/40] util: cgroup: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-07-23 Thread Erik Skultety
On Sat, Jul 21, 2018 at 05:36:41PM +0530, Sukrit Bhatnagar wrote:
> 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 
> ---
...

> @@ -1893,9 +1821,11 @@ virCgroupGetBlkioIoServiced(virCgroupPtr group,
>  long long *requests_write)
>  {
>  long long stats_val;
> -char *str1 = NULL, *str2 = NULL, *p1, *p2;
> +VIR_AUTOFREE(char *) str1 = NULL;
> +VIR_AUTOFREE(char *) str2 = NULL;
> +char *p1;
> +char *p2;

Could have initialized ^these 2 as well for that matter...

...
>
>
> @@ -2002,9 +1927,12 @@ virCgroupGetBlkioIoDeviceServiced(virCgroupPtr group,
>long long *requests_read,
>long long *requests_write)
>  {
> -char *str1 = NULL, *str2 = NULL, *str3 = NULL, *p1, *p2;
> +VIR_AUTOFREE(char *) str1 = NULL;
> +VIR_AUTOFREE(char *) str2 = NULL;
> +VIR_AUTOFREE(char *) str3 = NULL;
> +char *p1;
> +char *p2;

...here too...

Reviewed-by: Erik Skultety 

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