use NULL value in load_path_suffix to NOT load any files
[oweals/gnunet.git] / src / util / container_multipeermap.c
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() themselve 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  */
43 struct 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  */
65 struct 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  */
87 union 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  */
104 struct 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  */
152 struct 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 recipie 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  */
192 struct GNUNET_CONTAINER_MultiPeerMap *
193 GNUNET_CONTAINER_multipeermap_create (unsigned int len, int do_not_copy_keys)
194 {
195   struct GNUNET_CONTAINER_MultiPeerMap *map;
196
197   GNUNET_assert (len > 0);
198   map = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMap);
199   map->map = GNUNET_malloc_large (len * sizeof(union MapEntry));
200   if (NULL == map->map)
201   {
202     GNUNET_free (map);
203     return NULL;
204   }
205   map->map_length = len;
206   map->use_small_entries = do_not_copy_keys;
207   return map;
208 }
209
210
211 /**
212  * Destroy a hash map.  Will not free any values
213  * stored in the hash map!
214  *
215  * @param map the map
216  */
217 void
218 GNUNET_CONTAINER_multipeermap_destroy (
219   struct GNUNET_CONTAINER_MultiPeerMap *map)
220 {
221   GNUNET_assert (0 == map->next_cache_off);
222   for (unsigned int i = 0; i < map->map_length; i++)
223   {
224     union MapEntry me;
225
226     me = map->map[i];
227     if (map->use_small_entries)
228     {
229       struct SmallMapEntry *sme;
230       struct SmallMapEntry *nxt;
231
232       nxt = me.sme;
233       while (NULL != (sme = nxt))
234       {
235         nxt = sme->next;
236         GNUNET_free (sme);
237       }
238       me.sme = NULL;
239     }
240     else
241     {
242       struct BigMapEntry *bme;
243       struct BigMapEntry *nxt;
244
245       nxt = me.bme;
246       while (NULL != (bme = nxt))
247       {
248         nxt = bme->next;
249         GNUNET_free (bme);
250       }
251       me.bme = NULL;
252     }
253   }
254   GNUNET_free (map->map);
255   GNUNET_free (map);
256 }
257
258
259 /**
260  * Compute the index of the bucket for the given key.
261  *
262  * @param map hash map for which to compute the index
263  * @param key what key should the index be computed for
264  * @return offset into the "map" array of "map"
265  */
266 static unsigned int
267 idx_of (const struct GNUNET_CONTAINER_MultiPeerMap *map,
268         const struct GNUNET_PeerIdentity *key)
269 {
270   unsigned int kx;
271
272   GNUNET_assert (NULL != map);
273   GNUNET_memcpy (&kx, key, sizeof(kx));
274   return kx % map->map_length;
275 }
276
277
278 /**
279  * Get the number of key-value pairs in the map.
280  *
281  * @param map the map
282  * @return the number of key value pairs
283  */
284 unsigned int
285 GNUNET_CONTAINER_multipeermap_size (
286   const struct GNUNET_CONTAINER_MultiPeerMap *map)
287 {
288   return map->size;
289 }
290
291
292 /**
293  * Given a key find a value in the map matching the key.
294  *
295  * @param map the map
296  * @param key what to look for
297  * @return NULL if no value was found; note that
298  *   this is indistinguishable from values that just
299  *   happen to be NULL; use "contains" to test for
300  *   key-value pairs with value NULL
301  */
302 void *
303 GNUNET_CONTAINER_multipeermap_get (
304   const struct GNUNET_CONTAINER_MultiPeerMap *map,
305   const struct GNUNET_PeerIdentity *key)
306 {
307   union MapEntry me;
308
309   me = map->map[idx_of (map, key)];
310   if (map->use_small_entries)
311   {
312     for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
313       if (0 == GNUNET_memcmp (key, sme->key))
314         return sme->value;
315   }
316   else
317   {
318     for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
319       if (0 == GNUNET_memcmp (key, &bme->key))
320         return bme->value;
321   }
322   return NULL;
323 }
324
325
326 /**
327  * Iterate over all entries in the map.
328  *
329  * @param map the map
330  * @param it function to call on each entry
331  * @param it_cls extra argument to @a it
332  * @return the number of key value pairs processed,
333  *         #GNUNET_SYSERR if it aborted iteration
334  */
335 int
336 GNUNET_CONTAINER_multipeermap_iterate (
337   struct GNUNET_CONTAINER_MultiPeerMap *map,
338   GNUNET_CONTAINER_PeerMapIterator it,
339   void *it_cls)
340 {
341   int count;
342   union MapEntry me;
343   union MapEntry *ce;
344   struct GNUNET_PeerIdentity kc;
345
346   count = 0;
347   GNUNET_assert (NULL != map);
348   ce = &map->next_cache[map->next_cache_off];
349   GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
350   for (unsigned int i = 0; i < map->map_length; i++)
351   {
352     me = map->map[i];
353     if (map->use_small_entries)
354     {
355       struct SmallMapEntry *sme;
356
357       ce->sme = me.sme;
358       while (NULL != (sme = ce->sme))
359       {
360         ce->sme = sme->next;
361         if (NULL != it)
362         {
363           if (GNUNET_OK != it (it_cls, sme->key, sme->value))
364           {
365             GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
366             return GNUNET_SYSERR;
367           }
368         }
369         count++;
370       }
371     }
372     else
373     {
374       struct BigMapEntry *bme;
375
376       ce->bme = me.bme;
377       while (NULL != (bme = ce->bme))
378       {
379         ce->bme = bme->next;
380         if (NULL != it)
381         {
382           kc = bme->key;
383           if (GNUNET_OK != it (it_cls, &kc, bme->value))
384           {
385             GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
386             return GNUNET_SYSERR;
387           }
388         }
389         count++;
390       }
391     }
392   }
393   GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
394   return count;
395 }
396
397
398 /**
399  * We are about to free() the @a bme, make sure it is not in
400  * the list of next values for any iterator in the @a map's next_cache.
401  *
402  * @param map the map to check
403  * @param bme the entry that is about to be free'd
404  */
405 static void
406 update_next_cache_bme (struct GNUNET_CONTAINER_MultiPeerMap *map,
407                        const struct BigMapEntry *bme)
408 {
409   for (unsigned int i = 0; i < map->next_cache_off; i++)
410     if (map->next_cache[i].bme == bme)
411       map->next_cache[i].bme = bme->next;
412 }
413
414
415 /**
416  * We are about to free() the @a sme, make sure it is not in
417  * the list of next values for any iterator in the @a map's next_cache.
418  *
419  * @param map the map to check
420  * @param sme the entry that is about to be free'd
421  */
422 static void
423 update_next_cache_sme (struct GNUNET_CONTAINER_MultiPeerMap *map,
424                        const struct SmallMapEntry *sme)
425 {
426   for (unsigned int i = 0; i < map->next_cache_off; i++)
427     if (map->next_cache[i].sme == sme)
428       map->next_cache[i].sme = sme->next;
429 }
430
431
432 /**
433  * Remove the given key-value pair from the map.  Note that if the
434  * key-value pair is in the map multiple times, only one of the pairs
435  * will be removed.
436  *
437  * @param map the map
438  * @param key key of the key-value pair
439  * @param value value of the key-value pair
440  * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
441  *  is not in the map
442  */
443 int
444 GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map,
445                                       const struct GNUNET_PeerIdentity *key,
446                                       const void *value)
447 {
448   union MapEntry me;
449   unsigned int i;
450
451   map->modification_counter++;
452   i = idx_of (map, key);
453   me = map->map[i];
454   if (map->use_small_entries)
455   {
456     struct SmallMapEntry *p = NULL;
457
458     for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
459     {
460       if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value))
461       {
462         if (NULL == p)
463           map->map[i].sme = sme->next;
464         else
465           p->next = sme->next;
466         update_next_cache_sme (map, sme);
467         GNUNET_free (sme);
468         map->size--;
469         return GNUNET_YES;
470       }
471       p = sme;
472     }
473   }
474   else
475   {
476     struct BigMapEntry *p = NULL;
477
478     for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
479     {
480       if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value))
481       {
482         if (NULL == p)
483           map->map[i].bme = bme->next;
484         else
485           p->next = bme->next;
486         update_next_cache_bme (map, bme);
487         GNUNET_free (bme);
488         map->size--;
489         return GNUNET_YES;
490       }
491       p = bme;
492     }
493   }
494   return GNUNET_NO;
495 }
496
497
498 /**
499  * Remove all entries for the given key from the map.
500  * Note that the values would not be "freed".
501  *
502  * @param map the map
503  * @param key identifies values to be removed
504  * @return number of values removed
505  */
506 int
507 GNUNET_CONTAINER_multipeermap_remove_all (
508   struct GNUNET_CONTAINER_MultiPeerMap *map,
509   const struct GNUNET_PeerIdentity *key)
510 {
511   union MapEntry me;
512   unsigned int i;
513   int ret;
514
515   map->modification_counter++;
516
517   ret = 0;
518   i = idx_of (map, key);
519   me = map->map[i];
520   if (map->use_small_entries)
521   {
522     struct SmallMapEntry *sme;
523     struct SmallMapEntry *p;
524
525     p = NULL;
526     sme = me.sme;
527     while (NULL != sme)
528     {
529       if (0 == GNUNET_memcmp (key, sme->key))
530       {
531         if (NULL == p)
532           map->map[i].sme = sme->next;
533         else
534           p->next = sme->next;
535         update_next_cache_sme (map, sme);
536         GNUNET_free (sme);
537         map->size--;
538         if (NULL == p)
539           sme = map->map[i].sme;
540         else
541           sme = p->next;
542         ret++;
543       }
544       else
545       {
546         p = sme;
547         sme = sme->next;
548       }
549     }
550   }
551   else
552   {
553     struct BigMapEntry *bme;
554     struct BigMapEntry *p;
555
556     p = NULL;
557     bme = me.bme;
558     while (NULL != bme)
559     {
560       if (0 == GNUNET_memcmp (key, &bme->key))
561       {
562         if (NULL == p)
563           map->map[i].bme = bme->next;
564         else
565           p->next = bme->next;
566         update_next_cache_bme (map, bme);
567         GNUNET_free (bme);
568         map->size--;
569         if (NULL == p)
570           bme = map->map[i].bme;
571         else
572           bme = p->next;
573         ret++;
574       }
575       else
576       {
577         p = bme;
578         bme = bme->next;
579       }
580     }
581   }
582   return ret;
583 }
584
585
586 /**
587  * Check if the map contains any value under the given
588  * key (including values that are NULL).
589  *
590  * @param map the map
591  * @param key the key to test if a value exists for it
592  * @return #GNUNET_YES if such a value exists,
593  *         #GNUNET_NO if not
594  */
595 int
596 GNUNET_CONTAINER_multipeermap_contains (
597   const struct GNUNET_CONTAINER_MultiPeerMap *map,
598   const struct GNUNET_PeerIdentity *key)
599 {
600   union MapEntry me;
601
602   me = map->map[idx_of (map, key)];
603   if (map->use_small_entries)
604   {
605     for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
606       if (0 == GNUNET_memcmp (key, sme->key))
607         return GNUNET_YES;
608   }
609   else
610   {
611     for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
612       if (0 == GNUNET_memcmp (key, &bme->key))
613         return GNUNET_YES;
614   }
615   return GNUNET_NO;
616 }
617
618
619 /**
620  * Check if the map contains the given value under the given
621  * key.
622  *
623  * @param map the map
624  * @param key the key to test if a value exists for it
625  * @param value value to test for
626  * @return #GNUNET_YES if such a value exists,
627  *         #GNUNET_NO if not
628  */
629 int
630 GNUNET_CONTAINER_multipeermap_contains_value (
631   const struct GNUNET_CONTAINER_MultiPeerMap *map,
632   const struct GNUNET_PeerIdentity *key,
633   const void *value)
634 {
635   union MapEntry me;
636
637   me = map->map[idx_of (map, key)];
638   if (map->use_small_entries)
639   {
640     for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
641       if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value))
642         return GNUNET_YES;
643   }
644   else
645   {
646     for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
647       if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value))
648         return GNUNET_YES;
649   }
650   return GNUNET_NO;
651 }
652
653
654 /**
655  * Grow the given map to a more appropriate size.
656  *
657  * @param map the hash map to grow
658  */
659 static void
660 grow (struct GNUNET_CONTAINER_MultiPeerMap *map)
661 {
662   union MapEntry *old_map;
663   union MapEntry *new_map;
664   unsigned int old_len;
665   unsigned int new_len;
666   unsigned int idx;
667
668   old_map = map->map;
669   old_len = map->map_length;
670   GNUNET_assert (0 != old_len);
671   new_len = old_len * 2;
672   if (0 == new_len) /* 2^31 * 2 == 0 */
673     new_len = old_len; /* never use 0 */
674   if (new_len == old_len)
675     return; /* nothing changed */
676   new_map = GNUNET_malloc_large (new_len * sizeof(union MapEntry));
677   if (NULL == new_map)
678     return; /* grow not possible */
679   map->modification_counter++;
680   map->map_length = new_len;
681   map->map = new_map;
682   for (unsigned int i = 0; i < old_len; i++)
683   {
684     if (map->use_small_entries)
685     {
686       struct SmallMapEntry *sme;
687
688       while (NULL != (sme = old_map[i].sme))
689       {
690         old_map[i].sme = sme->next;
691         idx = idx_of (map, sme->key);
692         sme->next = new_map[idx].sme;
693         new_map[idx].sme = sme;
694       }
695     }
696     else
697     {
698       struct BigMapEntry *bme;
699
700       while (NULL != (bme = old_map[i].bme))
701       {
702         old_map[i].bme = bme->next;
703         idx = idx_of (map, &bme->key);
704         bme->next = new_map[idx].bme;
705         new_map[idx].bme = bme;
706       }
707     }
708   }
709   GNUNET_free (old_map);
710 }
711
712
713 /**
714  * Store a key-value pair in the map.
715  *
716  * @param map the map
717  * @param key key to use
718  * @param value value to use
719  * @param opt options for put
720  * @return #GNUNET_OK on success,
721  *         #GNUNET_NO if a value was replaced (with REPLACE)
722  *         #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
723  *                       value already exists
724  */
725 int
726 GNUNET_CONTAINER_multipeermap_put (struct GNUNET_CONTAINER_MultiPeerMap *map,
727                                    const struct GNUNET_PeerIdentity *key,
728                                    void *value,
729                                    enum GNUNET_CONTAINER_MultiHashMapOption opt)
730 {
731   union MapEntry me;
732   unsigned int i;
733
734   i = idx_of (map, key);
735   if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
736       (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
737   {
738     me = map->map[i];
739     if (map->use_small_entries)
740     {
741       struct SmallMapEntry *sme;
742
743       for (sme = me.sme; NULL != sme; sme = sme->next)
744         if (0 == GNUNET_memcmp (key, sme->key))
745         {
746           if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
747             return GNUNET_SYSERR;
748           sme->value = value;
749           return GNUNET_NO;
750         }
751     }
752     else
753     {
754       struct BigMapEntry *bme;
755
756       for (bme = me.bme; NULL != bme; bme = bme->next)
757         if (0 == GNUNET_memcmp (key, &bme->key))
758         {
759           if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
760             return GNUNET_SYSERR;
761           bme->value = value;
762           return GNUNET_NO;
763         }
764     }
765   }
766   if (map->size / 3 >= map->map_length / 4)
767   {
768     grow (map);
769     i = idx_of (map, key);
770   }
771   if (map->use_small_entries)
772   {
773     struct SmallMapEntry *sme;
774
775     sme = GNUNET_new (struct SmallMapEntry);
776     sme->key = key;
777     sme->value = value;
778     sme->next = map->map[i].sme;
779     map->map[i].sme = sme;
780   }
781   else
782   {
783     struct BigMapEntry *bme;
784
785     bme = GNUNET_new (struct BigMapEntry);
786     bme->key = *key;
787     bme->value = value;
788     bme->next = map->map[i].bme;
789     map->map[i].bme = bme;
790   }
791   map->size++;
792   return GNUNET_OK;
793 }
794
795
796 /**
797  * Iterate over all entries in the map that match a particular key.
798  *
799  * @param map the map
800  * @param key key that the entries must correspond to
801  * @param it function to call on each entry
802  * @param it_cls extra argument to @a it
803  * @return the number of key value pairs processed,
804  *         #GNUNET_SYSERR if it aborted iteration
805  */
806 int
807 GNUNET_CONTAINER_multipeermap_get_multiple (
808   struct GNUNET_CONTAINER_MultiPeerMap *map,
809   const struct GNUNET_PeerIdentity *key,
810   GNUNET_CONTAINER_PeerMapIterator it,
811   void *it_cls)
812 {
813   int count;
814   union MapEntry me;
815   union MapEntry *ce;
816
817   ce = &map->next_cache[map->next_cache_off];
818   GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
819   count = 0;
820   me = map->map[idx_of (map, key)];
821   if (map->use_small_entries)
822   {
823     struct SmallMapEntry *sme;
824
825     ce->sme = me.sme;
826     while (NULL != (sme = ce->sme))
827     {
828       ce->sme = sme->next;
829       if (0 != GNUNET_memcmp (key, sme->key))
830         continue;
831       if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value)))
832       {
833         GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
834         return GNUNET_SYSERR;
835       }
836       count++;
837     }
838   }
839   else
840   {
841     struct BigMapEntry *bme;
842
843     ce->bme = me.bme;
844     while (NULL != (bme = ce->bme))
845     {
846       ce->bme = bme->next;
847       if (0 != GNUNET_memcmp (key, &bme->key))
848         continue;
849       if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value)))
850       {
851         GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
852         return GNUNET_SYSERR;
853       }
854       count++;
855     }
856   }
857   GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
858   return count;
859 }
860
861
862 /**
863  * @ingroup hashmap
864  * Call @a it on a random value from the map, or not at all
865  * if the map is empty.  Note that this function has linear
866  * complexity (in the size of the map).
867  *
868  * @param map the map
869  * @param it function to call on a random entry
870  * @param it_cls extra argument to @a it
871  * @return the number of key value pairs processed, zero or one.
872  */
873 unsigned int
874 GNUNET_CONTAINER_multipeermap_get_random (
875   const struct GNUNET_CONTAINER_MultiPeerMap *map,
876   GNUNET_CONTAINER_PeerMapIterator it,
877   void *it_cls)
878 {
879   unsigned int off;
880   union MapEntry me;
881
882   if (0 == map->size)
883     return 0;
884   if (NULL == it)
885     return 1;
886   off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size);
887   for (unsigned int idx = 0; idx < map->map_length; idx++)
888   {
889     me = map->map[idx];
890     if (map->use_small_entries)
891     {
892       struct SmallMapEntry *sme;
893       struct SmallMapEntry *nxt;
894
895       nxt = me.sme;
896       while (NULL != (sme = nxt))
897       {
898         nxt = sme->next;
899         if (0 == off)
900         {
901           if (GNUNET_OK != it (it_cls, sme->key, sme->value))
902             return GNUNET_SYSERR;
903           return 1;
904         }
905         off--;
906       }
907     }
908     else
909     {
910       struct BigMapEntry *bme;
911       struct BigMapEntry *nxt;
912
913       nxt = me.bme;
914       while (NULL != (bme = nxt))
915       {
916         nxt = bme->next;
917         if (0 == off)
918         {
919           if (GNUNET_OK != it (it_cls, &bme->key, bme->value))
920             return GNUNET_SYSERR;
921           return 1;
922         }
923         off--;
924       }
925     }
926   }
927   GNUNET_break (0);
928   return GNUNET_SYSERR;
929 }
930
931
932 /**
933  * Create an iterator for a multipeermap.
934  * The iterator can be used to retrieve all the elements in the multipeermap
935  * one by one, without having to handle all elements at once (in contrast to
936  * #GNUNET_CONTAINER_multipeermap_iterate).  Note that the iterator can not be
937  * used anymore if elements have been removed from 'map' after the creation of
938  * the iterator, or 'map' has been destroyed.  Adding elements to 'map' may
939  * result in skipped or repeated elements.
940  *
941  * @param map the map to create an iterator for
942  * @return an iterator over the given multipeermap 'map'
943  */
944 struct GNUNET_CONTAINER_MultiPeerMapIterator *
945 GNUNET_CONTAINER_multipeermap_iterator_create (
946   const struct GNUNET_CONTAINER_MultiPeerMap *map)
947 {
948   struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
949
950   iter = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMapIterator);
951   iter->map = map;
952   iter->modification_counter = map->modification_counter;
953   iter->me = map->map[0];
954   return iter;
955 }
956
957
958 /**
959  * Retrieve the next element from the hash map at the iterator's position.
960  * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
961  * are not modified.
962  * This operation is only allowed if no elements have been removed from the
963  * multipeermap since the creation of 'iter', and the map has not been destroyed.
964  * Adding elements may result in repeating or skipping elements.
965  *
966  * @param iter the iterator to get the next element from
967  * @param key pointer to store the key in, can be NULL
968  * @param value pointer to store the value in, can be NULL
969  * @return #GNUNET_YES we returned an element,
970  *         #GNUNET_NO if we are out of elements
971  */
972 int
973 GNUNET_CONTAINER_multipeermap_iterator_next (
974   struct GNUNET_CONTAINER_MultiPeerMapIterator *iter,
975   struct GNUNET_PeerIdentity *key,
976   const void **value)
977 {
978   /* make sure the map has not been modified */
979   GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
980
981   /* look for the next entry, skipping empty buckets */
982   while (1)
983   {
984     if (iter->idx >= iter->map->map_length)
985       return GNUNET_NO;
986     if (GNUNET_YES == iter->map->use_small_entries)
987     {
988       if (NULL != iter->me.sme)
989       {
990         if (NULL != key)
991           *key = *iter->me.sme->key;
992         if (NULL != value)
993           *value = iter->me.sme->value;
994         iter->me.sme = iter->me.sme->next;
995         return GNUNET_YES;
996       }
997     }
998     else
999     {
1000       if (NULL != iter->me.bme)
1001       {
1002         if (NULL != key)
1003           *key = iter->me.bme->key;
1004         if (NULL != value)
1005           *value = iter->me.bme->value;
1006         iter->me.bme = iter->me.bme->next;
1007         return GNUNET_YES;
1008       }
1009     }
1010     iter->idx += 1;
1011     if (iter->idx < iter->map->map_length)
1012       iter->me = iter->map->map[iter->idx];
1013   }
1014 }
1015
1016
1017 /**
1018  * Destroy a multipeermap iterator.
1019  *
1020  * @param iter the iterator to destroy
1021  */
1022 void
1023 GNUNET_CONTAINER_multipeermap_iterator_destroy (
1024   struct GNUNET_CONTAINER_MultiPeerMapIterator *iter)
1025 {
1026   GNUNET_free (iter);
1027 }
1028
1029
1030 /* end of container_multipeermap.c */