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.
52 * If there is a hash collision, we create a linked list.
54 struct BigMapEntry *next;
59 struct GNUNET_HashCode key;
64 * An entry in the hash map with just a pointer to the key.
74 * If there is a hash collision, we create a linked list.
76 struct SmallMapEntry *next;
81 const struct GNUNET_HashCode *key;
91 * Variant used if map entries only contain a pointer to the key.
93 struct SmallMapEntry *sme;
96 * Variant used if map entries contain the full key.
98 struct BigMapEntry *bme;
103 * Internal representation of the hash map.
105 struct GNUNET_CONTAINER_MultiHashMap
108 * All of our buckets.
113 * Number of entries in the map.
118 * Length of the "map" array.
120 unsigned int map_length;
123 * #GNUNET_NO if the map entries are of type 'struct BigMapEntry',
124 * #GNUNET_YES if the map entries are of type 'struct SmallMapEntry'.
126 int use_small_entries;
129 * Counts the destructive modifications (grow, remove)
130 * to the map, so that iterators can check if they are still valid.
132 unsigned int modification_counter;
135 * Map entries indicating iteration positions currently
136 * in use by #GNUNET_CONTAINER_multihashmap_get_multiple().
137 * Only used up to @e next_cache_off.
139 union MapEntry next_cache[NEXT_CACHE_SIZE];
142 * Offset of @e next_cache entries in use, must be smaller
143 * than #NEXT_CACHE_SIZE.
145 unsigned int next_cache_off;
150 * Cursor into a multihashmap.
151 * Allows to enumerate elements asynchronously.
153 struct GNUNET_CONTAINER_MultiHashMapIterator
156 * Position in the bucket @e idx
161 * Current bucket index.
166 * Modification counter as observed on the map when the iterator
169 unsigned int modification_counter;
172 * Map that we are iterating over.
174 const struct GNUNET_CONTAINER_MultiHashMap *map;
179 * Create a multi hash map.
181 * @param len initial size (map will grow as needed)
182 * @param do_not_copy_keys #GNUNET_NO is always safe and should be used by default;
183 * #GNUNET_YES means that on 'put', the 'key' does not have
184 * to be copied as the destination of the pointer is
185 * guaranteed to be life as long as the value is stored in
186 * the hashmap. This can significantly reduce memory
187 * consumption, but of course is also a recipie for
188 * heap corruption if the assumption is not true. Only
189 * use this if (1) memory use is important in this case and
190 * (2) you have triple-checked that the invariant holds
191 * @return NULL on error
193 struct GNUNET_CONTAINER_MultiHashMap *
194 GNUNET_CONTAINER_multihashmap_create (unsigned int len, int do_not_copy_keys)
196 struct GNUNET_CONTAINER_MultiHashMap *hm;
198 GNUNET_assert (len > 0);
199 hm = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap);
200 if (len * sizeof(union MapEntry) > GNUNET_MAX_MALLOC_CHECKED)
203 /* application *explicitly* requested very large map, hopefully
204 it checks the return value... */
205 s = len * sizeof(union MapEntry);
206 if ((s / sizeof(union MapEntry)) != len)
207 return NULL; /* integer overflow on multiplication */
208 if (NULL == (hm->map = GNUNET_malloc_large (s)))
211 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
212 "Out of memory allocating large hash map (%u entries)\n",
220 hm->map = GNUNET_new_array (len, union MapEntry);
222 hm->map_length = len;
223 hm->use_small_entries = do_not_copy_keys;
229 * Destroy a hash map. Will not free any values stored in the hash
235 GNUNET_CONTAINER_multihashmap_destroy (
236 struct GNUNET_CONTAINER_MultiHashMap *map)
238 GNUNET_assert (0 == map->next_cache_off);
239 for (unsigned int i = 0; i < map->map_length; i++)
244 if (map->use_small_entries)
246 struct SmallMapEntry *sme;
247 struct SmallMapEntry *nxt;
250 while (NULL != (sme = nxt))
259 struct BigMapEntry *bme;
260 struct BigMapEntry *nxt;
263 while (NULL != (bme = nxt))
271 GNUNET_free (map->map);
277 * Compute the index of the bucket for the given key.
279 * @param map hash map for which to compute the index
280 * @param key what key should the index be computed for
281 * @return offset into the "map" array of "map"
284 idx_of (const struct GNUNET_CONTAINER_MultiHashMap *map,
285 const struct GNUNET_HashCode *key)
287 GNUNET_assert (map != NULL);
288 return (*(unsigned int *) key) % map->map_length;
293 * Get the number of key-value pairs in the map.
296 * @return the number of key value pairs
299 GNUNET_CONTAINER_multihashmap_size (
300 const struct GNUNET_CONTAINER_MultiHashMap *map)
307 * Given a key find a value in the map matching the key.
310 * @param key what to look for
311 * @return NULL if no value was found; note that
312 * this is indistinguishable from values that just
313 * happen to be NULL; use "contains" to test for
314 * key-value pairs with value NULL
317 GNUNET_CONTAINER_multihashmap_get (
318 const struct GNUNET_CONTAINER_MultiHashMap *map,
319 const struct GNUNET_HashCode *key)
323 me = map->map[idx_of (map, key)];
324 if (map->use_small_entries)
326 struct SmallMapEntry *sme;
328 for (sme = me.sme; NULL != sme; sme = sme->next)
329 if (0 == GNUNET_memcmp (key, sme->key))
334 struct BigMapEntry *bme;
336 for (bme = me.bme; NULL != bme; bme = bme->next)
337 if (0 == GNUNET_memcmp (key, &bme->key))
345 * Iterate over all entries in the map.
348 * @param it function to call on each entry
349 * @param it_cls extra argument to @a it
350 * @return the number of key value pairs processed,
351 * #GNUNET_SYSERR if it aborted iteration
354 GNUNET_CONTAINER_multihashmap_iterate (
355 struct GNUNET_CONTAINER_MultiHashMap *map,
356 GNUNET_CONTAINER_MulitHashMapIteratorCallback it,
362 struct GNUNET_HashCode kc;
364 GNUNET_assert (NULL != map);
365 ce = &map->next_cache[map->next_cache_off];
366 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
368 for (unsigned i = 0; i < map->map_length; i++)
371 if (map->use_small_entries)
373 struct SmallMapEntry *sme;
376 while (NULL != (sme = ce->sme))
381 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
383 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
384 return GNUNET_SYSERR;
392 struct BigMapEntry *bme;
395 while (NULL != (bme = ce->bme))
401 if (GNUNET_OK != it (it_cls, &kc, bme->value))
403 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
404 return GNUNET_SYSERR;
411 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
417 * We are about to free() the @a bme, make sure it is not in
418 * the list of next values for any iterator in the @a map's next_cache.
420 * @param map the map to check
421 * @param bme the entry that is about to be free'd
424 update_next_cache_bme (struct GNUNET_CONTAINER_MultiHashMap *map,
425 const struct BigMapEntry *bme)
427 for (unsigned int i = 0; i < map->next_cache_off; i++)
428 if (map->next_cache[i].bme == bme)
429 map->next_cache[i].bme = bme->next;
434 * We are about to free() the @a sme, make sure it is not in
435 * the list of next values for any iterator in the @a map's next_cache.
437 * @param map the map to check
438 * @param sme the entry that is about to be free'd
441 update_next_cache_sme (struct GNUNET_CONTAINER_MultiHashMap *map,
442 const struct SmallMapEntry *sme)
444 for (unsigned int i = 0; i < map->next_cache_off; i++)
445 if (map->next_cache[i].sme == sme)
446 map->next_cache[i].sme = sme->next;
451 * Remove the given key-value pair from the map. Note that if the
452 * key-value pair is in the map multiple times, only one of the pairs
456 * @param key key of the key-value pair
457 * @param value value of the key-value pair
458 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
462 GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map,
463 const struct GNUNET_HashCode *key,
469 map->modification_counter++;
471 i = idx_of (map, key);
473 if (map->use_small_entries)
475 struct SmallMapEntry *p;
478 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
480 if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value))
483 map->map[i].sme = sme->next;
486 update_next_cache_sme (map, sme);
496 struct BigMapEntry *p;
499 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
501 if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value))
504 map->map[i].bme = bme->next;
507 update_next_cache_bme (map, bme);
520 * Remove all entries for the given key from the map.
521 * Note that the values would not be "freed".
524 * @param key identifies values to be removed
525 * @return number of values removed
528 GNUNET_CONTAINER_multihashmap_remove_all (
529 struct GNUNET_CONTAINER_MultiHashMap *map,
530 const struct GNUNET_HashCode *key)
536 map->modification_counter++;
539 i = idx_of (map, key);
541 if (map->use_small_entries)
543 struct SmallMapEntry *sme;
544 struct SmallMapEntry *p;
550 if (0 == GNUNET_memcmp (key, sme->key))
553 map->map[i].sme = sme->next;
556 update_next_cache_sme (map, sme);
560 sme = map->map[i].sme;
574 struct BigMapEntry *bme;
575 struct BigMapEntry *p;
581 if (0 == GNUNET_memcmp (key, &bme->key))
584 map->map[i].bme = bme->next;
587 update_next_cache_bme (map, bme);
591 bme = map->map[i].bme;
608 * Callback used to remove all entries from the map.
610 * @param cls the `struct GNUNET_CONTAINER_MultiHashMap`
612 * @param value the value
613 * @return #GNUNET_OK (continue to iterate)
616 remove_all (void *cls, const struct GNUNET_HashCode *key, void *value)
618 struct GNUNET_CONTAINER_MultiHashMap *map = cls;
620 GNUNET_assert (GNUNET_YES ==
621 GNUNET_CONTAINER_multihashmap_remove (map, key, value));
628 * Remove all entries from the map.
629 * Note that the values would not be "freed".
632 * @return number of values removed
635 GNUNET_CONTAINER_multihashmap_clear (struct GNUNET_CONTAINER_MultiHashMap *map)
640 GNUNET_CONTAINER_multihashmap_iterate (map, &remove_all, map);
646 * Check if the map contains any value under the given
647 * key (including values that are NULL).
650 * @param key the key to test if a value exists for it
651 * @return #GNUNET_YES if such a value exists,
655 GNUNET_CONTAINER_multihashmap_contains (
656 const struct GNUNET_CONTAINER_MultiHashMap *map,
657 const struct GNUNET_HashCode *key)
661 me = map->map[idx_of (map, key)];
662 if (map->use_small_entries)
664 struct SmallMapEntry *sme;
666 for (sme = me.sme; NULL != sme; sme = sme->next)
667 if (0 == GNUNET_memcmp (key, sme->key))
672 struct BigMapEntry *bme;
674 for (bme = me.bme; NULL != bme; bme = bme->next)
675 if (0 == GNUNET_memcmp (key, &bme->key))
683 * Check if the map contains the given value under the given
687 * @param key the key to test if a value exists for it
688 * @param value value to test for
689 * @return #GNUNET_YES if such a value exists,
693 GNUNET_CONTAINER_multihashmap_contains_value (
694 const struct GNUNET_CONTAINER_MultiHashMap *map,
695 const struct GNUNET_HashCode *key,
700 me = map->map[idx_of (map, key)];
701 if (map->use_small_entries)
703 struct SmallMapEntry *sme;
705 for (sme = me.sme; NULL != sme; sme = sme->next)
706 if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value))
711 struct BigMapEntry *bme;
713 for (bme = me.bme; NULL != bme; bme = bme->next)
714 if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value))
722 * Grow the given map to a more appropriate size.
724 * @param map the hash map to grow
727 grow (struct GNUNET_CONTAINER_MultiHashMap *map)
729 union MapEntry *old_map;
730 union MapEntry *new_map;
731 unsigned int old_len;
732 unsigned int new_len;
736 old_len = map->map_length;
737 GNUNET_assert (0 != old_len);
738 new_len = old_len * 2;
739 if (0 == new_len) /* 2^31 * 2 == 0 */
740 new_len = old_len; /* never use 0 */
741 if (new_len == old_len)
742 return; /* nothing changed */
743 new_map = GNUNET_malloc_large (new_len * sizeof(union MapEntry));
745 return; /* grow not possible */
746 map->modification_counter++;
747 map->map_length = new_len;
749 for (unsigned int i = 0; i < old_len; i++)
751 if (map->use_small_entries)
753 struct SmallMapEntry *sme;
755 while (NULL != (sme = old_map[i].sme))
757 old_map[i].sme = sme->next;
758 idx = idx_of (map, sme->key);
759 sme->next = new_map[idx].sme;
760 new_map[idx].sme = sme;
765 struct BigMapEntry *bme;
767 while (NULL != (bme = old_map[i].bme))
769 old_map[i].bme = bme->next;
770 idx = idx_of (map, &bme->key);
771 bme->next = new_map[idx].bme;
772 new_map[idx].bme = bme;
776 GNUNET_free (old_map);
781 * Store a key-value pair in the map.
784 * @param key key to use
785 * @param value value to use
786 * @param opt options for put
787 * @return #GNUNET_OK on success,
788 * #GNUNET_NO if a value was replaced (with REPLACE)
789 * #GNUNET_SYSERR if UNIQUE_ONLY was the option and the
790 * value already exists
793 GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
794 const struct GNUNET_HashCode *key,
796 enum GNUNET_CONTAINER_MultiHashMapOption opt)
801 i = idx_of (map, key);
802 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
803 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
806 if (map->use_small_entries)
808 struct SmallMapEntry *sme;
810 for (sme = me.sme; NULL != sme; sme = sme->next)
811 if (0 == GNUNET_memcmp (key, sme->key))
813 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
814 return GNUNET_SYSERR;
821 struct BigMapEntry *bme;
823 for (bme = me.bme; NULL != bme; bme = bme->next)
824 if (0 == GNUNET_memcmp (key, &bme->key))
826 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
827 return GNUNET_SYSERR;
833 if (map->size / 3 >= map->map_length / 4)
836 i = idx_of (map, key);
838 if (map->use_small_entries)
840 struct SmallMapEntry *sme;
842 sme = GNUNET_new (struct SmallMapEntry);
845 sme->next = map->map[i].sme;
846 map->map[i].sme = sme;
850 struct BigMapEntry *bme;
852 bme = GNUNET_new (struct BigMapEntry);
855 bme->next = map->map[i].bme;
856 map->map[i].bme = bme;
864 * Iterate over all entries in the map that match a particular key.
867 * @param key key that the entries must correspond to
868 * @param it function to call on each entry
869 * @param it_cls extra argument to it
870 * @return the number of key value pairs processed,
871 * #GNUNET_SYSERR if it aborted iteration
874 GNUNET_CONTAINER_multihashmap_get_multiple (
875 struct GNUNET_CONTAINER_MultiHashMap *map,
876 const struct GNUNET_HashCode *key,
877 GNUNET_CONTAINER_MulitHashMapIteratorCallback it,
884 ce = &map->next_cache[map->next_cache_off];
885 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
887 me = &map->map[idx_of (map, key)];
888 if (map->use_small_entries)
890 struct SmallMapEntry *sme;
893 while (NULL != (sme = ce->sme))
896 if (0 != GNUNET_memcmp (key, sme->key))
898 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value)))
900 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
901 return GNUNET_SYSERR;
908 struct BigMapEntry *bme;
911 while (NULL != (bme = ce->bme))
914 if (0 != GNUNET_memcmp (key, &bme->key))
916 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value)))
918 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
919 return GNUNET_SYSERR;
924 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
931 * Call @a it on a random value from the map, or not at all
932 * if the map is empty. Note that this function has linear
933 * complexity (in the size of the map).
936 * @param it function to call on a random entry
937 * @param it_cls extra argument to @a it
938 * @return the number of key value pairs processed, zero or one.
941 GNUNET_CONTAINER_multihashmap_get_random (
942 const struct GNUNET_CONTAINER_MultiHashMap *map,
943 GNUNET_CONTAINER_MulitHashMapIteratorCallback it,
954 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size);
955 for (idx = 0; idx < map->map_length; idx++)
958 if (map->use_small_entries)
960 struct SmallMapEntry *sme;
961 struct SmallMapEntry *nxt;
964 while (NULL != (sme = nxt))
969 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
970 return GNUNET_SYSERR;
978 struct BigMapEntry *bme;
979 struct BigMapEntry *nxt;
982 while (NULL != (bme = nxt))
987 if (GNUNET_OK != it (it_cls, &bme->key, bme->value))
988 return GNUNET_SYSERR;
996 return GNUNET_SYSERR;
1001 * Create an iterator for a multihashmap.
1002 * The iterator can be used to retrieve all the elements in the multihashmap
1003 * one by one, without having to handle all elements at once (in contrast to
1004 * GNUNET_CONTAINER_multihashmap_iterate()). Note that the iterator can not be
1005 * used anymore if elements have been removed from 'map' after the creation of
1006 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
1007 * result in skipped or repeated elements.
1009 * @param map the map to create an iterator for
1010 * @return an iterator over the given multihashmap 'map'
1012 struct GNUNET_CONTAINER_MultiHashMapIterator *
1013 GNUNET_CONTAINER_multihashmap_iterator_create (
1014 const struct GNUNET_CONTAINER_MultiHashMap *map)
1016 struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
1018 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMapIterator);
1020 iter->modification_counter = map->modification_counter;
1021 iter->me = map->map[0];
1027 * Retrieve the next element from the hash map at the iterator's position.
1028 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
1030 * This operation is only allowed if no elements have been removed from the
1031 * multihashmap since the creation of 'iter', and the map has not been destroyed.
1032 * Adding elements may result in repeating or skipping elements.
1034 * @param iter the iterator to get the next element from
1035 * @param key pointer to store the key in, can be NULL
1036 * @param value pointer to store the value in, can be NULL
1037 * @return #GNUNET_YES we returned an element,
1038 * #GNUNET_NO if we are out of elements
1041 GNUNET_CONTAINER_multihashmap_iterator_next (
1042 struct GNUNET_CONTAINER_MultiHashMapIterator *iter,
1043 struct GNUNET_HashCode *key,
1046 /* make sure the map has not been modified */
1047 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
1049 /* look for the next entry, skipping empty buckets */
1052 if (iter->idx >= iter->map->map_length)
1054 if (GNUNET_YES == iter->map->use_small_entries)
1056 if (NULL != iter->me.sme)
1059 *key = *iter->me.sme->key;
1061 *value = iter->me.sme->value;
1062 iter->me.sme = iter->me.sme->next;
1068 if (NULL != iter->me.bme)
1071 *key = iter->me.bme->key;
1073 *value = iter->me.bme->value;
1074 iter->me.bme = iter->me.bme->next;
1079 if (iter->idx < iter->map->map_length)
1080 iter->me = iter->map->map[iter->idx];
1086 * Destroy a multihashmap iterator.
1088 * @param iter the iterator to destroy
1091 GNUNET_CONTAINER_multihashmap_iterator_destroy (
1092 struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
1098 /* end of container_multihashmap.c */