[PATCH v2 6/8] util: Create common error path for invalid object

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If virObjectIsClass fails "internally" to virobject.c, create a
macro to generate the VIR_WARN describing what the problem is.
Also improve the checks and message a bit to indicate which was
the failure - whether the obj was NULL or just not the right class

Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx>
---
 src/util/virobject.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/src/util/virobject.c b/src/util/virobject.c
index 85e5a53..af3f252 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -47,6 +47,17 @@ struct _virClass {
     virObjectDisposeCallback dispose;
 };
 
+#define VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, objclass)                    \
+    do {                                                                    \
+        virObjectPtr obj = anyobj;                                          \
+        if (!obj)                                                           \
+            VIR_WARN("Object cannot be NULL");                              \
+        else                                                                \
+            VIR_WARN("Object %p (%s) is not a %s instance",                 \
+                      anyobj, obj->klass->name, #objclass);                 \
+    } while (0)
+
+
 static virClassPtr virObjectClass;
 static virClassPtr virObjectLockableClass;
 static virClassPtr virObjectRWLockableClass;
@@ -370,15 +381,10 @@ virObjectRef(void *anyobj)
 static virObjectLockablePtr
 virObjectGetLockableObj(void *anyobj)
 {
-    virObjectPtr obj;
-
     if (virObjectIsClass(anyobj, virObjectLockableClass))
         return anyobj;
 
-    obj = anyobj;
-    VIR_WARN("Object %p (%s) is not a virObjectLockable instance",
-              anyobj, obj ? obj->klass->name : "(unknown)");
-
+    VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectLockable);
     return NULL;
 }
 
@@ -386,15 +392,10 @@ virObjectGetLockableObj(void *anyobj)
 static virObjectRWLockablePtr
 virObjectGetRWLockableObj(void *anyobj)
 {
-    virObjectPtr obj;
-
     if (virObjectIsClass(anyobj, virObjectRWLockableClass))
         return anyobj;
 
-    obj = anyobj;
-    VIR_WARN("Object %p (%s) is not a virObjectRWLockable instance",
-              anyobj, obj ? obj->klass->name : "(unknown)");
-
+    VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectRWLockable);
     return NULL;
 }
 
-- 
2.9.4

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



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]
  Powered by Linux