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, ...) \
30 GNUNET_log_from (kind, "util-container-multipeermap", __VA_ARGS__)
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.
38 #define NEXT_CACHE_SIZE 16
41 * 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;
63 * 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;
90 * Variant used if map entries only contain a pointer to the key.
92 struct SmallMapEntry *sme;
95 * Variant used if map entries contain the full key.
97 struct BigMapEntry *bme;
102 * Internal representation of the hash map.
104 struct GNUNET_CONTAINER_MultiPeerMap
107 * All of our buckets.
112 * Number of entries in the map.
117 * Length of the "map" array.
119 unsigned int map_length;
122 * #GNUNET_NO if the map entries are of type 'struct BigMapEntry',
123 * #GNUNET_YES if the map entries are of type 'struct SmallMapEntry'.
125 int use_small_entries;
128 * Counts the destructive modifications (grow, remove)
129 * to the map, so that iterators can check if they are still valid.
131 unsigned int modification_counter;
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.
138 union MapEntry next_cache[NEXT_CACHE_SIZE];
141 * Offset of @e next_cache entries in use, must be smaller
142 * than #NEXT_CACHE_SIZE.
144 unsigned int next_cache_off;
149 * Cursor into a multipeermap.
150 * Allows to enumerate elements asynchronously.
152 struct GNUNET_CONTAINER_MultiPeerMapIterator
155 * Position in the bucket 'idx'
160 * Current bucket index.
165 * Modification counter as observed on the map when the iterator
168 unsigned int modification_counter;
171 * Map that we are iterating over.
173 const struct GNUNET_CONTAINER_MultiPeerMap *map;
178 * Create a multi hash map.
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
192 struct GNUNET_CONTAINER_MultiPeerMap *
193 GNUNET_CONTAINER_multipeermap_create (unsigned int len, int do_not_copy_keys)
195 struct GNUNET_CONTAINER_MultiPeerMap *map;
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)
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 (
219 struct GNUNET_CONTAINER_MultiPeerMap *map)
221 GNUNET_assert (0 == map->next_cache_off);
222 for (unsigned int i = 0; i < map->map_length; i++)
227 if (map->use_small_entries)
229 struct SmallMapEntry *sme;
230 struct SmallMapEntry *nxt;
233 while (NULL != (sme = nxt))
242 struct BigMapEntry *bme;
243 struct BigMapEntry *nxt;
246 while (NULL != (bme = nxt))
254 GNUNET_free (map->map);
260 * Compute the index of the bucket for the given key.
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"
267 idx_of (const struct GNUNET_CONTAINER_MultiPeerMap *map,
268 const struct GNUNET_PeerIdentity *key)
272 GNUNET_assert (NULL != map);
273 GNUNET_memcpy (&kx, key, sizeof(kx));
274 return kx % map->map_length;
279 * Get the number of key-value pairs in the map.
282 * @return the number of key value pairs
285 GNUNET_CONTAINER_multipeermap_size (
286 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 (
304 const struct GNUNET_CONTAINER_MultiPeerMap *map,
305 const struct GNUNET_PeerIdentity *key)
309 me = map->map[idx_of (map, key)];
310 if (map->use_small_entries)
312 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
313 if (0 == GNUNET_memcmp (key, sme->key))
318 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
319 if (0 == GNUNET_memcmp (key, &bme->key))
327 * Iterate over all entries in 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
336 GNUNET_CONTAINER_multipeermap_iterate (
337 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, sme->key, sme->value))
365 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
366 return GNUNET_SYSERR;
374 struct BigMapEntry *bme;
377 while (NULL != (bme = ce->bme))
383 if (GNUNET_OK != it (it_cls, &kc, bme->value))
385 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
386 return GNUNET_SYSERR;
393 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
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.
402 * @param map the map to check
403 * @param bme the entry that is about to be free'd
406 update_next_cache_bme (struct GNUNET_CONTAINER_MultiPeerMap *map,
407 const struct BigMapEntry *bme)
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;
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.
419 * @param map the map to check
420 * @param sme the entry that is about to be free'd
423 update_next_cache_sme (struct GNUNET_CONTAINER_MultiPeerMap *map,
424 const struct SmallMapEntry *sme)
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;
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
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
444 GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map,
445 const struct GNUNET_PeerIdentity *key,
451 map->modification_counter++;
452 i = idx_of (map, key);
454 if (map->use_small_entries)
456 struct SmallMapEntry *p = NULL;
458 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
460 if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value))
463 map->map[i].sme = sme->next;
466 update_next_cache_sme (map, sme);
476 struct BigMapEntry *p = NULL;
478 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
480 if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value))
483 map->map[i].bme = bme->next;
486 update_next_cache_bme (map, bme);
499 * Remove all entries for the given key from the map.
500 * Note that the values would not be "freed".
503 * @param key identifies values to be removed
504 * @return number of values removed
507 GNUNET_CONTAINER_multipeermap_remove_all (
508 struct GNUNET_CONTAINER_MultiPeerMap *map,
509 const struct GNUNET_PeerIdentity *key)
515 map->modification_counter++;
518 i = idx_of (map, key);
520 if (map->use_small_entries)
522 struct SmallMapEntry *sme;
523 struct SmallMapEntry *p;
529 if (0 == GNUNET_memcmp (key, sme->key))
532 map->map[i].sme = sme->next;
535 update_next_cache_sme (map, sme);
539 sme = map->map[i].sme;
553 struct BigMapEntry *bme;
554 struct BigMapEntry *p;
560 if (0 == GNUNET_memcmp (key, &bme->key))
563 map->map[i].bme = bme->next;
566 update_next_cache_bme (map, bme);
570 bme = map->map[i].bme;
587 * Check if the map contains any value under the given
588 * key (including values that are NULL).
591 * @param key the key to test if a value exists for it
592 * @return #GNUNET_YES if such a value exists,
596 GNUNET_CONTAINER_multipeermap_contains (
597 const struct GNUNET_CONTAINER_MultiPeerMap *map,
598 const struct GNUNET_PeerIdentity *key)
602 me = map->map[idx_of (map, key)];
603 if (map->use_small_entries)
605 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
606 if (0 == GNUNET_memcmp (key, sme->key))
611 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
612 if (0 == GNUNET_memcmp (key, &bme->key))
620 * Check if the map contains the given value under the given
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,
630 GNUNET_CONTAINER_multipeermap_contains_value (
631 const struct GNUNET_CONTAINER_MultiPeerMap *map,
632 const struct GNUNET_PeerIdentity *key,
637 me = map->map[idx_of (map, key)];
638 if (map->use_small_entries)
640 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
641 if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value))
646 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
647 if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value))
655 * Grow the given map to a more appropriate size.
657 * @param map the hash map to grow
660 grow (struct GNUNET_CONTAINER_MultiPeerMap *map)
662 union MapEntry *old_map;
663 union MapEntry *new_map;
664 unsigned int old_len;
665 unsigned int new_len;
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));
678 return; /* grow not possible */
679 map->modification_counter++;
680 map->map_length = new_len;
682 for (unsigned int i = 0; i < old_len; i++)
684 if (map->use_small_entries)
686 struct SmallMapEntry *sme;
688 while (NULL != (sme = old_map[i].sme))
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;
698 struct BigMapEntry *bme;
700 while (NULL != (bme = old_map[i].bme))
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;
709 GNUNET_free (old_map);
714 * Store a key-value pair in 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
726 GNUNET_CONTAINER_multipeermap_put (struct GNUNET_CONTAINER_MultiPeerMap *map,
727 const struct GNUNET_PeerIdentity *key,
729 enum GNUNET_CONTAINER_MultiHashMapOption opt)
734 i = idx_of (map, key);
735 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
736 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
739 if (map->use_small_entries)
741 struct SmallMapEntry *sme;
743 for (sme = me.sme; NULL != sme; sme = sme->next)
744 if (0 == GNUNET_memcmp (key, sme->key))
746 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
747 return GNUNET_SYSERR;
754 struct BigMapEntry *bme;
756 for (bme = me.bme; NULL != bme; bme = bme->next)
757 if (0 == GNUNET_memcmp (key, &bme->key))
759 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
760 return GNUNET_SYSERR;
766 if (map->size / 3 >= map->map_length / 4)
769 i = idx_of (map, key);
771 if (map->use_small_entries)
773 struct SmallMapEntry *sme;
775 sme = GNUNET_new (struct SmallMapEntry);
778 sme->next = map->map[i].sme;
779 map->map[i].sme = sme;
783 struct BigMapEntry *bme;
785 bme = GNUNET_new (struct BigMapEntry);
788 bme->next = map->map[i].bme;
789 map->map[i].bme = bme;
797 * Iterate over all entries in the map that match a particular key.
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
807 GNUNET_CONTAINER_multipeermap_get_multiple (
808 struct GNUNET_CONTAINER_MultiPeerMap *map,
809 const struct GNUNET_PeerIdentity *key,
810 GNUNET_CONTAINER_PeerMapIterator it,
817 ce = &map->next_cache[map->next_cache_off];
818 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
820 me = map->map[idx_of (map, key)];
821 if (map->use_small_entries)
823 struct SmallMapEntry *sme;
826 while (NULL != (sme = ce->sme))
829 if (0 != GNUNET_memcmp (key, sme->key))
831 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value)))
833 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
834 return GNUNET_SYSERR;
841 struct BigMapEntry *bme;
844 while (NULL != (bme = ce->bme))
847 if (0 != GNUNET_memcmp (key, &bme->key))
849 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value)))
851 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
852 return GNUNET_SYSERR;
857 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
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).
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.
874 GNUNET_CONTAINER_multipeermap_get_random (
875 const struct GNUNET_CONTAINER_MultiPeerMap *map,
876 GNUNET_CONTAINER_PeerMapIterator it,
886 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size);
887 for (unsigned int idx = 0; idx < map->map_length; idx++)
890 if (map->use_small_entries)
892 struct SmallMapEntry *sme;
893 struct SmallMapEntry *nxt;
896 while (NULL != (sme = nxt))
901 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
902 return GNUNET_SYSERR;
910 struct BigMapEntry *bme;
911 struct BigMapEntry *nxt;
914 while (NULL != (bme = nxt))
919 if (GNUNET_OK != it (it_cls, &bme->key, bme->value))
920 return GNUNET_SYSERR;
928 return GNUNET_SYSERR;
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.
941 * @param map the map to create an iterator for
942 * @return an iterator over the given multipeermap 'map'
944 struct GNUNET_CONTAINER_MultiPeerMapIterator *
945 GNUNET_CONTAINER_multipeermap_iterator_create (
946 const struct GNUNET_CONTAINER_MultiPeerMap *map)
948 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
950 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMapIterator);
952 iter->modification_counter = map->modification_counter;
953 iter->me = map->map[0];
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'
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.
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
973 GNUNET_CONTAINER_multipeermap_iterator_next (
974 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter,
975 struct GNUNET_PeerIdentity *key,
978 /* make sure the map has not been modified */
979 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
981 /* look for the next entry, skipping empty buckets */
984 if (iter->idx >= iter->map->map_length)
986 if (GNUNET_YES == iter->map->use_small_entries)
988 if (NULL != iter->me.sme)
991 *key = *iter->me.sme->key;
993 *value = iter->me.sme->value;
994 iter->me.sme = iter->me.sme->next;
1000 if (NULL != iter->me.bme)
1003 *key = iter->me.bme->key;
1005 *value = iter->me.bme->value;
1006 iter->me.bme = iter->me.bme->next;
1011 if (iter->idx < iter->map->map_length)
1012 iter->me = iter->map->map[iter->idx];
1018 * Destroy a multipeermap iterator.
1020 * @param iter the iterator to destroy
1023 GNUNET_CONTAINER_multipeermap_iterator_destroy (
1024 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter)
1030 /* end of container_multipeermap.c */