[set] Make static init sets compatible with normal

When using SET_STATIC_INIT, the set size needs to be the same as what
set_new() would create for the same number of bits, otherwise the set
will possibly get resized incorrectly (which is bad news when the array
was allocated using alloca). While this is really a symptom of
set_bits_t not getting the right size, getting weird segfaults is not a
good way to diagnose the problem, and set_bits_t being the wrong size is
just a minor pessimism.
This commit is contained in:
Bill Currie 2023-03-28 17:01:05 +09:00
parent 451a98d391
commit d39f1307c5

View file

@ -51,6 +51,7 @@ typedef uint32_t set_bits_t;
- sizeof (int) - sizeof (unsigned))\ - sizeof (int) - sizeof (unsigned))\
/ sizeof (set_bits_t)) / sizeof (set_bits_t))
#define SET_BITS (sizeof (set_bits_t) * 8) #define SET_BITS (sizeof (set_bits_t) * 8)
#define SET_DEFMAP_BITS (SET_DEFMAP_SIZE * SET_BITS)
//NOTE: x is the element number, so size is x + 1 //NOTE: x is the element number, so size is x + 1
#define SET_SIZE(x) (((x) + SET_BITS) & ~(SET_BITS - 1)) #define SET_SIZE(x) (((x) + SET_BITS) & ~(SET_BITS - 1))
#define SET_WORDS_STATIC(x) (SET_SIZE (x) / SET_BITS) #define SET_WORDS_STATIC(x) (SET_SIZE (x) / SET_BITS)
@ -63,9 +64,11 @@ typedef uint32_t set_bits_t;
(((byte *)(s)->map)[(x) / 8] |= (SET_ONE << ((x) % 8))) (((byte *)(s)->map)[(x) / 8] |= (SET_ONE << ((x) % 8)))
#define SET_REMOVE(s, x) \ #define SET_REMOVE(s, x) \
(((byte *)(s)->map)[(x) / 8] &= ~(SET_ONE << ((x) % 8))) (((byte *)(s)->map)[(x) / 8] &= ~(SET_ONE << ((x) % 8)))
#define SET_LARGE_SET(x) (SET_SIZE (x) > SET_DEFMAP_BITS)
#define SET_SAFE_SIZE(x) (SET_LARGE_SET (x) ? SET_SIZE (x) : SET_DEFMAP_BITS)
#define SET_STATIC_INIT(x, alloc) { \ #define SET_STATIC_INIT(x, alloc) { \
.size = SET_SIZE (x), \ .size = SET_SAFE_SIZE (x), \
.map = alloc (SET_SIZE (x) / 8), \ .map = alloc (SET_SAFE_SIZE (x) / 8), \
} }
#define SET_STATIC_ARRAY(array) { \ #define SET_STATIC_ARRAY(array) { \
.size = 8 * __builtin_choose_expr ( \ .size = 8 * __builtin_choose_expr ( \