bitset: Allocate the right amount of memory

We were calculating the wrong number of words per byte in the first
place, and then passing the number of *words* to malloc, which expects
the number of *bytes*.

Fix both errors
This commit is contained in:
nick
2014-02-27 12:57:09 +00:00
parent d146102c2c
commit aba802d415

View File

@@ -27,9 +27,7 @@ typedef bitfield_word_t * bitfield_p;
* this is added to accommodate code that wants to use bytes sizes * this is added to accommodate code that wants to use bytes sizes
*/ */
#define BIT_WORDS_FOR_SIZE(_bytes) \ #define BIT_WORDS_FOR_SIZE(_bytes) \
(((_bytes) + (BITFIELD_WORD_SIZE -1)) & ~(BITFIELD_WORD_SIZE)) ((_bytes + (BITFIELD_WORD_SIZE-1)) / BITFIELD_WORD_SIZE)
#define BIT_WORDS_FOR_COUNT(_bits) \
(((_bits) + (BITS_PER_WORD -1)) / (BITFIELD_WORD_SIZE))
/** Return the bit value ''idx'' in array ''b'' */ /** Return the bit value ''idx'' in array ''b'' */
static inline int bit_get(bitfield_p b, uint64_t idx) { static inline int bit_get(bitfield_p b, uint64_t idx) {
@@ -187,7 +185,7 @@ static inline struct bitset *bitset_alloc( uint64_t size, int resolution )
// calculate a size to allocate that is a multiple of the size of the // calculate a size to allocate that is a multiple of the size of the
// bitfield word // bitfield word
size_t bitfield_size = size_t bitfield_size =
BIT_WORDS_FOR_SIZE((( size + resolution - 1 ) / resolution)); BIT_WORDS_FOR_SIZE((( size + resolution - 1 ) / resolution)) * sizeof( bitfield_word_t );
struct bitset *bitset = xmalloc(sizeof( struct bitset ) + bitfield_size); struct bitset *bitset = xmalloc(sizeof( struct bitset ) + bitfield_size);
bitset->size = size; bitset->size = size;