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_malloc_large (len *
206 sizeof (union MapEntry));
207 if (NULL == map->map)
212 map->map_length = len;
213 map->use_small_entries = do_not_copy_keys;
219 * Destroy a hash map. Will not free any values
220 * stored in the hash map!
225 GNUNET_CONTAINER_multishortmap_destroy (struct GNUNET_CONTAINER_MultiShortmap *map)
227 GNUNET_assert (0 == map->next_cache_off);
228 for (unsigned int i = 0; i < map->map_length; i++)
233 if (map->use_small_entries)
235 struct SmallMapEntry *sme;
236 struct SmallMapEntry *nxt;
239 while (NULL != (sme = nxt))
248 struct BigMapEntry *bme;
249 struct BigMapEntry *nxt;
252 while (NULL != (bme = nxt))
260 GNUNET_free (map->map);
266 * Compute the index of the bucket for the given key.
268 * @param map hash map for which to compute the index
269 * @param key what key should the index be computed for
270 * @return offset into the "map" array of "map"
273 idx_of (const struct GNUNET_CONTAINER_MultiShortmap *map,
274 const struct GNUNET_ShortHashCode *key)
278 GNUNET_assert (NULL != map);
279 GNUNET_memcpy (&kx, key, sizeof (kx));
280 return kx % map->map_length;
285 * Get the number of key-value pairs in the map.
288 * @return the number of key value pairs
291 GNUNET_CONTAINER_multishortmap_size (const struct GNUNET_CONTAINER_MultiShortmap *map)
298 * Given a key find a value in the map matching the key.
301 * @param key what to look for
302 * @return NULL if no value was found; note that
303 * this is indistinguishable from values that just
304 * happen to be NULL; use "contains" to test for
305 * key-value pairs with value NULL
308 GNUNET_CONTAINER_multishortmap_get (const struct GNUNET_CONTAINER_MultiShortmap *map,
309 const struct GNUNET_ShortHashCode *key)
313 me = map->map[idx_of (map, key)];
314 if (map->use_small_entries)
316 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
317 if (0 == memcmp (key,
319 sizeof (struct GNUNET_ShortHashCode)))
324 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
325 if (0 == memcmp (key,
327 sizeof (struct GNUNET_ShortHashCode)))
335 * Iterate over all entries in the map.
338 * @param it function to call on each entry
339 * @param it_cls extra argument to @a it
340 * @return the number of key value pairs processed,
341 * #GNUNET_SYSERR if it aborted iteration
344 GNUNET_CONTAINER_multishortmap_iterate (struct GNUNET_CONTAINER_MultiShortmap *map,
345 GNUNET_CONTAINER_ShortmapIterator it,
351 struct GNUNET_ShortHashCode kc;
354 GNUNET_assert (NULL != map);
355 ce = &map->next_cache[map->next_cache_off];
356 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
357 for (unsigned int i = 0; i < map->map_length; i++)
360 if (map->use_small_entries)
362 struct SmallMapEntry *sme;
365 while (NULL != (sme = ce->sme))
369 (GNUNET_OK != it (it_cls,
373 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
374 return GNUNET_SYSERR;
381 struct BigMapEntry *bme;
384 while (NULL != (bme = ce->bme))
390 if (GNUNET_OK != it (it_cls,
394 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
395 return GNUNET_SYSERR;
402 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
408 * We are about to free() the @a bme, make sure it is not in
409 * the list of next values for any iterator in the @a map's next_cache.
411 * @param map the map to check
412 * @param bme the entry that is about to be free'd
415 update_next_cache_bme (struct GNUNET_CONTAINER_MultiShortmap *map,
416 const struct BigMapEntry *bme)
418 for (unsigned int i=0;i<map->next_cache_off;i++)
419 if (map->next_cache[i].bme == bme)
420 map->next_cache[i].bme = bme->next;
425 * We are about to free() the @a sme, make sure it is not in
426 * the list of next values for any iterator in the @a map's next_cache.
428 * @param map the map to check
429 * @param sme the entry that is about to be free'd
432 update_next_cache_sme (struct GNUNET_CONTAINER_MultiShortmap *map,
433 const struct SmallMapEntry *sme)
435 for (unsigned int i=0;i<map->next_cache_off;i++)
436 if (map->next_cache[i].sme == sme)
437 map->next_cache[i].sme = sme->next;
442 * Remove the given key-value pair from the map. Note that if the
443 * key-value pair is in the map multiple times, only one of the pairs
447 * @param key key of the key-value pair
448 * @param value value of the key-value pair
449 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
453 GNUNET_CONTAINER_multishortmap_remove (struct GNUNET_CONTAINER_MultiShortmap *map,
454 const struct GNUNET_ShortHashCode *key,
460 map->modification_counter++;
461 i = idx_of (map, key);
463 if (map->use_small_entries)
465 struct SmallMapEntry *p = NULL;
467 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
469 if ((0 == memcmp (key,
471 sizeof (struct GNUNET_ShortHashCode))) &&
472 (value == sme->value))
475 map->map[i].sme = sme->next;
478 update_next_cache_sme (map,
489 struct BigMapEntry *p = NULL;
491 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
493 if ((0 == memcmp (key,
495 sizeof (struct GNUNET_ShortHashCode))) &&
496 (value == bme->value))
499 map->map[i].bme = bme->next;
502 update_next_cache_bme (map,
516 * Remove all entries for the given key from the map.
517 * Note that the values would not be "freed".
520 * @param key identifies values to be removed
521 * @return number of values removed
524 GNUNET_CONTAINER_multishortmap_remove_all (struct GNUNET_CONTAINER_MultiShortmap *map,
525 const struct GNUNET_ShortHashCode *key)
531 map->modification_counter++;
534 i = idx_of (map, key);
536 if (map->use_small_entries)
538 struct SmallMapEntry *sme;
539 struct SmallMapEntry *p;
545 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_ShortHashCode)))
548 map->map[i].sme = sme->next;
551 update_next_cache_sme (map,
556 sme = map->map[i].sme;
570 struct BigMapEntry *bme;
571 struct BigMapEntry *p;
577 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_ShortHashCode)))
580 map->map[i].bme = bme->next;
583 update_next_cache_bme (map,
588 bme = map->map[i].bme;
605 * Check if the map contains any value under the given
606 * key (including values that are NULL).
609 * @param key the key to test if a value exists for it
610 * @return #GNUNET_YES if such a value exists,
614 GNUNET_CONTAINER_multishortmap_contains (const struct GNUNET_CONTAINER_MultiShortmap *map,
615 const struct GNUNET_ShortHashCode *key)
619 me = map->map[idx_of (map, key)];
620 if (map->use_small_entries)
622 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
623 if (0 == memcmp (key,
625 sizeof (struct GNUNET_ShortHashCode)))
630 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
631 if (0 == memcmp (key,
633 sizeof (struct GNUNET_ShortHashCode)))
641 * Check if the map contains the given value under the given
645 * @param key the key to test if a value exists for it
646 * @param value value to test for
647 * @return #GNUNET_YES if such a value exists,
651 GNUNET_CONTAINER_multishortmap_contains_value (const struct GNUNET_CONTAINER_MultiShortmap *map,
652 const struct GNUNET_ShortHashCode *key,
657 me = map->map[idx_of (map, key)];
658 if (map->use_small_entries)
660 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
661 if ( (0 == memcmp (key,
663 sizeof (struct GNUNET_ShortHashCode))) &&
664 (sme->value == value) )
669 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
670 if ( (0 == memcmp (key,
672 sizeof (struct GNUNET_ShortHashCode))) &&
673 (bme->value == value) )
681 * Grow the given map to a more appropriate size.
683 * @param map the hash map to grow
686 grow (struct GNUNET_CONTAINER_MultiShortmap *map)
688 union MapEntry *old_map;
689 union MapEntry *new_map;
690 unsigned int old_len;
691 unsigned int new_len;
695 old_len = map->map_length;
696 new_len = old_len * 2;
697 if (0 == new_len) /* 2^31 * 2 == 0 */
698 new_len = old_len; /* never use 0 */
699 if (new_len == old_len)
700 return; /* nothing changed */
701 new_map = GNUNET_malloc_large (new_len *
702 sizeof (union MapEntry));
704 return; /* grow not possible */
705 map->modification_counter++;
706 map->map_length = new_len;
708 for (unsigned int i = 0; i < old_len; i++)
710 if (map->use_small_entries)
712 struct SmallMapEntry *sme;
714 while (NULL != (sme = old_map[i].sme))
716 old_map[i].sme = sme->next;
717 idx = idx_of (map, sme->key);
718 sme->next = new_map[idx].sme;
719 new_map[idx].sme = sme;
724 struct BigMapEntry *bme;
726 while (NULL != (bme = old_map[i].bme))
728 old_map[i].bme = bme->next;
729 idx = idx_of (map, &bme->key);
730 bme->next = new_map[idx].bme;
731 new_map[idx].bme = bme;
735 GNUNET_free (old_map);
740 * Store a key-value pair in the map.
743 * @param key key to use
744 * @param value value to use
745 * @param opt options for put
746 * @return #GNUNET_OK on success,
747 * #GNUNET_NO if a value was replaced (with REPLACE)
748 * #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
749 * value already exists
752 GNUNET_CONTAINER_multishortmap_put (struct GNUNET_CONTAINER_MultiShortmap *map,
753 const struct GNUNET_ShortHashCode *key,
755 enum GNUNET_CONTAINER_MultiHashMapOption opt)
760 i = idx_of (map, key);
761 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
762 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
765 if (map->use_small_entries)
767 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
768 if (0 == memcmp (key,
770 sizeof (struct GNUNET_ShortHashCode)))
772 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
773 return GNUNET_SYSERR;
780 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
781 if (0 == memcmp (key,
783 sizeof (struct GNUNET_ShortHashCode)))
785 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
786 return GNUNET_SYSERR;
792 if (map->size / 3 >= map->map_length / 4)
795 i = idx_of (map, key);
797 if (map->use_small_entries)
799 struct SmallMapEntry *sme;
801 sme = GNUNET_new (struct SmallMapEntry);
804 sme->next = map->map[i].sme;
805 map->map[i].sme = sme;
809 struct BigMapEntry *bme;
811 bme = GNUNET_new (struct BigMapEntry);
814 bme->next = map->map[i].bme;
815 map->map[i].bme = bme;
823 * Iterate over all entries in the map that match a particular key.
826 * @param key key that the entries must correspond to
827 * @param it function to call on each entry
828 * @param it_cls extra argument to @a it
829 * @return the number of key value pairs processed,
830 * #GNUNET_SYSERR if it aborted iteration
833 GNUNET_CONTAINER_multishortmap_get_multiple (struct GNUNET_CONTAINER_MultiShortmap *map,
834 const struct GNUNET_ShortHashCode *key,
835 GNUNET_CONTAINER_ShortmapIterator it,
842 ce = &map->next_cache[map->next_cache_off];
843 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
845 me = map->map[idx_of (map, key)];
846 if (map->use_small_entries)
848 struct SmallMapEntry *sme;
851 while (NULL != (sme = ce->sme))
854 if (0 != memcmp (key,
856 sizeof (struct GNUNET_ShortHashCode)))
859 (GNUNET_OK != it (it_cls,
863 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
864 return GNUNET_SYSERR;
871 struct BigMapEntry *bme;
874 while (NULL != (bme = ce->bme))
877 if (0 != memcmp (key,
879 sizeof (struct GNUNET_ShortHashCode)))
882 (GNUNET_OK != it (it_cls,
886 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
887 return GNUNET_SYSERR;
892 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
899 * Call @a it on a random value from the map, or not at all
900 * if the map is empty. Note that this function has linear
901 * complexity (in the size of the map).
904 * @param it function to call on a random entry
905 * @param it_cls extra argument to @a it
906 * @return the number of key value pairs processed, zero or one.
909 GNUNET_CONTAINER_multishortmap_get_random (const struct GNUNET_CONTAINER_MultiShortmap *map,
910 GNUNET_CONTAINER_ShortmapIterator it,
920 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
922 for (unsigned int idx = 0; idx < map->map_length; idx++)
925 if (map->use_small_entries)
927 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
931 if (GNUNET_OK != it (it_cls,
934 return GNUNET_SYSERR;
942 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
946 if (GNUNET_OK != it (it_cls,
947 &bme->key, bme->value))
948 return GNUNET_SYSERR;
956 return GNUNET_SYSERR;
961 * Create an iterator for a multishortmap.
962 * The iterator can be used to retrieve all the elements in the multishortmap
963 * one by one, without having to handle all elements at once (in contrast to
964 * #GNUNET_CONTAINER_multishortmap_iterate). Note that the iterator can not be
965 * used anymore if elements have been removed from 'map' after the creation of
966 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
967 * result in skipped or repeated elements.
969 * @param map the map to create an iterator for
970 * @return an iterator over the given multishortmap 'map'
972 struct GNUNET_CONTAINER_MultiShortmapIterator *
973 GNUNET_CONTAINER_multishortmap_iterator_create (const struct GNUNET_CONTAINER_MultiShortmap *map)
975 struct GNUNET_CONTAINER_MultiShortmapIterator *iter;
977 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiShortmapIterator);
979 iter->modification_counter = map->modification_counter;
980 iter->me = map->map[0];
986 * Retrieve the next element from the hash map at the iterator's position.
987 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
989 * This operation is only allowed if no elements have been removed from the
990 * multishortmap since the creation of 'iter', and the map has not been destroyed.
991 * Adding elements may result in repeating or skipping elements.
993 * @param iter the iterator to get the next element from
994 * @param key pointer to store the key in, can be NULL
995 * @param value pointer to store the value in, can be NULL
996 * @return #GNUNET_YES we returned an element,
997 * #GNUNET_NO if we are out of elements
1000 GNUNET_CONTAINER_multishortmap_iterator_next (struct GNUNET_CONTAINER_MultiShortmapIterator *iter,
1001 struct GNUNET_ShortHashCode *key,
1004 /* make sure the map has not been modified */
1005 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
1007 /* look for the next entry, skipping empty buckets */
1010 if (iter->idx >= iter->map->map_length)
1012 if (GNUNET_YES == iter->map->use_small_entries)
1014 if (NULL != iter->me.sme)
1017 *key = *iter->me.sme->key;
1019 *value = iter->me.sme->value;
1020 iter->me.sme = iter->me.sme->next;
1026 if (NULL != iter->me.bme)
1029 *key = iter->me.bme->key;
1031 *value = iter->me.bme->value;
1032 iter->me.bme = iter->me.bme->next;
1037 if (iter->idx < iter->map->map_length)
1038 iter->me = iter->map->map[iter->idx];
1044 * Destroy a multishortmap iterator.
1046 * @param iter the iterator to destroy
1049 GNUNET_CONTAINER_multishortmap_iterator_destroy (struct GNUNET_CONTAINER_MultiShortmapIterator *iter)
1055 /* end of container_multishortmap.c */