diff options
author | Nick Mathewson <nickm@torproject.org> | 2008-02-20 16:57:41 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2008-02-20 16:57:41 +0000 |
commit | 88efec10a2f4f06ee667c6dea49a27b87dc6aaa1 (patch) | |
tree | 30832890f9d45ad8cf119f49c030c0ab78f012da | |
parent | cefe0a1959bb62fb36170dc3ff8c15b9971bb822 (diff) | |
download | tor-88efec10a2f4f06ee667c6dea49a27b87dc6aaa1.tar.gz tor-88efec10a2f4f06ee667c6dea49a27b87dc6aaa1.zip |
r18256@catbus: nickm | 2008-02-20 11:57:31 -0500
Simplify rounding logic in bitarray; fix a bug in bitarray_expand().
svn:r13619
-rw-r--r-- | src/common/container.h | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/src/common/container.h b/src/common/container.h index 4622c934b7..058f22ef55 100644 --- a/src/common/container.h +++ b/src/common/container.h @@ -330,24 +330,25 @@ void* strmap_remove_lc(strmap_t *map, const char *key); typedef unsigned int bitarray_t; /** Create a new bit array that can hold <b>n_bits</b> bits. */ static INLINE bitarray_t * -bitarray_init_zero(int n_bits) +bitarray_init_zero(unsigned int n_bits) { - size_t sz = (n_bits+BITARRAY_MASK) / (1u << BITARRAY_SHIFT); + /* round up to the next int. */ + size_t sz = (n_bits+BITARRAY_MASK) >> BITARRAY_SHIFT; return tor_malloc_zero(sz*sizeof(unsigned int)); } static INLINE bitarray_t * -bitarray_expand(bitarray_t *ba, int n_bits_old, int n_bits_new) +bitarray_expand(bitarray_t *ba, + unsigned int n_bits_old, unsigned int n_bits_new) { - size_t sz_old = (n_bits_old+BITARRAY_MASK) / (1u << BITARRAY_SHIFT); - size_t sz_new = (n_bits_new+BITARRAY_MASK) / (1u << BITARRAY_SHIFT); + size_t sz_old = (n_bits_old+BITARRAY_MASK) >> BITARRAY_SHIFT; + size_t sz_new = (n_bits_new+BITARRAY_MASK) >> BITARRAY_SHIFT; char *ptr; if (sz_new <= sz_old) return ba; - ptr = tor_realloc(ba, sz_new); - memset(ptr+sz_old, 0, sz_new-sz_old); /* This does nothing to the older - * excess bytes. But they were - * already set to 0 by - * bitarry_init_zero. */ + ptr = tor_realloc(ba, sz_new*sizeof(unsigned int)); + /* This memset does nothing to the older excess bytes. But they were + * already set to 0 by bitarry_init_zero. */ + memset(ptr+sz_old, 0, (sz_new-sz_old)*sizeof(unsigned int)); return (bitarray_t*) ptr; } /** Free the bit array <b>ba</b>. */ |