2 This file is part of GNUnet.
3 Copyright (C) 2008, 2012 GNUnet e.V.
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.
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.
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/>.
19 * @file util/container_multipeermap.c
20 * @brief hash map where the same key may be present multiple times
21 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
27 #define LOG(kind,...) GNUNET_log_from (kind, "util-container-multipeermap", __VA_ARGS__)
30 * An entry in the hash map with the full key.
41 * If there is a hash collision, we create a linked list.
43 struct BigMapEntry *next;
48 struct GNUNET_PeerIdentity key;
54 * An entry in the hash map with just a pointer to the key.
65 * If there is a hash collision, we create a linked list.
67 struct SmallMapEntry *next;
72 const struct GNUNET_PeerIdentity *key;
83 * Variant used if map entries only contain a pointer to the key.
85 struct SmallMapEntry *sme;
88 * Variant used if map entries contain the full key.
90 struct BigMapEntry *bme;
95 * Internal representation of the hash map.
97 struct GNUNET_CONTAINER_MultiPeerMap
100 * All of our buckets.
105 * Number of entries in the map.
110 * Length of the "map" array.
112 unsigned int map_length;
115 * GNUNET_NO if the map entries are of type 'struct BigMapEntry',
116 * GNUNET_YES if the map entries are of type 'struct SmallMapEntry'.
118 int use_small_entries;
121 * Counts the destructive modifications (grow, remove)
122 * to the map, so that iterators can check if they are still valid.
124 unsigned int modification_counter;
129 * Cursor into a multipeermap.
130 * Allows to enumerate elements asynchronously.
132 struct GNUNET_CONTAINER_MultiPeerMapIterator
135 * Position in the bucket 'idx'
140 * Current bucket index.
145 * Modification counter as observed on the map when the iterator
148 unsigned int modification_counter;
151 * Map that we are iterating over.
153 const struct GNUNET_CONTAINER_MultiPeerMap *map;
158 * Create a multi hash map.
160 * @param len initial size (map will grow as needed)
161 * @param do_not_copy_keys GNUNET_NO is always safe and should be used by default;
162 * GNUNET_YES means that on 'put', the 'key' does not have
163 * to be copied as the destination of the pointer is
164 * guaranteed to be life as long as the value is stored in
165 * the hashmap. This can significantly reduce memory
166 * consumption, but of course is also a recipie for
167 * heap corruption if the assumption is not true. Only
168 * use this if (1) memory use is important in this case and
169 * (2) you have triple-checked that the invariant holds
170 * @return NULL on error
172 struct GNUNET_CONTAINER_MultiPeerMap *
173 GNUNET_CONTAINER_multipeermap_create (unsigned int len,
174 int do_not_copy_keys)
176 struct GNUNET_CONTAINER_MultiPeerMap *map;
178 GNUNET_assert (len > 0);
179 map = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMap);
180 map->map = GNUNET_malloc (len * sizeof (union MapEntry));
181 map->map_length = len;
182 map->use_small_entries = do_not_copy_keys;
188 * Destroy a hash map. Will not free any values
189 * stored in the hash map!
194 GNUNET_CONTAINER_multipeermap_destroy (struct GNUNET_CONTAINER_MultiPeerMap
200 for (i = 0; i < map->map_length; i++)
203 if (map->use_small_entries)
205 struct SmallMapEntry *sme;
206 struct SmallMapEntry *nxt;
209 while (NULL != (sme = nxt))
218 struct BigMapEntry *bme;
219 struct BigMapEntry *nxt;
222 while (NULL != (bme = nxt))
230 GNUNET_free (map->map);
236 * Compute the index of the bucket for the given key.
238 * @param map hash map for which to compute the index
239 * @param key what key should the index be computed for
240 * @return offset into the "map" array of "map"
243 idx_of (const struct GNUNET_CONTAINER_MultiPeerMap *map,
244 const struct GNUNET_PeerIdentity *key)
248 GNUNET_assert (NULL != map);
249 GNUNET_memcpy (&kx, key, sizeof (kx));
250 return kx % map->map_length;
255 * Get the number of key-value pairs in the map.
258 * @return the number of key value pairs
261 GNUNET_CONTAINER_multipeermap_size (const struct GNUNET_CONTAINER_MultiPeerMap
269 * Given a key find a value in the map matching the key.
272 * @param key what to look for
273 * @return NULL if no value was found; note that
274 * this is indistinguishable from values that just
275 * happen to be NULL; use "contains" to test for
276 * key-value pairs with value NULL
279 GNUNET_CONTAINER_multipeermap_get (const struct GNUNET_CONTAINER_MultiPeerMap
280 *map, const struct GNUNET_PeerIdentity *key)
284 me = map->map[idx_of (map, key)];
285 if (map->use_small_entries)
287 struct SmallMapEntry *sme;
289 for (sme = me.sme; NULL != sme; sme = sme->next)
290 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
295 struct BigMapEntry *bme;
297 for (bme = me.bme; NULL != bme; bme = bme->next)
298 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
306 * Iterate over all entries in the map.
309 * @param it function to call on each entry
310 * @param it_cls extra argument to @a it
311 * @return the number of key value pairs processed,
312 * #GNUNET_SYSERR if it aborted iteration
315 GNUNET_CONTAINER_multipeermap_iterate (const struct
316 GNUNET_CONTAINER_MultiPeerMap *map,
317 GNUNET_CONTAINER_PeerMapIterator it,
323 struct GNUNET_PeerIdentity kc;
326 GNUNET_assert (NULL != map);
327 for (i = 0; i < map->map_length; i++)
330 if (map->use_small_entries)
332 struct SmallMapEntry *sme;
333 struct SmallMapEntry *nxt;
336 while (NULL != (sme = nxt))
341 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
342 return GNUNET_SYSERR;
349 struct BigMapEntry *bme;
350 struct BigMapEntry *nxt;
353 while (NULL != (bme = nxt))
359 if (GNUNET_OK != it (it_cls, &kc, bme->value))
360 return GNUNET_SYSERR;
371 * Remove the given key-value pair from the map. Note that if the
372 * key-value pair is in the map multiple times, only one of the pairs
376 * @param key key of the key-value pair
377 * @param value value of the key-value pair
378 * @return GNUNET_YES on success, GNUNET_NO if the key-value pair
382 GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map,
383 const struct GNUNET_PeerIdentity *key,
389 map->modification_counter++;
391 i = idx_of (map, key);
393 if (map->use_small_entries)
395 struct SmallMapEntry *sme;
396 struct SmallMapEntry *p;
399 for (sme = me.sme; NULL != sme; sme = sme->next)
401 if ((0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity))) &&
402 (value == sme->value))
405 map->map[i].sme = sme->next;
417 struct BigMapEntry *bme;
418 struct BigMapEntry *p;
421 for (bme = me.bme; NULL != bme; bme = bme->next)
423 if ((0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity))) &&
424 (value == bme->value))
427 map->map[i].bme = bme->next;
442 * Remove all entries for the given key from the map.
443 * Note that the values would not be "freed".
446 * @param key identifies values to be removed
447 * @return number of values removed
450 GNUNET_CONTAINER_multipeermap_remove_all (struct GNUNET_CONTAINER_MultiPeerMap
451 *map, const struct GNUNET_PeerIdentity *key)
457 map->modification_counter++;
460 i = idx_of (map, key);
462 if (map->use_small_entries)
464 struct SmallMapEntry *sme;
465 struct SmallMapEntry *p;
471 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
474 map->map[i].sme = sme->next;
480 sme = map->map[i].sme;
494 struct BigMapEntry *bme;
495 struct BigMapEntry *p;
501 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
504 map->map[i].bme = bme->next;
510 bme = map->map[i].bme;
527 * Check if the map contains any value under the given
528 * key (including values that are NULL).
531 * @param key the key to test if a value exists for it
532 * @return GNUNET_YES if such a value exists,
536 GNUNET_CONTAINER_multipeermap_contains (const struct
537 GNUNET_CONTAINER_MultiPeerMap *map,
538 const struct GNUNET_PeerIdentity *key)
542 me = map->map[idx_of (map, key)];
543 if (map->use_small_entries)
545 struct SmallMapEntry *sme;
547 for (sme = me.sme; NULL != sme; sme = sme->next)
548 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
553 struct BigMapEntry *bme;
555 for (bme = me.bme; NULL != bme; bme = bme->next)
556 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
564 * Check if the map contains the given value under the given
568 * @param key the key to test if a value exists for it
569 * @param value value to test for
570 * @return GNUNET_YES if such a value exists,
574 GNUNET_CONTAINER_multipeermap_contains_value (const struct
575 GNUNET_CONTAINER_MultiPeerMap
576 *map, const struct GNUNET_PeerIdentity *key,
581 me = map->map[idx_of (map, key)];
582 if (map->use_small_entries)
584 struct SmallMapEntry *sme;
586 for (sme = me.sme; NULL != sme; sme = sme->next)
587 if ( (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity))) &&
588 (sme->value == value) )
593 struct BigMapEntry *bme;
595 for (bme = me.bme; NULL != bme; bme = bme->next)
596 if ( (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity))) &&
597 (bme->value == value) )
605 * Grow the given map to a more appropriate size.
607 * @param map the hash map to grow
610 grow (struct GNUNET_CONTAINER_MultiPeerMap *map)
612 union MapEntry *old_map;
613 union MapEntry *new_map;
614 unsigned int old_len;
615 unsigned int new_len;
619 map->modification_counter++;
622 old_len = map->map_length;
623 new_len = old_len * 2;
624 new_map = GNUNET_malloc (sizeof (union MapEntry) * new_len);
625 map->map_length = new_len;
627 for (i = 0; i < old_len; i++)
629 if (map->use_small_entries)
631 struct SmallMapEntry *sme;
633 while (NULL != (sme = old_map[i].sme))
635 old_map[i].sme = sme->next;
636 idx = idx_of (map, sme->key);
637 sme->next = new_map[idx].sme;
638 new_map[idx].sme = sme;
643 struct BigMapEntry *bme;
645 while (NULL != (bme = old_map[i].bme))
647 old_map[i].bme = bme->next;
648 idx = idx_of (map, &bme->key);
649 bme->next = new_map[idx].bme;
650 new_map[idx].bme = bme;
654 GNUNET_free (old_map);
659 * Store a key-value pair in the map.
662 * @param key key to use
663 * @param value value to use
664 * @param opt options for put
665 * @return #GNUNET_OK on success,
666 * #GNUNET_NO if a value was replaced (with REPLACE)
667 * #GNUNET_SYSERR if GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
668 * value already exists
671 GNUNET_CONTAINER_multipeermap_put (struct GNUNET_CONTAINER_MultiPeerMap *map,
672 const struct GNUNET_PeerIdentity *key,
674 enum GNUNET_CONTAINER_MultiHashMapOption opt)
679 i = idx_of (map, key);
680 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
681 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
684 if (map->use_small_entries)
686 struct SmallMapEntry *sme;
688 for (sme = me.sme; NULL != sme; sme = sme->next)
689 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
691 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
692 return GNUNET_SYSERR;
699 struct BigMapEntry *bme;
701 for (bme = me.bme; NULL != bme; bme = bme->next)
702 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
704 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
705 return GNUNET_SYSERR;
711 if (map->size / 3 >= map->map_length / 4)
714 i = idx_of (map, key);
716 if (map->use_small_entries)
718 struct SmallMapEntry *sme;
720 sme = GNUNET_new (struct SmallMapEntry);
723 sme->next = map->map[i].sme;
724 map->map[i].sme = sme;
728 struct BigMapEntry *bme;
730 bme = GNUNET_new (struct BigMapEntry);
733 bme->next = map->map[i].bme;
734 map->map[i].bme = bme;
742 * Iterate over all entries in the map that match a particular key.
745 * @param key key that the entries must correspond to
746 * @param it function to call on each entry
747 * @param it_cls extra argument to @a it
748 * @return the number of key value pairs processed,
749 * #GNUNET_SYSERR if it aborted iteration
752 GNUNET_CONTAINER_multipeermap_get_multiple (const struct GNUNET_CONTAINER_MultiPeerMap *map,
753 const struct GNUNET_PeerIdentity *key,
754 GNUNET_CONTAINER_PeerMapIterator it,
761 me = map->map[idx_of (map, key)];
762 if (map->use_small_entries)
764 struct SmallMapEntry *sme;
765 struct SmallMapEntry *nxt;
768 while (NULL != (sme = nxt))
771 if (0 != memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
773 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, sme->value)))
774 return GNUNET_SYSERR;
780 struct BigMapEntry *bme;
781 struct BigMapEntry *nxt;
784 while (NULL != (bme = nxt))
787 if (0 != memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
789 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, bme->value)))
790 return GNUNET_SYSERR;
800 * Call @a it on a random value from the map, or not at all
801 * if the map is empty. Note that this function has linear
802 * complexity (in the size of the map).
805 * @param it function to call on a random entry
806 * @param it_cls extra argument to @a it
807 * @return the number of key value pairs processed, zero or one.
810 GNUNET_CONTAINER_multipeermap_get_random (const struct GNUNET_CONTAINER_MultiPeerMap *map,
811 GNUNET_CONTAINER_PeerMapIterator it,
822 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
824 for (idx = 0; idx < map->map_length; idx++)
827 if (map->use_small_entries)
829 struct SmallMapEntry *sme;
830 struct SmallMapEntry *nxt;
833 while (NULL != (sme = nxt))
838 if (GNUNET_OK != it (it_cls,
841 return GNUNET_SYSERR;
849 struct BigMapEntry *bme;
850 struct BigMapEntry *nxt;
853 while (NULL != (bme = nxt))
858 if (GNUNET_OK != it (it_cls,
859 &bme->key, bme->value))
860 return GNUNET_SYSERR;
868 return GNUNET_SYSERR;
873 * Create an iterator for a multipeermap.
874 * The iterator can be used to retrieve all the elements in the multipeermap
875 * one by one, without having to handle all elements at once (in contrast to
876 * #GNUNET_CONTAINER_multipeermap_iterate). Note that the iterator can not be
877 * used anymore if elements have been removed from 'map' after the creation of
878 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
879 * result in skipped or repeated elements.
881 * @param map the map to create an iterator for
882 * @return an iterator over the given multipeermap 'map'
884 struct GNUNET_CONTAINER_MultiPeerMapIterator *
885 GNUNET_CONTAINER_multipeermap_iterator_create (const struct GNUNET_CONTAINER_MultiPeerMap *map)
887 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
889 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMapIterator);
891 iter->modification_counter = map->modification_counter;
892 iter->me = map->map[0];
898 * Retrieve the next element from the hash map at the iterator's position.
899 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
901 * This operation is only allowed if no elements have been removed from the
902 * multipeermap since the creation of 'iter', and the map has not been destroyed.
903 * Adding elements may result in repeating or skipping elements.
905 * @param iter the iterator to get the next element from
906 * @param key pointer to store the key in, can be NULL
907 * @param value pointer to store the value in, can be NULL
908 * @return #GNUNET_YES we returned an element,
909 * #GNUNET_NO if we are out of elements
912 GNUNET_CONTAINER_multipeermap_iterator_next (struct GNUNET_CONTAINER_MultiPeerMapIterator *iter,
913 struct GNUNET_PeerIdentity *key, const void **value)
915 /* make sure the map has not been modified */
916 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
918 /* look for the next entry, skipping empty buckets */
921 if (iter->idx >= iter->map->map_length)
923 if (GNUNET_YES == iter->map->use_small_entries)
925 if (NULL != iter->me.sme)
928 *key = *iter->me.sme->key;
930 *value = iter->me.sme->value;
931 iter->me.sme = iter->me.sme->next;
937 if (NULL != iter->me.bme)
940 *key = iter->me.bme->key;
942 *value = iter->me.bme->value;
943 iter->me.bme = iter->me.bme->next;
948 if (iter->idx < iter->map->map_length)
949 iter->me = iter->map->map[iter->idx];
955 * Destroy a multipeermap iterator.
957 * @param iter the iterator to destroy
960 GNUNET_CONTAINER_multipeermap_iterator_destroy (struct GNUNET_CONTAINER_MultiPeerMapIterator *iter)
966 /* end of container_multipeermap.c */