aboutsummaryrefslogtreecommitdiff
path: root/src/util/container_multipeermap.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/container_multipeermap.c')
-rw-r--r--src/util/container_multipeermap.c1031
1 files changed, 0 insertions, 1031 deletions
diff --git a/src/util/container_multipeermap.c b/src/util/container_multipeermap.c
deleted file mode 100644
index fa4d2210b..000000000
--- a/src/util/container_multipeermap.c
+++ /dev/null
@@ -1,1031 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2008, 2012 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file util/container_multipeermap.c
22 * @brief hash map where the same key may be present multiple times
23 * @author Christian Grothoff
24 */
25
26#include "platform.h"
27#include "gnunet_util_lib.h"
28
29#define LOG(kind, ...) \
30 GNUNET_log_from (kind, "util-container-multipeermap", __VA_ARGS__)
31
32/**
33 * Maximum recursion depth for callbacks of
34 * #GNUNET_CONTAINER_multihashmap_get_multiple() themselves s
35 * again calling #GNUNET_CONTAINER_multihashmap_get_multiple().
36 * Should be totally excessive, but if violated we die.
37 */
38#define NEXT_CACHE_SIZE 16
39
40/**
41 * An entry in the hash map with the full key.
42 */
43struct BigMapEntry
44{
45 /**
46 * Value of the entry.
47 */
48 void *value;
49
50 /**
51 * If there is a hash collision, we create a linked list.
52 */
53 struct BigMapEntry *next;
54
55 /**
56 * Key for the entry.
57 */
58 struct GNUNET_PeerIdentity key;
59};
60
61
62/**
63 * An entry in the hash map with just a pointer to the key.
64 */
65struct SmallMapEntry
66{
67 /**
68 * Value of the entry.
69 */
70 void *value;
71
72 /**
73 * If there is a hash collision, we create a linked list.
74 */
75 struct SmallMapEntry *next;
76
77 /**
78 * Key for the entry.
79 */
80 const struct GNUNET_PeerIdentity *key;
81};
82
83
84/**
85 * Entry in the map.
86 */
87union MapEntry
88{
89 /**
90 * Variant used if map entries only contain a pointer to the key.
91 */
92 struct SmallMapEntry *sme;
93
94 /**
95 * Variant used if map entries contain the full key.
96 */
97 struct BigMapEntry *bme;
98};
99
100
101/**
102 * Internal representation of the hash map.
103 */
104struct GNUNET_CONTAINER_MultiPeerMap
105{
106 /**
107 * All of our buckets.
108 */
109 union MapEntry *map;
110
111 /**
112 * Number of entries in the map.
113 */
114 unsigned int size;
115
116 /**
117 * Length of the "map" array.
118 */
119 unsigned int map_length;
120
121 /**
122 * #GNUNET_NO if the map entries are of type 'struct BigMapEntry',
123 * #GNUNET_YES if the map entries are of type 'struct SmallMapEntry'.
124 */
125 int use_small_entries;
126
127 /**
128 * Counts the destructive modifications (grow, remove)
129 * to the map, so that iterators can check if they are still valid.
130 */
131 unsigned int modification_counter;
132
133 /**
134 * Map entries indicating iteration positions currently
135 * in use by #GNUNET_CONTAINER_multihashmap_get_multiple().
136 * Only used up to @e next_cache_off.
137 */
138 union MapEntry next_cache[NEXT_CACHE_SIZE];
139
140 /**
141 * Offset of @e next_cache entries in use, must be smaller
142 * than #NEXT_CACHE_SIZE.
143 */
144 unsigned int next_cache_off;
145};
146
147
148/**
149 * Cursor into a multipeermap.
150 * Allows to enumerate elements asynchronously.
151 */
152struct GNUNET_CONTAINER_MultiPeerMapIterator
153{
154 /**
155 * Position in the bucket 'idx'
156 */
157 union MapEntry me;
158
159 /**
160 * Current bucket index.
161 */
162 unsigned int idx;
163
164 /**
165 * Modification counter as observed on the map when the iterator
166 * was created.
167 */
168 unsigned int modification_counter;
169
170 /**
171 * Map that we are iterating over.
172 */
173 const struct GNUNET_CONTAINER_MultiPeerMap *map;
174};
175
176
177/**
178 * Create a multi hash map.
179 *
180 * @param len initial size (map will grow as needed)
181 * @param do_not_copy_keys GNUNET_NO is always safe and should be used by default;
182 * GNUNET_YES means that on 'put', the 'key' does not have
183 * to be copied as the destination of the pointer is
184 * guaranteed to be life as long as the value is stored in
185 * the hashmap. This can significantly reduce memory
186 * consumption, but of course is also a recipe for
187 * heap corruption if the assumption is not true. Only
188 * use this if (1) memory use is important in this case and
189 * (2) you have triple-checked that the invariant holds
190 * @return NULL on error
191 */
192struct GNUNET_CONTAINER_MultiPeerMap *
193GNUNET_CONTAINER_multipeermap_create (unsigned int len,
194 int do_not_copy_keys)
195{
196 struct GNUNET_CONTAINER_MultiPeerMap *map;
197
198 GNUNET_assert (len > 0);
199 map = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMap);
200 map->map = GNUNET_malloc_large (len * sizeof(union MapEntry));
201 if (NULL == map->map)
202 {
203 GNUNET_free (map);
204 return NULL;
205 }
206 map->map_length = len;
207 map->use_small_entries = do_not_copy_keys;
208 return map;
209}
210
211
212/**
213 * Destroy a hash map. Will not free any values
214 * stored in the hash map!
215 *
216 * @param map the map
217 */
218void
219GNUNET_CONTAINER_multipeermap_destroy (
220 struct GNUNET_CONTAINER_MultiPeerMap *map)
221{
222 GNUNET_assert (0 == map->next_cache_off);
223 for (unsigned int i = 0; i < map->map_length; i++)
224 {
225 union MapEntry me;
226
227 me = map->map[i];
228 if (map->use_small_entries)
229 {
230 struct SmallMapEntry *sme;
231 struct SmallMapEntry *nxt;
232
233 nxt = me.sme;
234 while (NULL != (sme = nxt))
235 {
236 nxt = sme->next;
237 GNUNET_free (sme);
238 }
239 me.sme = NULL;
240 }
241 else
242 {
243 struct BigMapEntry *bme;
244 struct BigMapEntry *nxt;
245
246 nxt = me.bme;
247 while (NULL != (bme = nxt))
248 {
249 nxt = bme->next;
250 GNUNET_free (bme);
251 }
252 me.bme = NULL;
253 }
254 }
255 GNUNET_free (map->map);
256 GNUNET_free (map);
257}
258
259
260/**
261 * Compute the index of the bucket for the given key.
262 *
263 * @param map hash map for which to compute the index
264 * @param key what key should the index be computed for
265 * @return offset into the "map" array of "map"
266 */
267static unsigned int
268idx_of (const struct GNUNET_CONTAINER_MultiPeerMap *map,
269 const struct GNUNET_PeerIdentity *key)
270{
271 unsigned int kx;
272
273 GNUNET_assert (NULL != map);
274 GNUNET_memcpy (&kx, key, sizeof(kx));
275 return kx % map->map_length;
276}
277
278
279/**
280 * Get the number of key-value pairs in the map.
281 *
282 * @param map the map
283 * @return the number of key value pairs
284 */
285unsigned int
286GNUNET_CONTAINER_multipeermap_size (
287 const struct GNUNET_CONTAINER_MultiPeerMap *map)
288{
289 return map->size;
290}
291
292
293/**
294 * Given a key find a value in the map matching the key.
295 *
296 * @param map the map
297 * @param key what to look for
298 * @return NULL if no value was found; note that
299 * this is indistinguishable from values that just
300 * happen to be NULL; use "contains" to test for
301 * key-value pairs with value NULL
302 */
303void *
304GNUNET_CONTAINER_multipeermap_get (
305 const struct GNUNET_CONTAINER_MultiPeerMap *map,
306 const struct GNUNET_PeerIdentity *key)
307{
308 union MapEntry me;
309
310 me = map->map[idx_of (map, key)];
311 if (map->use_small_entries)
312 {
313 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
314 if (0 == GNUNET_memcmp (key, sme->key))
315 return sme->value;
316 }
317 else
318 {
319 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
320 if (0 == GNUNET_memcmp (key, &bme->key))
321 return bme->value;
322 }
323 return NULL;
324}
325
326
327/**
328 * Iterate over all entries in the map.
329 *
330 * @param map the map
331 * @param it function to call on each entry
332 * @param it_cls extra argument to @a it
333 * @return the number of key value pairs processed,
334 * #GNUNET_SYSERR if it aborted iteration
335 */
336int
337GNUNET_CONTAINER_multipeermap_iterate (
338 struct GNUNET_CONTAINER_MultiPeerMap *map,
339 GNUNET_CONTAINER_PeerMapIterator it,
340 void *it_cls)
341{
342 int count;
343 union MapEntry me;
344 union MapEntry *ce;
345 struct GNUNET_PeerIdentity kc;
346
347 count = 0;
348 GNUNET_assert (NULL != map);
349 ce = &map->next_cache[map->next_cache_off];
350 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
351 for (unsigned int i = 0; i < map->map_length; i++)
352 {
353 me = map->map[i];
354 if (map->use_small_entries)
355 {
356 struct SmallMapEntry *sme;
357
358 ce->sme = me.sme;
359 while (NULL != (sme = ce->sme))
360 {
361 ce->sme = sme->next;
362 if (NULL != it)
363 {
364 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
365 {
366 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
367 return GNUNET_SYSERR;
368 }
369 }
370 count++;
371 }
372 }
373 else
374 {
375 struct BigMapEntry *bme;
376
377 ce->bme = me.bme;
378 while (NULL != (bme = ce->bme))
379 {
380 ce->bme = bme->next;
381 if (NULL != it)
382 {
383 kc = bme->key;
384 if (GNUNET_OK != it (it_cls, &kc, bme->value))
385 {
386 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
387 return GNUNET_SYSERR;
388 }
389 }
390 count++;
391 }
392 }
393 }
394 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
395 return count;
396}
397
398
399/**
400 * We are about to free() the @a bme, make sure it is not in
401 * the list of next values for any iterator in the @a map's next_cache.
402 *
403 * @param map the map to check
404 * @param bme the entry that is about to be free'd
405 */
406static void
407update_next_cache_bme (struct GNUNET_CONTAINER_MultiPeerMap *map,
408 const struct BigMapEntry *bme)
409{
410 for (unsigned int i = 0; i < map->next_cache_off; i++)
411 if (map->next_cache[i].bme == bme)
412 map->next_cache[i].bme = bme->next;
413}
414
415
416/**
417 * We are about to free() the @a sme, make sure it is not in
418 * the list of next values for any iterator in the @a map's next_cache.
419 *
420 * @param map the map to check
421 * @param sme the entry that is about to be free'd
422 */
423static void
424update_next_cache_sme (struct GNUNET_CONTAINER_MultiPeerMap *map,
425 const struct SmallMapEntry *sme)
426{
427 for (unsigned int i = 0; i < map->next_cache_off; i++)
428 if (map->next_cache[i].sme == sme)
429 map->next_cache[i].sme = sme->next;
430}
431
432
433/**
434 * Remove the given key-value pair from the map. Note that if the
435 * key-value pair is in the map multiple times, only one of the pairs
436 * will be removed.
437 *
438 * @param map the map
439 * @param key key of the key-value pair
440 * @param value value of the key-value pair
441 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
442 * is not in the map
443 */
444int
445GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map,
446 const struct GNUNET_PeerIdentity *key,
447 const void *value)
448{
449 union MapEntry me;
450 unsigned int i;
451
452 map->modification_counter++;
453 i = idx_of (map, key);
454 me = map->map[i];
455 if (map->use_small_entries)
456 {
457 struct SmallMapEntry *p = NULL;
458
459 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
460 {
461 if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value))
462 {
463 if (NULL == p)
464 map->map[i].sme = sme->next;
465 else
466 p->next = sme->next;
467 update_next_cache_sme (map, sme);
468 GNUNET_free (sme);
469 map->size--;
470 return GNUNET_YES;
471 }
472 p = sme;
473 }
474 }
475 else
476 {
477 struct BigMapEntry *p = NULL;
478
479 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
480 {
481 if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value))
482 {
483 if (NULL == p)
484 map->map[i].bme = bme->next;
485 else
486 p->next = bme->next;
487 update_next_cache_bme (map, bme);
488 GNUNET_free (bme);
489 map->size--;
490 return GNUNET_YES;
491 }
492 p = bme;
493 }
494 }
495 return GNUNET_NO;
496}
497
498
499/**
500 * Remove all entries for the given key from the map.
501 * Note that the values would not be "freed".
502 *
503 * @param map the map
504 * @param key identifies values to be removed
505 * @return number of values removed
506 */
507int
508GNUNET_CONTAINER_multipeermap_remove_all (
509 struct GNUNET_CONTAINER_MultiPeerMap *map,
510 const struct GNUNET_PeerIdentity *key)
511{
512 union MapEntry me;
513 unsigned int i;
514 int ret;
515
516 map->modification_counter++;
517
518 ret = 0;
519 i = idx_of (map, key);
520 me = map->map[i];
521 if (map->use_small_entries)
522 {
523 struct SmallMapEntry *sme;
524 struct SmallMapEntry *p;
525
526 p = NULL;
527 sme = me.sme;
528 while (NULL != sme)
529 {
530 if (0 == GNUNET_memcmp (key, sme->key))
531 {
532 if (NULL == p)
533 map->map[i].sme = sme->next;
534 else
535 p->next = sme->next;
536 update_next_cache_sme (map, sme);
537 GNUNET_free (sme);
538 map->size--;
539 if (NULL == p)
540 sme = map->map[i].sme;
541 else
542 sme = p->next;
543 ret++;
544 }
545 else
546 {
547 p = sme;
548 sme = sme->next;
549 }
550 }
551 }
552 else
553 {
554 struct BigMapEntry *bme;
555 struct BigMapEntry *p;
556
557 p = NULL;
558 bme = me.bme;
559 while (NULL != bme)
560 {
561 if (0 == GNUNET_memcmp (key, &bme->key))
562 {
563 if (NULL == p)
564 map->map[i].bme = bme->next;
565 else
566 p->next = bme->next;
567 update_next_cache_bme (map, bme);
568 GNUNET_free (bme);
569 map->size--;
570 if (NULL == p)
571 bme = map->map[i].bme;
572 else
573 bme = p->next;
574 ret++;
575 }
576 else
577 {
578 p = bme;
579 bme = bme->next;
580 }
581 }
582 }
583 return ret;
584}
585
586
587/**
588 * Check if the map contains any value under the given
589 * key (including values that are NULL).
590 *
591 * @param map the map
592 * @param key the key to test if a value exists for it
593 * @return #GNUNET_YES if such a value exists,
594 * #GNUNET_NO if not
595 */
596int
597GNUNET_CONTAINER_multipeermap_contains (
598 const struct GNUNET_CONTAINER_MultiPeerMap *map,
599 const struct GNUNET_PeerIdentity *key)
600{
601 union MapEntry me;
602
603 me = map->map[idx_of (map, key)];
604 if (map->use_small_entries)
605 {
606 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
607 if (0 == GNUNET_memcmp (key, sme->key))
608 return GNUNET_YES;
609 }
610 else
611 {
612 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
613 if (0 == GNUNET_memcmp (key, &bme->key))
614 return GNUNET_YES;
615 }
616 return GNUNET_NO;
617}
618
619
620/**
621 * Check if the map contains the given value under the given
622 * key.
623 *
624 * @param map the map
625 * @param key the key to test if a value exists for it
626 * @param value value to test for
627 * @return #GNUNET_YES if such a value exists,
628 * #GNUNET_NO if not
629 */
630int
631GNUNET_CONTAINER_multipeermap_contains_value (
632 const struct GNUNET_CONTAINER_MultiPeerMap *map,
633 const struct GNUNET_PeerIdentity *key,
634 const void *value)
635{
636 union MapEntry me;
637
638 me = map->map[idx_of (map, key)];
639 if (map->use_small_entries)
640 {
641 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
642 if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value))
643 return GNUNET_YES;
644 }
645 else
646 {
647 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
648 if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value))
649 return GNUNET_YES;
650 }
651 return GNUNET_NO;
652}
653
654
655/**
656 * Grow the given map to a more appropriate size.
657 *
658 * @param map the hash map to grow
659 */
660static void
661grow (struct GNUNET_CONTAINER_MultiPeerMap *map)
662{
663 union MapEntry *old_map;
664 union MapEntry *new_map;
665 unsigned int old_len;
666 unsigned int new_len;
667 unsigned int idx;
668
669 old_map = map->map;
670 old_len = map->map_length;
671 GNUNET_assert (0 != old_len);
672 new_len = old_len * 2;
673 if (0 == new_len) /* 2^31 * 2 == 0 */
674 new_len = old_len; /* never use 0 */
675 if (new_len == old_len)
676 return; /* nothing changed */
677 new_map = GNUNET_malloc_large (new_len * sizeof(union MapEntry));
678 if (NULL == new_map)
679 return; /* grow not possible */
680 map->modification_counter++;
681 map->map_length = new_len;
682 map->map = new_map;
683 for (unsigned int i = 0; i < old_len; i++)
684 {
685 if (map->use_small_entries)
686 {
687 struct SmallMapEntry *sme;
688
689 while (NULL != (sme = old_map[i].sme))
690 {
691 old_map[i].sme = sme->next;
692 idx = idx_of (map, sme->key);
693 sme->next = new_map[idx].sme;
694 new_map[idx].sme = sme;
695 }
696 }
697 else
698 {
699 struct BigMapEntry *bme;
700
701 while (NULL != (bme = old_map[i].bme))
702 {
703 old_map[i].bme = bme->next;
704 idx = idx_of (map, &bme->key);
705 bme->next = new_map[idx].bme;
706 new_map[idx].bme = bme;
707 }
708 }
709 }
710 GNUNET_free (old_map);
711}
712
713
714/**
715 * Store a key-value pair in the map.
716 *
717 * @param map the map
718 * @param key key to use
719 * @param value value to use
720 * @param opt options for put
721 * @return #GNUNET_OK on success,
722 * #GNUNET_NO if a value was replaced (with REPLACE)
723 * #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
724 * value already exists
725 */
726int
727GNUNET_CONTAINER_multipeermap_put (struct GNUNET_CONTAINER_MultiPeerMap *map,
728 const struct GNUNET_PeerIdentity *key,
729 void *value,
730 enum GNUNET_CONTAINER_MultiHashMapOption opt)
731{
732 union MapEntry me;
733 unsigned int i;
734
735 i = idx_of (map, key);
736 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
737 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
738 {
739 me = map->map[i];
740 if (map->use_small_entries)
741 {
742 struct SmallMapEntry *sme;
743
744 for (sme = me.sme; NULL != sme; sme = sme->next)
745 if (0 == GNUNET_memcmp (key, sme->key))
746 {
747 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
748 return GNUNET_SYSERR;
749 sme->value = value;
750 return GNUNET_NO;
751 }
752 }
753 else
754 {
755 struct BigMapEntry *bme;
756
757 for (bme = me.bme; NULL != bme; bme = bme->next)
758 if (0 == GNUNET_memcmp (key, &bme->key))
759 {
760 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
761 return GNUNET_SYSERR;
762 bme->value = value;
763 return GNUNET_NO;
764 }
765 }
766 }
767 if (map->size / 3 >= map->map_length / 4)
768 {
769 grow (map);
770 i = idx_of (map, key);
771 }
772 if (map->use_small_entries)
773 {
774 struct SmallMapEntry *sme;
775
776 sme = GNUNET_new (struct SmallMapEntry);
777 sme->key = key;
778 sme->value = value;
779 sme->next = map->map[i].sme;
780 map->map[i].sme = sme;
781 }
782 else
783 {
784 struct BigMapEntry *bme;
785
786 bme = GNUNET_new (struct BigMapEntry);
787 bme->key = *key;
788 bme->value = value;
789 bme->next = map->map[i].bme;
790 map->map[i].bme = bme;
791 }
792 map->size++;
793 return GNUNET_OK;
794}
795
796
797/**
798 * Iterate over all entries in the map that match a particular key.
799 *
800 * @param map the map
801 * @param key key that the entries must correspond to
802 * @param it function to call on each entry
803 * @param it_cls extra argument to @a it
804 * @return the number of key value pairs processed,
805 * #GNUNET_SYSERR if it aborted iteration
806 */
807int
808GNUNET_CONTAINER_multipeermap_get_multiple (
809 struct GNUNET_CONTAINER_MultiPeerMap *map,
810 const struct GNUNET_PeerIdentity *key,
811 GNUNET_CONTAINER_PeerMapIterator it,
812 void *it_cls)
813{
814 int count;
815 union MapEntry me;
816 union MapEntry *ce;
817
818 ce = &map->next_cache[map->next_cache_off];
819 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
820 count = 0;
821 me = map->map[idx_of (map, key)];
822 if (map->use_small_entries)
823 {
824 struct SmallMapEntry *sme;
825
826 ce->sme = me.sme;
827 while (NULL != (sme = ce->sme))
828 {
829 ce->sme = sme->next;
830 if (0 != GNUNET_memcmp (key, sme->key))
831 continue;
832 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value)))
833 {
834 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
835 return GNUNET_SYSERR;
836 }
837 count++;
838 }
839 }
840 else
841 {
842 struct BigMapEntry *bme;
843
844 ce->bme = me.bme;
845 while (NULL != (bme = ce->bme))
846 {
847 ce->bme = bme->next;
848 if (0 != GNUNET_memcmp (key, &bme->key))
849 continue;
850 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value)))
851 {
852 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
853 return GNUNET_SYSERR;
854 }
855 count++;
856 }
857 }
858 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
859 return count;
860}
861
862
863/**
864 * @ingroup hashmap
865 * Call @a it on a random value from the map, or not at all
866 * if the map is empty. Note that this function has linear
867 * complexity (in the size of the map).
868 *
869 * @param map the map
870 * @param it function to call on a random entry
871 * @param it_cls extra argument to @a it
872 * @return the number of key value pairs processed, zero or one.
873 */
874unsigned int
875GNUNET_CONTAINER_multipeermap_get_random (
876 const struct GNUNET_CONTAINER_MultiPeerMap *map,
877 GNUNET_CONTAINER_PeerMapIterator it,
878 void *it_cls)
879{
880 unsigned int off;
881 union MapEntry me;
882
883 if (0 == map->size)
884 return 0;
885 if (NULL == it)
886 return 1;
887 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size);
888 for (unsigned int idx = 0; idx < map->map_length; idx++)
889 {
890 me = map->map[idx];
891 if (map->use_small_entries)
892 {
893 struct SmallMapEntry *sme;
894 struct SmallMapEntry *nxt;
895
896 nxt = me.sme;
897 while (NULL != (sme = nxt))
898 {
899 nxt = sme->next;
900 if (0 == off)
901 {
902 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
903 return GNUNET_SYSERR;
904 return 1;
905 }
906 off--;
907 }
908 }
909 else
910 {
911 struct BigMapEntry *bme;
912 struct BigMapEntry *nxt;
913
914 nxt = me.bme;
915 while (NULL != (bme = nxt))
916 {
917 nxt = bme->next;
918 if (0 == off)
919 {
920 if (GNUNET_OK != it (it_cls, &bme->key, bme->value))
921 return GNUNET_SYSERR;
922 return 1;
923 }
924 off--;
925 }
926 }
927 }
928 GNUNET_break (0);
929 return GNUNET_SYSERR;
930}
931
932
933/**
934 * Create an iterator for a multipeermap.
935 * The iterator can be used to retrieve all the elements in the multipeermap
936 * one by one, without having to handle all elements at once (in contrast to
937 * #GNUNET_CONTAINER_multipeermap_iterate). Note that the iterator can not be
938 * used anymore if elements have been removed from 'map' after the creation of
939 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
940 * result in skipped or repeated elements.
941 *
942 * @param map the map to create an iterator for
943 * @return an iterator over the given multipeermap 'map'
944 */
945struct GNUNET_CONTAINER_MultiPeerMapIterator *
946GNUNET_CONTAINER_multipeermap_iterator_create (
947 const struct GNUNET_CONTAINER_MultiPeerMap *map)
948{
949 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
950
951 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMapIterator);
952 iter->map = map;
953 iter->modification_counter = map->modification_counter;
954 iter->me = map->map[0];
955 return iter;
956}
957
958
959/**
960 * Retrieve the next element from the hash map at the iterator's position.
961 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
962 * are not modified.
963 * This operation is only allowed if no elements have been removed from the
964 * multipeermap since the creation of 'iter', and the map has not been destroyed.
965 * Adding elements may result in repeating or skipping elements.
966 *
967 * @param iter the iterator to get the next element from
968 * @param key pointer to store the key in, can be NULL
969 * @param value pointer to store the value in, can be NULL
970 * @return #GNUNET_YES we returned an element,
971 * #GNUNET_NO if we are out of elements
972 */
973int
974GNUNET_CONTAINER_multipeermap_iterator_next (
975 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter,
976 struct GNUNET_PeerIdentity *key,
977 const void **value)
978{
979 /* make sure the map has not been modified */
980 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
981
982 /* look for the next entry, skipping empty buckets */
983 while (1)
984 {
985 if (iter->idx >= iter->map->map_length)
986 return GNUNET_NO;
987 if (GNUNET_YES == iter->map->use_small_entries)
988 {
989 if (NULL != iter->me.sme)
990 {
991 if (NULL != key)
992 *key = *iter->me.sme->key;
993 if (NULL != value)
994 *value = iter->me.sme->value;
995 iter->me.sme = iter->me.sme->next;
996 return GNUNET_YES;
997 }
998 }
999 else
1000 {
1001 if (NULL != iter->me.bme)
1002 {
1003 if (NULL != key)
1004 *key = iter->me.bme->key;
1005 if (NULL != value)
1006 *value = iter->me.bme->value;
1007 iter->me.bme = iter->me.bme->next;
1008 return GNUNET_YES;
1009 }
1010 }
1011 iter->idx += 1;
1012 if (iter->idx < iter->map->map_length)
1013 iter->me = iter->map->map[iter->idx];
1014 }
1015}
1016
1017
1018/**
1019 * Destroy a multipeermap iterator.
1020 *
1021 * @param iter the iterator to destroy
1022 */
1023void
1024GNUNET_CONTAINER_multipeermap_iterator_destroy (
1025 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter)
1026{
1027 GNUNET_free (iter);
1028}
1029
1030
1031/* end of container_multipeermap.c */