aboutsummaryrefslogtreecommitdiff
path: root/src/block
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-02-26 22:42:40 +0100
committerChristian Grothoff <christian@grothoff.org>2017-02-26 22:42:40 +0100
commit8d7c29c4684f807d5e9a3004bbbab132b158c5aa (patch)
tree9eca177e00a387d0c18395fde7dbf91289272d98 /src/block
parente87663cf00dd102e8bb60f400db4bad6d82b3b6c (diff)
downloadgnunet-8d7c29c4684f807d5e9a3004bbbab132b158c5aa.tar.gz
gnunet-8d7c29c4684f807d5e9a3004bbbab132b158c5aa.zip
ensure all plugins properly use BF, move shared logic to shared library
Diffstat (limited to 'src/block')
-rw-r--r--src/block/bg_bf.c32
-rw-r--r--src/block/plugin_block_template.c34
-rw-r--r--src/block/plugin_block_test.c34
3 files changed, 36 insertions, 64 deletions
diff --git a/src/block/bg_bf.c b/src/block/bg_bf.c
index 9c4dc9060..1a17ec84e 100644
--- a/src/block/bg_bf.c
+++ b/src/block/bg_bf.c
@@ -232,4 +232,36 @@ GNUNET_BLOCK_GROUP_bf_test_and_set (struct GNUNET_BLOCK_Group *bg,
232} 232}
233 233
234 234
235/**
236 * How many bytes should a bloomfilter be if we have already seen
237 * entry_count responses? Sized so that do not have to
238 * re-size the filter too often (to keep it cheap).
239 *
240 * Since other peers will also add entries but not resize the filter,
241 * we should generally pick a slightly larger size than what the
242 * strict math would suggest.
243 *
244 * @param entry_count expected number of entries in the Bloom filter
245 * @param k number of bits set per entry
246 * @return must be a power of two and smaller or equal to 2^15.
247 */
248size_t
249GNUNET_BLOCK_GROUP_compute_bloomfilter_size (unsigned int entry_count,
250 unsigned int k)
251{
252 size_t size;
253 unsigned int ideal = (entry_count * k) / 4;
254 uint16_t max = 1 << 15;
255
256 if (entry_count > max)
257 return max;
258 size = 8;
259 while ((size < max) && (size < ideal))
260 size *= 2;
261 if (size > max)
262 return max;
263 return size;
264}
265
266
235/* end of bg_bf.c */ 267/* end of bg_bf.c */
diff --git a/src/block/plugin_block_template.c b/src/block/plugin_block_template.c
index 87adae7e9..b714b6858 100644
--- a/src/block/plugin_block_template.c
+++ b/src/block/plugin_block_template.c
@@ -44,37 +44,6 @@
44 44
45 45
46/** 46/**
47 * How many bytes should a bloomfilter be if we have already seen
48 * entry_count responses? Note that #GNUNET_CONSTANTS_BLOOMFILTER_K
49 * gives us the number of bits set per entry. Furthermore, we should
50 * not re-size the filter too often (to keep it cheap).
51 *
52 * Since other peers will also add entries but not resize the filter,
53 * we should generally pick a slightly larger size than what the
54 * strict math would suggest.
55 *
56 * @param entry_count expected number of entries in the Bloom filter
57 * @return must be a power of two and smaller or equal to 2^15.
58 */
59static size_t
60compute_bloomfilter_size (unsigned int entry_count)
61{
62 size_t size;
63 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
64 uint16_t max = 1 << 15;
65
66 if (entry_count > max)
67 return max;
68 size = 8;
69 while ((size < max) && (size < ideal))
70 size *= 2;
71 if (size > max)
72 return max;
73 return size;
74}
75
76
77/**
78 * Create a new block group. 47 * Create a new block group.
79 * 48 *
80 * @param ctx block context in which the block group is created 49 * @param ctx block context in which the block group is created
@@ -100,7 +69,8 @@ block_plugin_template_create_group (void *cls,
100 guard = va_arg (va, const char *); 69 guard = va_arg (va, const char *);
101 if (0 == strcmp (guard, 70 if (0 == strcmp (guard,
102 "seen-set-size")) 71 "seen-set-size"))
103 bf_size = compute_bloomfilter_size (va_arg (va, unsigned int)); 72 bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int),
73 BLOOMFILTER_K);
104 else if (0 == strcmp (guard, 74 else if (0 == strcmp (guard,
105 "filter-size")) 75 "filter-size"))
106 bf_size = va_arg (va, unsigned int); 76 bf_size = va_arg (va, unsigned int);
diff --git a/src/block/plugin_block_test.c b/src/block/plugin_block_test.c
index 31112e5dd..b9f71cacb 100644
--- a/src/block/plugin_block_test.c
+++ b/src/block/plugin_block_test.c
@@ -42,37 +42,6 @@
42 42
43 43
44/** 44/**
45 * How many bytes should a bloomfilter be if we have already seen
46 * entry_count responses? Note that #GNUNET_CONSTANTS_BLOOMFILTER_K
47 * gives us the number of bits set per entry. Furthermore, we should
48 * not re-size the filter too often (to keep it cheap).
49 *
50 * Since other peers will also add entries but not resize the filter,
51 * we should generally pick a slightly larger size than what the
52 * strict math would suggest.
53 *
54 * @param entry_count expected number of entries in the Bloom filter
55 * @return must be a power of two and smaller or equal to 2^15.
56 */
57static size_t
58compute_bloomfilter_size (unsigned int entry_count)
59{
60 size_t size;
61 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
62 uint16_t max = 1 << 15;
63
64 if (entry_count > max)
65 return max;
66 size = 8;
67 while ((size < max) && (size < ideal))
68 size *= 2;
69 if (size > max)
70 return max;
71 return size;
72}
73
74
75/**
76 * Create a new block group. 45 * Create a new block group.
77 * 46 *
78 * @param ctx block context in which the block group is created 47 * @param ctx block context in which the block group is created
@@ -98,7 +67,8 @@ block_plugin_test_create_group (void *cls,
98 guard = va_arg (va, const char *); 67 guard = va_arg (va, const char *);
99 if (0 == strcmp (guard, 68 if (0 == strcmp (guard,
100 "seen-set-size")) 69 "seen-set-size"))
101 bf_size = compute_bloomfilter_size (va_arg (va, unsigned int)); 70 bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int),
71 BLOOMFILTER_K);
102 else if (0 == strcmp (guard, 72 else if (0 == strcmp (guard,
103 "filter-size")) 73 "filter-size"))
104 bf_size = va_arg (va, unsigned int); 74 bf_size = va_arg (va, unsigned int);