#include "obst.h"
#define BITS_PER_ELEM (sizeof(unsigned) * 8)
-#define BITSET_SIZE_ELEMS(size_bits) ((size_bits)/BITS_PER_ELEM + 1)
+#define BITSET_SIZE_ELEMS(size_bits) ((size_bits+BITS_PER_ELEM-1)/BITS_PER_ELEM)
#define BITSET_SIZE_BYTES(size_bits) (BITSET_SIZE_ELEMS(size_bits) * sizeof(unsigned))
#define BITSET_ELEM(bitset,pos) bitset[pos / BITS_PER_ELEM]
*/
static inline unsigned *rbitset_malloc(unsigned size)
{
- unsigned size_bytes = BITSET_SIZE_BYTES(size);
- unsigned *res = xmalloc(size_bytes);
- memset(res, 0, size_bytes);
-
- return res;
+ return XMALLOCNZ(unsigned, BITSET_SIZE_ELEMS(size));
}
/**
#define rbitset_alloca(res, size) \
do { \
unsigned size_bytes = BITSET_SIZE_BYTES(size); \
- res = alloca(size_bytes); \
+ res = (unsigned*)alloca(size_bytes); \
memset(res, 0, size_bytes); \
} while(0)
static inline unsigned *rbitset_obstack_alloc(struct obstack *obst,
unsigned size)
{
- unsigned size_bytes = BITSET_SIZE_BYTES(size);
- unsigned *res = obstack_alloc(obst, size_bytes);
- memset(res, 0, size_bytes);
-
- return res;
-}
-
-/**
- * Allocate an empty raw bitset including the size on an obstack.
- * The size of this bitset can be accessed by bitset[-1].
- *
- * @param obst the obstack where the bitset is allocated on
- * @param size number of bits in the bitset
- *
- * @return the new bitset
- */
-static inline unsigned *rbitset_w_size_obstack_alloc(struct obstack *obst,
- unsigned size)
-{
- unsigned size_bytes = BITSET_SIZE_BYTES(size);
- unsigned *res = obstack_alloc(obst, size_bytes + sizeof(unsigned));
- *res = size;
- ++res;
- memset(res, 0, size_bytes);
-
- return res;
+ return OALLOCNZ(obst, unsigned, BITSET_SIZE_ELEMS(size));
}
-/** Return the size of a bitset allocated with a *_w_size_* function */
-#define rbitset_size(set) (set)[-1]
-
/**
* Duplicate a raw bitset on an obstack.
*
const unsigned *old_bitset, unsigned size)
{
unsigned size_bytes = BITSET_SIZE_BYTES(size);
- unsigned *res = obstack_alloc(obst, size_bytes);
+ unsigned *res = OALLOCN(obst, unsigned, BITSET_SIZE_ELEMS(size));
memcpy(res, old_bitset, size_bytes);
return res;
unsigned i;
unsigned n = BITSET_SIZE_ELEMS(size);
+ if (n == 0)
+ return;
+
for (i = 0; i < n-1; ++i) {
bitset[i] = ~0u;
}
static inline void rbitset_flip_all(unsigned *bitset, unsigned size)
{
unsigned pos;
- unsigned n = BITSET_SIZE_ELEMS(size);
+ unsigned n = BITSET_SIZE_ELEMS(size);
+
+ if (n == 0)
+ return;
+
for (pos = 0; pos < n-1; ++pos) {
bitset[pos] ^= ~0u;
}
*/
static inline bool rbitset_is_set(const unsigned *bitset, unsigned pos)
{
- return BITSET_ELEM(bitset, pos) & (1 << (pos % BITS_PER_ELEM));
+ return (BITSET_ELEM(bitset, pos) & (1 << (pos % BITS_PER_ELEM))) != 0;
}
/**