]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Introduce virCheckFlags for consistent flags checking
authorJiri Denemark <jdenemar@redhat.com>
Tue, 13 Apr 2010 13:48:04 +0000 (15:48 +0200)
committerJiri Denemark <jdenemar@redhat.com>
Fri, 16 Apr 2010 12:05:50 +0000 (14:05 +0200)
The idea is that every API implementation in driver which has flags
parameter should first call virCheckFlags() macro to check the function
was called with supported flags:

    virCheckFlags(VIR_SUPPORTED_FLAG_1 |
                  VIR_SUPPORTED_FLAG_2 |
                  VIR_ANOTHER_SUPPORTED_FLAG, -1);

The error massage which is printed when unsupported flags are passed
looks like:

    invalid argument in virFooBar: unsupported flags (0x2)

Where the unsupported flags part only prints those flags which were
passed but are not supported rather than all flags passed.

src/internal.h

index 896df22d74aef90159e9d62356b154dd4b21dd5b..c3c53112e70a8160a7c15336ca3c12cfecc0ec15 100644 (file)
     fprintf(stderr, "Unimplemented block at %s:%d\n",                  \
             __FILE__, __LINE__);
 
+/**
+ * virCheckFlags:
+ * @supported: an OR'ed set of supported flags
+ * @retval: return value in case unsupported flags were passed
+ *
+ * To avoid memory leaks this macro has to be used before any non-trivial
+ * code which could possibly allocate some memory.
+ *
+ * Returns nothing. Exits the caller function if unsupported flags were
+ * passed to it.
+ */
+# define virCheckFlags(supported, retval)                               \
+    do {                                                                \
+        if ((flags & ~(supported))) {                                   \
+            virReportErrorHelper(NULL,                                  \
+                                 VIR_FROM_THIS,                         \
+                                 VIR_ERR_INVALID_ARG,                   \
+                                 __FILE__,                              \
+                                 __FUNCTION__,                          \
+                                 __LINE__,                              \
+                                 _("%s: unsupported flags (0x%x)"),     \
+                                 __FUNCTION__, flags & ~(supported));   \
+            return retval;                                              \
+        }                                                               \
+    } while (0)
+
 #endif                          /* __VIR_INTERNAL_H__ */