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/>.
19 * @file util/container_multishortmap.c
20 * @brief hash map where the same key may be present multiple times
21 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
27 #define LOG(kind,...) GNUNET_log_from (kind, "util-container-multishortmap", __VA_ARGS__)
30 * Maximum recursion depth for callbacks of
31 * #GNUNET_CONTAINER_multihashmap_get_multiple() themselve s
32 * again calling #GNUNET_CONTAINER_multihashmap_get_multiple().
33 * Should be totally excessive, but if violated we die.
35 #define NEXT_CACHE_SIZE 16
39 * An entry in the hash map with the full key.
50 * If there is a hash collision, we create a linked list.
52 struct BigMapEntry *next;
57 struct GNUNET_ShortHashCode key;
63 * 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_ShortHashCode *key;
92 * Variant used if map entries only contain a pointer to the key.
94 struct SmallMapEntry *sme;
97 * Variant used if map entries contain the full key.
99 struct BigMapEntry *bme;
104 * Internal representation of the hash map.
106 struct GNUNET_CONTAINER_MultiShortmap
109 * All of our buckets.
114 * Number of entries in the map.
119 * Length of the "map" array.
121 unsigned int map_length;
124 * #GNUNET_NO if the map entries are of type 'struct BigMapEntry',
125 * #GNUNET_YES if the map entries are of type 'struct SmallMapEntry'.
127 int use_small_entries;
130 * Counts the destructive modifications (grow, remove)
131 * to the map, so that iterators can check if they are still valid.
133 unsigned int modification_counter;
136 * Map entries indicating iteration positions currently
137 * in use by #GNUNET_CONTAINER_multihashmap_get_multiple().
138 * Only used up to @e next_cache_off.
140 union MapEntry next_cache[NEXT_CACHE_SIZE];
143 * Offset of @e next_cache entries in use, must be smaller
144 * than #NEXT_CACHE_SIZE.
146 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,
197 int do_not_copy_keys)
199 struct GNUNET_CONTAINER_MultiShortmap *map;
201 GNUNET_assert (len > 0);
202 map = GNUNET_new (struct GNUNET_CONTAINER_MultiShortmap);
203 map->map = GNUNET_new_array (len,
205 map->map_length = len;
206 map->use_small_entries = do_not_copy_keys;
212 * Destroy a hash map. Will not free any values
213 * stored in the hash map!
218 GNUNET_CONTAINER_multishortmap_destroy (struct GNUNET_CONTAINER_MultiShortmap *map)
220 GNUNET_assert (0 == map->next_cache_off);
221 for (unsigned int i = 0; i < map->map_length; i++)
226 if (map->use_small_entries)
228 struct SmallMapEntry *sme;
229 struct SmallMapEntry *nxt;
232 while (NULL != (sme = nxt))
241 struct BigMapEntry *bme;
242 struct BigMapEntry *nxt;
245 while (NULL != (bme = nxt))
253 GNUNET_free (map->map);
259 * Compute the index of the bucket for the given key.
261 * @param map hash map for which to compute the index
262 * @param key what key should the index be computed for
263 * @return offset into the "map" array of "map"
266 idx_of (const struct GNUNET_CONTAINER_MultiShortmap *map,
267 const struct GNUNET_ShortHashCode *key)
271 GNUNET_assert (NULL != map);
272 GNUNET_memcpy (&kx, key, sizeof (kx));
273 return kx % map->map_length;
278 * Get the number of key-value pairs in the map.
281 * @return the number of key value pairs
284 GNUNET_CONTAINER_multishortmap_size (const struct GNUNET_CONTAINER_MultiShortmap *map)
291 * Given a key find a value in the map matching the key.
294 * @param key what to look for
295 * @return NULL if no value was found; note that
296 * this is indistinguishable from values that just
297 * happen to be NULL; use "contains" to test for
298 * key-value pairs with value NULL
301 GNUNET_CONTAINER_multishortmap_get (const struct GNUNET_CONTAINER_MultiShortmap *map,
302 const struct GNUNET_ShortHashCode *key)
306 me = map->map[idx_of (map, key)];
307 if (map->use_small_entries)
309 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
310 if (0 == memcmp (key,
312 sizeof (struct GNUNET_ShortHashCode)))
317 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
318 if (0 == memcmp (key,
320 sizeof (struct GNUNET_ShortHashCode)))
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_multishortmap_iterate (struct GNUNET_CONTAINER_MultiShortmap *map,
338 GNUNET_CONTAINER_ShortmapIterator it,
344 struct GNUNET_ShortHashCode kc;
347 GNUNET_assert (NULL != map);
348 ce = &map->next_cache[map->next_cache_off];
349 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
350 for (unsigned int i = 0; i < map->map_length; i++)
353 if (map->use_small_entries)
355 struct SmallMapEntry *sme;
358 while (NULL != (sme = ce->sme))
362 (GNUNET_OK != it (it_cls,
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,
387 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
388 return GNUNET_SYSERR;
395 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
401 * We are about to free() the @a bme, make sure it is not in
402 * the list of next values for any iterator in the @a map's next_cache.
404 * @param map the map to check
405 * @param bme the entry that is about to be free'd
408 update_next_cache_bme (struct GNUNET_CONTAINER_MultiShortmap *map,
409 const struct BigMapEntry *bme)
411 for (unsigned int i=0;i<map->next_cache_off;i++)
412 if (map->next_cache[i].bme == bme)
413 map->next_cache[i].bme = bme->next;
418 * We are about to free() the @a sme, make sure it is not in
419 * the list of next values for any iterator in the @a map's next_cache.
421 * @param map the map to check
422 * @param sme the entry that is about to be free'd
425 update_next_cache_sme (struct GNUNET_CONTAINER_MultiShortmap *map,
426 const struct SmallMapEntry *sme)
428 for (unsigned int i=0;i<map->next_cache_off;i++)
429 if (map->next_cache[i].sme == sme)
430 map->next_cache[i].sme = sme->next;
435 * Remove the given key-value pair from the map. Note that if the
436 * key-value pair is in the map multiple times, only one of the pairs
440 * @param key key of the key-value pair
441 * @param value value of the key-value pair
442 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
446 GNUNET_CONTAINER_multishortmap_remove (struct GNUNET_CONTAINER_MultiShortmap *map,
447 const struct GNUNET_ShortHashCode *key,
453 map->modification_counter++;
454 i = idx_of (map, key);
456 if (map->use_small_entries)
458 struct SmallMapEntry *p = NULL;
460 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
462 if ((0 == memcmp (key,
464 sizeof (struct GNUNET_ShortHashCode))) &&
465 (value == sme->value))
468 map->map[i].sme = sme->next;
471 update_next_cache_sme (map,
482 struct BigMapEntry *p = NULL;
484 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
486 if ((0 == memcmp (key,
488 sizeof (struct GNUNET_ShortHashCode))) &&
489 (value == bme->value))
492 map->map[i].bme = bme->next;
495 update_next_cache_bme (map,
509 * Remove all entries for the given key from the map.
510 * Note that the values would not be "freed".
513 * @param key identifies values to be removed
514 * @return number of values removed
517 GNUNET_CONTAINER_multishortmap_remove_all (struct GNUNET_CONTAINER_MultiShortmap *map,
518 const struct GNUNET_ShortHashCode *key)
524 map->modification_counter++;
527 i = idx_of (map, key);
529 if (map->use_small_entries)
531 struct SmallMapEntry *sme;
532 struct SmallMapEntry *p;
538 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_ShortHashCode)))
541 map->map[i].sme = sme->next;
544 update_next_cache_sme (map,
549 sme = map->map[i].sme;
563 struct BigMapEntry *bme;
564 struct BigMapEntry *p;
570 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_ShortHashCode)))
573 map->map[i].bme = bme->next;
576 update_next_cache_bme (map,
581 bme = map->map[i].bme;
598 * Check if the map contains any value under the given
599 * key (including values that are NULL).
602 * @param key the key to test if a value exists for it
603 * @return #GNUNET_YES if such a value exists,
607 GNUNET_CONTAINER_multishortmap_contains (const struct GNUNET_CONTAINER_MultiShortmap *map,
608 const struct GNUNET_ShortHashCode *key)
612 me = map->map[idx_of (map, key)];
613 if (map->use_small_entries)
615 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
616 if (0 == memcmp (key,
618 sizeof (struct GNUNET_ShortHashCode)))
623 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
624 if (0 == memcmp (key,
626 sizeof (struct GNUNET_ShortHashCode)))
634 * Check if the map contains the given value under the given
638 * @param key the key to test if a value exists for it
639 * @param value value to test for
640 * @return #GNUNET_YES if such a value exists,
644 GNUNET_CONTAINER_multishortmap_contains_value (const struct GNUNET_CONTAINER_MultiShortmap *map,
645 const struct GNUNET_ShortHashCode *key,
650 me = map->map[idx_of (map, key)];
651 if (map->use_small_entries)
653 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
654 if ( (0 == memcmp (key,
656 sizeof (struct GNUNET_ShortHashCode))) &&
657 (sme->value == value) )
662 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
663 if ( (0 == memcmp (key,
665 sizeof (struct GNUNET_ShortHashCode))) &&
666 (bme->value == value) )
674 * Grow the given map to a more appropriate size.
676 * @param map the hash map to grow
679 grow (struct GNUNET_CONTAINER_MultiShortmap *map)
681 union MapEntry *old_map;
682 union MapEntry *new_map;
683 unsigned int old_len;
684 unsigned int new_len;
687 map->modification_counter++;
690 old_len = map->map_length;
691 new_len = old_len * 2;
692 new_map = GNUNET_new_array (new_len,
694 map->map_length = new_len;
696 for (unsigned int i = 0; i < old_len; i++)
698 if (map->use_small_entries)
700 struct SmallMapEntry *sme;
702 while (NULL != (sme = old_map[i].sme))
704 old_map[i].sme = sme->next;
705 idx = idx_of (map, sme->key);
706 sme->next = new_map[idx].sme;
707 new_map[idx].sme = sme;
712 struct BigMapEntry *bme;
714 while (NULL != (bme = old_map[i].bme))
716 old_map[i].bme = bme->next;
717 idx = idx_of (map, &bme->key);
718 bme->next = new_map[idx].bme;
719 new_map[idx].bme = bme;
723 GNUNET_free (old_map);
728 * Store a key-value pair in the map.
731 * @param key key to use
732 * @param value value to use
733 * @param opt options for put
734 * @return #GNUNET_OK on success,
735 * #GNUNET_NO if a value was replaced (with REPLACE)
736 * #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
737 * value already exists
740 GNUNET_CONTAINER_multishortmap_put (struct GNUNET_CONTAINER_MultiShortmap *map,
741 const struct GNUNET_ShortHashCode *key,
743 enum GNUNET_CONTAINER_MultiHashMapOption opt)
748 i = idx_of (map, key);
749 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
750 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
753 if (map->use_small_entries)
755 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
756 if (0 == memcmp (key,
758 sizeof (struct GNUNET_ShortHashCode)))
760 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
761 return GNUNET_SYSERR;
768 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
769 if (0 == memcmp (key,
771 sizeof (struct GNUNET_ShortHashCode)))
773 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
774 return GNUNET_SYSERR;
780 if (map->size / 3 >= map->map_length / 4)
783 i = idx_of (map, key);
785 if (map->use_small_entries)
787 struct SmallMapEntry *sme;
789 sme = GNUNET_new (struct SmallMapEntry);
792 sme->next = map->map[i].sme;
793 map->map[i].sme = sme;
797 struct BigMapEntry *bme;
799 bme = GNUNET_new (struct BigMapEntry);
802 bme->next = map->map[i].bme;
803 map->map[i].bme = bme;
811 * Iterate over all entries in the map that match a particular key.
814 * @param key key that the entries must correspond to
815 * @param it function to call on each entry
816 * @param it_cls extra argument to @a it
817 * @return the number of key value pairs processed,
818 * #GNUNET_SYSERR if it aborted iteration
821 GNUNET_CONTAINER_multishortmap_get_multiple (struct GNUNET_CONTAINER_MultiShortmap *map,
822 const struct GNUNET_ShortHashCode *key,
823 GNUNET_CONTAINER_ShortmapIterator it,
830 ce = &map->next_cache[map->next_cache_off];
831 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
833 me = map->map[idx_of (map, key)];
834 if (map->use_small_entries)
836 struct SmallMapEntry *sme;
839 while (NULL != (sme = ce->sme))
842 if (0 != memcmp (key,
844 sizeof (struct GNUNET_ShortHashCode)))
847 (GNUNET_OK != it (it_cls,
851 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
852 return GNUNET_SYSERR;
859 struct BigMapEntry *bme;
862 while (NULL != (bme = ce->bme))
865 if (0 != memcmp (key,
867 sizeof (struct GNUNET_ShortHashCode)))
870 (GNUNET_OK != it (it_cls,
874 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
875 return GNUNET_SYSERR;
880 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
887 * Call @a it on a random value from the map, or not at all
888 * if the map is empty. Note that this function has linear
889 * complexity (in the size of the map).
892 * @param it function to call on a random entry
893 * @param it_cls extra argument to @a it
894 * @return the number of key value pairs processed, zero or one.
897 GNUNET_CONTAINER_multishortmap_get_random (const struct GNUNET_CONTAINER_MultiShortmap *map,
898 GNUNET_CONTAINER_ShortmapIterator it,
908 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
910 for (unsigned int idx = 0; idx < map->map_length; idx++)
913 if (map->use_small_entries)
915 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
919 if (GNUNET_OK != it (it_cls,
922 return GNUNET_SYSERR;
930 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
934 if (GNUNET_OK != it (it_cls,
935 &bme->key, bme->value))
936 return GNUNET_SYSERR;
944 return GNUNET_SYSERR;
949 * Create an iterator for a multishortmap.
950 * The iterator can be used to retrieve all the elements in the multishortmap
951 * one by one, without having to handle all elements at once (in contrast to
952 * #GNUNET_CONTAINER_multishortmap_iterate). Note that the iterator can not be
953 * used anymore if elements have been removed from 'map' after the creation of
954 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
955 * result in skipped or repeated elements.
957 * @param map the map to create an iterator for
958 * @return an iterator over the given multishortmap 'map'
960 struct GNUNET_CONTAINER_MultiShortmapIterator *
961 GNUNET_CONTAINER_multishortmap_iterator_create (const struct GNUNET_CONTAINER_MultiShortmap *map)
963 struct GNUNET_CONTAINER_MultiShortmapIterator *iter;
965 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiShortmapIterator);
967 iter->modification_counter = map->modification_counter;
968 iter->me = map->map[0];
974 * Retrieve the next element from the hash map at the iterator's position.
975 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
977 * This operation is only allowed if no elements have been removed from the
978 * multishortmap since the creation of 'iter', and the map has not been destroyed.
979 * Adding elements may result in repeating or skipping elements.
981 * @param iter the iterator to get the next element from
982 * @param key pointer to store the key in, can be NULL
983 * @param value pointer to store the value in, can be NULL
984 * @return #GNUNET_YES we returned an element,
985 * #GNUNET_NO if we are out of elements
988 GNUNET_CONTAINER_multishortmap_iterator_next (struct GNUNET_CONTAINER_MultiShortmapIterator *iter,
989 struct GNUNET_ShortHashCode *key,
992 /* make sure the map has not been modified */
993 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
995 /* look for the next entry, skipping empty buckets */
998 if (iter->idx >= iter->map->map_length)
1000 if (GNUNET_YES == iter->map->use_small_entries)
1002 if (NULL != iter->me.sme)
1005 *key = *iter->me.sme->key;
1007 *value = iter->me.sme->value;
1008 iter->me.sme = iter->me.sme->next;
1014 if (NULL != iter->me.bme)
1017 *key = iter->me.bme->key;
1019 *value = iter->me.bme->value;
1020 iter->me.bme = iter->me.bme->next;
1025 if (iter->idx < iter->map->map_length)
1026 iter->me = iter->map->map[iter->idx];
1032 * Destroy a multishortmap iterator.
1034 * @param iter the iterator to destroy
1037 GNUNET_CONTAINER_multishortmap_iterator_destroy (struct GNUNET_CONTAINER_MultiShortmapIterator *iter)
1043 /* end of container_multishortmap.c */