While all errors related to invalid node device appeared to be consistent, we might as well continue the trend of using a common macro. For now, we don't need virCheckNodeDeviceGoto(). * src/datatypes.h (virCheckNodeDeviceReturn): New macro. (VIR_IS_NODE_DEVICE, VIR_IS_CONNECTED_NODE_DEVICE): Drop unused macros. * src/libvirt.c: Use macro throughout. (virLibNodeDeviceError): Drop unused macro. Signed-off-by: Eric Blake <eblake@xxxxxxxxxx> --- src/datatypes.h | 17 +++++++++--- src/libvirt.c | 83 ++++++++++----------------------------------------------- 2 files changed, 27 insertions(+), 73 deletions(-) diff --git a/src/datatypes.h b/src/datatypes.h index 2f4b894..8b3b153 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -164,10 +164,19 @@ extern virClassPtr virStoragePoolClass; } \ } while (0) -# define VIR_IS_NODE_DEVICE(obj) \ - (virObjectIsClass((obj), virNodeDeviceClass)) -# define VIR_IS_CONNECTED_NODE_DEVICE(obj) \ - (VIR_IS_NODE_DEVICE(obj) && virObjectIsClass((obj)->conn, virConnectClass)) +# define virCheckNodeDeviceReturn(obj, retval) \ + do { \ + virNodeDevicePtr _node = (obj); \ + if (!virObjectIsClass(_node, virNodeDeviceClass) || \ + !virObjectIsClass(_node->conn, virConnectClass)) { \ + virReportErrorHelper(VIR_FROM_NODEDEV, \ + VIR_ERR_INVALID_NODE_DEVICE, \ + __FILE__, __FUNCTION__, __LINE__, \ + __FUNCTION__); \ + virDispatchError(NULL); \ + return retval; \ + } \ + } while (0) # define VIR_IS_SECRET(obj) \ (virObjectIsClass((obj), virSecretClass)) diff --git a/src/libvirt.c b/src/libvirt.c index 47e8bd3..6b6e19b 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -518,9 +518,6 @@ DllMain(HINSTANCE instance ATTRIBUTE_UNUSED, #define virLibDomainError(code, ...) \ virReportErrorHelper(VIR_FROM_DOM, code, __FILE__, \ __FUNCTION__, __LINE__, __VA_ARGS__) -#define virLibNodeDeviceError(code, ...) \ - virReportErrorHelper(VIR_FROM_NODEDEV, code, __FILE__, \ - __FUNCTION__, __LINE__, __VA_ARGS__) #define virLibSecretError(code, ...) \ virReportErrorHelper(VIR_FROM_SECRET, code, __FILE__, \ __FUNCTION__, __LINE__, __VA_ARGS__) @@ -14364,11 +14361,7 @@ char *virNodeDeviceGetXMLDesc(virNodeDevicePtr dev, unsigned int flags) virResetLastError(); - if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) { - virLibNodeDeviceError(VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__); - virDispatchError(NULL); - return NULL; - } + virCheckNodeDeviceReturn(dev, NULL); if (dev->conn->nodeDeviceDriver && dev->conn->nodeDeviceDriver->nodeDeviceGetXMLDesc) { char *ret; @@ -14400,11 +14393,7 @@ const char *virNodeDeviceGetName(virNodeDevicePtr dev) virResetLastError(); - if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) { - virLibNodeDeviceError(VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__); - virDispatchError(NULL); - return NULL; - } + virCheckNodeDeviceReturn(dev, NULL); return dev->name; } @@ -14425,11 +14414,7 @@ const char *virNodeDeviceGetParent(virNodeDevicePtr dev) virResetLastError(); - if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) { - virLibNodeDeviceError(VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__); - virDispatchError(NULL); - return NULL; - } + virCheckNodeDeviceReturn(dev, NULL); if (!dev->parent) { if (dev->conn->nodeDeviceDriver && dev->conn->nodeDeviceDriver->nodeDeviceGetParent) { @@ -14459,11 +14444,7 @@ virNodeDeviceNumOfCaps(virNodeDevicePtr dev) virResetLastError(); - if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) { - virLibNodeDeviceError(VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__); - virDispatchError(NULL); - return -1; - } + virCheckNodeDeviceReturn(dev, -1); if (dev->conn->nodeDeviceDriver && dev->conn->nodeDeviceDriver->nodeDeviceNumOfCaps) { int ret; @@ -14501,12 +14482,7 @@ virNodeDeviceListCaps(virNodeDevicePtr dev, virResetLastError(); - if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) { - virLibNodeDeviceError(VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__); - virDispatchError(NULL); - return -1; - } - + virCheckNodeDeviceReturn(dev, -1); virCheckNonNullArgGoto(names, error); virCheckNonNegativeArgGoto(maxnames, error); @@ -14542,11 +14518,8 @@ virNodeDeviceFree(virNodeDevicePtr dev) virResetLastError(); - if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) { - virLibNodeDeviceError(VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__); - virDispatchError(NULL); - return -1; - } + virCheckNodeDeviceReturn(dev, -1); + virObjectUnref(dev); return 0; } @@ -14576,11 +14549,8 @@ virNodeDeviceRef(virNodeDevicePtr dev) virResetLastError(); - if ((!VIR_IS_CONNECTED_NODE_DEVICE(dev))) { - virLibConnError(VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__); - virDispatchError(NULL); - return -1; - } + virCheckNodeDeviceReturn(dev, -1); + virObjectRef(dev); return 0; } @@ -14617,12 +14587,7 @@ virNodeDeviceDettach(virNodeDevicePtr dev) virResetLastError(); - if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) { - virLibNodeDeviceError(VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__); - virDispatchError(NULL); - return -1; - } - + virCheckNodeDeviceReturn(dev, -1); virCheckReadOnlyGoto(dev->conn->flags, error); if (dev->conn->driver->nodeDeviceDettach) { @@ -14679,12 +14644,7 @@ virNodeDeviceDetachFlags(virNodeDevicePtr dev, virResetLastError(); - if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) { - virLibNodeDeviceError(VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__); - virDispatchError(NULL); - return -1; - } - + virCheckNodeDeviceReturn(dev, -1); virCheckReadOnlyGoto(dev->conn->flags, error); if (dev->conn->driver->nodeDeviceDetachFlags) { @@ -14725,12 +14685,7 @@ virNodeDeviceReAttach(virNodeDevicePtr dev) virResetLastError(); - if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) { - virLibNodeDeviceError(VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__); - virDispatchError(NULL); - return -1; - } - + virCheckNodeDeviceReturn(dev, -1); virCheckReadOnlyGoto(dev->conn->flags, error); if (dev->conn->driver->nodeDeviceReAttach) { @@ -14773,12 +14728,7 @@ virNodeDeviceReset(virNodeDevicePtr dev) virResetLastError(); - if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) { - virLibNodeDeviceError(VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__); - virDispatchError(NULL); - return -1; - } - + virCheckNodeDeviceReturn(dev, -1); virCheckReadOnlyGoto(dev->conn->flags, error); if (dev->conn->driver->nodeDeviceReset) { @@ -14854,12 +14804,7 @@ virNodeDeviceDestroy(virNodeDevicePtr dev) virResetLastError(); - if (!VIR_IS_CONNECTED_NODE_DEVICE(dev)) { - virLibNodeDeviceError(VIR_ERR_INVALID_NODE_DEVICE, __FUNCTION__); - virDispatchError(NULL); - return -1; - } - + virCheckNodeDeviceReturn(dev, -1); virCheckReadOnlyGoto(dev->conn->flags, error); if (dev->conn->nodeDeviceDriver && -- 1.8.4.2 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list