From: Willy Tarreau Date: Fri, 7 Jun 2019 08:42:43 +0000 (+0200) Subject: MINOR: tools: add new bitmap manipulation functions X-Git-Tag: v2.0-dev7~33 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7355b040d1833aa671382dd47e31ae369681b66e;p=thirdparty%2Fhaproxy.git MINOR: tools: add new bitmap manipulation functions 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. --- diff --git a/include/common/standard.h b/include/common/standard.h index e88a79ddf4..5e0c4c5466 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -891,6 +891,30 @@ static inline unsigned long nbits(int bits) return (2UL << bits) - 1; } +/* sets bit into 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 from 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 from 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 from 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