-LRN: fix socket_close logging
[oweals/gnunet.git] / src / util / container_bloomfilter.c
index a0749a18a347da33aeae693e76dfabe043436779..180aab4c3ccbecc903f4d259fc62fc75b4d24db1 100644 (file)
 #include "gnunet_container_lib.h"
 #include "gnunet_disk_lib.h"
 
+#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
+
+#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
+
+#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
+
 struct GNUNET_CONTAINER_BloomFilter
 {
 
@@ -102,8 +108,7 @@ struct GNUNET_CONTAINER_BloomFilter *
 GNUNET_CONTAINER_bloomfilter_copy (const struct GNUNET_CONTAINER_BloomFilter
                                    *bf)
 {
-  return GNUNET_CONTAINER_bloomfilter_init (bf->bitArray,
-                                            bf->bitArraySize,
+  return GNUNET_CONTAINER_bloomfilter_init (bf->bitArray, bf->bitArraySize,
                                             bf->addressesPerElement);
 }
 
@@ -209,9 +214,8 @@ incrementBit (char *bitArray, unsigned int bitIdx,
       high++;
   }
   value = ((high << 4) | low);
-  GNUNET_assert (fileSlot == 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));
 }
 
@@ -318,10 +322,12 @@ makeEmptyFile (const struct GNUNET_DISK_FileHandle *fh, size_t size)
  * @param cls closure
  * @param bf the filter to manipulate
  * @param bit the current bit
+ * @return GNUNET_YES to continue, GNUNET_NO to stop early
  */
-typedef void (*BitIterator) (void *cls,
-                             const struct GNUNET_CONTAINER_BloomFilter * bf,
-                             unsigned int bit);
+typedef int (*BitIterator) (void *cls,
+                            const struct GNUNET_CONTAINER_BloomFilter * bf,
+                            unsigned int bit);
+
 
 /**
  * Call an iterator for each bit that the bloomfilter
@@ -338,20 +344,21 @@ iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
 {
   GNUNET_HashCode tmp[2];
   int bitCount;
-  int round;
+  unsigned int round;
   unsigned int slot = 0;
 
   bitCount = bf->addressesPerElement;
-  memcpy (&tmp[0], key, sizeof (GNUNET_HashCode));
+  tmp[0] = *key;
   round = 0;
   while (bitCount > 0)
   {
     while (slot < (sizeof (GNUNET_HashCode) / sizeof (uint32_t)))
     {
-      callback (arg,
-                bf,
-                (((uint32_t *) & tmp[round & 1])[slot]) &
-                ((bf->bitArraySize * 8) - 1));
+      if (GNUNET_YES !=
+          callback (arg, bf,
+                    (((uint32_t *) & tmp[round & 1])[slot]) &
+                    ((bf->bitArraySize * 8) - 1)))
+        return;
       slot++;
       bitCount--;
       if (bitCount == 0)
@@ -367,56 +374,65 @@ iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
   }
 }
 
+
 /**
  * Callback: increment bit
  *
  * @param cls pointer to writeable form of bf
  * @param bf the filter to manipulate
  * @param bit the bit to increment
+ * @return GNUNET_YES
  */
-static void
-incrementBitCallback (void *cls,
-                      const struct GNUNET_CONTAINER_BloomFilter *bf,
+static int
+incrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
                       unsigned int bit)
 {
   struct GNUNET_CONTAINER_BloomFilter *b = cls;
 
   incrementBit (b->bitArray, bit, bf->fh);
+  return GNUNET_YES;
 }
 
+
 /**
  * Callback: decrement bit
  *
  * @param cls pointer to writeable form of bf
  * @param bf the filter to manipulate
  * @param bit the bit to decrement
+ * @return GNUNET_YES
  */
-static void
-decrementBitCallback (void *cls,
-                      const struct GNUNET_CONTAINER_BloomFilter *bf,
+static int
+decrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
                       unsigned int bit)
 {
   struct GNUNET_CONTAINER_BloomFilter *b = cls;
 
   decrementBit (b->bitArray, bit, bf->fh);
+  return GNUNET_YES;
 }
 
+
 /**
  * Callback: test if all bits are set
  *
  * @param cls pointer set to GNUNET_NO if bit is not set
  * @param bf the filter
  * @param bit the bit to test
+ * @return YES if the bit is set, NO if not
  */
-static void
-testBitCallback (void *cls,
-                 const struct GNUNET_CONTAINER_BloomFilter *bf,
+static int
+testBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
                  unsigned int bit)
 {
   int *arg = cls;
 
   if (GNUNET_NO == testBit (bf->bitArray, bit))
+  {
     *arg = GNUNET_NO;
+    return GNUNET_NO;
+  }
+  return GNUNET_YES;
 }
 
 /* *********************** INTERFACE **************** */
@@ -432,8 +448,8 @@ testBitCallback (void *cls,
  * @return the bloomfilter
  */
 struct GNUNET_CONTAINER_BloomFilter *
-GNUNET_CONTAINER_bloomfilter_load (const char *filename,
-                                   size_t size, unsigned int k)
+GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
+                                   unsigned int k)
 {
   struct GNUNET_CONTAINER_BloomFilter *bf;
   char *rbuff;
@@ -453,10 +469,12 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename,
 
   bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter));
   /* Try to open a bloomfilter file */
-  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);
+  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);
   if (NULL == bf->fh)
   {
     GNUNET_free (bf);
@@ -487,8 +505,7 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename,
     res = GNUNET_DISK_file_read (bf->fh, rbuff, BUFFSIZE);
     if (res == -1)
     {
-      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
-                                "read", bf->filename);
+      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 */
@@ -522,8 +539,8 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename,
  * @return the bloomfilter
  */
 struct GNUNET_CONTAINER_BloomFilter *
-GNUNET_CONTAINER_bloomfilter_init (const char *data,
-                                   size_t size, unsigned int k)
+GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size,
+                                   unsigned int k)
 {
   struct GNUNET_CONTAINER_BloomFilter *bf;
   size_t ui;
@@ -579,6 +596,7 @@ GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct
   return GNUNET_OK;
 }
 
+
 /**
  * Free the space associated with a filter
  * in memory, flush to drive if needed (do not
@@ -598,6 +616,7 @@ GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter *bf)
   GNUNET_free (bf);
 }
 
+
 /**
  * Reset a bloom filter to empty. Clears the file on disk.
  *
@@ -635,6 +654,7 @@ GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter
   return res;
 }
 
+
 /**
  * Add an element to the filter
  *
@@ -645,7 +665,6 @@ void
 GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
                                   const GNUNET_HashCode * e)
 {
-
   if (NULL == bf)
     return;
   iterateBits (bf, &incrementBitCallback, bf, e);
@@ -752,8 +771,8 @@ GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
 void
 GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
                                      GNUNET_HashCodeIterator iterator,
-                                     void *iterator_cls,
-                                     size_t size, unsigned int k)
+                                     void *iterator_cls, size_t size,
+                                     unsigned int k)
 {
   GNUNET_HashCode hc;
   unsigned int i;