]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: stream-int: use bit fields to match multiple stream-int states at once
authorWilly Tarreau <w@1wt.eu>
Wed, 5 Jun 2019 12:45:06 +0000 (14:45 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 6 Jun 2019 14:36:19 +0000 (16:36 +0200)
At some places we do check for ranges of stream-int states but those
are confusing as states ordering is not well known (e.g. it's not obvious
that CER is between CON and EST). Let's create a bit field from states so
that we can match multiple states at once instead. The new enum si_state_bit
contains SI_SB_* which are state bits instead of state values. The function
si_state_in() indicates if the state in argument is one of those represented
by the bit mask in second argument.

include/proto/stream_interface.h
include/types/stream_interface.h

index 2f6a7a5bea7c3f17f0a1eb0245567205b51998d2..64aa9af949e4f36affdf2e3835d31658748aa6e0 100644 (file)
@@ -138,6 +138,20 @@ static inline void si_set_state(struct stream_interface *si, int state)
        si->state = si->prev_state = state;
 }
 
+/* returns a bit for a stream-int state, to match against SI_SB_* */
+static inline enum si_state_bit si_state_bit(enum si_state state)
+{
+       BUG_ON(state < SI_ST_INI || state > SI_ST_CLO);
+       return 1U << state;
+}
+
+/* returns true if <state> matches one of the SI_SB_* bits in <mask> */
+static inline int si_state_in(enum si_state state, enum si_state_bit mask)
+{
+       BUG_ON(mask & ~SI_SB_ALL);
+       return !!(si_state_bit(state) & mask);
+}
+
 /* only detaches the endpoint from the SI, which means that it's set to
  * NULL and that ->ops is mapped to si_embedded_ops. The previous endpoint
  * is returned.
index e11a6c903772edd8b67bd2c4c039f6802fb341c0..86df1d2698a6fb6b359fd3f3963556b42fd370f2 100644 (file)
@@ -43,6 +43,22 @@ enum si_state {
        SI_ST_CLO,               /* stream intf closed, might not existing anymore. Buffers shut. */
 } __attribute__((packed));
 
+/* state bits for use with lists of states */
+enum si_state_bit {
+       SI_SB_NONE = 0,
+       SI_SB_INI = 1U << SI_ST_INI,
+       SI_SB_REQ = 1U << SI_ST_REQ,
+       SI_SB_QUE = 1U << SI_ST_QUE,
+       SI_SB_TAR = 1U << SI_ST_TAR,
+       SI_SB_ASS = 1U << SI_ST_ASS,
+       SI_SB_CON = 1U << SI_ST_CON,
+       SI_SB_CER = 1U << SI_ST_CER,
+       SI_SB_EST = 1U << SI_ST_EST,
+       SI_SB_DIS = 1U << SI_ST_DIS,
+       SI_SB_CLO = 1U << SI_ST_CLO,
+       SI_SB_ALL = SI_SB_INI|SI_SB_REQ|SI_SB_QUE|SI_SB_TAR|SI_SB_ASS|SI_SB_CON|SI_SB_CER|SI_SB_EST|SI_SB_DIS|SI_SB_CLO,
+};
+
 /* error types reported on the streams interface for more accurate reporting */
 enum {
        SI_ET_NONE       = 0x0000,  /* no error yet, leave it to zero */