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_multishortmap.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,...) GNUNET_log_from (kind, "util-container-multishortmap", __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_ShortHashCode 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_ShortHashCode *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_MultiShortmap
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;
154 * Cursor into a multishortmap.
155 * Allows to enumerate elements asynchronously.
157 struct GNUNET_CONTAINER_MultiShortmapIterator
160 * Position in the bucket 'idx'
165 * Current bucket index.
170 * Modification counter as observed on the map when the iterator
173 unsigned int modification_counter;
176 * Map that we are iterating over.
178 const struct GNUNET_CONTAINER_MultiShortmap *map;
183 * Create a multi hash map.
185 * @param len initial size (map will grow as needed)
186 * @param do_not_copy_keys GNUNET_NO is always safe and should be used by default;
187 * GNUNET_YES means that on 'put', the 'key' does not have
188 * to be copied as the destination of the pointer is
189 * guaranteed to be life as long as the value is stored in
190 * the hashmap. This can significantly reduce memory
191 * consumption, but of course is also a recipie for
192 * heap corruption if the assumption is not true. Only
193 * use this if (1) memory use is important in this case and
194 * (2) you have triple-checked that the invariant holds
195 * @return NULL on error
197 struct GNUNET_CONTAINER_MultiShortmap *
198 GNUNET_CONTAINER_multishortmap_create (unsigned int len,
199 int do_not_copy_keys)
201 struct GNUNET_CONTAINER_MultiShortmap *map;
203 GNUNET_assert (len > 0);
204 map = GNUNET_new (struct GNUNET_CONTAINER_MultiShortmap);
205 map->map = GNUNET_new_array (len,
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_multishortmap_destroy (struct GNUNET_CONTAINER_MultiShortmap *map)
222 GNUNET_assert (0 == map->next_cache_off);
223 for (unsigned int i = 0; i < map->map_length; i++)
228 if (map->use_small_entries)
230 struct SmallMapEntry *sme;
231 struct SmallMapEntry *nxt;
234 while (NULL != (sme = nxt))
243 struct BigMapEntry *bme;
244 struct BigMapEntry *nxt;
247 while (NULL != (bme = nxt))
255 GNUNET_free (map->map);
261 * Compute the index of the bucket for the given key.
263 * @param map hash map for which to compute the index
264 * @param key what key should the index be computed for
265 * @return offset into the "map" array of "map"
268 idx_of (const struct GNUNET_CONTAINER_MultiShortmap *map,
269 const struct GNUNET_ShortHashCode *key)
273 GNUNET_assert (NULL != map);
274 GNUNET_memcpy (&kx, key, sizeof (kx));
275 return kx % map->map_length;
280 * Get the number of key-value pairs in the map.
283 * @return the number of key value pairs
286 GNUNET_CONTAINER_multishortmap_size (const struct GNUNET_CONTAINER_MultiShortmap *map)
293 * Given a key find a value in the map matching the key.
296 * @param key what to look for
297 * @return NULL if no value was found; note that
298 * this is indistinguishable from values that just
299 * happen to be NULL; use "contains" to test for
300 * key-value pairs with value NULL
303 GNUNET_CONTAINER_multishortmap_get (const struct GNUNET_CONTAINER_MultiShortmap *map,
304 const struct GNUNET_ShortHashCode *key)
308 me = map->map[idx_of (map, key)];
309 if (map->use_small_entries)
311 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
312 if (0 == memcmp (key,
314 sizeof (struct GNUNET_ShortHashCode)))
319 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
320 if (0 == memcmp (key,
322 sizeof (struct GNUNET_ShortHashCode)))
330 * Iterate over all entries in the map.
333 * @param it function to call on each entry
334 * @param it_cls extra argument to @a it
335 * @return the number of key value pairs processed,
336 * #GNUNET_SYSERR if it aborted iteration
339 GNUNET_CONTAINER_multishortmap_iterate (struct GNUNET_CONTAINER_MultiShortmap *map,
340 GNUNET_CONTAINER_ShortmapIterator it,
346 struct GNUNET_ShortHashCode 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))
364 (GNUNET_OK != it (it_cls,
368 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
369 return GNUNET_SYSERR;
376 struct BigMapEntry *bme;
379 while (NULL != (bme = ce->bme))
385 if (GNUNET_OK != it (it_cls,
389 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
390 return GNUNET_SYSERR;
397 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
403 * We are about to free() the @a bme, make sure it is not in
404 * the list of next values for any iterator in the @a map's next_cache.
406 * @param map the map to check
407 * @param bme the entry that is about to be free'd
410 update_next_cache_bme (struct GNUNET_CONTAINER_MultiShortmap *map,
411 const struct BigMapEntry *bme)
413 for (unsigned int i=0;i<map->next_cache_off;i++)
414 if (map->next_cache[i].bme == bme)
415 map->next_cache[i].bme = bme->next;
420 * We are about to free() the @a sme, make sure it is not in
421 * the list of next values for any iterator in the @a map's next_cache.
423 * @param map the map to check
424 * @param sme the entry that is about to be free'd
427 update_next_cache_sme (struct GNUNET_CONTAINER_MultiShortmap *map,
428 const struct SmallMapEntry *sme)
430 for (unsigned int i=0;i<map->next_cache_off;i++)
431 if (map->next_cache[i].sme == sme)
432 map->next_cache[i].sme = sme->next;
437 * Remove the given key-value pair from the map. Note that if the
438 * key-value pair is in the map multiple times, only one of the pairs
442 * @param key key of the key-value pair
443 * @param value value of the key-value pair
444 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
448 GNUNET_CONTAINER_multishortmap_remove (struct GNUNET_CONTAINER_MultiShortmap *map,
449 const struct GNUNET_ShortHashCode *key,
455 map->modification_counter++;
456 i = idx_of (map, key);
458 if (map->use_small_entries)
460 struct SmallMapEntry *p = NULL;
462 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
464 if ((0 == memcmp (key,
466 sizeof (struct GNUNET_ShortHashCode))) &&
467 (value == sme->value))
470 map->map[i].sme = sme->next;
473 update_next_cache_sme (map,
484 struct BigMapEntry *p = NULL;
486 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
488 if ((0 == memcmp (key,
490 sizeof (struct GNUNET_ShortHashCode))) &&
491 (value == bme->value))
494 map->map[i].bme = bme->next;
497 update_next_cache_bme (map,
511 * Remove all entries for the given key from the map.
512 * Note that the values would not be "freed".
515 * @param key identifies values to be removed
516 * @return number of values removed
519 GNUNET_CONTAINER_multishortmap_remove_all (struct GNUNET_CONTAINER_MultiShortmap *map,
520 const struct GNUNET_ShortHashCode *key)
526 map->modification_counter++;
529 i = idx_of (map, key);
531 if (map->use_small_entries)
533 struct SmallMapEntry *sme;
534 struct SmallMapEntry *p;
540 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_ShortHashCode)))
543 map->map[i].sme = sme->next;
546 update_next_cache_sme (map,
551 sme = map->map[i].sme;
565 struct BigMapEntry *bme;
566 struct BigMapEntry *p;
572 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_ShortHashCode)))
575 map->map[i].bme = bme->next;
578 update_next_cache_bme (map,
583 bme = map->map[i].bme;
600 * Check if the map contains any value under the given
601 * key (including values that are NULL).
604 * @param key the key to test if a value exists for it
605 * @return #GNUNET_YES if such a value exists,
609 GNUNET_CONTAINER_multishortmap_contains (const struct GNUNET_CONTAINER_MultiShortmap *map,
610 const struct GNUNET_ShortHashCode *key)
614 me = map->map[idx_of (map, key)];
615 if (map->use_small_entries)
617 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
618 if (0 == memcmp (key,
620 sizeof (struct GNUNET_ShortHashCode)))
625 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
626 if (0 == memcmp (key,
628 sizeof (struct GNUNET_ShortHashCode)))
636 * Check if the map contains the given value under the given
640 * @param key the key to test if a value exists for it
641 * @param value value to test for
642 * @return #GNUNET_YES if such a value exists,
646 GNUNET_CONTAINER_multishortmap_contains_value (const struct GNUNET_CONTAINER_MultiShortmap *map,
647 const struct GNUNET_ShortHashCode *key,
652 me = map->map[idx_of (map, key)];
653 if (map->use_small_entries)
655 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
656 if ( (0 == memcmp (key,
658 sizeof (struct GNUNET_ShortHashCode))) &&
659 (sme->value == value) )
664 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
665 if ( (0 == memcmp (key,
667 sizeof (struct GNUNET_ShortHashCode))) &&
668 (bme->value == value) )
676 * Grow the given map to a more appropriate size.
678 * @param map the hash map to grow
681 grow (struct GNUNET_CONTAINER_MultiShortmap *map)
683 union MapEntry *old_map;
684 union MapEntry *new_map;
685 unsigned int old_len;
686 unsigned int new_len;
689 map->modification_counter++;
692 old_len = map->map_length;
693 new_len = old_len * 2;
694 new_map = GNUNET_new_array (new_len,
696 map->map_length = new_len;
698 for (unsigned int i = 0; i < old_len; i++)
700 if (map->use_small_entries)
702 struct SmallMapEntry *sme;
704 while (NULL != (sme = old_map[i].sme))
706 old_map[i].sme = sme->next;
707 idx = idx_of (map, sme->key);
708 sme->next = new_map[idx].sme;
709 new_map[idx].sme = sme;
714 struct BigMapEntry *bme;
716 while (NULL != (bme = old_map[i].bme))
718 old_map[i].bme = bme->next;
719 idx = idx_of (map, &bme->key);
720 bme->next = new_map[idx].bme;
721 new_map[idx].bme = bme;
725 GNUNET_free (old_map);
730 * Store a key-value pair in the map.
733 * @param key key to use
734 * @param value value to use
735 * @param opt options for put
736 * @return #GNUNET_OK on success,
737 * #GNUNET_NO if a value was replaced (with REPLACE)
738 * #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
739 * value already exists
742 GNUNET_CONTAINER_multishortmap_put (struct GNUNET_CONTAINER_MultiShortmap *map,
743 const struct GNUNET_ShortHashCode *key,
745 enum GNUNET_CONTAINER_MultiHashMapOption opt)
750 i = idx_of (map, key);
751 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
752 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
755 if (map->use_small_entries)
757 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
758 if (0 == memcmp (key,
760 sizeof (struct GNUNET_ShortHashCode)))
762 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
763 return GNUNET_SYSERR;
770 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
771 if (0 == memcmp (key,
773 sizeof (struct GNUNET_ShortHashCode)))
775 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
776 return GNUNET_SYSERR;
782 if (map->size / 3 >= map->map_length / 4)
785 i = idx_of (map, key);
787 if (map->use_small_entries)
789 struct SmallMapEntry *sme;
791 sme = GNUNET_new (struct SmallMapEntry);
794 sme->next = map->map[i].sme;
795 map->map[i].sme = sme;
799 struct BigMapEntry *bme;
801 bme = GNUNET_new (struct BigMapEntry);
804 bme->next = map->map[i].bme;
805 map->map[i].bme = bme;
813 * Iterate over all entries in the map that match a particular key.
816 * @param key key that the entries must correspond to
817 * @param it function to call on each entry
818 * @param it_cls extra argument to @a it
819 * @return the number of key value pairs processed,
820 * #GNUNET_SYSERR if it aborted iteration
823 GNUNET_CONTAINER_multishortmap_get_multiple (struct GNUNET_CONTAINER_MultiShortmap *map,
824 const struct GNUNET_ShortHashCode *key,
825 GNUNET_CONTAINER_ShortmapIterator it,
832 ce = &map->next_cache[map->next_cache_off];
833 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
835 me = map->map[idx_of (map, key)];
836 if (map->use_small_entries)
838 struct SmallMapEntry *sme;
841 while (NULL != (sme = ce->sme))
844 if (0 != memcmp (key,
846 sizeof (struct GNUNET_ShortHashCode)))
849 (GNUNET_OK != it (it_cls,
853 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
854 return GNUNET_SYSERR;
861 struct BigMapEntry *bme;
864 while (NULL != (bme = ce->bme))
867 if (0 != memcmp (key,
869 sizeof (struct GNUNET_ShortHashCode)))
872 (GNUNET_OK != it (it_cls,
876 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
877 return GNUNET_SYSERR;
882 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
889 * Call @a it on a random value from the map, or not at all
890 * if the map is empty. Note that this function has linear
891 * complexity (in the size of the map).
894 * @param it function to call on a random entry
895 * @param it_cls extra argument to @a it
896 * @return the number of key value pairs processed, zero or one.
899 GNUNET_CONTAINER_multishortmap_get_random (const struct GNUNET_CONTAINER_MultiShortmap *map,
900 GNUNET_CONTAINER_ShortmapIterator it,
910 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
912 for (unsigned int idx = 0; idx < map->map_length; idx++)
915 if (map->use_small_entries)
917 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
921 if (GNUNET_OK != it (it_cls,
924 return GNUNET_SYSERR;
932 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
936 if (GNUNET_OK != it (it_cls,
937 &bme->key, bme->value))
938 return GNUNET_SYSERR;
946 return GNUNET_SYSERR;
951 * Create an iterator for a multishortmap.
952 * The iterator can be used to retrieve all the elements in the multishortmap
953 * one by one, without having to handle all elements at once (in contrast to
954 * #GNUNET_CONTAINER_multishortmap_iterate). Note that the iterator can not be
955 * used anymore if elements have been removed from 'map' after the creation of
956 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
957 * result in skipped or repeated elements.
959 * @param map the map to create an iterator for
960 * @return an iterator over the given multishortmap 'map'
962 struct GNUNET_CONTAINER_MultiShortmapIterator *
963 GNUNET_CONTAINER_multishortmap_iterator_create (const struct GNUNET_CONTAINER_MultiShortmap *map)
965 struct GNUNET_CONTAINER_MultiShortmapIterator *iter;
967 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiShortmapIterator);
969 iter->modification_counter = map->modification_counter;
970 iter->me = map->map[0];
976 * Retrieve the next element from the hash map at the iterator's position.
977 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
979 * This operation is only allowed if no elements have been removed from the
980 * multishortmap since the creation of 'iter', and the map has not been destroyed.
981 * Adding elements may result in repeating or skipping elements.
983 * @param iter the iterator to get the next element from
984 * @param key pointer to store the key in, can be NULL
985 * @param value pointer to store the value in, can be NULL
986 * @return #GNUNET_YES we returned an element,
987 * #GNUNET_NO if we are out of elements
990 GNUNET_CONTAINER_multishortmap_iterator_next (struct GNUNET_CONTAINER_MultiShortmapIterator *iter,
991 struct GNUNET_ShortHashCode *key,
994 /* make sure the map has not been modified */
995 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
997 /* look for the next entry, skipping empty buckets */
1000 if (iter->idx >= iter->map->map_length)
1002 if (GNUNET_YES == iter->map->use_small_entries)
1004 if (NULL != iter->me.sme)
1007 *key = *iter->me.sme->key;
1009 *value = iter->me.sme->value;
1010 iter->me.sme = iter->me.sme->next;
1016 if (NULL != iter->me.bme)
1019 *key = iter->me.bme->key;
1021 *value = iter->me.bme->value;
1022 iter->me.bme = iter->me.bme->next;
1027 if (iter->idx < iter->map->map_length)
1028 iter->me = iter->map->map[iter->idx];
1034 * Destroy a multishortmap iterator.
1036 * @param iter the iterator to destroy
1039 GNUNET_CONTAINER_multishortmap_iterator_destroy (struct GNUNET_CONTAINER_MultiShortmapIterator *iter)
1045 /* end of container_multishortmap.c */