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.
16 * @file util/container_multipeermap.c
17 * @brief hash map where the same key may be present multiple times
18 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
24 #define LOG(kind,...) GNUNET_log_from (kind, "util-container-multipeermap", __VA_ARGS__)
27 * An entry in the hash map with the full key.
38 * If there is a hash collision, we create a linked list.
40 struct BigMapEntry *next;
45 struct GNUNET_PeerIdentity key;
51 * An entry in the hash map with just a pointer to the key.
62 * If there is a hash collision, we create a linked list.
64 struct SmallMapEntry *next;
69 const struct GNUNET_PeerIdentity *key;
80 * Variant used if map entries only contain a pointer to the key.
82 struct SmallMapEntry *sme;
85 * Variant used if map entries contain the full key.
87 struct BigMapEntry *bme;
92 * Internal representation of the hash map.
94 struct GNUNET_CONTAINER_MultiPeerMap
102 * Number of entries in the map.
107 * Length of the "map" array.
109 unsigned int map_length;
112 * GNUNET_NO if the map entries are of type 'struct BigMapEntry',
113 * GNUNET_YES if the map entries are of type 'struct SmallMapEntry'.
115 int use_small_entries;
118 * Counts the destructive modifications (grow, remove)
119 * to the map, so that iterators can check if they are still valid.
121 unsigned int modification_counter;
126 * Cursor into a multipeermap.
127 * Allows to enumerate elements asynchronously.
129 struct GNUNET_CONTAINER_MultiPeerMapIterator
132 * Position in the bucket 'idx'
137 * Current bucket index.
142 * Modification counter as observed on the map when the iterator
145 unsigned int modification_counter;
148 * Map that we are iterating over.
150 const struct GNUNET_CONTAINER_MultiPeerMap *map;
155 * Create a multi hash map.
157 * @param len initial size (map will grow as needed)
158 * @param do_not_copy_keys GNUNET_NO is always safe and should be used by default;
159 * GNUNET_YES means that on 'put', the 'key' does not have
160 * to be copied as the destination of the pointer is
161 * guaranteed to be life as long as the value is stored in
162 * the hashmap. This can significantly reduce memory
163 * consumption, but of course is also a recipie for
164 * heap corruption if the assumption is not true. Only
165 * use this if (1) memory use is important in this case and
166 * (2) you have triple-checked that the invariant holds
167 * @return NULL on error
169 struct GNUNET_CONTAINER_MultiPeerMap *
170 GNUNET_CONTAINER_multipeermap_create (unsigned int len,
171 int do_not_copy_keys)
173 struct GNUNET_CONTAINER_MultiPeerMap *map;
175 GNUNET_assert (len > 0);
176 map = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMap);
177 map->map = GNUNET_malloc (len * sizeof (union MapEntry));
178 map->map_length = len;
179 map->use_small_entries = do_not_copy_keys;
185 * Destroy a hash map. Will not free any values
186 * stored in the hash map!
191 GNUNET_CONTAINER_multipeermap_destroy (struct GNUNET_CONTAINER_MultiPeerMap
197 for (i = 0; i < map->map_length; i++)
200 if (map->use_small_entries)
202 struct SmallMapEntry *sme;
203 struct SmallMapEntry *nxt;
206 while (NULL != (sme = nxt))
215 struct BigMapEntry *bme;
216 struct BigMapEntry *nxt;
219 while (NULL != (bme = nxt))
227 GNUNET_free (map->map);
233 * Compute the index of the bucket for the given key.
235 * @param map hash map for which to compute the index
236 * @param key what key should the index be computed for
237 * @return offset into the "map" array of "map"
240 idx_of (const struct GNUNET_CONTAINER_MultiPeerMap *map,
241 const struct GNUNET_PeerIdentity *key)
245 GNUNET_assert (NULL != map);
246 GNUNET_memcpy (&kx, key, sizeof (kx));
247 return kx % map->map_length;
252 * Get the number of key-value pairs in the map.
255 * @return the number of key value pairs
258 GNUNET_CONTAINER_multipeermap_size (const struct GNUNET_CONTAINER_MultiPeerMap
266 * Given a key find a value in the map matching the key.
269 * @param key what to look for
270 * @return NULL if no value was found; note that
271 * this is indistinguishable from values that just
272 * happen to be NULL; use "contains" to test for
273 * key-value pairs with value NULL
276 GNUNET_CONTAINER_multipeermap_get (const struct GNUNET_CONTAINER_MultiPeerMap
277 *map, const struct GNUNET_PeerIdentity *key)
281 me = map->map[idx_of (map, key)];
282 if (map->use_small_entries)
284 struct SmallMapEntry *sme;
286 for (sme = me.sme; NULL != sme; sme = sme->next)
287 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
292 struct BigMapEntry *bme;
294 for (bme = me.bme; NULL != bme; bme = bme->next)
295 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
303 * Iterate over all entries in the map.
306 * @param it function to call on each entry
307 * @param it_cls extra argument to @a it
308 * @return the number of key value pairs processed,
309 * #GNUNET_SYSERR if it aborted iteration
312 GNUNET_CONTAINER_multipeermap_iterate (const struct
313 GNUNET_CONTAINER_MultiPeerMap *map,
314 GNUNET_CONTAINER_PeerMapIterator it,
320 struct GNUNET_PeerIdentity kc;
323 GNUNET_assert (NULL != map);
324 for (i = 0; i < map->map_length; i++)
327 if (map->use_small_entries)
329 struct SmallMapEntry *sme;
330 struct SmallMapEntry *nxt;
333 while (NULL != (sme = nxt))
338 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
339 return GNUNET_SYSERR;
346 struct BigMapEntry *bme;
347 struct BigMapEntry *nxt;
350 while (NULL != (bme = nxt))
356 if (GNUNET_OK != it (it_cls, &kc, bme->value))
357 return GNUNET_SYSERR;
368 * Remove the given key-value pair from the map. Note that if the
369 * key-value pair is in the map multiple times, only one of the pairs
373 * @param key key of the key-value pair
374 * @param value value of the key-value pair
375 * @return GNUNET_YES on success, GNUNET_NO if the key-value pair
379 GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map,
380 const struct GNUNET_PeerIdentity *key,
386 map->modification_counter++;
388 i = idx_of (map, key);
390 if (map->use_small_entries)
392 struct SmallMapEntry *sme;
393 struct SmallMapEntry *p;
396 for (sme = me.sme; NULL != sme; sme = sme->next)
398 if ((0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity))) &&
399 (value == sme->value))
402 map->map[i].sme = sme->next;
414 struct BigMapEntry *bme;
415 struct BigMapEntry *p;
418 for (bme = me.bme; NULL != bme; bme = bme->next)
420 if ((0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity))) &&
421 (value == bme->value))
424 map->map[i].bme = bme->next;
439 * Remove all entries for the given key from the map.
440 * Note that the values would not be "freed".
443 * @param key identifies values to be removed
444 * @return number of values removed
447 GNUNET_CONTAINER_multipeermap_remove_all (struct GNUNET_CONTAINER_MultiPeerMap
448 *map, const struct GNUNET_PeerIdentity *key)
454 map->modification_counter++;
457 i = idx_of (map, key);
459 if (map->use_small_entries)
461 struct SmallMapEntry *sme;
462 struct SmallMapEntry *p;
468 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
471 map->map[i].sme = sme->next;
477 sme = map->map[i].sme;
491 struct BigMapEntry *bme;
492 struct BigMapEntry *p;
498 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
501 map->map[i].bme = bme->next;
507 bme = map->map[i].bme;
524 * Check if the map contains any value under the given
525 * key (including values that are NULL).
528 * @param key the key to test if a value exists for it
529 * @return GNUNET_YES if such a value exists,
533 GNUNET_CONTAINER_multipeermap_contains (const struct
534 GNUNET_CONTAINER_MultiPeerMap *map,
535 const struct GNUNET_PeerIdentity *key)
539 me = map->map[idx_of (map, key)];
540 if (map->use_small_entries)
542 struct SmallMapEntry *sme;
544 for (sme = me.sme; NULL != sme; sme = sme->next)
545 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
550 struct BigMapEntry *bme;
552 for (bme = me.bme; NULL != bme; bme = bme->next)
553 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
561 * Check if the map contains the given value under the given
565 * @param key the key to test if a value exists for it
566 * @param value value to test for
567 * @return GNUNET_YES if such a value exists,
571 GNUNET_CONTAINER_multipeermap_contains_value (const struct
572 GNUNET_CONTAINER_MultiPeerMap
573 *map, const struct GNUNET_PeerIdentity *key,
578 me = map->map[idx_of (map, key)];
579 if (map->use_small_entries)
581 struct SmallMapEntry *sme;
583 for (sme = me.sme; NULL != sme; sme = sme->next)
584 if ( (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity))) &&
585 (sme->value == value) )
590 struct BigMapEntry *bme;
592 for (bme = me.bme; NULL != bme; bme = bme->next)
593 if ( (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity))) &&
594 (bme->value == value) )
602 * Grow the given map to a more appropriate size.
604 * @param map the hash map to grow
607 grow (struct GNUNET_CONTAINER_MultiPeerMap *map)
609 union MapEntry *old_map;
610 union MapEntry *new_map;
611 unsigned int old_len;
612 unsigned int new_len;
616 map->modification_counter++;
619 old_len = map->map_length;
620 new_len = old_len * 2;
621 new_map = GNUNET_malloc (sizeof (union MapEntry) * new_len);
622 map->map_length = new_len;
624 for (i = 0; i < old_len; i++)
626 if (map->use_small_entries)
628 struct SmallMapEntry *sme;
630 while (NULL != (sme = old_map[i].sme))
632 old_map[i].sme = sme->next;
633 idx = idx_of (map, sme->key);
634 sme->next = new_map[idx].sme;
635 new_map[idx].sme = sme;
640 struct BigMapEntry *bme;
642 while (NULL != (bme = old_map[i].bme))
644 old_map[i].bme = bme->next;
645 idx = idx_of (map, &bme->key);
646 bme->next = new_map[idx].bme;
647 new_map[idx].bme = bme;
651 GNUNET_free (old_map);
656 * Store a key-value pair in the map.
659 * @param key key to use
660 * @param value value to use
661 * @param opt options for put
662 * @return #GNUNET_OK on success,
663 * #GNUNET_NO if a value was replaced (with REPLACE)
664 * #GNUNET_SYSERR if GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
665 * value already exists
668 GNUNET_CONTAINER_multipeermap_put (struct GNUNET_CONTAINER_MultiPeerMap *map,
669 const struct GNUNET_PeerIdentity *key,
671 enum GNUNET_CONTAINER_MultiHashMapOption opt)
676 i = idx_of (map, key);
677 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
678 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
681 if (map->use_small_entries)
683 struct SmallMapEntry *sme;
685 for (sme = me.sme; NULL != sme; sme = sme->next)
686 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
688 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
689 return GNUNET_SYSERR;
696 struct BigMapEntry *bme;
698 for (bme = me.bme; NULL != bme; bme = bme->next)
699 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
701 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
702 return GNUNET_SYSERR;
708 if (map->size / 3 >= map->map_length / 4)
711 i = idx_of (map, key);
713 if (map->use_small_entries)
715 struct SmallMapEntry *sme;
717 sme = GNUNET_new (struct SmallMapEntry);
720 sme->next = map->map[i].sme;
721 map->map[i].sme = sme;
725 struct BigMapEntry *bme;
727 bme = GNUNET_new (struct BigMapEntry);
730 bme->next = map->map[i].bme;
731 map->map[i].bme = bme;
739 * Iterate over all entries in the map that match a particular key.
742 * @param key key that the entries must correspond to
743 * @param it function to call on each entry
744 * @param it_cls extra argument to @a it
745 * @return the number of key value pairs processed,
746 * #GNUNET_SYSERR if it aborted iteration
749 GNUNET_CONTAINER_multipeermap_get_multiple (const struct GNUNET_CONTAINER_MultiPeerMap *map,
750 const struct GNUNET_PeerIdentity *key,
751 GNUNET_CONTAINER_PeerMapIterator it,
758 me = map->map[idx_of (map, key)];
759 if (map->use_small_entries)
761 struct SmallMapEntry *sme;
762 struct SmallMapEntry *nxt;
765 while (NULL != (sme = nxt))
768 if (0 != memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
770 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, sme->value)))
771 return GNUNET_SYSERR;
777 struct BigMapEntry *bme;
778 struct BigMapEntry *nxt;
781 while (NULL != (bme = nxt))
784 if (0 != memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
786 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, bme->value)))
787 return GNUNET_SYSERR;
797 * Call @a it on a random value from the map, or not at all
798 * if the map is empty. Note that this function has linear
799 * complexity (in the size of the map).
802 * @param it function to call on a random entry
803 * @param it_cls extra argument to @a it
804 * @return the number of key value pairs processed, zero or one.
807 GNUNET_CONTAINER_multipeermap_get_random (const struct GNUNET_CONTAINER_MultiPeerMap *map,
808 GNUNET_CONTAINER_PeerMapIterator it,
819 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
821 for (idx = 0; idx < map->map_length; idx++)
824 if (map->use_small_entries)
826 struct SmallMapEntry *sme;
827 struct SmallMapEntry *nxt;
830 while (NULL != (sme = nxt))
835 if (GNUNET_OK != it (it_cls,
838 return GNUNET_SYSERR;
846 struct BigMapEntry *bme;
847 struct BigMapEntry *nxt;
850 while (NULL != (bme = nxt))
855 if (GNUNET_OK != it (it_cls,
856 &bme->key, bme->value))
857 return GNUNET_SYSERR;
865 return GNUNET_SYSERR;
870 * Create an iterator for a multipeermap.
871 * The iterator can be used to retrieve all the elements in the multipeermap
872 * one by one, without having to handle all elements at once (in contrast to
873 * #GNUNET_CONTAINER_multipeermap_iterate). Note that the iterator can not be
874 * used anymore if elements have been removed from 'map' after the creation of
875 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
876 * result in skipped or repeated elements.
878 * @param map the map to create an iterator for
879 * @return an iterator over the given multipeermap 'map'
881 struct GNUNET_CONTAINER_MultiPeerMapIterator *
882 GNUNET_CONTAINER_multipeermap_iterator_create (const struct GNUNET_CONTAINER_MultiPeerMap *map)
884 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
886 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMapIterator);
888 iter->modification_counter = map->modification_counter;
889 iter->me = map->map[0];
895 * Retrieve the next element from the hash map at the iterator's position.
896 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
898 * This operation is only allowed if no elements have been removed from the
899 * multipeermap since the creation of 'iter', and the map has not been destroyed.
900 * Adding elements may result in repeating or skipping elements.
902 * @param iter the iterator to get the next element from
903 * @param key pointer to store the key in, can be NULL
904 * @param value pointer to store the value in, can be NULL
905 * @return #GNUNET_YES we returned an element,
906 * #GNUNET_NO if we are out of elements
909 GNUNET_CONTAINER_multipeermap_iterator_next (struct GNUNET_CONTAINER_MultiPeerMapIterator *iter,
910 struct GNUNET_PeerIdentity *key, const void **value)
912 /* make sure the map has not been modified */
913 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
915 /* look for the next entry, skipping empty buckets */
918 if (iter->idx >= iter->map->map_length)
920 if (GNUNET_YES == iter->map->use_small_entries)
922 if (NULL != iter->me.sme)
925 *key = *iter->me.sme->key;
927 *value = iter->me.sme->value;
928 iter->me.sme = iter->me.sme->next;
934 if (NULL != iter->me.bme)
937 *key = iter->me.bme->key;
939 *value = iter->me.bme->value;
940 iter->me.bme = iter->me.bme->next;
945 if (iter->idx < iter->map->map_length)
946 iter->me = iter->map->map[iter->idx];
952 * Destroy a multipeermap iterator.
954 * @param iter the iterator to destroy
957 GNUNET_CONTAINER_multipeermap_iterator_destroy (struct GNUNET_CONTAINER_MultiPeerMapIterator *iter)
963 /* end of container_multipeermap.c */