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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file util/container_multipeermap.c
22 * @brief hash map where the same key may be present multiple times
23 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "util-container-multipeermap", __VA_ARGS__)
32 * Maximum recursion depth for callbacks of
33 * #GNUNET_CONTAINER_multihashmap_get_multiple() themselve s
34 * again calling #GNUNET_CONTAINER_multihashmap_get_multiple().
35 * Should be totally excessive, but if violated we die.
37 #define NEXT_CACHE_SIZE 16
40 * An entry in the hash map with the full key.
51 * If there is a hash collision, we create a linked list.
53 struct BigMapEntry *next;
58 struct GNUNET_PeerIdentity key;
64 * An entry in the hash map with just a pointer to the key.
75 * If there is a hash collision, we create a linked list.
77 struct SmallMapEntry *next;
82 const struct GNUNET_PeerIdentity *key;
93 * Variant used if map entries only contain a pointer to the key.
95 struct SmallMapEntry *sme;
98 * Variant used if map entries contain the full key.
100 struct BigMapEntry *bme;
105 * Internal representation of the hash map.
107 struct GNUNET_CONTAINER_MultiPeerMap
110 * All of our buckets.
115 * Number of entries in the map.
120 * Length of the "map" array.
122 unsigned int map_length;
125 * #GNUNET_NO if the map entries are of type 'struct BigMapEntry',
126 * #GNUNET_YES if the map entries are of type 'struct SmallMapEntry'.
128 int use_small_entries;
131 * Counts the destructive modifications (grow, remove)
132 * to the map, so that iterators can check if they are still valid.
134 unsigned int modification_counter;
137 * Map entries indicating iteration positions currently
138 * in use by #GNUNET_CONTAINER_multihashmap_get_multiple().
139 * Only used up to @e next_cache_off.
141 union MapEntry next_cache[NEXT_CACHE_SIZE];
144 * Offset of @e next_cache entries in use, must be smaller
145 * than #NEXT_CACHE_SIZE.
147 unsigned int next_cache_off;
152 * Cursor into a multipeermap.
153 * Allows to enumerate elements asynchronously.
155 struct GNUNET_CONTAINER_MultiPeerMapIterator
158 * Position in the bucket 'idx'
163 * Current bucket index.
168 * Modification counter as observed on the map when the iterator
171 unsigned int modification_counter;
174 * Map that we are iterating over.
176 const struct GNUNET_CONTAINER_MultiPeerMap *map;
181 * Create a multi hash map.
183 * @param len initial size (map will grow as needed)
184 * @param do_not_copy_keys GNUNET_NO is always safe and should be used by default;
185 * GNUNET_YES means that on 'put', the 'key' does not have
186 * to be copied as the destination of the pointer is
187 * guaranteed to be life as long as the value is stored in
188 * the hashmap. This can significantly reduce memory
189 * consumption, but of course is also a recipie for
190 * heap corruption if the assumption is not true. Only
191 * use this if (1) memory use is important in this case and
192 * (2) you have triple-checked that the invariant holds
193 * @return NULL on error
195 struct GNUNET_CONTAINER_MultiPeerMap *
196 GNUNET_CONTAINER_multipeermap_create (unsigned int len,
197 int do_not_copy_keys)
199 struct GNUNET_CONTAINER_MultiPeerMap *map;
201 GNUNET_assert (len > 0);
202 map = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMap);
203 map->map = GNUNET_new_array (len,
205 map->map_length = len;
206 map->use_small_entries = do_not_copy_keys;
212 * Destroy a hash map. Will not free any values
213 * stored in the hash map!
218 GNUNET_CONTAINER_multipeermap_destroy (struct GNUNET_CONTAINER_MultiPeerMap *map)
220 GNUNET_assert (0 == map->next_cache_off);
221 for (unsigned int i = 0; i < map->map_length; i++)
226 if (map->use_small_entries)
228 struct SmallMapEntry *sme;
229 struct SmallMapEntry *nxt;
232 while (NULL != (sme = nxt))
241 struct BigMapEntry *bme;
242 struct BigMapEntry *nxt;
245 while (NULL != (bme = nxt))
253 GNUNET_free (map->map);
259 * Compute the index of the bucket for the given key.
261 * @param map hash map for which to compute the index
262 * @param key what key should the index be computed for
263 * @return offset into the "map" array of "map"
266 idx_of (const struct GNUNET_CONTAINER_MultiPeerMap *map,
267 const struct GNUNET_PeerIdentity *key)
271 GNUNET_assert (NULL != map);
275 return kx % map->map_length;
280 * Get the number of key-value pairs in the map.
283 * @return the number of key value pairs
286 GNUNET_CONTAINER_multipeermap_size (const struct GNUNET_CONTAINER_MultiPeerMap *map)
293 * Given a key find a value in the map matching the key.
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
303 GNUNET_CONTAINER_multipeermap_get (const struct GNUNET_CONTAINER_MultiPeerMap *map,
304 const struct GNUNET_PeerIdentity *key)
308 me = map->map[idx_of (map, key)];
309 if (map->use_small_entries)
311 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
312 if (0 == memcmp (key,
314 sizeof (struct GNUNET_PeerIdentity)))
319 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
320 if (0 == memcmp (key,
322 sizeof (struct GNUNET_PeerIdentity)))
330 * Iterate over all entries in the map.
333 * @param it function to call on each entry
334 * @param it_cls extra argument to @a it
335 * @return the number of key value pairs processed,
336 * #GNUNET_SYSERR if it aborted iteration
339 GNUNET_CONTAINER_multipeermap_iterate (struct GNUNET_CONTAINER_MultiPeerMap *map,
340 GNUNET_CONTAINER_PeerMapIterator it,
346 struct GNUNET_PeerIdentity kc;
349 GNUNET_assert (NULL != map);
350 ce = &map->next_cache[map->next_cache_off];
351 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
352 for (unsigned int i = 0; i < map->map_length; i++)
355 if (map->use_small_entries)
357 struct SmallMapEntry *sme;
360 while (NULL != (sme = ce->sme))
365 if (GNUNET_OK != it (it_cls,
369 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
370 return GNUNET_SYSERR;
378 struct BigMapEntry *bme;
381 while (NULL != (bme = ce->bme))
387 if (GNUNET_OK != it (it_cls,
391 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
392 return GNUNET_SYSERR;
399 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
405 * We are about to free() the @a bme, make sure it is not in
406 * the list of next values for any iterator in the @a map's next_cache.
408 * @param map the map to check
409 * @param bme the entry that is about to be free'd
412 update_next_cache_bme (struct GNUNET_CONTAINER_MultiPeerMap *map,
413 const struct BigMapEntry *bme)
415 for (unsigned int i=0;i<map->next_cache_off;i++)
416 if (map->next_cache[i].bme == bme)
417 map->next_cache[i].bme = bme->next;
422 * We are about to free() the @a sme, make sure it is not in
423 * the list of next values for any iterator in the @a map's next_cache.
425 * @param map the map to check
426 * @param sme the entry that is about to be free'd
429 update_next_cache_sme (struct GNUNET_CONTAINER_MultiPeerMap *map,
430 const struct SmallMapEntry *sme)
432 for (unsigned int i=0;i<map->next_cache_off;i++)
433 if (map->next_cache[i].sme == sme)
434 map->next_cache[i].sme = sme->next;
439 * Remove the given key-value pair from the map. Note that if the
440 * key-value pair is in the map multiple times, only one of the pairs
444 * @param key key of the key-value pair
445 * @param value value of the key-value pair
446 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
450 GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map,
451 const struct GNUNET_PeerIdentity *key,
457 map->modification_counter++;
458 i = idx_of (map, key);
460 if (map->use_small_entries)
462 struct SmallMapEntry *p = NULL;
464 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
466 if ((0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity))) &&
467 (value == sme->value))
470 map->map[i].sme = sme->next;
473 update_next_cache_sme (map,
484 struct BigMapEntry *p = NULL;
486 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
488 if ((0 == memcmp (key,
490 sizeof (struct GNUNET_PeerIdentity))) &&
491 (value == bme->value))
494 map->map[i].bme = bme->next;
497 update_next_cache_bme (map,
511 * Remove all entries for the given key from the map.
512 * Note that the values would not be "freed".
515 * @param key identifies values to be removed
516 * @return number of values removed
519 GNUNET_CONTAINER_multipeermap_remove_all (struct GNUNET_CONTAINER_MultiPeerMap *map,
520 const struct GNUNET_PeerIdentity *key)
526 map->modification_counter++;
529 i = idx_of (map, key);
531 if (map->use_small_entries)
533 struct SmallMapEntry *sme;
534 struct SmallMapEntry *p;
540 if (0 == memcmp (key,
542 sizeof (struct GNUNET_PeerIdentity)))
545 map->map[i].sme = sme->next;
548 update_next_cache_sme (map,
553 sme = map->map[i].sme;
567 struct BigMapEntry *bme;
568 struct BigMapEntry *p;
574 if (0 == memcmp (key,
576 sizeof (struct GNUNET_PeerIdentity)))
579 map->map[i].bme = bme->next;
582 update_next_cache_bme (map,
587 bme = map->map[i].bme;
604 * Check if the map contains any value under the given
605 * key (including values that are NULL).
608 * @param key the key to test if a value exists for it
609 * @return #GNUNET_YES if such a value exists,
613 GNUNET_CONTAINER_multipeermap_contains (const struct GNUNET_CONTAINER_MultiPeerMap *map,
614 const struct GNUNET_PeerIdentity *key)
618 me = map->map[idx_of (map, key)];
619 if (map->use_small_entries)
621 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
622 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
627 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
628 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
636 * Check if the map contains the given value under the given
640 * @param key the key to test if a value exists for it
641 * @param value value to test for
642 * @return #GNUNET_YES if such a value exists,
646 GNUNET_CONTAINER_multipeermap_contains_value (const struct GNUNET_CONTAINER_MultiPeerMap *map,
647 const struct GNUNET_PeerIdentity *key,
652 me = map->map[idx_of (map, key)];
653 if (map->use_small_entries)
655 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
656 if ( (0 == memcmp (key,
658 sizeof (struct GNUNET_PeerIdentity))) &&
659 (sme->value == value) )
664 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
665 if ( (0 == memcmp (key,
667 sizeof (struct GNUNET_PeerIdentity))) &&
668 (bme->value == value) )
676 * Grow the given map to a more appropriate size.
678 * @param map the hash map to grow
681 grow (struct GNUNET_CONTAINER_MultiPeerMap *map)
683 union MapEntry *old_map;
684 union MapEntry *new_map;
685 unsigned int old_len;
686 unsigned int new_len;
690 map->modification_counter++;
693 old_len = map->map_length;
694 new_len = old_len * 2;
695 new_map = GNUNET_new_array (new_len,
697 map->map_length = new_len;
699 for (i = 0; i < old_len; i++)
701 if (map->use_small_entries)
703 struct SmallMapEntry *sme;
705 while (NULL != (sme = old_map[i].sme))
707 old_map[i].sme = sme->next;
708 idx = idx_of (map, sme->key);
709 sme->next = new_map[idx].sme;
710 new_map[idx].sme = sme;
715 struct BigMapEntry *bme;
717 while (NULL != (bme = old_map[i].bme))
719 old_map[i].bme = bme->next;
720 idx = idx_of (map, &bme->key);
721 bme->next = new_map[idx].bme;
722 new_map[idx].bme = bme;
726 GNUNET_free (old_map);
731 * Store a key-value pair in the map.
734 * @param key key to use
735 * @param value value to use
736 * @param opt options for put
737 * @return #GNUNET_OK on success,
738 * #GNUNET_NO if a value was replaced (with REPLACE)
739 * #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
740 * value already exists
743 GNUNET_CONTAINER_multipeermap_put (struct GNUNET_CONTAINER_MultiPeerMap *map,
744 const struct GNUNET_PeerIdentity *key,
746 enum GNUNET_CONTAINER_MultiHashMapOption opt)
751 i = idx_of (map, key);
752 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
753 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
756 if (map->use_small_entries)
758 struct SmallMapEntry *sme;
760 for (sme = me.sme; NULL != sme; sme = sme->next)
761 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
763 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
764 return GNUNET_SYSERR;
771 struct BigMapEntry *bme;
773 for (bme = me.bme; NULL != bme; bme = bme->next)
774 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
776 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
777 return GNUNET_SYSERR;
783 if (map->size / 3 >= map->map_length / 4)
786 i = idx_of (map, key);
788 if (map->use_small_entries)
790 struct SmallMapEntry *sme;
792 sme = GNUNET_new (struct SmallMapEntry);
795 sme->next = map->map[i].sme;
796 map->map[i].sme = sme;
800 struct BigMapEntry *bme;
802 bme = GNUNET_new (struct BigMapEntry);
805 bme->next = map->map[i].bme;
806 map->map[i].bme = bme;
814 * Iterate over all entries in the map that match a particular key.
817 * @param key key that the entries must correspond to
818 * @param it function to call on each entry
819 * @param it_cls extra argument to @a it
820 * @return the number of key value pairs processed,
821 * #GNUNET_SYSERR if it aborted iteration
824 GNUNET_CONTAINER_multipeermap_get_multiple (struct GNUNET_CONTAINER_MultiPeerMap *map,
825 const struct GNUNET_PeerIdentity *key,
826 GNUNET_CONTAINER_PeerMapIterator it,
833 ce = &map->next_cache[map->next_cache_off];
834 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
836 me = map->map[idx_of (map, key)];
837 if (map->use_small_entries)
839 struct SmallMapEntry *sme;
842 while (NULL != (sme = ce->sme))
845 if (0 != memcmp (key,
847 sizeof (struct GNUNET_PeerIdentity)))
850 (GNUNET_OK != it (it_cls,
854 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
855 return GNUNET_SYSERR;
862 struct BigMapEntry *bme;
865 while (NULL != (bme = ce->bme))
868 if (0 != memcmp (key,
870 sizeof (struct GNUNET_PeerIdentity)))
873 (GNUNET_OK != it (it_cls,
877 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
878 return GNUNET_SYSERR;
883 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
890 * Call @a it on a random value from the map, or not at all
891 * if the map is empty. Note that this function has linear
892 * complexity (in the size of the map).
895 * @param it function to call on a random entry
896 * @param it_cls extra argument to @a it
897 * @return the number of key value pairs processed, zero or one.
900 GNUNET_CONTAINER_multipeermap_get_random (const struct GNUNET_CONTAINER_MultiPeerMap *map,
901 GNUNET_CONTAINER_PeerMapIterator it,
911 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
913 for (unsigned int idx = 0; idx < map->map_length; idx++)
916 if (map->use_small_entries)
918 struct SmallMapEntry *sme;
919 struct SmallMapEntry *nxt;
922 while (NULL != (sme = nxt))
927 if (GNUNET_OK != it (it_cls,
930 return GNUNET_SYSERR;
938 struct BigMapEntry *bme;
939 struct BigMapEntry *nxt;
942 while (NULL != (bme = nxt))
947 if (GNUNET_OK != it (it_cls,
950 return GNUNET_SYSERR;
958 return GNUNET_SYSERR;
963 * Create an iterator for a multipeermap.
964 * The iterator can be used to retrieve all the elements in the multipeermap
965 * one by one, without having to handle all elements at once (in contrast to
966 * #GNUNET_CONTAINER_multipeermap_iterate). Note that the iterator can not be
967 * used anymore if elements have been removed from 'map' after the creation of
968 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
969 * result in skipped or repeated elements.
971 * @param map the map to create an iterator for
972 * @return an iterator over the given multipeermap 'map'
974 struct GNUNET_CONTAINER_MultiPeerMapIterator *
975 GNUNET_CONTAINER_multipeermap_iterator_create (const struct GNUNET_CONTAINER_MultiPeerMap *map)
977 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
979 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMapIterator);
981 iter->modification_counter = map->modification_counter;
982 iter->me = map->map[0];
988 * Retrieve the next element from the hash map at the iterator's position.
989 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
991 * This operation is only allowed if no elements have been removed from the
992 * multipeermap since the creation of 'iter', and the map has not been destroyed.
993 * Adding elements may result in repeating or skipping elements.
995 * @param iter the iterator to get the next element from
996 * @param key pointer to store the key in, can be NULL
997 * @param value pointer to store the value in, can be NULL
998 * @return #GNUNET_YES we returned an element,
999 * #GNUNET_NO if we are out of elements
1002 GNUNET_CONTAINER_multipeermap_iterator_next (struct GNUNET_CONTAINER_MultiPeerMapIterator *iter,
1003 struct GNUNET_PeerIdentity *key, const void **value)
1005 /* make sure the map has not been modified */
1006 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
1008 /* look for the next entry, skipping empty buckets */
1011 if (iter->idx >= iter->map->map_length)
1013 if (GNUNET_YES == iter->map->use_small_entries)
1015 if (NULL != iter->me.sme)
1018 *key = *iter->me.sme->key;
1020 *value = iter->me.sme->value;
1021 iter->me.sme = iter->me.sme->next;
1027 if (NULL != iter->me.bme)
1030 *key = iter->me.bme->key;
1032 *value = iter->me.bme->value;
1033 iter->me.bme = iter->me.bme->next;
1038 if (iter->idx < iter->map->map_length)
1039 iter->me = iter->map->map[iter->idx];
1045 * Destroy a multipeermap iterator.
1047 * @param iter the iterator to destroy
1050 GNUNET_CONTAINER_multipeermap_iterator_destroy (struct GNUNET_CONTAINER_MultiPeerMapIterator *iter)
1056 /* end of container_multipeermap.c */