stuff
[oweals/gnunet.git] / src / util / container_bloomfilter.c
index 5cc947bd6663e41b4a2e526bc549f829188283a4..6257ea30a5535311811b406fb2c939335a86dc28 100644 (file)
@@ -70,7 +70,7 @@ struct GNUNET_CONTAINER_BloomFilter
   /**
    * Size of bitArray in bytes
    */
-  unsigned int bitArraySize;
+  size_t bitArraySize;
 
 };
 
@@ -85,7 +85,7 @@ struct GNUNET_CONTAINER_BloomFilter
 static void
 setBit (char *bitArray, unsigned int bitIdx)
 {
-  unsigned int arraySlot;
+  size_t arraySlot;
   unsigned int targetBit;
 
   arraySlot = bitIdx / 8;
@@ -103,7 +103,7 @@ setBit (char *bitArray, unsigned int bitIdx)
 static void
 clearBit (char *bitArray, unsigned int bitIdx)
 {
-  unsigned int slot;
+  size_t slot;
   unsigned int targetBit;
 
   slot = bitIdx / 8;
@@ -121,7 +121,7 @@ clearBit (char *bitArray, unsigned int bitIdx)
 static int
 testBit (char *bitArray, unsigned int bitIdx)
 {
-  unsigned int slot;
+  size_t slot;
   unsigned int targetBit;
 
   slot = bitIdx / 8;
@@ -142,9 +142,10 @@ testBit (char *bitArray, unsigned int bitIdx)
  * @param fh A file to keep the 4 bit address usage counters in
  */
 static void
-incrementBit (char *bitArray, unsigned int bitIdx, const struct GNUNET_DISK_FileHandle *fh)
+incrementBit (char *bitArray, unsigned int bitIdx,
+              const struct GNUNET_DISK_FileHandle *fh)
 {
-  unsigned int fileSlot;
+  off_t fileSlot;
   unsigned char value;
   unsigned int high;
   unsigned int low;
@@ -157,7 +158,8 @@ incrementBit (char *bitArray, unsigned int bitIdx, const struct GNUNET_DISK_File
   fileSlot = bitIdx / 2;
   targetLoc = bitIdx % 2;
 
-  GNUNET_assert (fileSlot == (unsigned int) GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET));
+  GNUNET_assert (fileSlot ==
+                 GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET));
   if (1 != GNUNET_DISK_file_read (fh, &value, 1))
     value = 0;
   low = value & 0xF;
@@ -174,7 +176,9 @@ incrementBit (char *bitArray, unsigned int bitIdx, const struct GNUNET_DISK_File
         high++;
     }
   value = ((high << 4) | low);
-  GNUNET_assert (fileSlot == (unsigned int) GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET));
+  GNUNET_assert (fileSlot == GNUNET_DISK_file_seek (fh,
+                                                    fileSlot,
+                                                    GNUNET_DISK_SEEK_SET));
   GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
 }
 
@@ -187,9 +191,10 @@ incrementBit (char *bitArray, unsigned int bitIdx, const struct GNUNET_DISK_File
  * @param fh A file to keep the 4bit address usage counters in
  */
 static void
-decrementBit (char *bitArray, unsigned int bitIdx, const struct GNUNET_DISK_FileHandle *fh)
+decrementBit (char *bitArray, unsigned int bitIdx,
+              const struct GNUNET_DISK_FileHandle *fh)
 {
-  unsigned int fileSlot;
+  off_t fileSlot;
   unsigned char value;
   unsigned int high;
   unsigned int low;
@@ -240,10 +245,10 @@ decrementBit (char *bitArray, unsigned int bitIdx, const struct GNUNET_DISK_File
  * @return GNUNET_OK if created ok, GNUNET_SYSERR otherwise
  */
 static int
-makeEmptyFile (const struct GNUNET_DISK_FileHandle *fh, unsigned int size)
+makeEmptyFile (const struct GNUNET_DISK_FileHandle *fh, size_t size)
 {
   char *buffer;
-  unsigned int bytesleft = size;
+  size_t bytesleft = size;
   int res = 0;
 
   if (GNUNET_DISK_handle_invalid (fh))
@@ -270,7 +275,7 @@ makeEmptyFile (const struct GNUNET_DISK_FileHandle *fh, unsigned int size)
   return GNUNET_OK;
 }
 
-/* ************** GNUNET_CONTAINER_BloomFilter GNUNET_CRYPTO_hash iterator ********* */
+/* ************** GNUNET_CONTAINER_BloomFilter iterator ********* */
 
 /**
  * Iterator (callback) method to be called by the
@@ -282,7 +287,7 @@ makeEmptyFile (const struct GNUNET_DISK_FileHandle *fh, unsigned int size)
  * @param bit the current bit
  */
 typedef void (*BitIterator) (void *cls,
-                            struct GNUNET_CONTAINER_BloomFilter * bf,
+                             struct GNUNET_CONTAINER_BloomFilter * bf,
                              unsigned int bit);
 
 /**
@@ -308,11 +313,11 @@ iterateBits (struct GNUNET_CONTAINER_BloomFilter *bf,
   round = 0;
   while (bitCount > 0)
     {
-      while (slot < (sizeof (GNUNET_HashCode) / sizeof (unsigned int)))
+      while (slot < (sizeof (GNUNET_HashCode) / sizeof (uint32_t)))
         {
-          callback (arg, 
-                   bf,
-                    (((unsigned int *) &tmp[round & 1])[slot]) &
+          callback (arg,
+                    bf,
+                    (((uint32_t *) & tmp[round & 1])[slot]) &
                     ((bf->bitArraySize * 8) - 1));
           slot++;
           bitCount--;
@@ -338,7 +343,7 @@ iterateBits (struct GNUNET_CONTAINER_BloomFilter *bf,
  */
 static void
 incrementBitCallback (void *cls,
-                     struct GNUNET_CONTAINER_BloomFilter *bf,
+                      struct GNUNET_CONTAINER_BloomFilter *bf,
                       unsigned int bit)
 {
   incrementBit (bf->bitArray, bit, bf->fh);
@@ -353,7 +358,7 @@ incrementBitCallback (void *cls,
  */
 static void
 decrementBitCallback (void *cls,
-                     struct GNUNET_CONTAINER_BloomFilter *bf,
+                      struct GNUNET_CONTAINER_BloomFilter *bf,
                       unsigned int bit)
 {
   decrementBit (bf->bitArray, bit, bf->fh);
@@ -368,7 +373,7 @@ decrementBitCallback (void *cls,
  */
 static void
 testBitCallback (void *cls,
-                struct GNUNET_CONTAINER_BloomFilter *bf, unsigned int bit)
+                 struct GNUNET_CONTAINER_BloomFilter *bf, unsigned int bit)
 {
   int *arg = cls;
   if (GNUNET_NO == testBit (bf->bitArray, bit))
@@ -388,14 +393,14 @@ testBitCallback (void *cls,
  * @return the bloomfilter
  */
 struct GNUNET_CONTAINER_BloomFilter *
-GNUNET_CONTAINER_bloomfilter_load (const char *filename, unsigned int size,
-                                   unsigned int k)
+GNUNET_CONTAINER_bloomfilter_load (const char *filename,
+                                   size_t size, unsigned int k)
 {
   struct GNUNET_CONTAINER_BloomFilter *bf;
   char *rbuff;
-  unsigned int pos;
+  off_t pos;
   int i;
-  unsigned int ui;
+  size_t ui;
 
   if ((k == 0) || (size == 0))
     return NULL;
@@ -411,8 +416,9 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, unsigned int size,
   if (filename != NULL)
     {
       bf->fh = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READWRITE
-                                     | GNUNET_DISK_OPEN_CREATE,
-          GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
+                                      | GNUNET_DISK_OPEN_CREATE,
+                                      GNUNET_DISK_PERM_USER_READ |
+                                      GNUNET_DISK_PERM_USER_WRITE);
       if (NULL == bf->fh)
         {
           GNUNET_free (bf);
@@ -441,12 +447,11 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, unsigned int size,
           int res;
 
           res = GNUNET_DISK_file_read (bf->fh, rbuff, BUFFSIZE);
-         if (res == -1)
-           {
-             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
-                                       "read",
-                                       bf->filename);
-           }
+          if (res == -1)
+            {
+              GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
+                                        "read", bf->filename);
+            }
           if (res == 0)
             break;              /* is ok! we just did not use that many bits yet */
           for (i = 0; i < res; i++)
@@ -480,11 +485,11 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, unsigned int size,
  * @return the bloomfilter
  */
 struct GNUNET_CONTAINER_BloomFilter *
-GNUNET_CONTAINER_bloomfilter_init (const char *data, unsigned int size,
-                                   unsigned int k)
+GNUNET_CONTAINER_bloomfilter_init (const char *data,
+                                   size_t size, unsigned int k)
 {
   struct GNUNET_CONTAINER_BloomFilter *bf;
-  unsigned int ui;
+  size_t ui;
 
   if ((k == 0) || (size == 0))
     return NULL;
@@ -514,13 +519,14 @@ GNUNET_CONTAINER_bloomfilter_init (const char *data, unsigned int size,
  * Copy the raw data of this bloomfilter into
  * the given data array.
  *
+ * @param bf bloomfilter to take the raw data from
  * @param data where to write the data
  * @param size the size of the given data array
  * @return GNUNET_SYSERR if the data array is not big enough
  */
 int
 GNUNET_CONTAINER_bloomfilter_get_raw_data (struct GNUNET_CONTAINER_BloomFilter
-                                           *bf, char *data, unsigned int size)
+                                           *bf, char *data, size_t size)
 {
   if (NULL == bf)
     return GNUNET_SYSERR;
@@ -543,11 +549,9 @@ GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter *bf)
 {
   if (NULL == bf)
     return;
-  if (bf->filename != NULL)
-    {
-      GNUNET_DISK_file_close (bf->fh);
-      GNUNET_free (bf->filename);
-    }
+  if (bf->fh != NULL)
+    GNUNET_DISK_file_close (bf->fh);
+  GNUNET_free_non_null (bf->filename);
   GNUNET_free (bf->bitArray);
   GNUNET_free (bf);
 }
@@ -611,21 +615,31 @@ GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
  * data of the given bloom filter.  Assumes that
  * the size of the data array and the current filter
  * match.
+ *
  * @param bf the filter
+ * @param data the data to or-in
+ * @param size number of bytes in data
  */
 int
 GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
-                                 const char *data, unsigned int size)
+                                 const char *data, 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;
-  /* FIXME: we could do this 4-8x faster by
-     going over int/long arrays */
-  for (i = 0; i < size; i++)
+  fc = (unsigned long long*) bf->bitArray;
+  dc = (const unsigned long long*) data;
+  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] |= data[i];
   return GNUNET_OK;
 }
@@ -654,15 +668,15 @@ GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
  *
  * @param bf the filter
  * @param iterator an iterator over all elements stored in the BF
- * @param iterator_arg argument to the iterator function
+ * @param iterator_cls argument to the iterator function
  * @param size the new size for the filter
  * @param k the new number of GNUNET_CRYPTO_hash-function to apply per element
  */
 void
 GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
                                      GNUNET_HashCodeIterator iterator,
-                                     void *iterator_arg, unsigned int size,
-                                     unsigned int k)
+                                     void *iterator_cls,
+                                     size_t size, unsigned int k)
 {
   GNUNET_HashCode hc;
   unsigned int i;
@@ -678,7 +692,7 @@ GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
   memset (bf->bitArray, 0, bf->bitArraySize);
   if (bf->filename != NULL)
     makeEmptyFile (bf->fh, bf->bitArraySize * 4);
-  while (GNUNET_YES == iterator (iterator_arg, &hc))
+  while (GNUNET_YES == iterator (iterator_cls, &hc))
     GNUNET_CONTAINER_bloomfilter_add (bf, &hc);
 }