From 070da02fb5a2ab749a06f75cf21e452f9f98031a Mon Sep 17 00:00:00 2001 From: Jiri Denemark Date: Tue, 13 Apr 2010 15:48:04 +0200 Subject: [PATCH] Introduce virCheckFlags for consistent flags checking 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 | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/internal.h b/src/internal.h index 896df22d74..c3c53112e7 100644 --- a/src/internal.h +++ b/src/internal.h @@ -199,4 +199,30 @@ 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__ */ -- GitLab