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 * Maximum recursion depth for callbacks of
31 * #GNUNET_CONTAINER_multihashmap_get_multiple() themselve s
32 * again calling #GNUNET_CONTAINER_multihashmap_get_multiple().
33 * Should be totally excessive, but if violated we die.
35 #define NEXT_CACHE_SIZE 16
38 * An entry in the hash map with the full key.
49 * If there is a hash collision, we create a linked list.
51 struct BigMapEntry *next;
56 struct GNUNET_PeerIdentity key;
62 * An entry in the hash map with just a pointer to the key.
73 * If there is a hash collision, we create a linked list.
75 struct SmallMapEntry *next;
80 const struct GNUNET_PeerIdentity *key;
91 * Variant used if map entries only contain a pointer to the key.
93 struct SmallMapEntry *sme;
96 * Variant used if map entries contain the full key.
98 struct BigMapEntry *bme;
103 * Internal representation of the hash map.
105 struct GNUNET_CONTAINER_MultiPeerMap
108 * All of our buckets.
113 * Number of entries in the map.
118 * Length of the "map" array.
120 unsigned int map_length;
123 * #GNUNET_NO if the map entries are of type 'struct BigMapEntry',
124 * #GNUNET_YES if the map entries are of type 'struct SmallMapEntry'.
126 int use_small_entries;
129 * Counts the destructive modifications (grow, remove)
130 * to the map, so that iterators can check if they are still valid.
132 unsigned int modification_counter;
135 * Map entries indicating iteration positions currently
136 * in use by #GNUNET_CONTAINER_multihashmap_get_multiple().
137 * Only used up to @e next_cache_off.
139 union MapEntry next_cache[NEXT_CACHE_SIZE];
142 * Offset of @e next_cache entries in use, must be smaller
143 * than #NEXT_CACHE_SIZE.
145 unsigned int next_cache_off;
150 * Cursor into a multipeermap.
151 * Allows to enumerate elements asynchronously.
153 struct GNUNET_CONTAINER_MultiPeerMapIterator
156 * Position in the bucket 'idx'
161 * Current bucket index.
166 * Modification counter as observed on the map when the iterator
169 unsigned int modification_counter;
172 * Map that we are iterating over.
174 const struct GNUNET_CONTAINER_MultiPeerMap *map;
179 * Create a multi hash map.
181 * @param len initial size (map will grow as needed)
182 * @param do_not_copy_keys GNUNET_NO is always safe and should be used by default;
183 * GNUNET_YES means that on 'put', the 'key' does not have
184 * to be copied as the destination of the pointer is
185 * guaranteed to be life as long as the value is stored in
186 * the hashmap. This can significantly reduce memory
187 * consumption, but of course is also a recipie for
188 * heap corruption if the assumption is not true. Only
189 * use this if (1) memory use is important in this case and
190 * (2) you have triple-checked that the invariant holds
191 * @return NULL on error
193 struct GNUNET_CONTAINER_MultiPeerMap *
194 GNUNET_CONTAINER_multipeermap_create (unsigned int len,
195 int do_not_copy_keys)
197 struct GNUNET_CONTAINER_MultiPeerMap *map;
199 GNUNET_assert (len > 0);
200 map = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMap);
201 map->map = GNUNET_new_array (len,
203 map->map_length = len;
204 map->use_small_entries = do_not_copy_keys;
210 * Destroy a hash map. Will not free any values
211 * stored in the hash map!
216 GNUNET_CONTAINER_multipeermap_destroy (struct GNUNET_CONTAINER_MultiPeerMap *map)
218 GNUNET_assert (0 == map->next_cache_off);
219 for (unsigned int i = 0; i < map->map_length; i++)
224 if (map->use_small_entries)
226 struct SmallMapEntry *sme;
227 struct SmallMapEntry *nxt;
230 while (NULL != (sme = nxt))
239 struct BigMapEntry *bme;
240 struct BigMapEntry *nxt;
243 while (NULL != (bme = nxt))
251 GNUNET_free (map->map);
257 * Compute the index of the bucket for the given key.
259 * @param map hash map for which to compute the index
260 * @param key what key should the index be computed for
261 * @return offset into the "map" array of "map"
264 idx_of (const struct GNUNET_CONTAINER_MultiPeerMap *map,
265 const struct GNUNET_PeerIdentity *key)
269 GNUNET_assert (NULL != map);
273 return kx % map->map_length;
278 * Get the number of key-value pairs in the map.
281 * @return the number of key value pairs
284 GNUNET_CONTAINER_multipeermap_size (const struct GNUNET_CONTAINER_MultiPeerMap *map)
291 * Given a key find a value in the map matching the key.
294 * @param key what to look for
295 * @return NULL if no value was found; note that
296 * this is indistinguishable from values that just
297 * happen to be NULL; use "contains" to test for
298 * key-value pairs with value NULL
301 GNUNET_CONTAINER_multipeermap_get (const struct GNUNET_CONTAINER_MultiPeerMap *map,
302 const struct GNUNET_PeerIdentity *key)
306 me = map->map[idx_of (map, key)];
307 if (map->use_small_entries)
309 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
310 if (0 == memcmp (key,
312 sizeof (struct GNUNET_PeerIdentity)))
317 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
318 if (0 == memcmp (key,
320 sizeof (struct GNUNET_PeerIdentity)))
328 * Iterate over all entries in 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
337 GNUNET_CONTAINER_multipeermap_iterate (struct GNUNET_CONTAINER_MultiPeerMap *map,
338 GNUNET_CONTAINER_PeerMapIterator it,
344 struct GNUNET_PeerIdentity kc;
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++)
353 if (map->use_small_entries)
355 struct SmallMapEntry *sme;
358 while (NULL != (sme = ce->sme))
363 if (GNUNET_OK != it (it_cls,
367 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
368 return GNUNET_SYSERR;
376 struct BigMapEntry *bme;
379 while (NULL != (bme = ce->bme))
385 if (GNUNET_OK != it (it_cls,
389 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
390 return GNUNET_SYSERR;
397 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
403 * We are about to free() the @a bme, make sure it is not in
404 * the list of next values for any iterator in the @a map's next_cache.
406 * @param map the map to check
407 * @param bme the entry that is about to be free'd
410 update_next_cache_bme (struct GNUNET_CONTAINER_MultiPeerMap *map,
411 const struct BigMapEntry *bme)
413 for (unsigned int i=0;i<map->next_cache_off;i++)
414 if (map->next_cache[i].bme == bme)
415 map->next_cache[i].bme = bme->next;
420 * We are about to free() the @a sme, make sure it is not in
421 * the list of next values for any iterator in the @a map's next_cache.
423 * @param map the map to check
424 * @param sme the entry that is about to be free'd
427 update_next_cache_sme (struct GNUNET_CONTAINER_MultiPeerMap *map,
428 const struct SmallMapEntry *sme)
430 for (unsigned int i=0;i<map->next_cache_off;i++)
431 if (map->next_cache[i].sme == sme)
432 map->next_cache[i].sme = sme->next;
437 * Remove the given key-value pair from the map. Note that if the
438 * key-value pair is in the map multiple times, only one of the pairs
442 * @param key key of the key-value pair
443 * @param value value of the key-value pair
444 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
448 GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map,
449 const struct GNUNET_PeerIdentity *key,
455 map->modification_counter++;
456 i = idx_of (map, key);
458 if (map->use_small_entries)
460 struct SmallMapEntry *p = NULL;
462 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
464 if ((0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity))) &&
465 (value == sme->value))
468 map->map[i].sme = sme->next;
471 update_next_cache_sme (map,
482 struct BigMapEntry *p = NULL;
484 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
486 if ((0 == memcmp (key,
488 sizeof (struct GNUNET_PeerIdentity))) &&
489 (value == bme->value))
492 map->map[i].bme = bme->next;
495 update_next_cache_bme (map,
509 * Remove all entries for the given key from the map.
510 * Note that the values would not be "freed".
513 * @param key identifies values to be removed
514 * @return number of values removed
517 GNUNET_CONTAINER_multipeermap_remove_all (struct GNUNET_CONTAINER_MultiPeerMap *map,
518 const struct GNUNET_PeerIdentity *key)
524 map->modification_counter++;
527 i = idx_of (map, key);
529 if (map->use_small_entries)
531 struct SmallMapEntry *sme;
532 struct SmallMapEntry *p;
538 if (0 == memcmp (key,
540 sizeof (struct GNUNET_PeerIdentity)))
543 map->map[i].sme = sme->next;
546 update_next_cache_sme (map,
551 sme = map->map[i].sme;
565 struct BigMapEntry *bme;
566 struct BigMapEntry *p;
572 if (0 == memcmp (key,
574 sizeof (struct GNUNET_PeerIdentity)))
577 map->map[i].bme = bme->next;
580 update_next_cache_bme (map,
585 bme = map->map[i].bme;
602 * Check if the map contains any value under the given
603 * key (including values that are NULL).
606 * @param key the key to test if a value exists for it
607 * @return #GNUNET_YES if such a value exists,
611 GNUNET_CONTAINER_multipeermap_contains (const struct GNUNET_CONTAINER_MultiPeerMap *map,
612 const struct GNUNET_PeerIdentity *key)
616 me = map->map[idx_of (map, key)];
617 if (map->use_small_entries)
619 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
620 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
625 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
626 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
634 * Check if the map contains the given value under the given
638 * @param key the key to test if a value exists for it
639 * @param value value to test for
640 * @return #GNUNET_YES if such a value exists,
644 GNUNET_CONTAINER_multipeermap_contains_value (const struct GNUNET_CONTAINER_MultiPeerMap *map,
645 const struct GNUNET_PeerIdentity *key,
650 me = map->map[idx_of (map, key)];
651 if (map->use_small_entries)
653 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
654 if ( (0 == memcmp (key,
656 sizeof (struct GNUNET_PeerIdentity))) &&
657 (sme->value == value) )
662 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
663 if ( (0 == memcmp (key,
665 sizeof (struct GNUNET_PeerIdentity))) &&
666 (bme->value == value) )
674 * Grow the given map to a more appropriate size.
676 * @param map the hash map to grow
679 grow (struct GNUNET_CONTAINER_MultiPeerMap *map)
681 union MapEntry *old_map;
682 union MapEntry *new_map;
683 unsigned int old_len;
684 unsigned int new_len;
688 map->modification_counter++;
691 old_len = map->map_length;
692 new_len = old_len * 2;
693 new_map = GNUNET_new_array (new_len,
695 map->map_length = new_len;
697 for (i = 0; i < old_len; i++)
699 if (map->use_small_entries)
701 struct SmallMapEntry *sme;
703 while (NULL != (sme = old_map[i].sme))
705 old_map[i].sme = sme->next;
706 idx = idx_of (map, sme->key);
707 sme->next = new_map[idx].sme;
708 new_map[idx].sme = sme;
713 struct BigMapEntry *bme;
715 while (NULL != (bme = old_map[i].bme))
717 old_map[i].bme = bme->next;
718 idx = idx_of (map, &bme->key);
719 bme->next = new_map[idx].bme;
720 new_map[idx].bme = bme;
724 GNUNET_free (old_map);
729 * Store a key-value pair in the map.
732 * @param key key to use
733 * @param value value to use
734 * @param opt options for put
735 * @return #GNUNET_OK on success,
736 * #GNUNET_NO if a value was replaced (with REPLACE)
737 * #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
738 * value already exists
741 GNUNET_CONTAINER_multipeermap_put (struct GNUNET_CONTAINER_MultiPeerMap *map,
742 const struct GNUNET_PeerIdentity *key,
744 enum GNUNET_CONTAINER_MultiHashMapOption opt)
749 i = idx_of (map, key);
750 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
751 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
754 if (map->use_small_entries)
756 struct SmallMapEntry *sme;
758 for (sme = me.sme; NULL != sme; sme = sme->next)
759 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
761 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
762 return GNUNET_SYSERR;
769 struct BigMapEntry *bme;
771 for (bme = me.bme; NULL != bme; bme = bme->next)
772 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
774 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
775 return GNUNET_SYSERR;
781 if (map->size / 3 >= map->map_length / 4)
784 i = idx_of (map, key);
786 if (map->use_small_entries)
788 struct SmallMapEntry *sme;
790 sme = GNUNET_new (struct SmallMapEntry);
793 sme->next = map->map[i].sme;
794 map->map[i].sme = sme;
798 struct BigMapEntry *bme;
800 bme = GNUNET_new (struct BigMapEntry);
803 bme->next = map->map[i].bme;
804 map->map[i].bme = bme;
812 * Iterate over all entries in the map that match a particular key.
815 * @param key key that the entries must correspond to
816 * @param it function to call on each entry
817 * @param it_cls extra argument to @a it
818 * @return the number of key value pairs processed,
819 * #GNUNET_SYSERR if it aborted iteration
822 GNUNET_CONTAINER_multipeermap_get_multiple (struct GNUNET_CONTAINER_MultiPeerMap *map,
823 const struct GNUNET_PeerIdentity *key,
824 GNUNET_CONTAINER_PeerMapIterator it,
831 ce = &map->next_cache[map->next_cache_off];
832 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
834 me = map->map[idx_of (map, key)];
835 if (map->use_small_entries)
837 struct SmallMapEntry *sme;
840 while (NULL != (sme = ce->sme))
843 if (0 != memcmp (key,
845 sizeof (struct GNUNET_PeerIdentity)))
848 (GNUNET_OK != it (it_cls,
852 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
853 return GNUNET_SYSERR;
860 struct BigMapEntry *bme;
863 while (NULL != (bme = ce->bme))
866 if (0 != memcmp (key,
868 sizeof (struct GNUNET_PeerIdentity)))
871 (GNUNET_OK != it (it_cls,
875 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
876 return GNUNET_SYSERR;
881 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
888 * Call @a it on a random value from the map, or not at all
889 * if the map is empty. Note that this function has linear
890 * complexity (in the size of the map).
893 * @param it function to call on a random entry
894 * @param it_cls extra argument to @a it
895 * @return the number of key value pairs processed, zero or one.
898 GNUNET_CONTAINER_multipeermap_get_random (const struct GNUNET_CONTAINER_MultiPeerMap *map,
899 GNUNET_CONTAINER_PeerMapIterator it,
909 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
911 for (unsigned int idx = 0; idx < map->map_length; idx++)
914 if (map->use_small_entries)
916 struct SmallMapEntry *sme;
917 struct SmallMapEntry *nxt;
920 while (NULL != (sme = nxt))
925 if (GNUNET_OK != it (it_cls,
928 return GNUNET_SYSERR;
936 struct BigMapEntry *bme;
937 struct BigMapEntry *nxt;
940 while (NULL != (bme = nxt))
945 if (GNUNET_OK != it (it_cls,
948 return GNUNET_SYSERR;
956 return GNUNET_SYSERR;
961 * Create an iterator for a multipeermap.
962 * The iterator can be used to retrieve all the elements in the multipeermap
963 * one by one, without having to handle all elements at once (in contrast to
964 * #GNUNET_CONTAINER_multipeermap_iterate). Note that the iterator can not be
965 * used anymore if elements have been removed from 'map' after the creation of
966 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
967 * result in skipped or repeated elements.
969 * @param map the map to create an iterator for
970 * @return an iterator over the given multipeermap 'map'
972 struct GNUNET_CONTAINER_MultiPeerMapIterator *
973 GNUNET_CONTAINER_multipeermap_iterator_create (const struct GNUNET_CONTAINER_MultiPeerMap *map)
975 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
977 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMapIterator);
979 iter->modification_counter = map->modification_counter;
980 iter->me = map->map[0];
986 * Retrieve the next element from the hash map at the iterator's position.
987 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
989 * This operation is only allowed if no elements have been removed from the
990 * multipeermap since the creation of 'iter', and the map has not been destroyed.
991 * Adding elements may result in repeating or skipping elements.
993 * @param iter the iterator to get the next element from
994 * @param key pointer to store the key in, can be NULL
995 * @param value pointer to store the value in, can be NULL
996 * @return #GNUNET_YES we returned an element,
997 * #GNUNET_NO if we are out of elements
1000 GNUNET_CONTAINER_multipeermap_iterator_next (struct GNUNET_CONTAINER_MultiPeerMapIterator *iter,
1001 struct GNUNET_PeerIdentity *key, const void **value)
1003 /* make sure the map has not been modified */
1004 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
1006 /* look for the next entry, skipping empty buckets */
1009 if (iter->idx >= iter->map->map_length)
1011 if (GNUNET_YES == iter->map->use_small_entries)
1013 if (NULL != iter->me.sme)
1016 *key = *iter->me.sme->key;
1018 *value = iter->me.sme->value;
1019 iter->me.sme = iter->me.sme->next;
1025 if (NULL != iter->me.bme)
1028 *key = iter->me.bme->key;
1030 *value = iter->me.bme->value;
1031 iter->me.bme = iter->me.bme->next;
1036 if (iter->idx < iter->map->map_length)
1037 iter->me = iter->map->map[iter->idx];
1043 * Destroy a multipeermap iterator.
1045 * @param iter the iterator to destroy
1048 GNUNET_CONTAINER_multipeermap_iterator_destroy (struct GNUNET_CONTAINER_MultiPeerMapIterator *iter)
1054 /* end of container_multipeermap.c */