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.
53 * If there is a hash collision, we create a linked list.
55 struct BigMapEntry *next;
60 struct GNUNET_Uuid 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_Uuid *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_MultiUuidmap
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 multiuuidmap.
153 * Allows to enumerate elements asynchronously.
155 struct GNUNET_CONTAINER_MultiUuidmapIterator
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_MultiUuidmap *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_MultiUuidmap *
196 GNUNET_CONTAINER_multiuuidmap_create (unsigned int len, int do_not_copy_keys)
198 struct GNUNET_CONTAINER_MultiUuidmap *map;
200 GNUNET_assert (len > 0);
201 map = GNUNET_new (struct GNUNET_CONTAINER_MultiUuidmap);
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_multiuuidmap_destroy (
222 struct GNUNET_CONTAINER_MultiUuidmap *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_MultiUuidmap *map,
271 const struct GNUNET_Uuid *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_multiuuidmap_size (
289 const struct GNUNET_CONTAINER_MultiUuidmap *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_multiuuidmap_get (
307 const struct GNUNET_CONTAINER_MultiUuidmap *map,
308 const struct GNUNET_Uuid *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_multiuuidmap_iterate (
340 struct GNUNET_CONTAINER_MultiUuidmap *map,
341 GNUNET_CONTAINER_MultiUuidmapIterator it,
347 struct GNUNET_Uuid 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_MultiUuidmap *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_MultiUuidmap *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_multiuuidmap_remove (struct GNUNET_CONTAINER_MultiUuidmap *map,
445 const struct GNUNET_Uuid *key,
451 map->modification_counter++;
452 i = idx_of (map, key);
454 if (map->use_small_entries)
456 struct SmallMapEntry *p = NULL;
458 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
460 if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value))
463 map->map[i].sme = sme->next;
466 update_next_cache_sme (map, sme);
476 struct BigMapEntry *p = NULL;
478 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
480 if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value))
483 map->map[i].bme = bme->next;
486 update_next_cache_bme (map, bme);
499 * Remove all entries for the given key from the map.
500 * Note that the values would not be "freed".
503 * @param key identifies values to be removed
504 * @return number of values removed
507 GNUNET_CONTAINER_multiuuidmap_remove_all (
508 struct GNUNET_CONTAINER_MultiUuidmap *map,
509 const struct GNUNET_Uuid *key)
515 map->modification_counter++;
518 i = idx_of (map, key);
520 if (map->use_small_entries)
522 struct SmallMapEntry *sme;
523 struct SmallMapEntry *p;
529 if (0 == GNUNET_memcmp (key, sme->key))
532 map->map[i].sme = sme->next;
535 update_next_cache_sme (map, sme);
539 sme = map->map[i].sme;
553 struct BigMapEntry *bme;
554 struct BigMapEntry *p;
560 if (0 == GNUNET_memcmp (key, &bme->key))
563 map->map[i].bme = bme->next;
566 update_next_cache_bme (map, bme);
570 bme = map->map[i].bme;
587 * Check if the map contains any value under the given
588 * key (including values that are NULL).
591 * @param key the key to test if a value exists for it
592 * @return #GNUNET_YES if such a value exists,
596 GNUNET_CONTAINER_multiuuidmap_contains (
597 const struct GNUNET_CONTAINER_MultiUuidmap *map,
598 const struct GNUNET_Uuid *key)
602 me = map->map[idx_of (map, key)];
603 if (map->use_small_entries)
605 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
606 if (0 == GNUNET_memcmp (key, sme->key))
611 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
612 if (0 == GNUNET_memcmp (key, &bme->key))
620 * Check if the map contains the given value under the given
624 * @param key the key to test if a value exists for it
625 * @param value value to test for
626 * @return #GNUNET_YES if such a value exists,
630 GNUNET_CONTAINER_multiuuidmap_contains_value (
631 const struct GNUNET_CONTAINER_MultiUuidmap *map,
632 const struct GNUNET_Uuid *key,
637 me = map->map[idx_of (map, key)];
638 if (map->use_small_entries)
640 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
641 if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value))
646 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
647 if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value))
655 * Grow the given map to a more appropriate size.
657 * @param map the hash map to grow
660 grow (struct GNUNET_CONTAINER_MultiUuidmap *map)
662 union MapEntry *old_map;
663 union MapEntry *new_map;
664 unsigned int old_len;
665 unsigned int new_len;
669 old_len = map->map_length;
670 new_len = old_len * 2;
671 if (0 == new_len) /* 2^31 * 2 == 0 */
672 new_len = old_len; /* never use 0 */
673 if (new_len == old_len)
674 return; /* nothing changed */
675 new_map = GNUNET_malloc_large (new_len * sizeof (union MapEntry));
677 return; /* grow not possible */
678 map->modification_counter++;
679 map->map_length = new_len;
681 for (unsigned int i = 0; i < old_len; i++)
683 if (map->use_small_entries)
685 struct SmallMapEntry *sme;
687 while (NULL != (sme = old_map[i].sme))
689 old_map[i].sme = sme->next;
690 idx = idx_of (map, sme->key);
691 sme->next = new_map[idx].sme;
692 new_map[idx].sme = sme;
697 struct BigMapEntry *bme;
699 while (NULL != (bme = old_map[i].bme))
701 old_map[i].bme = bme->next;
702 idx = idx_of (map, &bme->key);
703 bme->next = new_map[idx].bme;
704 new_map[idx].bme = bme;
708 GNUNET_free (old_map);
713 * Store a key-value pair in the map.
716 * @param key key to use
717 * @param value value to use
718 * @param opt options for put
719 * @return #GNUNET_OK on success,
720 * #GNUNET_NO if a value was replaced (with REPLACE)
721 * #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
722 * value already exists
725 GNUNET_CONTAINER_multiuuidmap_put (struct GNUNET_CONTAINER_MultiUuidmap *map,
726 const struct GNUNET_Uuid *key,
728 enum GNUNET_CONTAINER_MultiHashMapOption opt)
733 i = idx_of (map, key);
734 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
735 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
738 if (map->use_small_entries)
740 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
741 if (0 == GNUNET_memcmp (key, sme->key))
743 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
744 return GNUNET_SYSERR;
751 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
752 if (0 == GNUNET_memcmp (key, &bme->key))
754 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
755 return GNUNET_SYSERR;
761 if (map->size / 3 >= map->map_length / 4)
764 i = idx_of (map, key);
766 if (map->use_small_entries)
768 struct SmallMapEntry *sme;
770 sme = GNUNET_new (struct SmallMapEntry);
773 sme->next = map->map[i].sme;
774 map->map[i].sme = sme;
778 struct BigMapEntry *bme;
780 bme = GNUNET_new (struct BigMapEntry);
783 bme->next = map->map[i].bme;
784 map->map[i].bme = bme;
792 * Iterate over all entries in the map that match a particular key.
795 * @param key key that the entries must correspond to
796 * @param it function to call on each entry
797 * @param it_cls extra argument to @a it
798 * @return the number of key value pairs processed,
799 * #GNUNET_SYSERR if it aborted iteration
802 GNUNET_CONTAINER_multiuuidmap_get_multiple (
803 struct GNUNET_CONTAINER_MultiUuidmap *map,
804 const struct GNUNET_Uuid *key,
805 GNUNET_CONTAINER_MultiUuidmapIterator it,
812 ce = &map->next_cache[map->next_cache_off];
813 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
815 me = map->map[idx_of (map, key)];
816 if (map->use_small_entries)
818 struct SmallMapEntry *sme;
821 while (NULL != (sme = ce->sme))
824 if (0 != GNUNET_memcmp (key, sme->key))
826 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value)))
828 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
829 return GNUNET_SYSERR;
836 struct BigMapEntry *bme;
839 while (NULL != (bme = ce->bme))
842 if (0 != GNUNET_memcmp (key, &bme->key))
844 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value)))
846 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
847 return GNUNET_SYSERR;
852 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
859 * Call @a it on a random value from the map, or not at all
860 * if the map is empty. Note that this function has linear
861 * complexity (in the size of the map).
864 * @param it function to call on a random entry
865 * @param it_cls extra argument to @a it
866 * @return the number of key value pairs processed, zero or one.
869 GNUNET_CONTAINER_multiuuidmap_get_random (
870 const struct GNUNET_CONTAINER_MultiUuidmap *map,
871 GNUNET_CONTAINER_MultiUuidmapIterator it,
881 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size);
882 for (unsigned int idx = 0; idx < map->map_length; idx++)
885 if (map->use_small_entries)
887 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
891 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
892 return GNUNET_SYSERR;
900 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
904 if (GNUNET_OK != it (it_cls, &bme->key, bme->value))
905 return GNUNET_SYSERR;
913 return GNUNET_SYSERR;
918 * Create an iterator for a multiuuidmap.
919 * The iterator can be used to retrieve all the elements in the multiuuidmap
920 * one by one, without having to handle all elements at once (in contrast to
921 * #GNUNET_CONTAINER_multiuuidmap_iterate). Note that the iterator can not be
922 * used anymore if elements have been removed from 'map' after the creation of
923 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
924 * result in skipped or repeated elements.
926 * @param map the map to create an iterator for
927 * @return an iterator over the given multiuuidmap 'map'
929 struct GNUNET_CONTAINER_MultiUuidmapIterator *
930 GNUNET_CONTAINER_multiuuidmap_iterator_create (
931 const struct GNUNET_CONTAINER_MultiUuidmap *map)
933 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter;
935 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiUuidmapIterator);
937 iter->modification_counter = map->modification_counter;
938 iter->me = map->map[0];
944 * Retrieve the next element from the hash map at the iterator's position.
945 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
947 * This operation is only allowed if no elements have been removed from the
948 * multiuuidmap since the creation of 'iter', and the map has not been destroyed.
949 * Adding elements may result in repeating or skipping elements.
951 * @param iter the iterator to get the next element from
952 * @param key pointer to store the key in, can be NULL
953 * @param value pointer to store the value in, can be NULL
954 * @return #GNUNET_YES we returned an element,
955 * #GNUNET_NO if we are out of elements
958 GNUNET_CONTAINER_multiuuidmap_iterator_next (
959 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter,
960 struct GNUNET_Uuid *key,
963 /* make sure the map has not been modified */
964 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
966 /* look for the next entry, skipping empty buckets */
969 if (iter->idx >= iter->map->map_length)
971 if (GNUNET_YES == iter->map->use_small_entries)
973 if (NULL != iter->me.sme)
976 *key = *iter->me.sme->key;
978 *value = iter->me.sme->value;
979 iter->me.sme = iter->me.sme->next;
985 if (NULL != iter->me.bme)
988 *key = iter->me.bme->key;
990 *value = iter->me.bme->value;
991 iter->me.bme = iter->me.bme->next;
996 if (iter->idx < iter->map->map_length)
997 iter->me = iter->map->map[iter->idx];
1003 * Destroy a multiuuidmap iterator.
1005 * @param iter the iterator to destroy
1008 GNUNET_CONTAINER_multiuuidmap_iterator_destroy (
1009 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter)
1015 /* end of container_multiuuidmap.c */