bloomfilt.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file container.c
  7. * \brief Implements a smartlist (a resizable array) along
  8. * with helper functions to use smartlists. Also includes
  9. * hash table implementations of a string-to-void* map, and of
  10. * a digest-to-void* map.
  11. **/
  12. #include <stdlib.h>
  13. #include "lib/malloc/util_malloc.h"
  14. #include "lib/container/bloomfilt.h"
  15. #include "lib/intmath/bits.h"
  16. /** Return a newly allocated digestset_t, optimized to hold a total of
  17. * <b>max_elements</b> digests with a reasonably low false positive weight. */
  18. digestset_t *
  19. digestset_new(int max_elements)
  20. {
  21. /* The probability of false positives is about P=(1 - exp(-kn/m))^k, where k
  22. * is the number of hash functions per entry, m is the bits in the array,
  23. * and n is the number of elements inserted. For us, k==4, n<=max_elements,
  24. * and m==n_bits= approximately max_elements*32. This gives
  25. * P<(1-exp(-4*n/(32*n)))^4 == (1-exp(1/-8))^4 == .00019
  26. *
  27. * It would be more optimal in space vs false positives to get this false
  28. * positive rate by going for k==13, and m==18.5n, but we also want to
  29. * conserve CPU, and k==13 is pretty big.
  30. */
  31. int n_bits = 1u << (tor_log2(max_elements)+5);
  32. digestset_t *r = tor_malloc(sizeof(digestset_t));
  33. r->mask = n_bits - 1;
  34. r->ba = bitarray_init_zero(n_bits);
  35. return r;
  36. }
  37. /** Free all storage held in <b>set</b>. */
  38. void
  39. digestset_free_(digestset_t *set)
  40. {
  41. if (!set)
  42. return;
  43. bitarray_free(set->ba);
  44. tor_free(set);
  45. }