[or-cvs] r10938: Add a bit-array type with reasonably fast inline functions. (in tor/trunk: . src/common src/or)

nickm at seul.org nickm at seul.org
Thu Jul 26 21:26:54 UTC 2007


Author: nickm
Date: 2007-07-26 17:26:53 -0400 (Thu, 26 Jul 2007)
New Revision: 10938

Modified:
   tor/trunk/
   tor/trunk/src/common/container.h
   tor/trunk/src/or/test.c
Log:
 r13926 at catbus:  nickm | 2007-07-26 17:21:06 -0400
 Add a bit-array type with reasonably fast inline functions.



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

Modified: tor/trunk/src/common/container.h
===================================================================
--- tor/trunk/src/common/container.h	2007-07-26 20:49:04 UTC (rev 10937)
+++ tor/trunk/src/common/container.h	2007-07-26 21:26:53 UTC (rev 10938)
@@ -267,5 +267,49 @@
     return digestmap_iter_done((digestmap_iter_t*)iter);                \
   }
 
+#if SIZEOF_INT == 4
+#define BITARRAY_SHIFT 5
+#define BITARRAY_MASK 31
+#elif SIZEOF_INT == 8
+#define BITARRAY_SHIFT 6
+#define BITARRAY_MASK 63
+#else
+#error "int is neither 4 nor 8 bites. I can't deal with that."
 #endif
 
+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)
+{
+  size_t sz = (n_bits+BITARRAY_MASK) & BITARRAY_MASK;
+  return tor_malloc_zero(sz*sizeof(unsigned int));
+}
+/** Free the bit array <b>ba</b>. */
+static INLINE void
+bitarray_free(bitarray_t *ba)
+{
+  tor_free(ba);
+}
+/** Set the <b>bit</b>th bit in <b>b</b> to 1. */
+static INLINE void
+bitarray_set(bitarray_t *b, int bit)
+{
+  b[bit >> BITARRAY_SHIFT] |= (1u << (bit & BITARRAY_MASK));
+}
+/** Set the <b>bit</b>th bit in <b>b</b> to 0. */
+static INLINE void
+bitarray_clear(bitarray_t *b, int bit)
+{
+  b[bit >> BITARRAY_SHIFT] &= ~ (1u << (bit & BITARRAY_MASK));
+}
+/** Return true iff <b>bit</b>th bit in <b>b</b> is nonzero.  NOTE: does
+ * not necessarily return 1 on true. */
+static INLINE unsigned int
+bitarray_is_set(bitarray_t *b, int bit)
+{
+  return b[bit >> BITARRAY_SHIFT] & (1u << (bit & BITARRAY_MASK));
+}
+
+#endif
+

Modified: tor/trunk/src/or/test.c
===================================================================
--- tor/trunk/src/or/test.c	2007-07-26 20:49:04 UTC (rev 10937)
+++ tor/trunk/src/or/test.c	2007-07-26 21:26:53 UTC (rev 10938)
@@ -1544,6 +1544,43 @@
   smartlist_free(sl);
 }
 
+static void
+test_bitarray(void)
+{
+  bitarray_t *ba;
+  int i, j, ok=1;
+
+  ba = bitarray_init_zero(1);
+  test_assert(! bitarray_is_set(ba, 0));
+  bitarray_set(ba, 0);
+  test_assert(bitarray_is_set(ba, 0));
+  bitarray_clear(ba, 0);
+  test_assert(! bitarray_is_set(ba, 0));
+  bitarray_free(ba);
+
+  ba = bitarray_init_zero(1023);
+  for (i = 1; i < 64; ) {
+    for (j = 0; j < 1023; ++j) {
+      if (j % i)
+        bitarray_set(ba, j);
+      else
+        bitarray_clear(ba, j);
+    }
+    for (j = 0; j < 1023; ++j) {
+      if (!bool_eq(bitarray_is_set(ba, j), j%i))
+        ok = 0;
+    }
+    test_assert(ok);
+    if (i < 7)
+      ++i;
+    else if (i == 28)
+      i = 32;
+    else
+      i += 7;
+  }
+  bitarray_free(ba);
+}
+
 /* stop threads running at once. */
 static tor_mutex_t *_thread_test_mutex = NULL;
 /* make sure that threads have to run at the same time. */
@@ -2990,6 +3027,8 @@
   test_util();
   puts("\n--smartlist");
   test_smartlist();
+  puts("\n--bitarray");
+  test_bitarray();
   puts("\n--mempool");
   test_mempool();
   puts("\n--strmap");



More information about the tor-commits mailing list