[or-cvs] r13619: Simplify rounding logic in bitarray; fix a bug in bitarray_e (in tor/trunk: . src/common)

nickm at seul.org nickm at seul.org
Wed Feb 20 16:57:41 UTC 2008


Author: nickm
Date: 2008-02-20 11:57:41 -0500 (Wed, 20 Feb 2008)
New Revision: 13619

Modified:
   tor/trunk/
   tor/trunk/src/common/container.h
Log:
 r18256 at catbus:  nickm | 2008-02-20 11:57:31 -0500
 Simplify rounding logic in bitarray; fix a bug in bitarray_expand().



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r18256] on 8246c3cf-6607-4228-993b-4d95d33730f1

Modified: tor/trunk/src/common/container.h
===================================================================
--- tor/trunk/src/common/container.h	2008-02-20 16:57:39 UTC (rev 13618)
+++ tor/trunk/src/common/container.h	2008-02-20 16:57:41 UTC (rev 13619)
@@ -330,24 +330,25 @@
 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>. */



More information about the tor-commits mailing list