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_multiuuidmap.c
22 * @brief hash map for UUIDs 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-multiuuidmap", __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_Uuid 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_Uuid *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_MultiUuidmap
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 multiuuidmap.
151 * Allows to enumerate elements asynchronously.
153 struct GNUNET_CONTAINER_MultiUuidmapIterator
156 * Position in the bucket '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_MultiUuidmap *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_MultiUuidmap *
194 GNUNET_CONTAINER_multiuuidmap_create (unsigned int len, int do_not_copy_keys)
196 struct GNUNET_CONTAINER_MultiUuidmap *map;
198 GNUNET_assert (len > 0);
199 map = GNUNET_new (struct GNUNET_CONTAINER_MultiUuidmap);
200 map->map = GNUNET_malloc_large (len * sizeof(union MapEntry));
201 if (NULL == map->map)
206 map->map_length = len;
207 map->use_small_entries = do_not_copy_keys;
213 * Destroy a hash map. Will not free any values
214 * stored in the hash map!
219 GNUNET_CONTAINER_multiuuidmap_destroy (
220 struct GNUNET_CONTAINER_MultiUuidmap *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_MultiUuidmap *map,
269 const struct GNUNET_Uuid *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_multiuuidmap_size (
287 const struct GNUNET_CONTAINER_MultiUuidmap *map)
294 * Given a key find a value in the map matching the key.
297 * @param key what to look for
298 * @return NULL if no value was found; note that
299 * this is indistinguishable from values that just
300 * happen to be NULL; use "contains" to test for
301 * key-value pairs with value NULL
304 GNUNET_CONTAINER_multiuuidmap_get (
305 const struct GNUNET_CONTAINER_MultiUuidmap *map,
306 const struct GNUNET_Uuid *key)
310 me = map->map[idx_of (map, key)];
311 if (map->use_small_entries)
313 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
314 if (0 == GNUNET_memcmp (key, sme->key))
319 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
320 if (0 == GNUNET_memcmp (key, &bme->key))
328 * Iterate over all entries in the map.
331 * @param it function to call on each entry
332 * @param it_cls extra argument to @a it
333 * @return the number of key value pairs processed,
334 * #GNUNET_SYSERR if it aborted iteration
337 GNUNET_CONTAINER_multiuuidmap_iterate (
338 struct GNUNET_CONTAINER_MultiUuidmap *map,
339 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it,
345 struct GNUNET_Uuid kc;
348 GNUNET_assert (NULL != map);
349 ce = &map->next_cache[map->next_cache_off];
350 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
351 for (unsigned int i = 0; i < map->map_length; i++)
354 if (map->use_small_entries)
356 struct SmallMapEntry *sme;
359 while (NULL != (sme = ce->sme))
362 if ((NULL != it) && (GNUNET_OK != it (it_cls, sme->key, sme->value)))
364 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
365 return GNUNET_SYSERR;
372 struct BigMapEntry *bme;
375 while (NULL != (bme = ce->bme))
381 if (GNUNET_OK != it (it_cls, &kc, bme->value))
383 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
384 return GNUNET_SYSERR;
391 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
397 * We are about to free() the @a bme, make sure it is not in
398 * the list of next values for any iterator in the @a map's next_cache.
400 * @param map the map to check
401 * @param bme the entry that is about to be free'd
404 update_next_cache_bme (struct GNUNET_CONTAINER_MultiUuidmap *map,
405 const struct BigMapEntry *bme)
407 for (unsigned int i = 0; i < map->next_cache_off; i++)
408 if (map->next_cache[i].bme == bme)
409 map->next_cache[i].bme = bme->next;
414 * We are about to free() the @a sme, make sure it is not in
415 * the list of next values for any iterator in the @a map's next_cache.
417 * @param map the map to check
418 * @param sme the entry that is about to be free'd
421 update_next_cache_sme (struct GNUNET_CONTAINER_MultiUuidmap *map,
422 const struct SmallMapEntry *sme)
424 for (unsigned int i = 0; i < map->next_cache_off; i++)
425 if (map->next_cache[i].sme == sme)
426 map->next_cache[i].sme = sme->next;
431 * Remove the given key-value pair from the map. Note that if the
432 * key-value pair is in the map multiple times, only one of the pairs
436 * @param key key of the key-value pair
437 * @param value value of the key-value pair
438 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
442 GNUNET_CONTAINER_multiuuidmap_remove (struct GNUNET_CONTAINER_MultiUuidmap *map,
443 const struct GNUNET_Uuid *key,
449 map->modification_counter++;
450 i = idx_of (map, key);
452 if (map->use_small_entries)
454 struct SmallMapEntry *p = NULL;
456 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
458 if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value))
461 map->map[i].sme = sme->next;
464 update_next_cache_sme (map, sme);
474 struct BigMapEntry *p = NULL;
476 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
478 if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value))
481 map->map[i].bme = bme->next;
484 update_next_cache_bme (map, bme);
497 * Remove all entries for the given key from the map.
498 * Note that the values would not be "freed".
501 * @param key identifies values to be removed
502 * @return number of values removed
505 GNUNET_CONTAINER_multiuuidmap_remove_all (
506 struct GNUNET_CONTAINER_MultiUuidmap *map,
507 const struct GNUNET_Uuid *key)
513 map->modification_counter++;
516 i = idx_of (map, key);
518 if (map->use_small_entries)
520 struct SmallMapEntry *sme;
521 struct SmallMapEntry *p;
527 if (0 == GNUNET_memcmp (key, sme->key))
530 map->map[i].sme = sme->next;
533 update_next_cache_sme (map, sme);
537 sme = map->map[i].sme;
551 struct BigMapEntry *bme;
552 struct BigMapEntry *p;
558 if (0 == GNUNET_memcmp (key, &bme->key))
561 map->map[i].bme = bme->next;
564 update_next_cache_bme (map, bme);
568 bme = map->map[i].bme;
585 * Check if the map contains any value under the given
586 * key (including values that are NULL).
589 * @param key the key to test if a value exists for it
590 * @return #GNUNET_YES if such a value exists,
594 GNUNET_CONTAINER_multiuuidmap_contains (
595 const struct GNUNET_CONTAINER_MultiUuidmap *map,
596 const struct GNUNET_Uuid *key)
600 me = map->map[idx_of (map, key)];
601 if (map->use_small_entries)
603 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
604 if (0 == GNUNET_memcmp (key, sme->key))
609 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
610 if (0 == GNUNET_memcmp (key, &bme->key))
618 * Check if the map contains the given value under the given
622 * @param key the key to test if a value exists for it
623 * @param value value to test for
624 * @return #GNUNET_YES if such a value exists,
628 GNUNET_CONTAINER_multiuuidmap_contains_value (
629 const struct GNUNET_CONTAINER_MultiUuidmap *map,
630 const struct GNUNET_Uuid *key,
635 me = map->map[idx_of (map, key)];
636 if (map->use_small_entries)
638 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
639 if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value))
644 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
645 if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value))
653 * Grow the given map to a more appropriate size.
655 * @param map the hash map to grow
658 grow (struct GNUNET_CONTAINER_MultiUuidmap *map)
660 union MapEntry *old_map;
661 union MapEntry *new_map;
662 unsigned int old_len;
663 unsigned int new_len;
667 old_len = map->map_length;
668 new_len = old_len * 2;
669 if (0 == new_len) /* 2^31 * 2 == 0 */
670 new_len = old_len; /* never use 0 */
671 if (new_len == old_len)
672 return; /* nothing changed */
673 new_map = GNUNET_malloc_large (new_len * sizeof(union MapEntry));
675 return; /* grow not possible */
676 map->modification_counter++;
677 map->map_length = new_len;
679 for (unsigned int i = 0; i < old_len; i++)
681 if (map->use_small_entries)
683 struct SmallMapEntry *sme;
685 while (NULL != (sme = old_map[i].sme))
687 old_map[i].sme = sme->next;
688 idx = idx_of (map, sme->key);
689 sme->next = new_map[idx].sme;
690 new_map[idx].sme = sme;
695 struct BigMapEntry *bme;
697 while (NULL != (bme = old_map[i].bme))
699 old_map[i].bme = bme->next;
700 idx = idx_of (map, &bme->key);
701 bme->next = new_map[idx].bme;
702 new_map[idx].bme = bme;
706 GNUNET_free (old_map);
711 * Store a key-value pair in the map.
714 * @param key key to use
715 * @param value value to use
716 * @param opt options for put
717 * @return #GNUNET_OK on success,
718 * #GNUNET_NO if a value was replaced (with REPLACE)
719 * #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
720 * value already exists
723 GNUNET_CONTAINER_multiuuidmap_put (struct GNUNET_CONTAINER_MultiUuidmap *map,
724 const struct GNUNET_Uuid *key,
726 enum GNUNET_CONTAINER_MultiHashMapOption opt)
731 i = idx_of (map, key);
732 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
733 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
736 if (map->use_small_entries)
738 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
739 if (0 == GNUNET_memcmp (key, sme->key))
741 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
742 return GNUNET_SYSERR;
749 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
750 if (0 == GNUNET_memcmp (key, &bme->key))
752 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
753 return GNUNET_SYSERR;
759 if (map->size / 3 >= map->map_length / 4)
762 i = idx_of (map, key);
764 if (map->use_small_entries)
766 struct SmallMapEntry *sme;
768 sme = GNUNET_new (struct SmallMapEntry);
771 sme->next = map->map[i].sme;
772 map->map[i].sme = sme;
776 struct BigMapEntry *bme;
778 bme = GNUNET_new (struct BigMapEntry);
781 bme->next = map->map[i].bme;
782 map->map[i].bme = bme;
790 * Iterate over all entries in the map that match a particular key.
793 * @param key key that the entries must correspond to
794 * @param it function to call on each entry
795 * @param it_cls extra argument to @a it
796 * @return the number of key value pairs processed,
797 * #GNUNET_SYSERR if it aborted iteration
800 GNUNET_CONTAINER_multiuuidmap_get_multiple (
801 struct GNUNET_CONTAINER_MultiUuidmap *map,
802 const struct GNUNET_Uuid *key,
803 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it,
810 ce = &map->next_cache[map->next_cache_off];
811 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
813 me = map->map[idx_of (map, key)];
814 if (map->use_small_entries)
816 struct SmallMapEntry *sme;
819 while (NULL != (sme = ce->sme))
822 if (0 != GNUNET_memcmp (key, sme->key))
824 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value)))
826 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
827 return GNUNET_SYSERR;
834 struct BigMapEntry *bme;
837 while (NULL != (bme = ce->bme))
840 if (0 != GNUNET_memcmp (key, &bme->key))
842 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value)))
844 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
845 return GNUNET_SYSERR;
850 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
857 * Call @a it on a random value from the map, or not at all
858 * if the map is empty. Note that this function has linear
859 * complexity (in the size of the map).
862 * @param it function to call on a random entry
863 * @param it_cls extra argument to @a it
864 * @return the number of key value pairs processed, zero or one.
867 GNUNET_CONTAINER_multiuuidmap_get_random (
868 const struct GNUNET_CONTAINER_MultiUuidmap *map,
869 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it,
879 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size);
880 for (unsigned int idx = 0; idx < map->map_length; idx++)
883 if (map->use_small_entries)
885 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
889 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
890 return GNUNET_SYSERR;
898 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
902 if (GNUNET_OK != it (it_cls, &bme->key, bme->value))
903 return GNUNET_SYSERR;
911 return GNUNET_SYSERR;
916 * Create an iterator for a multiuuidmap.
917 * The iterator can be used to retrieve all the elements in the multiuuidmap
918 * one by one, without having to handle all elements at once (in contrast to
919 * #GNUNET_CONTAINER_multiuuidmap_iterate). Note that the iterator can not be
920 * used anymore if elements have been removed from 'map' after the creation of
921 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
922 * result in skipped or repeated elements.
924 * @param map the map to create an iterator for
925 * @return an iterator over the given multiuuidmap 'map'
927 struct GNUNET_CONTAINER_MultiUuidmapIterator *
928 GNUNET_CONTAINER_multiuuidmap_iterator_create (
929 const struct GNUNET_CONTAINER_MultiUuidmap *map)
931 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter;
933 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiUuidmapIterator);
935 iter->modification_counter = map->modification_counter;
936 iter->me = map->map[0];
942 * Retrieve the next element from the hash map at the iterator's position.
943 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
945 * This operation is only allowed if no elements have been removed from the
946 * multiuuidmap since the creation of 'iter', and the map has not been destroyed.
947 * Adding elements may result in repeating or skipping elements.
949 * @param iter the iterator to get the next element from
950 * @param key pointer to store the key in, can be NULL
951 * @param value pointer to store the value in, can be NULL
952 * @return #GNUNET_YES we returned an element,
953 * #GNUNET_NO if we are out of elements
956 GNUNET_CONTAINER_multiuuidmap_iterator_next (
957 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter,
958 struct GNUNET_Uuid *key,
961 /* make sure the map has not been modified */
962 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
964 /* look for the next entry, skipping empty buckets */
967 if (iter->idx >= iter->map->map_length)
969 if (GNUNET_YES == iter->map->use_small_entries)
971 if (NULL != iter->me.sme)
974 *key = *iter->me.sme->key;
976 *value = iter->me.sme->value;
977 iter->me.sme = iter->me.sme->next;
983 if (NULL != iter->me.bme)
986 *key = iter->me.bme->key;
988 *value = iter->me.bme->value;
989 iter->me.bme = iter->me.bme->next;
994 if (iter->idx < iter->map->map_length)
995 iter->me = iter->map->map[iter->idx];
1001 * Destroy a multiuuidmap iterator.
1003 * @param iter the iterator to destroy
1006 GNUNET_CONTAINER_multiuuidmap_iterator_destroy (
1007 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter)
1013 /* end of container_multiuuidmap.c */