Tell core that we want to have this packet delivered
[oweals/gnunet.git] / src / util / container_bloomfilter.c
index 6257ea30a5535311811b406fb2c939335a86dc28..d9cadb9451c9c16e93212991efdf677119b25fd4 100644 (file)
@@ -75,6 +75,23 @@ struct GNUNET_CONTAINER_BloomFilter
 };
 
 
+
+/**
+ * Get size of the bloom filter.
+ *
+ * @param bf the filter
+ * @return number of bytes used for the data of the bloom filter
+ */
+size_t 
+GNUNET_CONTAINER_bloomfilter_get_size (const struct GNUNET_CONTAINER_BloomFilter
+                                      *bf)
+{
+  if (bf == NULL)
+    return 0;
+  return bf->bitArraySize;
+}
+
+
 /**
  * Sets a bit active in the bitArray. Increment bit-specific
  * usage counter on disk only if below 4bit max (==15).
@@ -287,7 +304,7 @@ makeEmptyFile (const struct GNUNET_DISK_FileHandle *fh, size_t size)
  * @param bit the current bit
  */
 typedef void (*BitIterator) (void *cls,
-                             struct GNUNET_CONTAINER_BloomFilter * bf,
+                             const struct GNUNET_CONTAINER_BloomFilter * bf,
                              unsigned int bit);
 
 /**
@@ -300,7 +317,7 @@ typedef void (*BitIterator) (void *cls,
  * @param key the key for which we iterate over the BF bits
  */
 static void
-iterateBits (struct GNUNET_CONTAINER_BloomFilter *bf,
+iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
              BitIterator callback, void *arg, const GNUNET_HashCode * key)
 {
   GNUNET_HashCode tmp[2];
@@ -337,31 +354,33 @@ iterateBits (struct GNUNET_CONTAINER_BloomFilter *bf,
 /**
  * Callback: increment bit
  *
- * @param cls not used
+ * @param cls pointer to writeable form of bf
  * @param bf the filter to manipulate
  * @param bit the bit to increment
  */
 static void
 incrementBitCallback (void *cls,
-                      struct GNUNET_CONTAINER_BloomFilter *bf,
+                      const struct GNUNET_CONTAINER_BloomFilter *bf,
                       unsigned int bit)
 {
-  incrementBit (bf->bitArray, bit, bf->fh);
+  struct GNUNET_CONTAINER_BloomFilter *b = cls;
+  incrementBit (b->bitArray, bit, bf->fh);
 }
 
 /**
  * Callback: decrement bit
  *
- * @param cls not used
+ * @param cls pointer to writeable form of bf
  * @param bf the filter to manipulate
  * @param bit the bit to decrement
  */
 static void
 decrementBitCallback (void *cls,
-                      struct GNUNET_CONTAINER_BloomFilter *bf,
+                      const struct GNUNET_CONTAINER_BloomFilter *bf,
                       unsigned int bit)
 {
-  decrementBit (bf->bitArray, bit, bf->fh);
+  struct GNUNET_CONTAINER_BloomFilter *b = cls;
+  decrementBit (b->bitArray, bit, bf->fh);
 }
 
 /**
@@ -373,7 +392,8 @@ decrementBitCallback (void *cls,
  */
 static void
 testBitCallback (void *cls,
-                 struct GNUNET_CONTAINER_BloomFilter *bf, unsigned int bit)
+                 const struct GNUNET_CONTAINER_BloomFilter *bf,
+                unsigned int bit)
 {
   int *arg = cls;
   if (GNUNET_NO == testBit (bf->bitArray, bit))
@@ -433,6 +453,14 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename,
     }
   /* Alloc block */
   bf->bitArray = GNUNET_malloc_large (size);
+  if (bf->bitArray == NULL)
+    {
+      if (bf->fh != NULL)
+       GNUNET_DISK_file_close (bf->fh);
+      GNUNET_free_non_null (bf->filename);
+      GNUNET_free (bf);
+      return NULL;
+    }
   bf->bitArraySize = size;
   bf->addressesPerElement = k;
   memset (bf->bitArray, 0, bf->bitArraySize);
@@ -505,6 +533,11 @@ GNUNET_CONTAINER_bloomfilter_init (const char *data,
   bf->filename = NULL;
   bf->fh = NULL;
   bf->bitArray = GNUNET_malloc_large (size);
+  if (bf->bitArray == NULL)
+    {
+      GNUNET_free (bf);
+      return NULL;
+    }
   bf->bitArraySize = size;
   bf->addressesPerElement = k;
   if (data != NULL)
@@ -525,12 +558,11 @@ GNUNET_CONTAINER_bloomfilter_init (const char *data,
  * @return GNUNET_SYSERR if the data array is not big enough
  */
 int
-GNUNET_CONTAINER_bloomfilter_get_raw_data (struct GNUNET_CONTAINER_BloomFilter
+GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct GNUNET_CONTAINER_BloomFilter
                                            *bf, char *data, size_t size)
 {
   if (NULL == bf)
     return GNUNET_SYSERR;
-
   if (bf->bitArraySize != size)
     return GNUNET_SYSERR;
   memcpy (data, bf->bitArray, size);
@@ -581,7 +613,7 @@ GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf)
  * @return GNUNET_YES if the element is in the filter, GNUNET_NO if not
  */
 int
-GNUNET_CONTAINER_bloomfilter_test (struct GNUNET_CONTAINER_BloomFilter *bf,
+GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter *bf,
                                    const GNUNET_HashCode * e)
 {
   int res;
@@ -606,7 +638,7 @@ GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
 
   if (NULL == bf)
     return;
-  iterateBits (bf, &incrementBitCallback, NULL, e);
+  iterateBits (bf, &incrementBitCallback, bf, e);
 }
 
 
@@ -644,6 +676,41 @@ GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
   return GNUNET_OK;
 }
 
+/**
+ * Or the entries of the given raw data array with the
+ * data of the given bloom filter.  Assumes that
+ * the size of the data array and the current filter
+ * match.
+ *
+ * @param bf the filter
+ * @param to_or the bloomfilter to or-in
+ * @param size number of bytes in data
+ */
+int
+GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf,
+                                  const struct GNUNET_CONTAINER_BloomFilter *to_or,
+                                  size_t size)
+{
+  unsigned int i;
+  unsigned int n;
+  unsigned long long* fc;
+  const unsigned long long* dc;
+
+  if (NULL == bf)
+    return GNUNET_YES;
+  if (bf->bitArraySize != size)
+    return GNUNET_SYSERR;
+  fc = (unsigned long long*) bf->bitArray;
+  dc = (const unsigned long long*) to_or->bitArray;
+  n = size / sizeof (unsigned long long);
+
+  for (i = 0; i < n; i++)
+    fc[i] |= dc[i];
+  for (i = n * sizeof(unsigned long long); i < size; i++)
+    bf->bitArray[i] |= to_or->bitArray[i];
+  return GNUNET_OK;
+}
+
 /**
  * Remove an element from the filter.
  *
@@ -658,7 +725,7 @@ GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
     return;
   if (bf->filename == NULL)
     return;
-  iterateBits (bf, &decrementBitCallback, NULL, e);
+  iterateBits (bf, &decrementBitCallback, bf, e);
 }
 
 /**