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.
52 * If there is a hash collision, we create a linked list.
54 struct BigMapEntry *next;
59 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;
92 * Variant used if map entries only contain a pointer to the key.
94 struct SmallMapEntry *sme;
97 * Variant used if map entries contain the full key.
99 struct BigMapEntry *bme;
104 * Internal representation of the hash map.
106 struct GNUNET_CONTAINER_MultiPeerMap
109 * All of our buckets.
114 * Number of entries in the map.
119 * Length of the "map" array.
121 unsigned int map_length;
124 * #GNUNET_NO if the map entries are of type 'struct BigMapEntry',
125 * #GNUNET_YES if the map entries are of type 'struct SmallMapEntry'.
127 int use_small_entries;
130 * Counts the destructive modifications (grow, remove)
131 * to the map, so that iterators can check if they are still valid.
133 unsigned int modification_counter;
136 * Map entries indicating iteration positions currently
137 * in use by #GNUNET_CONTAINER_multihashmap_get_multiple().
138 * Only used up to @e next_cache_off.
140 union MapEntry next_cache[NEXT_CACHE_SIZE];
143 * Offset of @e next_cache entries in use, must be smaller
144 * than #NEXT_CACHE_SIZE.
146 unsigned int next_cache_off;
151 * Cursor into a multipeermap.
152 * Allows to enumerate elements asynchronously.
154 struct GNUNET_CONTAINER_MultiPeerMapIterator
157 * Position in the bucket 'idx'
162 * Current bucket index.
167 * Modification counter as observed on the map when the iterator
170 unsigned int modification_counter;
173 * Map that we are iterating over.
175 const struct GNUNET_CONTAINER_MultiPeerMap *map;
180 * Create a multi hash map.
182 * @param len initial size (map will grow as needed)
183 * @param do_not_copy_keys GNUNET_NO is always safe and should be used by default;
184 * GNUNET_YES means that on 'put', the 'key' does not have
185 * to be copied as the destination of the pointer is
186 * guaranteed to be life as long as the value is stored in
187 * the hashmap. This can significantly reduce memory
188 * consumption, but of course is also a recipie for
189 * heap corruption if the assumption is not true. Only
190 * use this if (1) memory use is important in this case and
191 * (2) you have triple-checked that the invariant holds
192 * @return NULL on error
194 struct GNUNET_CONTAINER_MultiPeerMap *
195 GNUNET_CONTAINER_multipeermap_create (unsigned int len, 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_malloc_large (len * sizeof (union MapEntry));
202 if (NULL == map->map)
207 map->map_length = len;
208 map->use_small_entries = do_not_copy_keys;
214 * Destroy a hash map. Will not free any values
215 * stored in the hash map!
220 GNUNET_CONTAINER_multipeermap_destroy (
221 struct GNUNET_CONTAINER_MultiPeerMap *map)
223 GNUNET_assert (0 == map->next_cache_off);
224 for (unsigned int i = 0; i < map->map_length; i++)
229 if (map->use_small_entries)
231 struct SmallMapEntry *sme;
232 struct SmallMapEntry *nxt;
235 while (NULL != (sme = nxt))
244 struct BigMapEntry *bme;
245 struct BigMapEntry *nxt;
248 while (NULL != (bme = nxt))
256 GNUNET_free (map->map);
262 * Compute the index of the bucket for the given key.
264 * @param map hash map for which to compute the index
265 * @param key what key should the index be computed for
266 * @return offset into the "map" array of "map"
269 idx_of (const struct GNUNET_CONTAINER_MultiPeerMap *map,
270 const struct GNUNET_PeerIdentity *key)
274 GNUNET_assert (NULL != map);
275 GNUNET_memcpy (&kx, key, sizeof (kx));
276 return kx % map->map_length;
281 * Get the number of key-value pairs in the map.
284 * @return the number of key value pairs
287 GNUNET_CONTAINER_multipeermap_size (
288 const struct GNUNET_CONTAINER_MultiPeerMap *map)
295 * Given a key find a value in the map matching the key.
298 * @param key what to look for
299 * @return NULL if no value was found; note that
300 * this is indistinguishable from values that just
301 * happen to be NULL; use "contains" to test for
302 * key-value pairs with value NULL
305 GNUNET_CONTAINER_multipeermap_get (
306 const struct GNUNET_CONTAINER_MultiPeerMap *map,
307 const struct GNUNET_PeerIdentity *key)
311 me = map->map[idx_of (map, key)];
312 if (map->use_small_entries)
314 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
315 if (0 == GNUNET_memcmp (key, sme->key))
320 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
321 if (0 == GNUNET_memcmp (key, &bme->key))
329 * Iterate over all entries in the map.
332 * @param it function to call on each entry
333 * @param it_cls extra argument to @a it
334 * @return the number of key value pairs processed,
335 * #GNUNET_SYSERR if it aborted iteration
338 GNUNET_CONTAINER_multipeermap_iterate (
339 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, sme->key, sme->value))
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, &kc, bme->value))
387 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
388 return GNUNET_SYSERR;
395 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
401 * We are about to free() the @a bme, make sure it is not in
402 * the list of next values for any iterator in the @a map's next_cache.
404 * @param map the map to check
405 * @param bme the entry that is about to be free'd
408 update_next_cache_bme (struct GNUNET_CONTAINER_MultiPeerMap *map,
409 const struct BigMapEntry *bme)
411 for (unsigned int i = 0; i < map->next_cache_off; i++)
412 if (map->next_cache[i].bme == bme)
413 map->next_cache[i].bme = bme->next;
418 * We are about to free() the @a sme, make sure it is not in
419 * the list of next values for any iterator in the @a map's next_cache.
421 * @param map the map to check
422 * @param sme the entry that is about to be free'd
425 update_next_cache_sme (struct GNUNET_CONTAINER_MultiPeerMap *map,
426 const struct SmallMapEntry *sme)
428 for (unsigned int i = 0; i < map->next_cache_off; i++)
429 if (map->next_cache[i].sme == sme)
430 map->next_cache[i].sme = sme->next;
435 * Remove the given key-value pair from the map. Note that if the
436 * key-value pair is in the map multiple times, only one of the pairs
440 * @param key key of the key-value pair
441 * @param value value of the key-value pair
442 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
446 GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map,
447 const struct GNUNET_PeerIdentity *key,
453 map->modification_counter++;
454 i = idx_of (map, key);
456 if (map->use_small_entries)
458 struct SmallMapEntry *p = NULL;
460 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
462 if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value))
465 map->map[i].sme = sme->next;
468 update_next_cache_sme (map, sme);
478 struct BigMapEntry *p = NULL;
480 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
482 if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value))
485 map->map[i].bme = bme->next;
488 update_next_cache_bme (map, bme);
501 * Remove all entries for the given key from the map.
502 * Note that the values would not be "freed".
505 * @param key identifies values to be removed
506 * @return number of values removed
509 GNUNET_CONTAINER_multipeermap_remove_all (
510 struct GNUNET_CONTAINER_MultiPeerMap *map,
511 const struct GNUNET_PeerIdentity *key)
517 map->modification_counter++;
520 i = idx_of (map, key);
522 if (map->use_small_entries)
524 struct SmallMapEntry *sme;
525 struct SmallMapEntry *p;
531 if (0 == GNUNET_memcmp (key, sme->key))
534 map->map[i].sme = sme->next;
537 update_next_cache_sme (map, sme);
541 sme = map->map[i].sme;
555 struct BigMapEntry *bme;
556 struct BigMapEntry *p;
562 if (0 == GNUNET_memcmp (key, &bme->key))
565 map->map[i].bme = bme->next;
568 update_next_cache_bme (map, bme);
572 bme = map->map[i].bme;
589 * Check if the map contains any value under the given
590 * key (including values that are NULL).
593 * @param key the key to test if a value exists for it
594 * @return #GNUNET_YES if such a value exists,
598 GNUNET_CONTAINER_multipeermap_contains (
599 const struct GNUNET_CONTAINER_MultiPeerMap *map,
600 const struct GNUNET_PeerIdentity *key)
604 me = map->map[idx_of (map, key)];
605 if (map->use_small_entries)
607 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
608 if (0 == GNUNET_memcmp (key, sme->key))
613 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
614 if (0 == GNUNET_memcmp (key, &bme->key))
622 * Check if the map contains the given value under the given
626 * @param key the key to test if a value exists for it
627 * @param value value to test for
628 * @return #GNUNET_YES if such a value exists,
632 GNUNET_CONTAINER_multipeermap_contains_value (
633 const struct GNUNET_CONTAINER_MultiPeerMap *map,
634 const struct GNUNET_PeerIdentity *key,
639 me = map->map[idx_of (map, key)];
640 if (map->use_small_entries)
642 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
643 if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value))
648 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
649 if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value))
657 * Grow the given map to a more appropriate size.
659 * @param map the hash map to grow
662 grow (struct GNUNET_CONTAINER_MultiPeerMap *map)
664 union MapEntry *old_map;
665 union MapEntry *new_map;
666 unsigned int old_len;
667 unsigned int new_len;
671 old_len = map->map_length;
672 GNUNET_assert (0 != old_len);
673 new_len = old_len * 2;
674 if (0 == new_len) /* 2^31 * 2 == 0 */
675 new_len = old_len; /* never use 0 */
676 if (new_len == old_len)
677 return; /* nothing changed */
678 new_map = GNUNET_malloc_large (new_len * sizeof (union MapEntry));
680 return; /* grow not possible */
681 map->modification_counter++;
682 map->map_length = new_len;
684 for (unsigned int i = 0; i < old_len; i++)
686 if (map->use_small_entries)
688 struct SmallMapEntry *sme;
690 while (NULL != (sme = old_map[i].sme))
692 old_map[i].sme = sme->next;
693 idx = idx_of (map, sme->key);
694 sme->next = new_map[idx].sme;
695 new_map[idx].sme = sme;
700 struct BigMapEntry *bme;
702 while (NULL != (bme = old_map[i].bme))
704 old_map[i].bme = bme->next;
705 idx = idx_of (map, &bme->key);
706 bme->next = new_map[idx].bme;
707 new_map[idx].bme = bme;
711 GNUNET_free (old_map);
716 * Store a key-value pair in the map.
719 * @param key key to use
720 * @param value value to use
721 * @param opt options for put
722 * @return #GNUNET_OK on success,
723 * #GNUNET_NO if a value was replaced (with REPLACE)
724 * #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
725 * value already exists
728 GNUNET_CONTAINER_multipeermap_put (struct GNUNET_CONTAINER_MultiPeerMap *map,
729 const struct GNUNET_PeerIdentity *key,
731 enum GNUNET_CONTAINER_MultiHashMapOption opt)
736 i = idx_of (map, key);
737 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
738 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
741 if (map->use_small_entries)
743 struct SmallMapEntry *sme;
745 for (sme = me.sme; NULL != sme; sme = sme->next)
746 if (0 == GNUNET_memcmp (key, sme->key))
748 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
749 return GNUNET_SYSERR;
756 struct BigMapEntry *bme;
758 for (bme = me.bme; NULL != bme; bme = bme->next)
759 if (0 == GNUNET_memcmp (key, &bme->key))
761 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
762 return GNUNET_SYSERR;
768 if (map->size / 3 >= map->map_length / 4)
771 i = idx_of (map, key);
773 if (map->use_small_entries)
775 struct SmallMapEntry *sme;
777 sme = GNUNET_new (struct SmallMapEntry);
780 sme->next = map->map[i].sme;
781 map->map[i].sme = sme;
785 struct BigMapEntry *bme;
787 bme = GNUNET_new (struct BigMapEntry);
790 bme->next = map->map[i].bme;
791 map->map[i].bme = bme;
799 * Iterate over all entries in the map that match a particular key.
802 * @param key key that the entries must correspond to
803 * @param it function to call on each entry
804 * @param it_cls extra argument to @a it
805 * @return the number of key value pairs processed,
806 * #GNUNET_SYSERR if it aborted iteration
809 GNUNET_CONTAINER_multipeermap_get_multiple (
810 struct GNUNET_CONTAINER_MultiPeerMap *map,
811 const struct GNUNET_PeerIdentity *key,
812 GNUNET_CONTAINER_PeerMapIterator it,
819 ce = &map->next_cache[map->next_cache_off];
820 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
822 me = map->map[idx_of (map, key)];
823 if (map->use_small_entries)
825 struct SmallMapEntry *sme;
828 while (NULL != (sme = ce->sme))
831 if (0 != GNUNET_memcmp (key, sme->key))
833 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value)))
835 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
836 return GNUNET_SYSERR;
843 struct BigMapEntry *bme;
846 while (NULL != (bme = ce->bme))
849 if (0 != GNUNET_memcmp (key, &bme->key))
851 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value)))
853 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
854 return GNUNET_SYSERR;
859 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
866 * Call @a it on a random value from the map, or not at all
867 * if the map is empty. Note that this function has linear
868 * complexity (in the size of the map).
871 * @param it function to call on a random entry
872 * @param it_cls extra argument to @a it
873 * @return the number of key value pairs processed, zero or one.
876 GNUNET_CONTAINER_multipeermap_get_random (
877 const struct GNUNET_CONTAINER_MultiPeerMap *map,
878 GNUNET_CONTAINER_PeerMapIterator it,
888 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size);
889 for (unsigned int idx = 0; idx < map->map_length; idx++)
892 if (map->use_small_entries)
894 struct SmallMapEntry *sme;
895 struct SmallMapEntry *nxt;
898 while (NULL != (sme = nxt))
903 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
904 return GNUNET_SYSERR;
912 struct BigMapEntry *bme;
913 struct BigMapEntry *nxt;
916 while (NULL != (bme = nxt))
921 if (GNUNET_OK != it (it_cls, &bme->key, bme->value))
922 return GNUNET_SYSERR;
930 return GNUNET_SYSERR;
935 * Create an iterator for a multipeermap.
936 * The iterator can be used to retrieve all the elements in the multipeermap
937 * one by one, without having to handle all elements at once (in contrast to
938 * #GNUNET_CONTAINER_multipeermap_iterate). Note that the iterator can not be
939 * used anymore if elements have been removed from 'map' after the creation of
940 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
941 * result in skipped or repeated elements.
943 * @param map the map to create an iterator for
944 * @return an iterator over the given multipeermap 'map'
946 struct GNUNET_CONTAINER_MultiPeerMapIterator *
947 GNUNET_CONTAINER_multipeermap_iterator_create (
948 const struct GNUNET_CONTAINER_MultiPeerMap *map)
950 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
952 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMapIterator);
954 iter->modification_counter = map->modification_counter;
955 iter->me = map->map[0];
961 * Retrieve the next element from the hash map at the iterator's position.
962 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
964 * This operation is only allowed if no elements have been removed from the
965 * multipeermap since the creation of 'iter', and the map has not been destroyed.
966 * Adding elements may result in repeating or skipping elements.
968 * @param iter the iterator to get the next element from
969 * @param key pointer to store the key in, can be NULL
970 * @param value pointer to store the value in, can be NULL
971 * @return #GNUNET_YES we returned an element,
972 * #GNUNET_NO if we are out of elements
975 GNUNET_CONTAINER_multipeermap_iterator_next (
976 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter,
977 struct GNUNET_PeerIdentity *key,
980 /* make sure the map has not been modified */
981 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
983 /* look for the next entry, skipping empty buckets */
986 if (iter->idx >= iter->map->map_length)
988 if (GNUNET_YES == iter->map->use_small_entries)
990 if (NULL != iter->me.sme)
993 *key = *iter->me.sme->key;
995 *value = iter->me.sme->value;
996 iter->me.sme = iter->me.sme->next;
1002 if (NULL != iter->me.bme)
1005 *key = iter->me.bme->key;
1007 *value = iter->me.bme->value;
1008 iter->me.bme = iter->me.bme->next;
1013 if (iter->idx < iter->map->map_length)
1014 iter->me = iter->map->map[iter->idx];
1020 * Destroy a multipeermap iterator.
1022 * @param iter the iterator to destroy
1025 GNUNET_CONTAINER_multipeermap_iterator_destroy (
1026 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter)
1032 /* end of container_multipeermap.c */