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

Erik Skultety eskultet at redhat.com
Mon Jul 23 14:13:12 UTC 2018


On Sat, Jul 21, 2018 at 05:36:52PM +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 <skrtbhtngr at gmail.com>
> ---

...

>
> @@ -1278,8 +1247,7 @@ virPCIDeviceUnbindFromStubWithOverride(virPCIDevicePtr dev)
>  static int
>  virPCIDeviceUnbindFromStub(virPCIDevicePtr dev)
>  {
> -    int ret;
> -    char *path;
> +    VIR_AUTOFREE(char *) path = NULL;
>
>      /*
>       * Prefer using the device's driver_override interface, falling back
> @@ -1289,12 +1257,9 @@ virPCIDeviceUnbindFromStub(virPCIDevicePtr dev)
>          return -1;
>
>      if (virFileExists(path))
> -        ret = virPCIDeviceUnbindFromStubWithOverride(dev);
> +        return virPCIDeviceUnbindFromStubWithOverride(dev);
>      else

Drop the 'else' clause, it's not needed anymore.

...

>  static int
>  virPCIDeviceBindToStub(virPCIDevicePtr dev)
>  {
> -    int ret;
> -    char *path;
> +    VIR_AUTOFREE(char *) path = NULL;
>
>      /*
>       * Prefer using the device's driver_override interface, falling back
> @@ -1511,12 +1464,9 @@ virPCIDeviceBindToStub(virPCIDevicePtr dev)
>          return -1;
>
>      if (virFileExists(path))
> -        ret = virPCIDeviceBindToStubWithOverride(dev);
> +        return virPCIDeviceBindToStubWithOverride(dev);
>      else
... here too..

...

> @@ -1755,8 +1701,8 @@ virPCIDeviceNew(unsigned int domain,
>                  unsigned int function)
>  {
>      virPCIDevicePtr dev;

virPCIDevicePtr ret = NULL;
VIR_AUTOPTR(virPCIDevicePtr) tmp = NULL;

> -    char *vendor = NULL;
> -    char *product = NULL;
> +    VIR_AUTOFREE(char *) vendor = NULL;
> +    VIR_AUTOFREE(char *) product = NULL;
>
>      if (VIR_ALLOC(dev) < 0)
>          return NULL;
> @@ -1805,15 +1751,11 @@ virPCIDeviceNew(unsigned int domain,
>
>      VIR_DEBUG("%s %s: initialized", dev->id, dev->name);
>

VIR_STEAL_PTR(ret, dev);

> - cleanup:

Preserve the 'cleanup' label...

> -    VIR_FREE(product);
> -    VIR_FREE(vendor);
>      return dev;
>
>   error:

Drop 'error' label

>      virPCIDeviceFree(dev);

^this can be dropped too...

> -    dev = NULL;
> -    goto cleanup;
> +    return NULL;

^return ret;

Erik




More information about the libvir-list mailing list