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_malloc_large (len *
204 sizeof (union MapEntry));
205 if (NULL == map->map)
210 map->map_length = len;
211 map->use_small_entries = do_not_copy_keys;
217 * Destroy a hash map. Will not free any values
218 * stored in the hash map!
223 GNUNET_CONTAINER_multipeermap_destroy (struct GNUNET_CONTAINER_MultiPeerMap *map)
225 GNUNET_assert (0 == map->next_cache_off);
226 for (unsigned int i = 0; i < map->map_length; i++)
231 if (map->use_small_entries)
233 struct SmallMapEntry *sme;
234 struct SmallMapEntry *nxt;
237 while (NULL != (sme = nxt))
246 struct BigMapEntry *bme;
247 struct BigMapEntry *nxt;
250 while (NULL != (bme = nxt))
258 GNUNET_free (map->map);
264 * Compute the index of the bucket for the given key.
266 * @param map hash map for which to compute the index
267 * @param key what key should the index be computed for
268 * @return offset into the "map" array of "map"
271 idx_of (const struct GNUNET_CONTAINER_MultiPeerMap *map,
272 const struct GNUNET_PeerIdentity *key)
276 GNUNET_assert (NULL != map);
280 return kx % map->map_length;
285 * Get the number of key-value pairs in the map.
288 * @return the number of key value pairs
291 GNUNET_CONTAINER_multipeermap_size (const struct GNUNET_CONTAINER_MultiPeerMap *map)
298 * Given a key find a value in the map matching the key.
301 * @param key what to look for
302 * @return NULL if no value was found; note that
303 * this is indistinguishable from values that just
304 * happen to be NULL; use "contains" to test for
305 * key-value pairs with value NULL
308 GNUNET_CONTAINER_multipeermap_get (const struct GNUNET_CONTAINER_MultiPeerMap *map,
309 const struct GNUNET_PeerIdentity *key)
313 me = map->map[idx_of (map, key)];
314 if (map->use_small_entries)
316 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
317 if (0 == memcmp (key,
319 sizeof (struct GNUNET_PeerIdentity)))
324 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
325 if (0 == memcmp (key,
327 sizeof (struct GNUNET_PeerIdentity)))
335 * Iterate over all entries in the map.
338 * @param it function to call on each entry
339 * @param it_cls extra argument to @a it
340 * @return the number of key value pairs processed,
341 * #GNUNET_SYSERR if it aborted iteration
344 GNUNET_CONTAINER_multipeermap_iterate (struct GNUNET_CONTAINER_MultiPeerMap *map,
345 GNUNET_CONTAINER_PeerMapIterator it,
351 struct GNUNET_PeerIdentity kc;
354 GNUNET_assert (NULL != map);
355 ce = &map->next_cache[map->next_cache_off];
356 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
357 for (unsigned int i = 0; i < map->map_length; i++)
360 if (map->use_small_entries)
362 struct SmallMapEntry *sme;
365 while (NULL != (sme = ce->sme))
370 if (GNUNET_OK != it (it_cls,
374 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
375 return GNUNET_SYSERR;
383 struct BigMapEntry *bme;
386 while (NULL != (bme = ce->bme))
392 if (GNUNET_OK != it (it_cls,
396 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
397 return GNUNET_SYSERR;
404 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
410 * We are about to free() the @a bme, make sure it is not in
411 * the list of next values for any iterator in the @a map's next_cache.
413 * @param map the map to check
414 * @param bme the entry that is about to be free'd
417 update_next_cache_bme (struct GNUNET_CONTAINER_MultiPeerMap *map,
418 const struct BigMapEntry *bme)
420 for (unsigned int i=0;i<map->next_cache_off;i++)
421 if (map->next_cache[i].bme == bme)
422 map->next_cache[i].bme = bme->next;
427 * We are about to free() the @a sme, make sure it is not in
428 * the list of next values for any iterator in the @a map's next_cache.
430 * @param map the map to check
431 * @param sme the entry that is about to be free'd
434 update_next_cache_sme (struct GNUNET_CONTAINER_MultiPeerMap *map,
435 const struct SmallMapEntry *sme)
437 for (unsigned int i=0;i<map->next_cache_off;i++)
438 if (map->next_cache[i].sme == sme)
439 map->next_cache[i].sme = sme->next;
444 * Remove the given key-value pair from the map. Note that if the
445 * key-value pair is in the map multiple times, only one of the pairs
449 * @param key key of the key-value pair
450 * @param value value of the key-value pair
451 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
455 GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map,
456 const struct GNUNET_PeerIdentity *key,
462 map->modification_counter++;
463 i = idx_of (map, key);
465 if (map->use_small_entries)
467 struct SmallMapEntry *p = NULL;
469 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
471 if ((0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity))) &&
472 (value == sme->value))
475 map->map[i].sme = sme->next;
478 update_next_cache_sme (map,
489 struct BigMapEntry *p = NULL;
491 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
493 if ((0 == memcmp (key,
495 sizeof (struct GNUNET_PeerIdentity))) &&
496 (value == bme->value))
499 map->map[i].bme = bme->next;
502 update_next_cache_bme (map,
516 * Remove all entries for the given key from the map.
517 * Note that the values would not be "freed".
520 * @param key identifies values to be removed
521 * @return number of values removed
524 GNUNET_CONTAINER_multipeermap_remove_all (struct GNUNET_CONTAINER_MultiPeerMap *map,
525 const struct GNUNET_PeerIdentity *key)
531 map->modification_counter++;
534 i = idx_of (map, key);
536 if (map->use_small_entries)
538 struct SmallMapEntry *sme;
539 struct SmallMapEntry *p;
545 if (0 == memcmp (key,
547 sizeof (struct GNUNET_PeerIdentity)))
550 map->map[i].sme = sme->next;
553 update_next_cache_sme (map,
558 sme = map->map[i].sme;
572 struct BigMapEntry *bme;
573 struct BigMapEntry *p;
579 if (0 == memcmp (key,
581 sizeof (struct GNUNET_PeerIdentity)))
584 map->map[i].bme = bme->next;
587 update_next_cache_bme (map,
592 bme = map->map[i].bme;
609 * Check if the map contains any value under the given
610 * key (including values that are NULL).
613 * @param key the key to test if a value exists for it
614 * @return #GNUNET_YES if such a value exists,
618 GNUNET_CONTAINER_multipeermap_contains (const struct GNUNET_CONTAINER_MultiPeerMap *map,
619 const struct GNUNET_PeerIdentity *key)
623 me = map->map[idx_of (map, key)];
624 if (map->use_small_entries)
626 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
627 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
632 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
633 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
641 * Check if the map contains the given value under the given
645 * @param key the key to test if a value exists for it
646 * @param value value to test for
647 * @return #GNUNET_YES if such a value exists,
651 GNUNET_CONTAINER_multipeermap_contains_value (const struct GNUNET_CONTAINER_MultiPeerMap *map,
652 const struct GNUNET_PeerIdentity *key,
657 me = map->map[idx_of (map, key)];
658 if (map->use_small_entries)
660 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
661 if ( (0 == memcmp (key,
663 sizeof (struct GNUNET_PeerIdentity))) &&
664 (sme->value == value) )
669 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
670 if ( (0 == memcmp (key,
672 sizeof (struct GNUNET_PeerIdentity))) &&
673 (bme->value == value) )
681 * Grow the given map to a more appropriate size.
683 * @param map the hash map to grow
686 grow (struct GNUNET_CONTAINER_MultiPeerMap *map)
688 union MapEntry *old_map;
689 union MapEntry *new_map;
690 unsigned int old_len;
691 unsigned int new_len;
695 old_len = map->map_length;
696 GNUNET_assert (0 != old_len);
697 new_len = old_len * 2;
698 if (0 == new_len) /* 2^31 * 2 == 0 */
699 new_len = old_len; /* never use 0 */
700 if (new_len == old_len)
701 return; /* nothing changed */
702 new_map = GNUNET_malloc_large (new_len *
703 sizeof (union MapEntry));
705 return; /* grow not possible */
706 map->modification_counter++;
707 map->map_length = new_len;
709 for (unsigned int i = 0; i < old_len; i++)
711 if (map->use_small_entries)
713 struct SmallMapEntry *sme;
715 while (NULL != (sme = old_map[i].sme))
717 old_map[i].sme = sme->next;
718 idx = idx_of (map, sme->key);
719 sme->next = new_map[idx].sme;
720 new_map[idx].sme = sme;
725 struct BigMapEntry *bme;
727 while (NULL != (bme = old_map[i].bme))
729 old_map[i].bme = bme->next;
730 idx = idx_of (map, &bme->key);
731 bme->next = new_map[idx].bme;
732 new_map[idx].bme = bme;
736 GNUNET_free (old_map);
741 * Store a key-value pair in the map.
744 * @param key key to use
745 * @param value value to use
746 * @param opt options for put
747 * @return #GNUNET_OK on success,
748 * #GNUNET_NO if a value was replaced (with REPLACE)
749 * #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
750 * value already exists
753 GNUNET_CONTAINER_multipeermap_put (struct GNUNET_CONTAINER_MultiPeerMap *map,
754 const struct GNUNET_PeerIdentity *key,
756 enum GNUNET_CONTAINER_MultiHashMapOption opt)
761 i = idx_of (map, key);
762 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
763 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
766 if (map->use_small_entries)
768 struct SmallMapEntry *sme;
770 for (sme = me.sme; NULL != sme; sme = sme->next)
771 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
773 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
774 return GNUNET_SYSERR;
781 struct BigMapEntry *bme;
783 for (bme = me.bme; NULL != bme; bme = bme->next)
784 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
786 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
787 return GNUNET_SYSERR;
793 if (map->size / 3 >= map->map_length / 4)
796 i = idx_of (map, key);
798 if (map->use_small_entries)
800 struct SmallMapEntry *sme;
802 sme = GNUNET_new (struct SmallMapEntry);
805 sme->next = map->map[i].sme;
806 map->map[i].sme = sme;
810 struct BigMapEntry *bme;
812 bme = GNUNET_new (struct BigMapEntry);
815 bme->next = map->map[i].bme;
816 map->map[i].bme = bme;
824 * Iterate over all entries in the map that match a particular key.
827 * @param key key that the entries must correspond to
828 * @param it function to call on each entry
829 * @param it_cls extra argument to @a it
830 * @return the number of key value pairs processed,
831 * #GNUNET_SYSERR if it aborted iteration
834 GNUNET_CONTAINER_multipeermap_get_multiple (struct GNUNET_CONTAINER_MultiPeerMap *map,
835 const struct GNUNET_PeerIdentity *key,
836 GNUNET_CONTAINER_PeerMapIterator it,
843 ce = &map->next_cache[map->next_cache_off];
844 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
846 me = map->map[idx_of (map, key)];
847 if (map->use_small_entries)
849 struct SmallMapEntry *sme;
852 while (NULL != (sme = ce->sme))
855 if (0 != memcmp (key,
857 sizeof (struct GNUNET_PeerIdentity)))
860 (GNUNET_OK != it (it_cls,
864 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
865 return GNUNET_SYSERR;
872 struct BigMapEntry *bme;
875 while (NULL != (bme = ce->bme))
878 if (0 != memcmp (key,
880 sizeof (struct GNUNET_PeerIdentity)))
883 (GNUNET_OK != it (it_cls,
887 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
888 return GNUNET_SYSERR;
893 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
900 * Call @a it on a random value from the map, or not at all
901 * if the map is empty. Note that this function has linear
902 * complexity (in the size of the map).
905 * @param it function to call on a random entry
906 * @param it_cls extra argument to @a it
907 * @return the number of key value pairs processed, zero or one.
910 GNUNET_CONTAINER_multipeermap_get_random (const struct GNUNET_CONTAINER_MultiPeerMap *map,
911 GNUNET_CONTAINER_PeerMapIterator it,
921 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
923 for (unsigned int idx = 0; idx < map->map_length; idx++)
926 if (map->use_small_entries)
928 struct SmallMapEntry *sme;
929 struct SmallMapEntry *nxt;
932 while (NULL != (sme = nxt))
937 if (GNUNET_OK != it (it_cls,
940 return GNUNET_SYSERR;
948 struct BigMapEntry *bme;
949 struct BigMapEntry *nxt;
952 while (NULL != (bme = nxt))
957 if (GNUNET_OK != it (it_cls,
960 return GNUNET_SYSERR;
968 return GNUNET_SYSERR;
973 * Create an iterator for a multipeermap.
974 * The iterator can be used to retrieve all the elements in the multipeermap
975 * one by one, without having to handle all elements at once (in contrast to
976 * #GNUNET_CONTAINER_multipeermap_iterate). Note that the iterator can not be
977 * used anymore if elements have been removed from 'map' after the creation of
978 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
979 * result in skipped or repeated elements.
981 * @param map the map to create an iterator for
982 * @return an iterator over the given multipeermap 'map'
984 struct GNUNET_CONTAINER_MultiPeerMapIterator *
985 GNUNET_CONTAINER_multipeermap_iterator_create (const struct GNUNET_CONTAINER_MultiPeerMap *map)
987 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
989 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMapIterator);
991 iter->modification_counter = map->modification_counter;
992 iter->me = map->map[0];
998 * Retrieve the next element from the hash map at the iterator's position.
999 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
1001 * This operation is only allowed if no elements have been removed from the
1002 * multipeermap since the creation of 'iter', and the map has not been destroyed.
1003 * Adding elements may result in repeating or skipping elements.
1005 * @param iter the iterator to get the next element from
1006 * @param key pointer to store the key in, can be NULL
1007 * @param value pointer to store the value in, can be NULL
1008 * @return #GNUNET_YES we returned an element,
1009 * #GNUNET_NO if we are out of elements
1012 GNUNET_CONTAINER_multipeermap_iterator_next (struct GNUNET_CONTAINER_MultiPeerMapIterator *iter,
1013 struct GNUNET_PeerIdentity *key, const void **value)
1015 /* make sure the map has not been modified */
1016 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
1018 /* look for the next entry, skipping empty buckets */
1021 if (iter->idx >= iter->map->map_length)
1023 if (GNUNET_YES == iter->map->use_small_entries)
1025 if (NULL != iter->me.sme)
1028 *key = *iter->me.sme->key;
1030 *value = iter->me.sme->value;
1031 iter->me.sme = iter->me.sme->next;
1037 if (NULL != iter->me.bme)
1040 *key = iter->me.bme->key;
1042 *value = iter->me.bme->value;
1043 iter->me.bme = iter->me.bme->next;
1048 if (iter->idx < iter->map->map_length)
1049 iter->me = iter->map->map[iter->idx];
1055 * Destroy a multipeermap iterator.
1057 * @param iter the iterator to destroy
1060 GNUNET_CONTAINER_multipeermap_iterator_destroy (struct GNUNET_CONTAINER_MultiPeerMapIterator *iter)
1066 /* end of container_multipeermap.c */