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, ...) \
30 GNUNET_log_from (kind, "util-container-multihashmap", __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
42 * An entry in the hash map with the full key.
53 * If there is a hash collision, we create a linked list.
55 struct BigMapEntry *next;
60 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;
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_MultiHashMap
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 multihashmap.
153 * Allows to enumerate elements asynchronously.
155 struct GNUNET_CONTAINER_MultiHashMapIterator
158 * Position in the bucket @e 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_MultiHashMap *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_MultiHashMap *
196 GNUNET_CONTAINER_multihashmap_create (unsigned int len, int do_not_copy_keys)
198 struct GNUNET_CONTAINER_MultiHashMap *hm;
200 GNUNET_assert (len > 0);
201 hm = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap);
202 if (len * sizeof (union MapEntry) > GNUNET_MAX_MALLOC_CHECKED)
205 /* application *explicitly* requested very large map, hopefully
206 it checks the return value... */
207 s = len * sizeof (union MapEntry);
208 if ((s / sizeof (union MapEntry)) != len)
209 return NULL; /* integer overflow on multiplication */
210 if (NULL == (hm->map = GNUNET_malloc_large (s)))
213 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
214 "Out of memory allocating large hash map (%u entries)\n",
222 hm->map = GNUNET_new_array (len, union MapEntry);
224 hm->map_length = len;
225 hm->use_small_entries = do_not_copy_keys;
231 * Destroy a hash map. Will not free any values stored in the hash
237 GNUNET_CONTAINER_multihashmap_destroy (
238 struct GNUNET_CONTAINER_MultiHashMap *map)
240 GNUNET_assert (0 == map->next_cache_off);
241 for (unsigned int i = 0; i < map->map_length; i++)
246 if (map->use_small_entries)
248 struct SmallMapEntry *sme;
249 struct SmallMapEntry *nxt;
252 while (NULL != (sme = nxt))
261 struct BigMapEntry *bme;
262 struct BigMapEntry *nxt;
265 while (NULL != (bme = nxt))
273 GNUNET_free (map->map);
279 * Compute the index of the bucket for the given key.
281 * @param map hash map for which to compute the index
282 * @param key what key should the index be computed for
283 * @return offset into the "map" array of "map"
286 idx_of (const struct GNUNET_CONTAINER_MultiHashMap *map,
287 const struct GNUNET_HashCode *key)
289 GNUNET_assert (map != NULL);
290 return (*(unsigned int *) key) % map->map_length;
295 * Get the number of key-value pairs in the map.
298 * @return the number of key value pairs
301 GNUNET_CONTAINER_multihashmap_size (
302 const struct GNUNET_CONTAINER_MultiHashMap *map)
309 * Given a key find a value in the map matching the key.
312 * @param key what to look for
313 * @return NULL if no value was found; note that
314 * this is indistinguishable from values that just
315 * happen to be NULL; use "contains" to test for
316 * key-value pairs with value NULL
319 GNUNET_CONTAINER_multihashmap_get (
320 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 == GNUNET_memcmp (key, sme->key))
336 struct BigMapEntry *bme;
338 for (bme = me.bme; NULL != bme; bme = bme->next)
339 if (0 == GNUNET_memcmp (key, &bme->key))
347 * Iterate over all entries in the map.
350 * @param it function to call on each entry
351 * @param it_cls extra argument to @a it
352 * @return the number of key value pairs processed,
353 * #GNUNET_SYSERR if it aborted iteration
356 GNUNET_CONTAINER_multihashmap_iterate (
357 struct GNUNET_CONTAINER_MultiHashMap *map,
358 GNUNET_CONTAINER_HashMapIterator it,
364 struct GNUNET_HashCode kc;
366 GNUNET_assert (NULL != map);
367 ce = &map->next_cache[map->next_cache_off];
368 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
370 for (unsigned i = 0; i < map->map_length; i++)
373 if (map->use_small_entries)
375 struct SmallMapEntry *sme;
378 while (NULL != (sme = ce->sme))
383 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
385 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
386 return GNUNET_SYSERR;
394 struct BigMapEntry *bme;
397 while (NULL != (bme = ce->bme))
403 if (GNUNET_OK != it (it_cls, &kc, bme->value))
405 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
406 return GNUNET_SYSERR;
413 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
419 * We are about to free() the @a bme, make sure it is not in
420 * the list of next values for any iterator in the @a map's next_cache.
422 * @param map the map to check
423 * @param bme the entry that is about to be free'd
426 update_next_cache_bme (struct GNUNET_CONTAINER_MultiHashMap *map,
427 const struct BigMapEntry *bme)
429 for (unsigned int i = 0; i < map->next_cache_off; i++)
430 if (map->next_cache[i].bme == bme)
431 map->next_cache[i].bme = bme->next;
436 * We are about to free() the @a sme, make sure it is not in
437 * the list of next values for any iterator in the @a map's next_cache.
439 * @param map the map to check
440 * @param sme the entry that is about to be free'd
443 update_next_cache_sme (struct GNUNET_CONTAINER_MultiHashMap *map,
444 const struct SmallMapEntry *sme)
446 for (unsigned int i = 0; i < map->next_cache_off; i++)
447 if (map->next_cache[i].sme == sme)
448 map->next_cache[i].sme = sme->next;
453 * Remove the given key-value pair from the map. Note that if the
454 * key-value pair is in the map multiple times, only one of the pairs
458 * @param key key of the key-value pair
459 * @param value value of the key-value pair
460 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
464 GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map,
465 const struct GNUNET_HashCode *key,
471 map->modification_counter++;
473 i = idx_of (map, key);
475 if (map->use_small_entries)
477 struct SmallMapEntry *p;
480 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
482 if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value))
485 map->map[i].sme = sme->next;
488 update_next_cache_sme (map, sme);
498 struct BigMapEntry *p;
501 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
503 if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value))
506 map->map[i].bme = bme->next;
509 update_next_cache_bme (map, bme);
522 * Remove all entries for the given key from the map.
523 * Note that the values would not be "freed".
526 * @param key identifies values to be removed
527 * @return number of values removed
530 GNUNET_CONTAINER_multihashmap_remove_all (
531 struct GNUNET_CONTAINER_MultiHashMap *map,
532 const struct GNUNET_HashCode *key)
538 map->modification_counter++;
541 i = idx_of (map, key);
543 if (map->use_small_entries)
545 struct SmallMapEntry *sme;
546 struct SmallMapEntry *p;
552 if (0 == GNUNET_memcmp (key, sme->key))
555 map->map[i].sme = sme->next;
558 update_next_cache_sme (map, sme);
562 sme = map->map[i].sme;
576 struct BigMapEntry *bme;
577 struct BigMapEntry *p;
583 if (0 == GNUNET_memcmp (key, &bme->key))
586 map->map[i].bme = bme->next;
589 update_next_cache_bme (map, bme);
593 bme = map->map[i].bme;
610 * Callback used to remove all entries from the map.
612 * @param cls the `struct GNUNET_CONTAINER_MultiHashMap`
614 * @param value the value
615 * @return #GNUNET_OK (continue to iterate)
618 remove_all (void *cls, const struct GNUNET_HashCode *key, void *value)
620 struct GNUNET_CONTAINER_MultiHashMap *map = cls;
622 GNUNET_assert (GNUNET_YES ==
623 GNUNET_CONTAINER_multihashmap_remove (map, key, value));
630 * Remove all entries from the map.
631 * Note that the values would not be "freed".
634 * @return number of values removed
637 GNUNET_CONTAINER_multihashmap_clear (struct GNUNET_CONTAINER_MultiHashMap *map)
642 GNUNET_CONTAINER_multihashmap_iterate (map, &remove_all, map);
648 * Check if the map contains any value under the given
649 * key (including values that are NULL).
652 * @param key the key to test if a value exists for it
653 * @return #GNUNET_YES if such a value exists,
657 GNUNET_CONTAINER_multihashmap_contains (
658 const struct GNUNET_CONTAINER_MultiHashMap *map,
659 const struct GNUNET_HashCode *key)
663 me = map->map[idx_of (map, key)];
664 if (map->use_small_entries)
666 struct SmallMapEntry *sme;
668 for (sme = me.sme; NULL != sme; sme = sme->next)
669 if (0 == GNUNET_memcmp (key, sme->key))
674 struct BigMapEntry *bme;
676 for (bme = me.bme; NULL != bme; bme = bme->next)
677 if (0 == GNUNET_memcmp (key, &bme->key))
685 * Check if the map contains the given value under the given
689 * @param key the key to test if a value exists for it
690 * @param value value to test for
691 * @return #GNUNET_YES if such a value exists,
695 GNUNET_CONTAINER_multihashmap_contains_value (
696 const struct GNUNET_CONTAINER_MultiHashMap *map,
697 const struct GNUNET_HashCode *key,
702 me = map->map[idx_of (map, key)];
703 if (map->use_small_entries)
705 struct SmallMapEntry *sme;
707 for (sme = me.sme; NULL != sme; sme = sme->next)
708 if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value))
713 struct BigMapEntry *bme;
715 for (bme = me.bme; NULL != bme; bme = bme->next)
716 if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value))
724 * Grow the given map to a more appropriate size.
726 * @param map the hash map to grow
729 grow (struct GNUNET_CONTAINER_MultiHashMap *map)
731 union MapEntry *old_map;
732 union MapEntry *new_map;
733 unsigned int old_len;
734 unsigned int new_len;
738 old_len = map->map_length;
739 GNUNET_assert (0 != old_len);
740 new_len = old_len * 2;
741 if (0 == new_len) /* 2^31 * 2 == 0 */
742 new_len = old_len; /* never use 0 */
743 if (new_len == old_len)
744 return; /* nothing changed */
745 new_map = GNUNET_malloc_large (new_len * sizeof (union MapEntry));
747 return; /* grow not possible */
748 map->modification_counter++;
749 map->map_length = new_len;
751 for (unsigned int i = 0; i < old_len; i++)
753 if (map->use_small_entries)
755 struct SmallMapEntry *sme;
757 while (NULL != (sme = old_map[i].sme))
759 old_map[i].sme = sme->next;
760 idx = idx_of (map, sme->key);
761 sme->next = new_map[idx].sme;
762 new_map[idx].sme = sme;
767 struct BigMapEntry *bme;
769 while (NULL != (bme = old_map[i].bme))
771 old_map[i].bme = bme->next;
772 idx = idx_of (map, &bme->key);
773 bme->next = new_map[idx].bme;
774 new_map[idx].bme = bme;
778 GNUNET_free (old_map);
783 * Store a key-value pair in the map.
786 * @param key key to use
787 * @param value value to use
788 * @param opt options for put
789 * @return #GNUNET_OK on success,
790 * #GNUNET_NO if a value was replaced (with REPLACE)
791 * #GNUNET_SYSERR if UNIQUE_ONLY was the option and the
792 * value already exists
795 GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
796 const struct GNUNET_HashCode *key,
798 enum GNUNET_CONTAINER_MultiHashMapOption opt)
803 i = idx_of (map, key);
804 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
805 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
808 if (map->use_small_entries)
810 struct SmallMapEntry *sme;
812 for (sme = me.sme; NULL != sme; sme = sme->next)
813 if (0 == GNUNET_memcmp (key, sme->key))
815 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
816 return GNUNET_SYSERR;
823 struct BigMapEntry *bme;
825 for (bme = me.bme; NULL != bme; bme = bme->next)
826 if (0 == GNUNET_memcmp (key, &bme->key))
828 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
829 return GNUNET_SYSERR;
835 if (map->size / 3 >= map->map_length / 4)
838 i = idx_of (map, key);
840 if (map->use_small_entries)
842 struct SmallMapEntry *sme;
844 sme = GNUNET_new (struct SmallMapEntry);
847 sme->next = map->map[i].sme;
848 map->map[i].sme = sme;
852 struct BigMapEntry *bme;
854 bme = GNUNET_new (struct BigMapEntry);
857 bme->next = map->map[i].bme;
858 map->map[i].bme = bme;
866 * Iterate over all entries in the map that match a particular key.
869 * @param key key that the entries must correspond to
870 * @param it function to call on each entry
871 * @param it_cls extra argument to it
872 * @return the number of key value pairs processed,
873 * #GNUNET_SYSERR if it aborted iteration
876 GNUNET_CONTAINER_multihashmap_get_multiple (
877 struct GNUNET_CONTAINER_MultiHashMap *map,
878 const struct GNUNET_HashCode *key,
879 GNUNET_CONTAINER_HashMapIterator it,
886 ce = &map->next_cache[map->next_cache_off];
887 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
889 me = &map->map[idx_of (map, key)];
890 if (map->use_small_entries)
892 struct SmallMapEntry *sme;
895 while (NULL != (sme = ce->sme))
898 if (0 != GNUNET_memcmp (key, sme->key))
900 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value)))
902 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
903 return GNUNET_SYSERR;
910 struct BigMapEntry *bme;
913 while (NULL != (bme = ce->bme))
916 if (0 != GNUNET_memcmp (key, &bme->key))
918 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value)))
920 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
921 return GNUNET_SYSERR;
926 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
933 * Call @a it on a random value from the map, or not at all
934 * if the map is empty. Note that this function has linear
935 * complexity (in the size of the map).
938 * @param it function to call on a random entry
939 * @param it_cls extra argument to @a it
940 * @return the number of key value pairs processed, zero or one.
943 GNUNET_CONTAINER_multihashmap_get_random (
944 const struct GNUNET_CONTAINER_MultiHashMap *map,
945 GNUNET_CONTAINER_HashMapIterator it,
956 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size);
957 for (idx = 0; idx < map->map_length; idx++)
960 if (map->use_small_entries)
962 struct SmallMapEntry *sme;
963 struct SmallMapEntry *nxt;
966 while (NULL != (sme = nxt))
971 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
972 return GNUNET_SYSERR;
980 struct BigMapEntry *bme;
981 struct BigMapEntry *nxt;
984 while (NULL != (bme = nxt))
989 if (GNUNET_OK != it (it_cls, &bme->key, bme->value))
990 return GNUNET_SYSERR;
998 return GNUNET_SYSERR;
1003 * Create an iterator for a multihashmap.
1004 * The iterator can be used to retrieve all the elements in the multihashmap
1005 * one by one, without having to handle all elements at once (in contrast to
1006 * GNUNET_CONTAINER_multihashmap_iterate()). Note that the iterator can not be
1007 * used anymore if elements have been removed from 'map' after the creation of
1008 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
1009 * result in skipped or repeated elements.
1011 * @param map the map to create an iterator for
1012 * @return an iterator over the given multihashmap 'map'
1014 struct GNUNET_CONTAINER_MultiHashMapIterator *
1015 GNUNET_CONTAINER_multihashmap_iterator_create (
1016 const struct GNUNET_CONTAINER_MultiHashMap *map)
1018 struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
1020 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMapIterator);
1022 iter->modification_counter = map->modification_counter;
1023 iter->me = map->map[0];
1029 * Retrieve the next element from the hash map at the iterator's position.
1030 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
1032 * This operation is only allowed if no elements have been removed from the
1033 * multihashmap since the creation of 'iter', and the map has not been destroyed.
1034 * Adding elements may result in repeating or skipping elements.
1036 * @param iter the iterator to get the next element from
1037 * @param key pointer to store the key in, can be NULL
1038 * @param value pointer to store the value in, can be NULL
1039 * @return #GNUNET_YES we returned an element,
1040 * #GNUNET_NO if we are out of elements
1043 GNUNET_CONTAINER_multihashmap_iterator_next (
1044 struct GNUNET_CONTAINER_MultiHashMapIterator *iter,
1045 struct GNUNET_HashCode *key,
1048 /* make sure the map has not been modified */
1049 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
1051 /* look for the next entry, skipping empty buckets */
1054 if (iter->idx >= iter->map->map_length)
1056 if (GNUNET_YES == iter->map->use_small_entries)
1058 if (NULL != iter->me.sme)
1061 *key = *iter->me.sme->key;
1063 *value = iter->me.sme->value;
1064 iter->me.sme = iter->me.sme->next;
1070 if (NULL != iter->me.bme)
1073 *key = iter->me.bme->key;
1075 *value = iter->me.bme->value;
1076 iter->me.bme = iter->me.bme->next;
1081 if (iter->idx < iter->map->map_length)
1082 iter->me = iter->map->map[iter->idx];
1088 * Destroy a multihashmap iterator.
1090 * @param iter the iterator to destroy
1093 GNUNET_CONTAINER_multihashmap_iterator_destroy (
1094 struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
1100 /* end of container_multihashmap.c */