From d431de70acb7688ef769f0e960b5d2413a99584f Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 20 Feb 2019 16:06:06 +0100 Subject: [PATCH] platform: refactor FOR_EACH_DELAYED_ACTION() macro to have only one for(;;) statement for-each macros can be nice to use. However, such macros are potentially error prone, as they abstract C control statements and scoping blocks. I find it ugly to expand the macro to: for (...) if (...) Instead, move the additional "if" check inside the loop's condition expression, so that the macro only expands to one "for(;;)" statement. --- src/platform/nm-linux-platform.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index 3370620e3a..f5f75e3d97 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -321,8 +321,22 @@ typedef enum { } DelayedActionType; #define FOR_EACH_DELAYED_ACTION(iflags, flags_all) \ - for ((iflags) = (DelayedActionType) 0x1LL; (iflags) <= DELAYED_ACTION_TYPE_MAX; (iflags) <<= 1) \ - if (NM_FLAGS_ANY (flags_all, iflags)) + for ((iflags) = (DelayedActionType) 0x1LL; \ + ({ \ + gboolean _good = FALSE; \ + \ + nm_assert (nm_utils_is_power_of_two (iflags)); \ + \ + while ((iflags) <= DELAYED_ACTION_TYPE_MAX) { \ + if (NM_FLAGS_ANY ((flags_all), (iflags))) { \ + _good = TRUE; \ + break; \ + } \ + (iflags) <<= 1; \ + } \ + _good; \ + }); \ + (iflags) <<= 1) typedef enum { /* Negative values are errors from kernel. Add dummy member to @@ -4633,9 +4647,8 @@ delayed_action_handle_one (NMPlatform *platform) priv->delayed_action.flags &= ~DELAYED_ACTION_TYPE_REFRESH_ALL; if (_LOGt_ENABLED ()) { - FOR_EACH_DELAYED_ACTION (iflags, flags) { + FOR_EACH_DELAYED_ACTION (iflags, flags) _LOGt_delayed_action (iflags, NULL, "handle"); - } } delayed_action_handle_REFRESH_ALL (platform, flags); @@ -4719,9 +4732,8 @@ delayed_action_schedule (NMPlatform *platform, DelayedActionType action_type, gp priv->delayed_action.flags |= action_type; if (_LOGt_ENABLED ()) { - FOR_EACH_DELAYED_ACTION (iflags, action_type) { + FOR_EACH_DELAYED_ACTION (iflags, action_type) _LOGt_delayed_action (iflags, user_data, "schedule"); - } } }