From aba802d4156d8c0110c05cf04a611f14d248e7d1 Mon Sep 17 00:00:00 2001 From: nick Date: Thu, 27 Feb 2014 12:57:09 +0000 Subject: [PATCH] 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 --- src/server/bitset.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/server/bitset.h b/src/server/bitset.h index cd0927d..ee7a3a3 100644 --- a/src/server/bitset.h +++ b/src/server/bitset.h @@ -27,9 +27,7 @@ typedef bitfield_word_t * bitfield_p; * this is added to accommodate code that wants to use bytes sizes */ #define BIT_WORDS_FOR_SIZE(_bytes) \ - (((_bytes) + (BITFIELD_WORD_SIZE -1)) & ~(BITFIELD_WORD_SIZE)) -#define BIT_WORDS_FOR_COUNT(_bits) \ - (((_bits) + (BITS_PER_WORD -1)) / (BITFIELD_WORD_SIZE)) + ((_bytes + (BITFIELD_WORD_SIZE-1)) / BITFIELD_WORD_SIZE) /** Return the bit value ''idx'' in array ''b'' */ 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 // bitfield word 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); bitset->size = size;