mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
util: mdev: use VIR_AUTOPTR for aggregate types
By making use of GNU C's cleanup attribute handled by the VIR_AUTOPTR macro for declaring aggregate pointer variables, majority of the calls to *Free functions 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:
parent
4632e02db5
commit
d4e7ad8da5
@ -137,20 +137,20 @@ virMediatedDevicePtr
|
|||||||
virMediatedDeviceNew(const char *uuidstr, virMediatedDeviceModelType model)
|
virMediatedDeviceNew(const char *uuidstr, virMediatedDeviceModelType model)
|
||||||
{
|
{
|
||||||
virMediatedDevicePtr ret = NULL;
|
virMediatedDevicePtr ret = NULL;
|
||||||
virMediatedDevicePtr dev = NULL;
|
VIR_AUTOPTR(virMediatedDevice) dev = NULL;
|
||||||
VIR_AUTOFREE(char *) sysfspath = NULL;
|
VIR_AUTOFREE(char *) sysfspath = NULL;
|
||||||
|
|
||||||
if (!(sysfspath = virMediatedDeviceGetSysfsPath(uuidstr)))
|
if (!(sysfspath = virMediatedDeviceGetSysfsPath(uuidstr)))
|
||||||
goto cleanup;
|
return NULL;
|
||||||
|
|
||||||
if (!virFileExists(sysfspath)) {
|
if (!virFileExists(sysfspath)) {
|
||||||
virReportError(VIR_ERR_DEVICE_MISSING,
|
virReportError(VIR_ERR_DEVICE_MISSING,
|
||||||
_("mediated device '%s' not found"), uuidstr);
|
_("mediated device '%s' not found"), uuidstr);
|
||||||
goto cleanup;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (VIR_ALLOC(dev) < 0)
|
if (VIR_ALLOC(dev) < 0)
|
||||||
goto cleanup;
|
return NULL;
|
||||||
|
|
||||||
VIR_STEAL_PTR(dev->path, sysfspath);
|
VIR_STEAL_PTR(dev->path, sysfspath);
|
||||||
|
|
||||||
@ -158,13 +158,11 @@ virMediatedDeviceNew(const char *uuidstr, virMediatedDeviceModelType model)
|
|||||||
* supported mediated device's API.
|
* supported mediated device's API.
|
||||||
*/
|
*/
|
||||||
if (virMediatedDeviceCheckModel(dev, model))
|
if (virMediatedDeviceCheckModel(dev, model))
|
||||||
goto cleanup;
|
return NULL;
|
||||||
|
|
||||||
dev->model = model;
|
dev->model = model;
|
||||||
VIR_STEAL_PTR(ret, dev);
|
VIR_STEAL_PTR(ret, dev);
|
||||||
|
|
||||||
cleanup:
|
|
||||||
virMediatedDeviceFree(dev);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -372,8 +370,7 @@ void
|
|||||||
virMediatedDeviceListDel(virMediatedDeviceListPtr list,
|
virMediatedDeviceListDel(virMediatedDeviceListPtr list,
|
||||||
virMediatedDevicePtr dev)
|
virMediatedDevicePtr dev)
|
||||||
{
|
{
|
||||||
virMediatedDevicePtr ret = virMediatedDeviceListSteal(list, dev);
|
VIR_AUTOPTR(virMediatedDevice) ret = virMediatedDeviceListSteal(list, dev);
|
||||||
virMediatedDeviceFree(ret);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -494,23 +491,22 @@ int
|
|||||||
virMediatedDeviceTypeReadAttrs(const char *sysfspath,
|
virMediatedDeviceTypeReadAttrs(const char *sysfspath,
|
||||||
virMediatedDeviceTypePtr *type)
|
virMediatedDeviceTypePtr *type)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
VIR_AUTOPTR(virMediatedDeviceType) tmp = NULL;
|
||||||
virMediatedDeviceTypePtr tmp = NULL;
|
|
||||||
|
|
||||||
#define MDEV_GET_SYSFS_ATTR(attr, dst, cb, optional) \
|
#define MDEV_GET_SYSFS_ATTR(attr, dst, cb, optional) \
|
||||||
do { \
|
do { \
|
||||||
int rc; \
|
int rc; \
|
||||||
if ((rc = cb(dst, "%s/%s", sysfspath, attr)) < 0) { \
|
if ((rc = cb(dst, "%s/%s", sysfspath, attr)) < 0) { \
|
||||||
if (rc != -2 || !optional) \
|
if (rc != -2 || !optional) \
|
||||||
goto cleanup; \
|
return -1; \
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
if (VIR_ALLOC(tmp) < 0)
|
if (VIR_ALLOC(tmp) < 0)
|
||||||
goto cleanup;
|
return -1;
|
||||||
|
|
||||||
if (VIR_STRDUP(tmp->id, last_component(sysfspath)) < 0)
|
if (VIR_STRDUP(tmp->id, last_component(sysfspath)) < 0)
|
||||||
goto cleanup;
|
return -1;
|
||||||
|
|
||||||
/* @name sysfs attribute is optional, so getting ENOENT is fine */
|
/* @name sysfs attribute is optional, so getting ENOENT is fine */
|
||||||
MDEV_GET_SYSFS_ATTR("name", &tmp->name, virFileReadValueString, true);
|
MDEV_GET_SYSFS_ATTR("name", &tmp->name, virFileReadValueString, true);
|
||||||
@ -522,8 +518,6 @@ virMediatedDeviceTypeReadAttrs(const char *sysfspath,
|
|||||||
#undef MDEV_GET_SYSFS_ATTR
|
#undef MDEV_GET_SYSFS_ATTR
|
||||||
|
|
||||||
VIR_STEAL_PTR(*type, tmp);
|
VIR_STEAL_PTR(*type, tmp);
|
||||||
ret = 0;
|
|
||||||
cleanup:
|
return 0;
|
||||||
virMediatedDeviceTypeFree(tmp);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user