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/>.
19 * @file util/container_multihashmap.c
20 * @brief hash map where the same key may be present multiple times
21 * @author Christian Grothoff
25 #include "gnunet_container_lib.h"
27 #define LOG(kind,...) GNUNET_log_from (kind, "util-container-multihashmap", __VA_ARGS__)
30 * Maximum recursion depth for callbacks of
31 * #GNUNET_CONTAINER_multihashmap_get_multiple() themselve s
32 * again calling #GNUNET_CONTAINER_multihashmap_get_multiple().
33 * Should be totally excessive, but if violated we die.
35 #define NEXT_CACHE_SIZE 16
39 * An entry in the hash map with the full key.
50 * If there is a hash collision, we create a linked list.
52 struct BigMapEntry *next;
57 struct GNUNET_HashCode key;
63 * 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;
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_MultiHashMap
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 multihashmap.
152 * Allows to enumerate elements asynchronously.
154 struct GNUNET_CONTAINER_MultiHashMapIterator
157 * Position in the bucket @e 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_MultiHashMap *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_MultiHashMap *
195 GNUNET_CONTAINER_multihashmap_create (unsigned int len,
196 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,
225 hm->map_length = len;
226 hm->use_small_entries = do_not_copy_keys;
232 * Destroy a hash map. Will not free any values stored in the hash
238 GNUNET_CONTAINER_multihashmap_destroy (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 (const struct GNUNET_CONTAINER_MultiHashMap *map)
308 * Given a key find a value in the map matching the key.
311 * @param key what to look for
312 * @return NULL if no value was found; note that
313 * this is indistinguishable from values that just
314 * happen to be NULL; use "contains" to test for
315 * key-value pairs with value NULL
318 GNUNET_CONTAINER_multihashmap_get (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 == memcmp (key,
331 sizeof (struct GNUNET_HashCode)))
336 struct BigMapEntry *bme;
338 for (bme = me.bme; NULL != bme; bme = bme->next)
339 if (0 == memcmp (key,
341 sizeof (struct GNUNET_HashCode)))
349 * Iterate over all entries in the map.
352 * @param it function to call on each entry
353 * @param it_cls extra argument to @a it
354 * @return the number of key value pairs processed,
355 * #GNUNET_SYSERR if it aborted iteration
358 GNUNET_CONTAINER_multihashmap_iterate (struct GNUNET_CONTAINER_MultiHashMap *map,
359 GNUNET_CONTAINER_HashMapIterator it,
365 struct GNUNET_HashCode kc;
367 GNUNET_assert (NULL != map);
368 ce = &map->next_cache[map->next_cache_off];
369 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
371 for (unsigned i = 0; i < map->map_length; i++)
374 if (map->use_small_entries)
376 struct SmallMapEntry *sme;
379 while (NULL != (sme = ce->sme))
384 if (GNUNET_OK != it (it_cls,
388 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
389 return GNUNET_SYSERR;
397 struct BigMapEntry *bme;
400 while (NULL != (bme = ce->bme))
406 if (GNUNET_OK != it (it_cls,
410 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
411 return GNUNET_SYSERR;
418 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
424 * We are about to free() the @a bme, make sure it is not in
425 * the list of next values for any iterator in the @a map's next_cache.
427 * @param map the map to check
428 * @param bme the entry that is about to be free'd
431 update_next_cache_bme (struct GNUNET_CONTAINER_MultiHashMap *map,
432 const struct BigMapEntry *bme)
434 for (unsigned int i=0;i<map->next_cache_off;i++)
435 if (map->next_cache[i].bme == bme)
436 map->next_cache[i].bme = bme->next;
441 * We are about to free() the @a sme, make sure it is not in
442 * the list of next values for any iterator in the @a map's next_cache.
444 * @param map the map to check
445 * @param sme the entry that is about to be free'd
448 update_next_cache_sme (struct GNUNET_CONTAINER_MultiHashMap *map,
449 const struct SmallMapEntry *sme)
451 for (unsigned int i=0;i<map->next_cache_off;i++)
452 if (map->next_cache[i].sme == sme)
453 map->next_cache[i].sme = sme->next;
458 * Remove the given key-value pair from the map. Note that if the
459 * key-value pair is in the map multiple times, only one of the pairs
463 * @param key key of the key-value pair
464 * @param value value of the key-value pair
465 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
469 GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map,
470 const struct GNUNET_HashCode *key,
476 map->modification_counter++;
478 i = idx_of (map, key);
480 if (map->use_small_entries)
482 struct SmallMapEntry *p;
485 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
487 if ( (0 == memcmp (key,
489 sizeof (struct GNUNET_HashCode))) &&
490 (value == sme->value) )
493 map->map[i].sme = sme->next;
496 update_next_cache_sme (map,
507 struct BigMapEntry *p;
510 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
512 if ( (0 == memcmp (key,
514 sizeof (struct GNUNET_HashCode))) &&
515 (value == bme->value) )
518 map->map[i].bme = bme->next;
521 update_next_cache_bme (map,
535 * Remove all entries for the given key from the map.
536 * Note that the values would not be "freed".
539 * @param key identifies values to be removed
540 * @return number of values removed
543 GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap *map,
544 const struct GNUNET_HashCode *key)
550 map->modification_counter++;
553 i = idx_of (map, key);
555 if (map->use_small_entries)
557 struct SmallMapEntry *sme;
558 struct SmallMapEntry *p;
564 if (0 == memcmp (key,
566 sizeof (struct GNUNET_HashCode)))
569 map->map[i].sme = sme->next;
572 update_next_cache_sme (map,
577 sme = map->map[i].sme;
591 struct BigMapEntry *bme;
592 struct BigMapEntry *p;
598 if (0 == memcmp (key,
600 sizeof (struct GNUNET_HashCode)))
603 map->map[i].bme = bme->next;
606 update_next_cache_bme (map,
611 bme = map->map[i].bme;
628 * Callback used to remove all entries from the map.
630 * @param cls the `struct GNUNET_CONTAINER_MultiHashMap`
632 * @param value the value
633 * @return #GNUNET_OK (continue to iterate)
636 remove_all (void *cls,
637 const struct GNUNET_HashCode *key,
640 struct GNUNET_CONTAINER_MultiHashMap *map = cls;
642 GNUNET_CONTAINER_multihashmap_remove (map,
651 * Remove all entries from the map.
652 * Note that the values would not be "freed".
655 * @return number of values removed
658 GNUNET_CONTAINER_multihashmap_clear (struct GNUNET_CONTAINER_MultiHashMap *map)
663 GNUNET_CONTAINER_multihashmap_iterate (map,
671 * Check if the map contains any value under the given
672 * key (including values that are NULL).
675 * @param key the key to test if a value exists for it
676 * @return #GNUNET_YES if such a value exists,
680 GNUNET_CONTAINER_multihashmap_contains (const struct
681 GNUNET_CONTAINER_MultiHashMap *map,
682 const struct GNUNET_HashCode *key)
686 me = map->map[idx_of (map, key)];
687 if (map->use_small_entries)
689 struct SmallMapEntry *sme;
691 for (sme = me.sme; NULL != sme; sme = sme->next)
692 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
697 struct BigMapEntry *bme;
699 for (bme = me.bme; NULL != bme; bme = bme->next)
700 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
708 * Check if the map contains the given value under the given
712 * @param key the key to test if a value exists for it
713 * @param value value to test for
714 * @return #GNUNET_YES if such a value exists,
718 GNUNET_CONTAINER_multihashmap_contains_value (const struct GNUNET_CONTAINER_MultiHashMap *map,
719 const struct GNUNET_HashCode *key,
724 me = map->map[idx_of (map, key)];
725 if (map->use_small_entries)
727 struct SmallMapEntry *sme;
729 for (sme = me.sme; NULL != sme; sme = sme->next)
730 if ( (0 == memcmp (key,
732 sizeof (struct GNUNET_HashCode))) &&
733 (sme->value == value) )
738 struct BigMapEntry *bme;
740 for (bme = me.bme; NULL != bme; bme = bme->next)
741 if ( (0 == memcmp (key,
743 sizeof (struct GNUNET_HashCode))) &&
744 (bme->value == value) )
752 * Grow the given map to a more appropriate size.
754 * @param map the hash map to grow
757 grow (struct GNUNET_CONTAINER_MultiHashMap *map)
759 union MapEntry *old_map;
760 union MapEntry *new_map;
761 unsigned int old_len;
762 unsigned int new_len;
765 map->modification_counter++;
768 old_len = map->map_length;
769 new_len = old_len * 2;
770 /* if we would exceed heap size limit for the _first_ time,
771 try staying just below the limit */
772 if ( (new_len * sizeof (union MapEntry) > GNUNET_MAX_MALLOC_CHECKED) &&
773 ((old_len+1) * sizeof (union MapEntry) < GNUNET_MAX_MALLOC_CHECKED) )
774 new_len = GNUNET_MAX_MALLOC_CHECKED / sizeof (union MapEntry);
775 new_map = GNUNET_new_array (new_len,
777 map->map_length = new_len;
779 for (unsigned int i = 0; i < old_len; i++)
781 if (map->use_small_entries)
783 struct SmallMapEntry *sme;
785 while (NULL != (sme = old_map[i].sme))
787 old_map[i].sme = sme->next;
788 idx = idx_of (map, sme->key);
789 sme->next = new_map[idx].sme;
790 new_map[idx].sme = sme;
795 struct BigMapEntry *bme;
797 while (NULL != (bme = old_map[i].bme))
799 old_map[i].bme = bme->next;
800 idx = idx_of (map, &bme->key);
801 bme->next = new_map[idx].bme;
802 new_map[idx].bme = bme;
806 GNUNET_free (old_map);
811 * Store a key-value pair in the map.
814 * @param key key to use
815 * @param value value to use
816 * @param opt options for put
817 * @return #GNUNET_OK on success,
818 * #GNUNET_NO if a value was replaced (with REPLACE)
819 * #GNUNET_SYSERR if UNIQUE_ONLY was the option and the
820 * value already exists
823 GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
824 const struct GNUNET_HashCode *key,
826 enum GNUNET_CONTAINER_MultiHashMapOption opt)
831 i = idx_of (map, key);
832 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
833 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
836 if (map->use_small_entries)
838 struct SmallMapEntry *sme;
840 for (sme = me.sme; NULL != sme; sme = sme->next)
841 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
843 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
844 return GNUNET_SYSERR;
851 struct BigMapEntry *bme;
853 for (bme = me.bme; NULL != bme; bme = bme->next)
854 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
856 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
857 return GNUNET_SYSERR;
863 if (map->size / 3 >= map->map_length / 4)
866 i = idx_of (map, key);
868 if (map->use_small_entries)
870 struct SmallMapEntry *sme;
872 sme = GNUNET_new (struct SmallMapEntry);
875 sme->next = map->map[i].sme;
876 map->map[i].sme = sme;
880 struct BigMapEntry *bme;
882 bme = GNUNET_new (struct BigMapEntry);
885 bme->next = map->map[i].bme;
886 map->map[i].bme = bme;
894 * Iterate over all entries in the map that match a particular key.
897 * @param key key that the entries must correspond to
898 * @param it function to call on each entry
899 * @param it_cls extra argument to it
900 * @return the number of key value pairs processed,
901 * #GNUNET_SYSERR if it aborted iteration
904 GNUNET_CONTAINER_multihashmap_get_multiple (struct GNUNET_CONTAINER_MultiHashMap *map,
905 const struct GNUNET_HashCode *key,
906 GNUNET_CONTAINER_HashMapIterator it,
913 ce = &map->next_cache[map->next_cache_off];
914 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
916 me = &map->map[idx_of (map, key)];
917 if (map->use_small_entries)
919 struct SmallMapEntry *sme;
922 while (NULL != (sme = ce->sme))
925 if (0 != memcmp (key,
927 sizeof (struct GNUNET_HashCode)))
930 (GNUNET_OK != it (it_cls,
934 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
935 return GNUNET_SYSERR;
942 struct BigMapEntry *bme;
945 while (NULL != (bme = ce->bme))
948 if (0 != memcmp (key,
950 sizeof (struct GNUNET_HashCode)))
953 (GNUNET_OK != it (it_cls,
957 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
958 return GNUNET_SYSERR;
963 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
970 * Call @a it on a random value from the map, or not at all
971 * if the map is empty. Note that this function has linear
972 * complexity (in the size of the map).
975 * @param it function to call on a random entry
976 * @param it_cls extra argument to @a it
977 * @return the number of key value pairs processed, zero or one.
980 GNUNET_CONTAINER_multihashmap_get_random (const struct GNUNET_CONTAINER_MultiHashMap *map,
981 GNUNET_CONTAINER_HashMapIterator it,
992 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
994 for (idx = 0; idx < map->map_length; idx++)
997 if (map->use_small_entries)
999 struct SmallMapEntry *sme;
1000 struct SmallMapEntry *nxt;
1003 while (NULL != (sme = nxt))
1008 if (GNUNET_OK != it (it_cls,
1011 return GNUNET_SYSERR;
1019 struct BigMapEntry *bme;
1020 struct BigMapEntry *nxt;
1023 while (NULL != (bme = nxt))
1028 if (GNUNET_OK != it (it_cls,
1029 &bme->key, bme->value))
1030 return GNUNET_SYSERR;
1038 return GNUNET_SYSERR;
1043 * Create an iterator for a multihashmap.
1044 * The iterator can be used to retrieve all the elements in the multihashmap
1045 * one by one, without having to handle all elements at once (in contrast to
1046 * GNUNET_CONTAINER_multihashmap_iterate()). Note that the iterator can not be
1047 * used anymore if elements have been removed from 'map' after the creation of
1048 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
1049 * result in skipped or repeated elements.
1051 * @param map the map to create an iterator for
1052 * @return an iterator over the given multihashmap 'map'
1054 struct GNUNET_CONTAINER_MultiHashMapIterator *
1055 GNUNET_CONTAINER_multihashmap_iterator_create (const struct GNUNET_CONTAINER_MultiHashMap *map)
1057 struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
1059 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMapIterator);
1061 iter->modification_counter = map->modification_counter;
1062 iter->me = map->map[0];
1068 * Retrieve the next element from the hash map at the iterator's position.
1069 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
1071 * This operation is only allowed if no elements have been removed from the
1072 * multihashmap since the creation of 'iter', and the map has not been destroyed.
1073 * Adding elements may result in repeating or skipping elements.
1075 * @param iter the iterator to get the next element from
1076 * @param key pointer to store the key in, can be NULL
1077 * @param value pointer to store the value in, can be NULL
1078 * @return #GNUNET_YES we returned an element,
1079 * #GNUNET_NO if we are out of elements
1082 GNUNET_CONTAINER_multihashmap_iterator_next (struct GNUNET_CONTAINER_MultiHashMapIterator *iter,
1083 struct GNUNET_HashCode *key,
1086 /* make sure the map has not been modified */
1087 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
1089 /* look for the next entry, skipping empty buckets */
1092 if (iter->idx >= iter->map->map_length)
1094 if (GNUNET_YES == iter->map->use_small_entries)
1096 if (NULL != iter->me.sme)
1099 *key = *iter->me.sme->key;
1101 *value = iter->me.sme->value;
1102 iter->me.sme = iter->me.sme->next;
1108 if (NULL != iter->me.bme)
1111 *key = iter->me.bme->key;
1113 *value = iter->me.bme->value;
1114 iter->me.bme = iter->me.bme->next;
1119 if (iter->idx < iter->map->map_length)
1120 iter->me = iter->map->map[iter->idx];
1126 * Destroy a multihashmap iterator.
1128 * @param iter the iterator to destroy
1131 GNUNET_CONTAINER_multihashmap_iterator_destroy (struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
1137 /* end of container_multihashmap.c */