Browse code

Replace MSG_TEST() macro for static inline msg_test()

Using a static inline function instead of a macro has the advantages that
(1) 'flags' is not evaluated twice and (2) coverity will stop complaining
that 'Macro compares unsigned to 0 (NO_EFFECT)' each time we use flags
with loglevel 0 (e.g. M_FATAL or M_WARN).

This has a performance impact when compiler optimizations are fully
disabled ('-O0'), but should otherwise be as fast as using a macro.

Signed-off-by: Steffan Karger <steffan@karger.me>
Acked-by: Gert Doering <gert@greenie.muc.de>
Message-Id: <1459088296-5046-1-git-send-email-steffan@karger.me>
URL: http://article.gmane.org/gmane.network.openvpn.devel/11368
Signed-off-by: Gert Doering <gert@greenie.muc.de>

Steffan Karger authored on 2016/03/27 23:18:16
Showing 3 changed files
... ...
@@ -228,7 +228,7 @@ void x_msg_va (const unsigned int flags, const char *format, va_list arglist)
228 228
 
229 229
 #ifndef HAVE_VARARG_MACROS
230 230
   /* the macro has checked this otherwise */
231
-  if (!MSG_TEST (flags))
231
+  if (!msg_test (flags))
232 232
     return;
233 233
 #endif
234 234
 
... ...
@@ -135,26 +135,31 @@ extern int x_msg_line_num;
135 135
  * msg() as a macro for optimization win.
136 136
  */
137 137
 
138
-bool dont_mute (unsigned int flags); /* check muting filter */
138
+/** Check muting filter */
139
+bool dont_mute (unsigned int flags);
139 140
 
140
-#define MSG_TEST(flags) (unlikely((((unsigned int)flags) & M_DEBUG_LEVEL) <= x_debug_level) && dont_mute (flags))
141
+/** Return true if flags represent an enabled, not muted log level */
142
+static inline bool msg_test (unsigned int flags)
143
+{
144
+  return ((flags & M_DEBUG_LEVEL) <= x_debug_level) && dont_mute (flags);
145
+}
141 146
 
142 147
 /* Macro to ensure (and teach static analysis tools) we exit on fatal errors */
143 148
 #define EXIT_FATAL(flags) do { if ((flags) & M_FATAL) _exit(1); } while (false)
144 149
 
145 150
 #if defined(HAVE_CPP_VARARG_MACRO_ISO) && !defined(__LCLINT__)
146 151
 # define HAVE_VARARG_MACROS
147
-# define msg(flags, ...) do { if (MSG_TEST(flags)) x_msg((flags), __VA_ARGS__); EXIT_FATAL(flags); } while (false)
152
+# define msg(flags, ...) do { if (msg_test(flags)) x_msg((flags), __VA_ARGS__); EXIT_FATAL(flags); } while (false)
148 153
 # ifdef ENABLE_DEBUG
149
-#  define dmsg(flags, ...) do { if (MSG_TEST(flags)) x_msg((flags), __VA_ARGS__); EXIT_FATAL(flags); } while (false)
154
+#  define dmsg(flags, ...) do { if (msg_test(flags)) x_msg((flags), __VA_ARGS__); EXIT_FATAL(flags); } while (false)
150 155
 # else
151 156
 #  define dmsg(flags, ...)
152 157
 # endif
153 158
 #elif defined(HAVE_CPP_VARARG_MACRO_GCC) && !defined(__LCLINT__)
154 159
 # define HAVE_VARARG_MACROS
155
-# define msg(flags, args...) do { if (MSG_TEST(flags)) x_msg((flags), args); EXIT_FATAL(flags); } while (false)
160
+# define msg(flags, args...) do { if (msg_test(flags)) x_msg((flags), args); EXIT_FATAL(flags); } while (false)
156 161
 # ifdef ENABLE_DEBUG
157
-#  define dmsg(flags, args...) do { if (MSG_TEST(flags)) x_msg((flags), args); EXIT_FATAL(flags); } while (false)
162
+#  define dmsg(flags, args...) do { if (msg_test(flags)) x_msg((flags), args); EXIT_FATAL(flags); } while (false)
158 163
 # else
159 164
 #  define dmsg(flags, args...)
160 165
 # endif
... ...
@@ -319,7 +319,7 @@ plugin_vlog (openvpn_plugin_log_flags_t flags, const char *name, const char *for
319 319
   if (flags & PLOG_NOMUTE)
320 320
     msg_flags |= M_NOMUTE;
321 321
 
322
-  if (MSG_TEST (msg_flags))
322
+  if (msg_test (msg_flags))
323 323
     {
324 324
       struct gc_arena gc;
325 325
       char* msg_fmt;