]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: add new bitmap manipulation functions
authorWilly Tarreau <w@1wt.eu>
Fri, 7 Jun 2019 08:42:43 +0000 (10:42 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 7 Jun 2019 08:44:49 +0000 (10:44 +0200)
We now have ha_bit_{set,clr,flip,test} to manipulate bitfields made
of arrays of longs. The goal is to get rid of the remaining non-portable
FD_{SET,CLR,ISSET} that still exist at a few places.

include/common/standard.h

index e88a79ddf440fb0c5f030338ce1696e98eeaed58..5e0c4c5466df97e071131884d6a26075e7655cc8 100644 (file)
@@ -891,6 +891,30 @@ static inline unsigned long nbits(int bits)
                return (2UL << bits) - 1;
 }
 
+/* sets bit <bit> into map <map>, which must be long-aligned */
+static inline void ha_bit_set(unsigned long bit, long *map)
+{
+       map[bit / (8 * sizeof(*map))] |= 1UL << (bit & (8 * sizeof(*map) - 1));
+}
+
+/* clears bit <bit> from map <map>, which must be long-aligned */
+static inline void ha_bit_clr(unsigned long bit, long *map)
+{
+       map[bit / (8 * sizeof(*map))] &= ~(1UL << (bit & (8 * sizeof(*map) - 1)));
+}
+
+/* flips bit <bit> from map <map>, which must be long-aligned */
+static inline void ha_bit_flip(unsigned long bit, long *map)
+{
+       map[bit / (8 * sizeof(*map))] ^= 1UL << (bit & (8 * sizeof(*map) - 1));
+}
+
+/* returns non-zero if bit <bit> from map <map> is set, otherwise 0 */
+static inline int ha_bit_test(unsigned long bit, const long *map)
+{
+       return !!(map[bit / (8 * sizeof(*map))] & 1UL << (bit & (8 * sizeof(*map) - 1)));
+}
+
 /*
  * Parse binary string written in hexadecimal (source) and store the decoded
  * result into binstr and set binstrlen to the lengh of binstr. Memory for