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, ...) \
30 GNUNET_log_from (kind, "util-container-multishortmap", __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_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;
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_MultiShortmap
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 multishortmap.
153 * Allows to enumerate elements asynchronously.
155 struct GNUNET_CONTAINER_MultiShortmapIterator
158 * Position in the bucket '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_MultiShortmap *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_MultiShortmap *
196 GNUNET_CONTAINER_multishortmap_create (unsigned int len, int do_not_copy_keys)
198 struct GNUNET_CONTAINER_MultiShortmap *map;
200 GNUNET_assert (len > 0);
201 map = GNUNET_new (struct GNUNET_CONTAINER_MultiShortmap);
202 map->map = GNUNET_malloc_large (len * sizeof (union MapEntry));
203 if (NULL == map->map)
208 map->map_length = len;
209 map->use_small_entries = do_not_copy_keys;
215 * Destroy a hash map. Will not free any values
216 * stored in the hash map!
221 GNUNET_CONTAINER_multishortmap_destroy (
222 struct GNUNET_CONTAINER_MultiShortmap *map)
224 GNUNET_assert (0 == map->next_cache_off);
225 for (unsigned int i = 0; i < map->map_length; i++)
230 if (map->use_small_entries)
232 struct SmallMapEntry *sme;
233 struct SmallMapEntry *nxt;
236 while (NULL != (sme = nxt))
245 struct BigMapEntry *bme;
246 struct BigMapEntry *nxt;
249 while (NULL != (bme = nxt))
257 GNUNET_free (map->map);
263 * Compute the index of the bucket for the given key.
265 * @param map hash map for which to compute the index
266 * @param key what key should the index be computed for
267 * @return offset into the "map" array of "map"
270 idx_of (const struct GNUNET_CONTAINER_MultiShortmap *map,
271 const struct GNUNET_ShortHashCode *key)
275 GNUNET_assert (NULL != map);
276 GNUNET_memcpy (&kx, key, sizeof (kx));
277 return kx % map->map_length;
282 * Get the number of key-value pairs in the map.
285 * @return the number of key value pairs
288 GNUNET_CONTAINER_multishortmap_size (
289 const struct GNUNET_CONTAINER_MultiShortmap *map)
296 * Given a key find a value in the map matching the key.
299 * @param key what to look for
300 * @return NULL if no value was found; note that
301 * this is indistinguishable from values that just
302 * happen to be NULL; use "contains" to test for
303 * key-value pairs with value NULL
306 GNUNET_CONTAINER_multishortmap_get (
307 const struct GNUNET_CONTAINER_MultiShortmap *map,
308 const struct GNUNET_ShortHashCode *key)
312 me = map->map[idx_of (map, key)];
313 if (map->use_small_entries)
315 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
316 if (0 == GNUNET_memcmp (key, sme->key))
321 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
322 if (0 == GNUNET_memcmp (key, &bme->key))
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 (
340 struct GNUNET_CONTAINER_MultiShortmap *map,
341 GNUNET_CONTAINER_ShortmapIterator it,
347 struct GNUNET_ShortHashCode kc;
350 GNUNET_assert (NULL != map);
351 ce = &map->next_cache[map->next_cache_off];
352 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
353 for (unsigned int i = 0; i < map->map_length; i++)
356 if (map->use_small_entries)
358 struct SmallMapEntry *sme;
361 while (NULL != (sme = ce->sme))
364 if ((NULL != it) && (GNUNET_OK != it (it_cls, sme->key, sme->value)))
366 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
367 return GNUNET_SYSERR;
374 struct BigMapEntry *bme;
377 while (NULL != (bme = ce->bme))
383 if (GNUNET_OK != it (it_cls, &kc, bme->value))
385 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
386 return GNUNET_SYSERR;
393 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
399 * We are about to free() the @a bme, make sure it is not in
400 * the list of next values for any iterator in the @a map's next_cache.
402 * @param map the map to check
403 * @param bme the entry that is about to be free'd
406 update_next_cache_bme (struct GNUNET_CONTAINER_MultiShortmap *map,
407 const struct BigMapEntry *bme)
409 for (unsigned int i = 0; i < map->next_cache_off; i++)
410 if (map->next_cache[i].bme == bme)
411 map->next_cache[i].bme = bme->next;
416 * We are about to free() the @a sme, make sure it is not in
417 * the list of next values for any iterator in the @a map's next_cache.
419 * @param map the map to check
420 * @param sme the entry that is about to be free'd
423 update_next_cache_sme (struct GNUNET_CONTAINER_MultiShortmap *map,
424 const struct SmallMapEntry *sme)
426 for (unsigned int i = 0; i < map->next_cache_off; i++)
427 if (map->next_cache[i].sme == sme)
428 map->next_cache[i].sme = sme->next;
433 * Remove the given key-value pair from the map. Note that if the
434 * key-value pair is in the map multiple times, only one of the pairs
438 * @param key key of the key-value pair
439 * @param value value of the key-value pair
440 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
444 GNUNET_CONTAINER_multishortmap_remove (
445 struct GNUNET_CONTAINER_MultiShortmap *map,
446 const struct GNUNET_ShortHashCode *key,
452 map->modification_counter++;
453 i = idx_of (map, key);
455 if (map->use_small_entries)
457 struct SmallMapEntry *p = NULL;
459 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
461 if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value))
464 map->map[i].sme = sme->next;
467 update_next_cache_sme (map, sme);
477 struct BigMapEntry *p = NULL;
479 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
481 if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value))
484 map->map[i].bme = bme->next;
487 update_next_cache_bme (map, bme);
500 * Remove all entries for the given key from the map.
501 * Note that the values would not be "freed".
504 * @param key identifies values to be removed
505 * @return number of values removed
508 GNUNET_CONTAINER_multishortmap_remove_all (
509 struct GNUNET_CONTAINER_MultiShortmap *map,
510 const struct GNUNET_ShortHashCode *key)
516 map->modification_counter++;
519 i = idx_of (map, key);
521 if (map->use_small_entries)
523 struct SmallMapEntry *sme;
524 struct SmallMapEntry *p;
530 if (0 == GNUNET_memcmp (key, sme->key))
533 map->map[i].sme = sme->next;
536 update_next_cache_sme (map, sme);
540 sme = map->map[i].sme;
554 struct BigMapEntry *bme;
555 struct BigMapEntry *p;
561 if (0 == GNUNET_memcmp (key, &bme->key))
564 map->map[i].bme = bme->next;
567 update_next_cache_bme (map, bme);
571 bme = map->map[i].bme;
588 * Check if the map contains any value under the given
589 * key (including values that are NULL).
592 * @param key the key to test if a value exists for it
593 * @return #GNUNET_YES if such a value exists,
597 GNUNET_CONTAINER_multishortmap_contains (
598 const struct GNUNET_CONTAINER_MultiShortmap *map,
599 const struct GNUNET_ShortHashCode *key)
603 me = map->map[idx_of (map, key)];
604 if (map->use_small_entries)
606 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
607 if (0 == GNUNET_memcmp (key, sme->key))
612 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
613 if (0 == GNUNET_memcmp (key, &bme->key))
621 * Check if the map contains the given value under the given
625 * @param key the key to test if a value exists for it
626 * @param value value to test for
627 * @return #GNUNET_YES if such a value exists,
631 GNUNET_CONTAINER_multishortmap_contains_value (
632 const struct GNUNET_CONTAINER_MultiShortmap *map,
633 const struct GNUNET_ShortHashCode *key,
638 me = map->map[idx_of (map, key)];
639 if (map->use_small_entries)
641 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
642 if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value))
647 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
648 if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value))
656 * Grow the given map to a more appropriate size.
658 * @param map the hash map to grow
661 grow (struct GNUNET_CONTAINER_MultiShortmap *map)
663 union MapEntry *old_map;
664 union MapEntry *new_map;
665 unsigned int old_len;
666 unsigned int new_len;
670 old_len = map->map_length;
671 new_len = old_len * 2;
672 if (0 == new_len) /* 2^31 * 2 == 0 */
673 new_len = old_len; /* never use 0 */
674 if (new_len == old_len)
675 return; /* nothing changed */
676 new_map = GNUNET_malloc_large (new_len * sizeof (union MapEntry));
678 return; /* grow not possible */
679 map->modification_counter++;
680 map->map_length = new_len;
682 for (unsigned int i = 0; i < old_len; i++)
684 if (map->use_small_entries)
686 struct SmallMapEntry *sme;
688 while (NULL != (sme = old_map[i].sme))
690 old_map[i].sme = sme->next;
691 idx = idx_of (map, sme->key);
692 sme->next = new_map[idx].sme;
693 new_map[idx].sme = sme;
698 struct BigMapEntry *bme;
700 while (NULL != (bme = old_map[i].bme))
702 old_map[i].bme = bme->next;
703 idx = idx_of (map, &bme->key);
704 bme->next = new_map[idx].bme;
705 new_map[idx].bme = bme;
709 GNUNET_free (old_map);
714 * Store a key-value pair in the map.
717 * @param key key to use
718 * @param value value to use
719 * @param opt options for put
720 * @return #GNUNET_OK on success,
721 * #GNUNET_NO if a value was replaced (with REPLACE)
722 * #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
723 * value already exists
726 GNUNET_CONTAINER_multishortmap_put (
727 struct GNUNET_CONTAINER_MultiShortmap *map,
728 const struct GNUNET_ShortHashCode *key,
730 enum GNUNET_CONTAINER_MultiHashMapOption opt)
735 i = idx_of (map, key);
736 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
737 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
740 if (map->use_small_entries)
742 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
743 if (0 == GNUNET_memcmp (key, sme->key))
745 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
746 return GNUNET_SYSERR;
753 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
754 if (0 == GNUNET_memcmp (key, &bme->key))
756 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
757 return GNUNET_SYSERR;
763 if (map->size / 3 >= map->map_length / 4)
766 i = idx_of (map, key);
768 if (map->use_small_entries)
770 struct SmallMapEntry *sme;
772 sme = GNUNET_new (struct SmallMapEntry);
775 sme->next = map->map[i].sme;
776 map->map[i].sme = sme;
780 struct BigMapEntry *bme;
782 bme = GNUNET_new (struct BigMapEntry);
785 bme->next = map->map[i].bme;
786 map->map[i].bme = bme;
794 * Iterate over all entries in the map that match a particular key.
797 * @param key key that the entries must correspond to
798 * @param it function to call on each entry
799 * @param it_cls extra argument to @a it
800 * @return the number of key value pairs processed,
801 * #GNUNET_SYSERR if it aborted iteration
804 GNUNET_CONTAINER_multishortmap_get_multiple (
805 struct GNUNET_CONTAINER_MultiShortmap *map,
806 const struct GNUNET_ShortHashCode *key,
807 GNUNET_CONTAINER_ShortmapIterator it,
814 ce = &map->next_cache[map->next_cache_off];
815 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
817 me = map->map[idx_of (map, key)];
818 if (map->use_small_entries)
820 struct SmallMapEntry *sme;
823 while (NULL != (sme = ce->sme))
826 if (0 != GNUNET_memcmp (key, sme->key))
828 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value)))
830 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
831 return GNUNET_SYSERR;
838 struct BigMapEntry *bme;
841 while (NULL != (bme = ce->bme))
844 if (0 != GNUNET_memcmp (key, &bme->key))
846 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value)))
848 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
849 return GNUNET_SYSERR;
854 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
861 * Call @a it on a random value from the map, or not at all
862 * if the map is empty. Note that this function has linear
863 * complexity (in the size of the map).
866 * @param it function to call on a random entry
867 * @param it_cls extra argument to @a it
868 * @return the number of key value pairs processed, zero or one.
871 GNUNET_CONTAINER_multishortmap_get_random (
872 const struct GNUNET_CONTAINER_MultiShortmap *map,
873 GNUNET_CONTAINER_ShortmapIterator it,
883 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size);
884 for (unsigned int idx = 0; idx < map->map_length; idx++)
887 if (map->use_small_entries)
889 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
893 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
894 return GNUNET_SYSERR;
902 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
906 if (GNUNET_OK != it (it_cls, &bme->key, bme->value))
907 return GNUNET_SYSERR;
915 return GNUNET_SYSERR;
920 * Create an iterator for a multishortmap.
921 * The iterator can be used to retrieve all the elements in the multishortmap
922 * one by one, without having to handle all elements at once (in contrast to
923 * #GNUNET_CONTAINER_multishortmap_iterate). Note that the iterator can not be
924 * used anymore if elements have been removed from 'map' after the creation of
925 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
926 * result in skipped or repeated elements.
928 * @param map the map to create an iterator for
929 * @return an iterator over the given multishortmap 'map'
931 struct GNUNET_CONTAINER_MultiShortmapIterator *
932 GNUNET_CONTAINER_multishortmap_iterator_create (
933 const struct GNUNET_CONTAINER_MultiShortmap *map)
935 struct GNUNET_CONTAINER_MultiShortmapIterator *iter;
937 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiShortmapIterator);
939 iter->modification_counter = map->modification_counter;
940 iter->me = map->map[0];
946 * Retrieve the next element from the hash map at the iterator's position.
947 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
949 * This operation is only allowed if no elements have been removed from the
950 * multishortmap since the creation of 'iter', and the map has not been destroyed.
951 * Adding elements may result in repeating or skipping elements.
953 * @param iter the iterator to get the next element from
954 * @param key pointer to store the key in, can be NULL
955 * @param value pointer to store the value in, can be NULL
956 * @return #GNUNET_YES we returned an element,
957 * #GNUNET_NO if we are out of elements
960 GNUNET_CONTAINER_multishortmap_iterator_next (
961 struct GNUNET_CONTAINER_MultiShortmapIterator *iter,
962 struct GNUNET_ShortHashCode *key,
965 /* make sure the map has not been modified */
966 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
968 /* look for the next entry, skipping empty buckets */
971 if (iter->idx >= iter->map->map_length)
973 if (GNUNET_YES == iter->map->use_small_entries)
975 if (NULL != iter->me.sme)
978 *key = *iter->me.sme->key;
980 *value = iter->me.sme->value;
981 iter->me.sme = iter->me.sme->next;
987 if (NULL != iter->me.bme)
990 *key = iter->me.bme->key;
992 *value = iter->me.bme->value;
993 iter->me.bme = iter->me.bme->next;
998 if (iter->idx < iter->map->map_length)
999 iter->me = iter->map->map[iter->idx];
1005 * Destroy a multishortmap iterator.
1007 * @param iter the iterator to destroy
1010 GNUNET_CONTAINER_multishortmap_iterator_destroy (
1011 struct GNUNET_CONTAINER_MultiShortmapIterator *iter)
1017 /* end of container_multishortmap.c */