diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-04-21 18:16:21 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-04-21 18:16:21 +0000 |
commit | 2f9e8c5139931e25f621b604fc3e142df54dc70d (patch) | |
tree | 545cee525080db7f99917534b1b847b5fa91e30a /src/util/container_bloomfilter.c | |
parent | 5d578f1d05cb1d3ea47bbe605d378f48bd596359 (diff) | |
download | gnunet-2f9e8c5139931e25f621b604fc3e142df54dc70d.tar.gz gnunet-2f9e8c5139931e25f621b604fc3e142df54dc70d.zip |
changing bloomfilter to allow GNUNET_CONTAINER_bloomfilter_init with sizes that are not powers of 2 -- GNUNET_CONTAINER_bloomfilter_load will continue to round up to power of two
Diffstat (limited to 'src/util/container_bloomfilter.c')
-rw-r--r-- | src/util/container_bloomfilter.c | 31 |
1 files changed, 10 insertions, 21 deletions
diff --git a/src/util/container_bloomfilter.c b/src/util/container_bloomfilter.c index 84aab6b17..c2f376b40 100644 --- a/src/util/container_bloomfilter.c +++ b/src/util/container_bloomfilter.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | (C) 2001, 2002, 2003, 2004, 2006, 2008, 2011 Christian Grothoff (and other contributing authors) | 3 | (C) 2001, 2002, 2003, 2004, 2006, 2008, 2011, 2012 Christian Grothoff (and other contributing authors) |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 5 | GNUnet is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published | 6 | it under the terms of the GNU General Public License as published |
@@ -350,14 +350,16 @@ iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf, | |||
350 | bitCount = bf->addressesPerElement; | 350 | bitCount = bf->addressesPerElement; |
351 | tmp[0] = *key; | 351 | tmp[0] = *key; |
352 | round = 0; | 352 | round = 0; |
353 | GNUNET_assert (bf->bitArraySize > 0); | ||
354 | GNUNET_assert (bf->bitArraySize * 8LL > bf->bitArraySize); | ||
353 | while (bitCount > 0) | 355 | while (bitCount > 0) |
354 | { | 356 | { |
355 | while (slot < (sizeof (GNUNET_HashCode) / sizeof (uint32_t))) | 357 | while (slot < (sizeof (GNUNET_HashCode) / sizeof (uint32_t))) |
356 | { | 358 | { |
357 | if (GNUNET_YES != | 359 | if (GNUNET_YES != |
358 | callback (arg, bf, | 360 | callback (arg, bf, |
359 | (((uint32_t *) & tmp[round & 1])[slot]) & | 361 | (((uint32_t *) & tmp[round & 1])[slot]) % |
360 | ((bf->bitArraySize * 8) - 1))) | 362 | ((bf->bitArraySize * 8LL) - 1))) |
361 | return; | 363 | return; |
362 | slot++; | 364 | slot++; |
363 | bitCount--; | 365 | bitCount--; |
@@ -442,7 +444,8 @@ testBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf, | |||
442 | * | 444 | * |
443 | * @param filename the name of the file (or the prefix) | 445 | * @param filename the name of the file (or the prefix) |
444 | * @param size the size of the bloom-filter (number of | 446 | * @param size the size of the bloom-filter (number of |
445 | * bytes of storage space to use) | 447 | * bytes of storage space to use); will be rounded up |
448 | * to next power of 2 | ||
446 | * @param k the number of GNUNET_CRYPTO_hash-functions to apply per | 449 | * @param k the number of GNUNET_CRYPTO_hash-functions to apply per |
447 | * element (number of bits set per element in the set) | 450 | * element (number of bits set per element in the set) |
448 | * @return the bloomfilter | 451 | * @return the bloomfilter |
@@ -549,8 +552,6 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size, | |||
549 | } | 552 | } |
550 | bf->bitArraySize = size; | 553 | bf->bitArraySize = size; |
551 | bf->addressesPerElement = k; | 554 | bf->addressesPerElement = k; |
552 | memset (bf->bitArray, 0, bf->bitArraySize); | ||
553 | |||
554 | if (GNUNET_YES != must_read) | 555 | if (GNUNET_YES != must_read) |
555 | return bf; /* already done! */ | 556 | return bf; /* already done! */ |
556 | /* Read from the file what bits we can */ | 557 | /* Read from the file what bits we can */ |
@@ -606,33 +607,22 @@ GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size, | |||
606 | unsigned int k) | 607 | unsigned int k) |
607 | { | 608 | { |
608 | struct GNUNET_CONTAINER_BloomFilter *bf; | 609 | struct GNUNET_CONTAINER_BloomFilter *bf; |
609 | size_t ui; | ||
610 | 610 | ||
611 | if ((k == 0) || (size == 0)) | 611 | if ((0 == k) || (0 == size)) |
612 | return NULL; | ||
613 | ui = 1; | ||
614 | while (ui < size) | ||
615 | ui *= 2; | ||
616 | if (size != ui) | ||
617 | { | ||
618 | GNUNET_break (0); | ||
619 | return NULL; | 612 | return NULL; |
620 | } | ||
621 | bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter)); | 613 | bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter)); |
622 | bf->filename = NULL; | 614 | bf->filename = NULL; |
623 | bf->fh = NULL; | 615 | bf->fh = NULL; |
624 | bf->bitArray = GNUNET_malloc_large (size); | 616 | bf->bitArray = GNUNET_malloc_large (size); |
625 | if (bf->bitArray == NULL) | 617 | if (NULL == bf->bitArray) |
626 | { | 618 | { |
627 | GNUNET_free (bf); | 619 | GNUNET_free (bf); |
628 | return NULL; | 620 | return NULL; |
629 | } | 621 | } |
630 | bf->bitArraySize = size; | 622 | bf->bitArraySize = size; |
631 | bf->addressesPerElement = k; | 623 | bf->addressesPerElement = k; |
632 | if (data != NULL) | 624 | if (NULL != data) |
633 | memcpy (bf->bitArray, data, size); | 625 | memcpy (bf->bitArray, data, size); |
634 | else | ||
635 | memset (bf->bitArray, 0, bf->bitArraySize); | ||
636 | return bf; | 626 | return bf; |
637 | } | 627 | } |
638 | 628 | ||
@@ -848,7 +838,6 @@ GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf, | |||
848 | 838 | ||
849 | bf->bitArraySize = size; | 839 | bf->bitArraySize = size; |
850 | bf->bitArray = GNUNET_malloc (size); | 840 | bf->bitArray = GNUNET_malloc (size); |
851 | memset (bf->bitArray, 0, bf->bitArraySize); | ||
852 | if (bf->filename != NULL) | 841 | if (bf->filename != NULL) |
853 | make_empty_file (bf->fh, bf->bitArraySize * 4LL); | 842 | make_empty_file (bf->fh, bf->bitArraySize * 4LL); |
854 | while (GNUNET_YES == iterator (iterator_cls, &hc)) | 843 | while (GNUNET_YES == iterator (iterator_cls, &hc)) |