From 7a955b5d735e1a2457daaa6059c4edc85ab75a2f Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 9 Sep 2022 16:05:10 +0200 Subject: [PATCH] MINOR: flags: implement a macro used to dump enums inside masks Some of our flags have enums inside a mask. The new macro __APPEND_ENUM is able to deal with that by comparing the flag's value against an exact one under the mask. One needs to take care of eliminating the zero value though, otherwise delimiters will not always be properly placed (e.g. if some flags were dumped before and what remains is exactly zero). The bits of the mask are cleared only upon exact matches. --- include/haproxy/show_flags-t.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/haproxy/show_flags-t.h b/include/haproxy/show_flags-t.h index 3c045944e..72176618b 100644 --- a/include/haproxy/show_flags-t.h +++ b/include/haproxy/show_flags-t.h @@ -44,6 +44,10 @@ * _(X_FLAG1, _(X_FLAG2, _(X_FLAG3))); * _(~0); * #undef _ + * + * __APPEND_ENUM() works a bit differently in that it takes an additional mask + * to isolate bits to compare to the enum's value, and will remove the mask's + * bits at once in case of match. */ #ifdef EOF @@ -70,9 +74,25 @@ } \ } while (0) +#define __APPEND_ENUM(_buf, _len, _del, _flg, _msk, _val, _nam, ...) \ + do { \ + size_t _ret = 0; \ + do { __VA_ARGS__; } while (0); \ + if (((_flg) & (_msk)) == (_val)) { \ + (_flg) &= ~(_msk); \ + _ret = snprintf(_buf, _len, _nam "%s", \ + (_flg) ? (_del) : ""); \ + } \ + if (_ret < _len) { \ + _len -= _ret; \ + _buf += _ret; \ + } \ + } while (0) + #else /* EOF not defined => no stdio, do nothing */ #define __APPEND_FLAG(_buf, _len, _del, _flg, _val, _nam) do { } while (0) +#define __APPEND_ENUM(_buf, _len, _del, _flg, _msk, _val, _nam) do { } while (0) #endif /* EOF */