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.
51 * If there is a hash collision, we create a linked list.
53 struct BigMapEntry *next;
58 struct GNUNET_Uuid key;
63 * An entry in the hash map with just a pointer to the key.
65 struct SmallMapEntry {
72 * If there is a hash collision, we create a linked list.
74 struct SmallMapEntry *next;
79 const struct GNUNET_Uuid *key;
88 * Variant used if map entries only contain a pointer to the key.
90 struct SmallMapEntry *sme;
93 * Variant used if map entries contain the full key.
95 struct BigMapEntry *bme;
100 * Internal representation of the hash map.
102 struct GNUNET_CONTAINER_MultiUuidmap {
104 * All of our buckets.
109 * Number of entries in the map.
114 * Length of the "map" array.
116 unsigned int map_length;
119 * #GNUNET_NO if the map entries are of type 'struct BigMapEntry',
120 * #GNUNET_YES if the map entries are of type 'struct SmallMapEntry'.
122 int use_small_entries;
125 * Counts the destructive modifications (grow, remove)
126 * to the map, so that iterators can check if they are still valid.
128 unsigned int modification_counter;
131 * Map entries indicating iteration positions currently
132 * in use by #GNUNET_CONTAINER_multihashmap_get_multiple().
133 * Only used up to @e next_cache_off.
135 union MapEntry next_cache[NEXT_CACHE_SIZE];
138 * Offset of @e next_cache entries in use, must be smaller
139 * than #NEXT_CACHE_SIZE.
141 unsigned int next_cache_off;
146 * Cursor into a multiuuidmap.
147 * Allows to enumerate elements asynchronously.
149 struct GNUNET_CONTAINER_MultiUuidmapIterator {
151 * Position in the bucket 'idx'
156 * Current bucket index.
161 * Modification counter as observed on the map when the iterator
164 unsigned int modification_counter;
167 * Map that we are iterating over.
169 const struct GNUNET_CONTAINER_MultiUuidmap *map;
174 * Create a multi hash map.
176 * @param len initial size (map will grow as needed)
177 * @param do_not_copy_keys #GNUNET_NO is always safe and should be used by default;
178 * #GNUNET_YES means that on 'put', the 'key' does not have
179 * to be copied as the destination of the pointer is
180 * guaranteed to be life as long as the value is stored in
181 * the hashmap. This can significantly reduce memory
182 * consumption, but of course is also a recipie for
183 * heap corruption if the assumption is not true. Only
184 * use this if (1) memory use is important in this case and
185 * (2) you have triple-checked that the invariant holds
186 * @return NULL on error
188 struct GNUNET_CONTAINER_MultiUuidmap *
189 GNUNET_CONTAINER_multiuuidmap_create(unsigned int len, int do_not_copy_keys)
191 struct GNUNET_CONTAINER_MultiUuidmap *map;
193 GNUNET_assert(len > 0);
194 map = GNUNET_new(struct GNUNET_CONTAINER_MultiUuidmap);
195 map->map = GNUNET_malloc_large(len * sizeof(union MapEntry));
196 if (NULL == map->map)
201 map->map_length = len;
202 map->use_small_entries = do_not_copy_keys;
208 * Destroy a hash map. Will not free any values
209 * stored in the hash map!
214 GNUNET_CONTAINER_multiuuidmap_destroy(
215 struct GNUNET_CONTAINER_MultiUuidmap *map)
217 GNUNET_assert(0 == map->next_cache_off);
218 for (unsigned int i = 0; i < map->map_length; i++)
223 if (map->use_small_entries)
225 struct SmallMapEntry *sme;
226 struct SmallMapEntry *nxt;
229 while (NULL != (sme = nxt))
238 struct BigMapEntry *bme;
239 struct BigMapEntry *nxt;
242 while (NULL != (bme = nxt))
250 GNUNET_free(map->map);
256 * Compute the index of the bucket for the given key.
258 * @param map hash map for which to compute the index
259 * @param key what key should the index be computed for
260 * @return offset into the "map" array of "map"
263 idx_of(const struct GNUNET_CONTAINER_MultiUuidmap *map,
264 const struct GNUNET_Uuid *key)
268 GNUNET_assert(NULL != map);
269 GNUNET_memcpy(&kx, key, sizeof(kx));
270 return kx % map->map_length;
275 * Get the number of key-value pairs in the map.
278 * @return the number of key value pairs
281 GNUNET_CONTAINER_multiuuidmap_size(
282 const struct GNUNET_CONTAINER_MultiUuidmap *map)
289 * Given a key find a value in the map matching the key.
292 * @param key what to look for
293 * @return NULL if no value was found; note that
294 * this is indistinguishable from values that just
295 * happen to be NULL; use "contains" to test for
296 * key-value pairs with value NULL
299 GNUNET_CONTAINER_multiuuidmap_get(
300 const struct GNUNET_CONTAINER_MultiUuidmap *map,
301 const struct GNUNET_Uuid *key)
305 me = map->map[idx_of(map, key)];
306 if (map->use_small_entries)
308 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
309 if (0 == GNUNET_memcmp(key, sme->key))
314 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
315 if (0 == GNUNET_memcmp(key, &bme->key))
323 * Iterate over all entries in the map.
326 * @param it function to call on each entry
327 * @param it_cls extra argument to @a it
328 * @return the number of key value pairs processed,
329 * #GNUNET_SYSERR if it aborted iteration
332 GNUNET_CONTAINER_multiuuidmap_iterate(
333 struct GNUNET_CONTAINER_MultiUuidmap *map,
334 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it,
340 struct GNUNET_Uuid kc;
343 GNUNET_assert(NULL != map);
344 ce = &map->next_cache[map->next_cache_off];
345 GNUNET_assert(++map->next_cache_off < NEXT_CACHE_SIZE);
346 for (unsigned int i = 0; i < map->map_length; i++)
349 if (map->use_small_entries)
351 struct SmallMapEntry *sme;
354 while (NULL != (sme = ce->sme))
357 if ((NULL != it) && (GNUNET_OK != it(it_cls, sme->key, sme->value)))
359 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
360 return GNUNET_SYSERR;
367 struct BigMapEntry *bme;
370 while (NULL != (bme = ce->bme))
376 if (GNUNET_OK != it(it_cls, &kc, bme->value))
378 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
379 return GNUNET_SYSERR;
386 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
392 * We are about to free() the @a bme, make sure it is not in
393 * the list of next values for any iterator in the @a map's next_cache.
395 * @param map the map to check
396 * @param bme the entry that is about to be free'd
399 update_next_cache_bme(struct GNUNET_CONTAINER_MultiUuidmap *map,
400 const struct BigMapEntry *bme)
402 for (unsigned int i = 0; i < map->next_cache_off; i++)
403 if (map->next_cache[i].bme == bme)
404 map->next_cache[i].bme = bme->next;
409 * We are about to free() the @a sme, make sure it is not in
410 * the list of next values for any iterator in the @a map's next_cache.
412 * @param map the map to check
413 * @param sme the entry that is about to be free'd
416 update_next_cache_sme(struct GNUNET_CONTAINER_MultiUuidmap *map,
417 const struct SmallMapEntry *sme)
419 for (unsigned int i = 0; i < map->next_cache_off; i++)
420 if (map->next_cache[i].sme == sme)
421 map->next_cache[i].sme = sme->next;
426 * Remove the given key-value pair from the map. Note that if the
427 * key-value pair is in the map multiple times, only one of the pairs
431 * @param key key of the key-value pair
432 * @param value value of the key-value pair
433 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
437 GNUNET_CONTAINER_multiuuidmap_remove(struct GNUNET_CONTAINER_MultiUuidmap *map,
438 const struct GNUNET_Uuid *key,
444 map->modification_counter++;
445 i = idx_of(map, key);
447 if (map->use_small_entries)
449 struct SmallMapEntry *p = NULL;
451 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
453 if ((0 == GNUNET_memcmp(key, sme->key)) && (value == sme->value))
456 map->map[i].sme = sme->next;
459 update_next_cache_sme(map, sme);
469 struct BigMapEntry *p = NULL;
471 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
473 if ((0 == GNUNET_memcmp(key, &bme->key)) && (value == bme->value))
476 map->map[i].bme = bme->next;
479 update_next_cache_bme(map, bme);
492 * Remove all entries for the given key from the map.
493 * Note that the values would not be "freed".
496 * @param key identifies values to be removed
497 * @return number of values removed
500 GNUNET_CONTAINER_multiuuidmap_remove_all(
501 struct GNUNET_CONTAINER_MultiUuidmap *map,
502 const struct GNUNET_Uuid *key)
508 map->modification_counter++;
511 i = idx_of(map, key);
513 if (map->use_small_entries)
515 struct SmallMapEntry *sme;
516 struct SmallMapEntry *p;
522 if (0 == GNUNET_memcmp(key, sme->key))
525 map->map[i].sme = sme->next;
528 update_next_cache_sme(map, sme);
532 sme = map->map[i].sme;
546 struct BigMapEntry *bme;
547 struct BigMapEntry *p;
553 if (0 == GNUNET_memcmp(key, &bme->key))
556 map->map[i].bme = bme->next;
559 update_next_cache_bme(map, bme);
563 bme = map->map[i].bme;
580 * Check if the map contains any value under the given
581 * key (including values that are NULL).
584 * @param key the key to test if a value exists for it
585 * @return #GNUNET_YES if such a value exists,
589 GNUNET_CONTAINER_multiuuidmap_contains(
590 const struct GNUNET_CONTAINER_MultiUuidmap *map,
591 const struct GNUNET_Uuid *key)
595 me = map->map[idx_of(map, key)];
596 if (map->use_small_entries)
598 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
599 if (0 == GNUNET_memcmp(key, sme->key))
604 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
605 if (0 == GNUNET_memcmp(key, &bme->key))
613 * Check if the map contains the given value under the given
617 * @param key the key to test if a value exists for it
618 * @param value value to test for
619 * @return #GNUNET_YES if such a value exists,
623 GNUNET_CONTAINER_multiuuidmap_contains_value(
624 const struct GNUNET_CONTAINER_MultiUuidmap *map,
625 const struct GNUNET_Uuid *key,
630 me = map->map[idx_of(map, key)];
631 if (map->use_small_entries)
633 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
634 if ((0 == GNUNET_memcmp(key, sme->key)) && (sme->value == value))
639 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
640 if ((0 == GNUNET_memcmp(key, &bme->key)) && (bme->value == value))
648 * Grow the given map to a more appropriate size.
650 * @param map the hash map to grow
653 grow(struct GNUNET_CONTAINER_MultiUuidmap *map)
655 union MapEntry *old_map;
656 union MapEntry *new_map;
657 unsigned int old_len;
658 unsigned int new_len;
662 old_len = map->map_length;
663 new_len = old_len * 2;
664 if (0 == new_len) /* 2^31 * 2 == 0 */
665 new_len = old_len; /* never use 0 */
666 if (new_len == old_len)
667 return; /* nothing changed */
668 new_map = GNUNET_malloc_large(new_len * sizeof(union MapEntry));
670 return; /* grow not possible */
671 map->modification_counter++;
672 map->map_length = new_len;
674 for (unsigned int i = 0; i < old_len; i++)
676 if (map->use_small_entries)
678 struct SmallMapEntry *sme;
680 while (NULL != (sme = old_map[i].sme))
682 old_map[i].sme = sme->next;
683 idx = idx_of(map, sme->key);
684 sme->next = new_map[idx].sme;
685 new_map[idx].sme = sme;
690 struct BigMapEntry *bme;
692 while (NULL != (bme = old_map[i].bme))
694 old_map[i].bme = bme->next;
695 idx = idx_of(map, &bme->key);
696 bme->next = new_map[idx].bme;
697 new_map[idx].bme = bme;
701 GNUNET_free(old_map);
706 * Store a key-value pair in the map.
709 * @param key key to use
710 * @param value value to use
711 * @param opt options for put
712 * @return #GNUNET_OK on success,
713 * #GNUNET_NO if a value was replaced (with REPLACE)
714 * #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
715 * value already exists
718 GNUNET_CONTAINER_multiuuidmap_put(struct GNUNET_CONTAINER_MultiUuidmap *map,
719 const struct GNUNET_Uuid *key,
721 enum GNUNET_CONTAINER_MultiHashMapOption opt)
726 i = idx_of(map, key);
727 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
728 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
731 if (map->use_small_entries)
733 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
734 if (0 == GNUNET_memcmp(key, sme->key))
736 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
737 return GNUNET_SYSERR;
744 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
745 if (0 == GNUNET_memcmp(key, &bme->key))
747 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
748 return GNUNET_SYSERR;
754 if (map->size / 3 >= map->map_length / 4)
757 i = idx_of(map, key);
759 if (map->use_small_entries)
761 struct SmallMapEntry *sme;
763 sme = GNUNET_new(struct SmallMapEntry);
766 sme->next = map->map[i].sme;
767 map->map[i].sme = sme;
771 struct BigMapEntry *bme;
773 bme = GNUNET_new(struct BigMapEntry);
776 bme->next = map->map[i].bme;
777 map->map[i].bme = bme;
785 * Iterate over all entries in the map that match a particular key.
788 * @param key key that the entries must correspond to
789 * @param it function to call on each entry
790 * @param it_cls extra argument to @a it
791 * @return the number of key value pairs processed,
792 * #GNUNET_SYSERR if it aborted iteration
795 GNUNET_CONTAINER_multiuuidmap_get_multiple(
796 struct GNUNET_CONTAINER_MultiUuidmap *map,
797 const struct GNUNET_Uuid *key,
798 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it,
805 ce = &map->next_cache[map->next_cache_off];
806 GNUNET_assert(++map->next_cache_off < NEXT_CACHE_SIZE);
808 me = map->map[idx_of(map, key)];
809 if (map->use_small_entries)
811 struct SmallMapEntry *sme;
814 while (NULL != (sme = ce->sme))
817 if (0 != GNUNET_memcmp(key, sme->key))
819 if ((NULL != it) && (GNUNET_OK != it(it_cls, key, sme->value)))
821 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
822 return GNUNET_SYSERR;
829 struct BigMapEntry *bme;
832 while (NULL != (bme = ce->bme))
835 if (0 != GNUNET_memcmp(key, &bme->key))
837 if ((NULL != it) && (GNUNET_OK != it(it_cls, key, bme->value)))
839 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
840 return GNUNET_SYSERR;
845 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
852 * Call @a it on a random value from the map, or not at all
853 * if the map is empty. Note that this function has linear
854 * complexity (in the size of the map).
857 * @param it function to call on a random entry
858 * @param it_cls extra argument to @a it
859 * @return the number of key value pairs processed, zero or one.
862 GNUNET_CONTAINER_multiuuidmap_get_random(
863 const struct GNUNET_CONTAINER_MultiUuidmap *map,
864 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it,
874 off = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, map->size);
875 for (unsigned int idx = 0; idx < map->map_length; idx++)
878 if (map->use_small_entries)
880 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
884 if (GNUNET_OK != it(it_cls, sme->key, sme->value))
885 return GNUNET_SYSERR;
893 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
897 if (GNUNET_OK != it(it_cls, &bme->key, bme->value))
898 return GNUNET_SYSERR;
906 return GNUNET_SYSERR;
911 * Create an iterator for a multiuuidmap.
912 * The iterator can be used to retrieve all the elements in the multiuuidmap
913 * one by one, without having to handle all elements at once (in contrast to
914 * #GNUNET_CONTAINER_multiuuidmap_iterate). Note that the iterator can not be
915 * used anymore if elements have been removed from 'map' after the creation of
916 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
917 * result in skipped or repeated elements.
919 * @param map the map to create an iterator for
920 * @return an iterator over the given multiuuidmap 'map'
922 struct GNUNET_CONTAINER_MultiUuidmapIterator *
923 GNUNET_CONTAINER_multiuuidmap_iterator_create(
924 const struct GNUNET_CONTAINER_MultiUuidmap *map)
926 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter;
928 iter = GNUNET_new(struct GNUNET_CONTAINER_MultiUuidmapIterator);
930 iter->modification_counter = map->modification_counter;
931 iter->me = map->map[0];
937 * Retrieve the next element from the hash map at the iterator's position.
938 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
940 * This operation is only allowed if no elements have been removed from the
941 * multiuuidmap since the creation of 'iter', and the map has not been destroyed.
942 * Adding elements may result in repeating or skipping elements.
944 * @param iter the iterator to get the next element from
945 * @param key pointer to store the key in, can be NULL
946 * @param value pointer to store the value in, can be NULL
947 * @return #GNUNET_YES we returned an element,
948 * #GNUNET_NO if we are out of elements
951 GNUNET_CONTAINER_multiuuidmap_iterator_next(
952 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter,
953 struct GNUNET_Uuid *key,
956 /* make sure the map has not been modified */
957 GNUNET_assert(iter->modification_counter == iter->map->modification_counter);
959 /* look for the next entry, skipping empty buckets */
962 if (iter->idx >= iter->map->map_length)
964 if (GNUNET_YES == iter->map->use_small_entries)
966 if (NULL != iter->me.sme)
969 *key = *iter->me.sme->key;
971 *value = iter->me.sme->value;
972 iter->me.sme = iter->me.sme->next;
978 if (NULL != iter->me.bme)
981 *key = iter->me.bme->key;
983 *value = iter->me.bme->value;
984 iter->me.bme = iter->me.bme->next;
989 if (iter->idx < iter->map->map_length)
990 iter->me = iter->map->map[iter->idx];
996 * Destroy a multiuuidmap iterator.
998 * @param iter the iterator to destroy
1001 GNUNET_CONTAINER_multiuuidmap_iterator_destroy(
1002 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter)
1008 /* end of container_multiuuidmap.c */