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_multihashmap.c
22 * @brief hash map where the same key may be present multiple times
23 * @author Christian Grothoff
27 #include "gnunet_container_lib.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "util-container-multihashmap", __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
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_HashCode key;
65 * An entry in the hash map with just a pointer to the key.
76 * If there is a hash collision, we create a linked list.
78 struct SmallMapEntry *next;
83 const struct GNUNET_HashCode *key;
94 * Variant used if map entries only contain a pointer to the key.
96 struct SmallMapEntry *sme;
99 * Variant used if map entries contain the full key.
101 struct BigMapEntry *bme;
106 * Internal representation of the hash map.
108 struct GNUNET_CONTAINER_MultiHashMap
111 * All of our buckets.
116 * Number of entries in the map.
121 * Length of the "map" array.
123 unsigned int map_length;
126 * #GNUNET_NO if the map entries are of type 'struct BigMapEntry',
127 * #GNUNET_YES if the map entries are of type 'struct SmallMapEntry'.
129 int use_small_entries;
132 * Counts the destructive modifications (grow, remove)
133 * to the map, so that iterators can check if they are still valid.
135 unsigned int modification_counter;
138 * Map entries indicating iteration positions currently
139 * in use by #GNUNET_CONTAINER_multihashmap_get_multiple().
140 * Only used up to @e next_cache_off.
142 union MapEntry next_cache[NEXT_CACHE_SIZE];
145 * Offset of @e next_cache entries in use, must be smaller
146 * than #NEXT_CACHE_SIZE.
148 unsigned int next_cache_off;
153 * Cursor into a multihashmap.
154 * Allows to enumerate elements asynchronously.
156 struct GNUNET_CONTAINER_MultiHashMapIterator
159 * Position in the bucket @e idx
164 * Current bucket index.
169 * Modification counter as observed on the map when the iterator
172 unsigned int modification_counter;
175 * Map that we are iterating over.
177 const struct GNUNET_CONTAINER_MultiHashMap *map;
182 * Create a multi hash map.
184 * @param len initial size (map will grow as needed)
185 * @param do_not_copy_keys #GNUNET_NO is always safe and should be used by default;
186 * #GNUNET_YES means that on 'put', the 'key' does not have
187 * to be copied as the destination of the pointer is
188 * guaranteed to be life as long as the value is stored in
189 * the hashmap. This can significantly reduce memory
190 * consumption, but of course is also a recipie for
191 * heap corruption if the assumption is not true. Only
192 * use this if (1) memory use is important in this case and
193 * (2) you have triple-checked that the invariant holds
194 * @return NULL on error
196 struct GNUNET_CONTAINER_MultiHashMap *
197 GNUNET_CONTAINER_multihashmap_create (unsigned int len,
198 int do_not_copy_keys)
200 struct GNUNET_CONTAINER_MultiHashMap *hm;
202 GNUNET_assert (len > 0);
203 hm = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap);
204 if (len * sizeof (union MapEntry) > GNUNET_MAX_MALLOC_CHECKED)
207 /* application *explicitly* requested very large map, hopefully
208 it checks the return value... */
209 s = len * sizeof (union MapEntry);
210 if ( (s / sizeof (union MapEntry)) != len)
211 return NULL; /* integer overflow on multiplication */
212 if (NULL == (hm->map = GNUNET_malloc_large (s)))
215 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
216 "Out of memory allocating large hash map (%u entries)\n",
224 hm->map = GNUNET_new_array (len,
227 hm->map_length = len;
228 hm->use_small_entries = do_not_copy_keys;
234 * Destroy a hash map. Will not free any values stored in the hash
240 GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap *map)
242 GNUNET_assert (0 == map->next_cache_off);
243 for (unsigned int i = 0; i < map->map_length; i++)
248 if (map->use_small_entries)
250 struct SmallMapEntry *sme;
251 struct SmallMapEntry *nxt;
254 while (NULL != (sme = nxt))
263 struct BigMapEntry *bme;
264 struct BigMapEntry *nxt;
267 while (NULL != (bme = nxt))
275 GNUNET_free (map->map);
281 * Compute the index of the bucket for the given key.
283 * @param map hash map for which to compute the index
284 * @param key what key should the index be computed for
285 * @return offset into the "map" array of "map"
288 idx_of (const struct GNUNET_CONTAINER_MultiHashMap *map,
289 const struct GNUNET_HashCode *key)
291 GNUNET_assert (map != NULL);
292 return (*(unsigned int *) key) % map->map_length;
297 * Get the number of key-value pairs in the map.
300 * @return the number of key value pairs
303 GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap *map)
310 * Given a key find a value in the map matching the key.
313 * @param key what to look for
314 * @return NULL if no value was found; note that
315 * this is indistinguishable from values that just
316 * happen to be NULL; use "contains" to test for
317 * key-value pairs with value NULL
320 GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap *map,
321 const struct GNUNET_HashCode *key)
325 me = map->map[idx_of (map, key)];
326 if (map->use_small_entries)
328 struct SmallMapEntry *sme;
330 for (sme = me.sme; NULL != sme; sme = sme->next)
331 if (0 == memcmp (key,
333 sizeof (struct GNUNET_HashCode)))
338 struct BigMapEntry *bme;
340 for (bme = me.bme; NULL != bme; bme = bme->next)
341 if (0 == memcmp (key,
343 sizeof (struct GNUNET_HashCode)))
351 * Iterate over all entries in the map.
354 * @param it function to call on each entry
355 * @param it_cls extra argument to @a it
356 * @return the number of key value pairs processed,
357 * #GNUNET_SYSERR if it aborted iteration
360 GNUNET_CONTAINER_multihashmap_iterate (struct GNUNET_CONTAINER_MultiHashMap *map,
361 GNUNET_CONTAINER_HashMapIterator it,
367 struct GNUNET_HashCode kc;
369 GNUNET_assert (NULL != map);
370 ce = &map->next_cache[map->next_cache_off];
371 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
373 for (unsigned i = 0; i < map->map_length; i++)
376 if (map->use_small_entries)
378 struct SmallMapEntry *sme;
381 while (NULL != (sme = ce->sme))
386 if (GNUNET_OK != it (it_cls,
390 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
391 return GNUNET_SYSERR;
399 struct BigMapEntry *bme;
402 while (NULL != (bme = ce->bme))
408 if (GNUNET_OK != it (it_cls,
412 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
413 return GNUNET_SYSERR;
420 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
426 * We are about to free() the @a bme, make sure it is not in
427 * the list of next values for any iterator in the @a map's next_cache.
429 * @param map the map to check
430 * @param bme the entry that is about to be free'd
433 update_next_cache_bme (struct GNUNET_CONTAINER_MultiHashMap *map,
434 const struct BigMapEntry *bme)
436 for (unsigned int i=0;i<map->next_cache_off;i++)
437 if (map->next_cache[i].bme == bme)
438 map->next_cache[i].bme = bme->next;
443 * We are about to free() the @a sme, make sure it is not in
444 * the list of next values for any iterator in the @a map's next_cache.
446 * @param map the map to check
447 * @param sme the entry that is about to be free'd
450 update_next_cache_sme (struct GNUNET_CONTAINER_MultiHashMap *map,
451 const struct SmallMapEntry *sme)
453 for (unsigned int i=0;i<map->next_cache_off;i++)
454 if (map->next_cache[i].sme == sme)
455 map->next_cache[i].sme = sme->next;
460 * Remove the given key-value pair from the map. Note that if the
461 * key-value pair is in the map multiple times, only one of the pairs
465 * @param key key of the key-value pair
466 * @param value value of the key-value pair
467 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
471 GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map,
472 const struct GNUNET_HashCode *key,
478 map->modification_counter++;
480 i = idx_of (map, key);
482 if (map->use_small_entries)
484 struct SmallMapEntry *p;
487 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
489 if ( (0 == memcmp (key,
491 sizeof (struct GNUNET_HashCode))) &&
492 (value == sme->value) )
495 map->map[i].sme = sme->next;
498 update_next_cache_sme (map,
509 struct BigMapEntry *p;
512 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
514 if ( (0 == memcmp (key,
516 sizeof (struct GNUNET_HashCode))) &&
517 (value == bme->value) )
520 map->map[i].bme = bme->next;
523 update_next_cache_bme (map,
537 * Remove all entries for the given key from the map.
538 * Note that the values would not be "freed".
541 * @param key identifies values to be removed
542 * @return number of values removed
545 GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap *map,
546 const struct GNUNET_HashCode *key)
552 map->modification_counter++;
555 i = idx_of (map, key);
557 if (map->use_small_entries)
559 struct SmallMapEntry *sme;
560 struct SmallMapEntry *p;
566 if (0 == memcmp (key,
568 sizeof (struct GNUNET_HashCode)))
571 map->map[i].sme = sme->next;
574 update_next_cache_sme (map,
579 sme = map->map[i].sme;
593 struct BigMapEntry *bme;
594 struct BigMapEntry *p;
600 if (0 == memcmp (key,
602 sizeof (struct GNUNET_HashCode)))
605 map->map[i].bme = bme->next;
608 update_next_cache_bme (map,
613 bme = map->map[i].bme;
630 * Callback used to remove all entries from the map.
632 * @param cls the `struct GNUNET_CONTAINER_MultiHashMap`
634 * @param value the value
635 * @return #GNUNET_OK (continue to iterate)
638 remove_all (void *cls,
639 const struct GNUNET_HashCode *key,
642 struct GNUNET_CONTAINER_MultiHashMap *map = cls;
644 GNUNET_CONTAINER_multihashmap_remove (map,
653 * Remove all entries from the map.
654 * Note that the values would not be "freed".
657 * @return number of values removed
660 GNUNET_CONTAINER_multihashmap_clear (struct GNUNET_CONTAINER_MultiHashMap *map)
665 GNUNET_CONTAINER_multihashmap_iterate (map,
673 * Check if the map contains any value under the given
674 * key (including values that are NULL).
677 * @param key the key to test if a value exists for it
678 * @return #GNUNET_YES if such a value exists,
682 GNUNET_CONTAINER_multihashmap_contains (const struct
683 GNUNET_CONTAINER_MultiHashMap *map,
684 const struct GNUNET_HashCode *key)
688 me = map->map[idx_of (map, key)];
689 if (map->use_small_entries)
691 struct SmallMapEntry *sme;
693 for (sme = me.sme; NULL != sme; sme = sme->next)
694 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
699 struct BigMapEntry *bme;
701 for (bme = me.bme; NULL != bme; bme = bme->next)
702 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
710 * Check if the map contains the given value under the given
714 * @param key the key to test if a value exists for it
715 * @param value value to test for
716 * @return #GNUNET_YES if such a value exists,
720 GNUNET_CONTAINER_multihashmap_contains_value (const struct GNUNET_CONTAINER_MultiHashMap *map,
721 const struct GNUNET_HashCode *key,
726 me = map->map[idx_of (map, key)];
727 if (map->use_small_entries)
729 struct SmallMapEntry *sme;
731 for (sme = me.sme; NULL != sme; sme = sme->next)
732 if ( (0 == memcmp (key,
734 sizeof (struct GNUNET_HashCode))) &&
735 (sme->value == value) )
740 struct BigMapEntry *bme;
742 for (bme = me.bme; NULL != bme; bme = bme->next)
743 if ( (0 == memcmp (key,
745 sizeof (struct GNUNET_HashCode))) &&
746 (bme->value == value) )
754 * Grow the given map to a more appropriate size.
756 * @param map the hash map to grow
759 grow (struct GNUNET_CONTAINER_MultiHashMap *map)
761 union MapEntry *old_map;
762 union MapEntry *new_map;
763 unsigned int old_len;
764 unsigned int new_len;
768 old_len = map->map_length;
769 GNUNET_assert (0 != old_len);
770 new_len = old_len * 2;
771 if (0 == new_len) /* 2^31 * 2 == 0 */
772 new_len = old_len; /* never use 0 */
773 if (new_len == old_len)
774 return; /* nothing changed */
775 new_map = GNUNET_malloc_large (new_len *
776 sizeof (union MapEntry));
778 return; /* grow not possible */
779 map->modification_counter++;
780 map->map_length = new_len;
782 for (unsigned int i = 0; i < old_len; i++)
784 if (map->use_small_entries)
786 struct SmallMapEntry *sme;
788 while (NULL != (sme = old_map[i].sme))
790 old_map[i].sme = sme->next;
791 idx = idx_of (map, sme->key);
792 sme->next = new_map[idx].sme;
793 new_map[idx].sme = sme;
798 struct BigMapEntry *bme;
800 while (NULL != (bme = old_map[i].bme))
802 old_map[i].bme = bme->next;
803 idx = idx_of (map, &bme->key);
804 bme->next = new_map[idx].bme;
805 new_map[idx].bme = bme;
809 GNUNET_free (old_map);
814 * Store a key-value pair in the map.
817 * @param key key to use
818 * @param value value to use
819 * @param opt options for put
820 * @return #GNUNET_OK on success,
821 * #GNUNET_NO if a value was replaced (with REPLACE)
822 * #GNUNET_SYSERR if UNIQUE_ONLY was the option and the
823 * value already exists
826 GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
827 const struct GNUNET_HashCode *key,
829 enum GNUNET_CONTAINER_MultiHashMapOption opt)
834 i = idx_of (map, key);
835 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
836 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
839 if (map->use_small_entries)
841 struct SmallMapEntry *sme;
843 for (sme = me.sme; NULL != sme; sme = sme->next)
844 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
846 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
847 return GNUNET_SYSERR;
854 struct BigMapEntry *bme;
856 for (bme = me.bme; NULL != bme; bme = bme->next)
857 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
859 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
860 return GNUNET_SYSERR;
866 if (map->size / 3 >= map->map_length / 4)
869 i = idx_of (map, key);
871 if (map->use_small_entries)
873 struct SmallMapEntry *sme;
875 sme = GNUNET_new (struct SmallMapEntry);
878 sme->next = map->map[i].sme;
879 map->map[i].sme = sme;
883 struct BigMapEntry *bme;
885 bme = GNUNET_new (struct BigMapEntry);
888 bme->next = map->map[i].bme;
889 map->map[i].bme = bme;
897 * Iterate over all entries in the map that match a particular key.
900 * @param key key that the entries must correspond to
901 * @param it function to call on each entry
902 * @param it_cls extra argument to it
903 * @return the number of key value pairs processed,
904 * #GNUNET_SYSERR if it aborted iteration
907 GNUNET_CONTAINER_multihashmap_get_multiple (struct GNUNET_CONTAINER_MultiHashMap *map,
908 const struct GNUNET_HashCode *key,
909 GNUNET_CONTAINER_HashMapIterator it,
916 ce = &map->next_cache[map->next_cache_off];
917 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
919 me = &map->map[idx_of (map, key)];
920 if (map->use_small_entries)
922 struct SmallMapEntry *sme;
925 while (NULL != (sme = ce->sme))
928 if (0 != memcmp (key,
930 sizeof (struct GNUNET_HashCode)))
933 (GNUNET_OK != it (it_cls,
937 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
938 return GNUNET_SYSERR;
945 struct BigMapEntry *bme;
948 while (NULL != (bme = ce->bme))
951 if (0 != memcmp (key,
953 sizeof (struct GNUNET_HashCode)))
956 (GNUNET_OK != it (it_cls,
960 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
961 return GNUNET_SYSERR;
966 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
973 * Call @a it on a random value from the map, or not at all
974 * if the map is empty. Note that this function has linear
975 * complexity (in the size of the map).
978 * @param it function to call on a random entry
979 * @param it_cls extra argument to @a it
980 * @return the number of key value pairs processed, zero or one.
983 GNUNET_CONTAINER_multihashmap_get_random (const struct GNUNET_CONTAINER_MultiHashMap *map,
984 GNUNET_CONTAINER_HashMapIterator it,
995 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
997 for (idx = 0; idx < map->map_length; idx++)
1000 if (map->use_small_entries)
1002 struct SmallMapEntry *sme;
1003 struct SmallMapEntry *nxt;
1006 while (NULL != (sme = nxt))
1011 if (GNUNET_OK != it (it_cls,
1014 return GNUNET_SYSERR;
1022 struct BigMapEntry *bme;
1023 struct BigMapEntry *nxt;
1026 while (NULL != (bme = nxt))
1031 if (GNUNET_OK != it (it_cls,
1032 &bme->key, bme->value))
1033 return GNUNET_SYSERR;
1041 return GNUNET_SYSERR;
1046 * Create an iterator for a multihashmap.
1047 * The iterator can be used to retrieve all the elements in the multihashmap
1048 * one by one, without having to handle all elements at once (in contrast to
1049 * GNUNET_CONTAINER_multihashmap_iterate()). Note that the iterator can not be
1050 * used anymore if elements have been removed from 'map' after the creation of
1051 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
1052 * result in skipped or repeated elements.
1054 * @param map the map to create an iterator for
1055 * @return an iterator over the given multihashmap 'map'
1057 struct GNUNET_CONTAINER_MultiHashMapIterator *
1058 GNUNET_CONTAINER_multihashmap_iterator_create (const struct GNUNET_CONTAINER_MultiHashMap *map)
1060 struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
1062 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMapIterator);
1064 iter->modification_counter = map->modification_counter;
1065 iter->me = map->map[0];
1071 * Retrieve the next element from the hash map at the iterator's position.
1072 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
1074 * This operation is only allowed if no elements have been removed from the
1075 * multihashmap since the creation of 'iter', and the map has not been destroyed.
1076 * Adding elements may result in repeating or skipping elements.
1078 * @param iter the iterator to get the next element from
1079 * @param key pointer to store the key in, can be NULL
1080 * @param value pointer to store the value in, can be NULL
1081 * @return #GNUNET_YES we returned an element,
1082 * #GNUNET_NO if we are out of elements
1085 GNUNET_CONTAINER_multihashmap_iterator_next (struct GNUNET_CONTAINER_MultiHashMapIterator *iter,
1086 struct GNUNET_HashCode *key,
1089 /* make sure the map has not been modified */
1090 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
1092 /* look for the next entry, skipping empty buckets */
1095 if (iter->idx >= iter->map->map_length)
1097 if (GNUNET_YES == iter->map->use_small_entries)
1099 if (NULL != iter->me.sme)
1102 *key = *iter->me.sme->key;
1104 *value = iter->me.sme->value;
1105 iter->me.sme = iter->me.sme->next;
1111 if (NULL != iter->me.bme)
1114 *key = iter->me.bme->key;
1116 *value = iter->me.bme->value;
1117 iter->me.bme = iter->me.bme->next;
1122 if (iter->idx < iter->map->map_length)
1123 iter->me = iter->map->map[iter->idx];
1129 * Destroy a multihashmap iterator.
1131 * @param iter the iterator to destroy
1134 GNUNET_CONTAINER_multihashmap_iterator_destroy (struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
1140 /* end of container_multihashmap.c */