diff options
Diffstat (limited to 'src/util/container_multiuuidmap.c')
-rw-r--r-- | src/util/container_multiuuidmap.c | 1013 |
1 files changed, 0 insertions, 1013 deletions
diff --git a/src/util/container_multiuuidmap.c b/src/util/container_multiuuidmap.c deleted file mode 100644 index f2ed304b3..000000000 --- a/src/util/container_multiuuidmap.c +++ /dev/null | |||
@@ -1,1013 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2008, 2012 GNUnet e.V. | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
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 | ||
24 | */ | ||
25 | |||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | |||
29 | #define LOG(kind, ...) \ | ||
30 | GNUNET_log_from (kind, "util-container-multiuuidmap", __VA_ARGS__) | ||
31 | |||
32 | /** | ||
33 | * Maximum recursion depth for callbacks of | ||
34 | * #GNUNET_CONTAINER_multihashmap_get_multiple() themselves s | ||
35 | * again calling #GNUNET_CONTAINER_multihashmap_get_multiple(). | ||
36 | * Should be totally excessive, but if violated we die. | ||
37 | */ | ||
38 | #define NEXT_CACHE_SIZE 16 | ||
39 | |||
40 | |||
41 | /** | ||
42 | * An entry in the hash map with the full key. | ||
43 | */ | ||
44 | struct BigMapEntry | ||
45 | { | ||
46 | /** | ||
47 | * Value of the entry. | ||
48 | */ | ||
49 | void *value; | ||
50 | |||
51 | /** | ||
52 | * If there is a hash collision, we create a linked list. | ||
53 | */ | ||
54 | struct BigMapEntry *next; | ||
55 | |||
56 | /** | ||
57 | * Key for the entry. | ||
58 | */ | ||
59 | struct GNUNET_Uuid key; | ||
60 | }; | ||
61 | |||
62 | |||
63 | /** | ||
64 | * An entry in the hash map with just a pointer to the key. | ||
65 | */ | ||
66 | struct SmallMapEntry | ||
67 | { | ||
68 | /** | ||
69 | * Value of the entry. | ||
70 | */ | ||
71 | void *value; | ||
72 | |||
73 | /** | ||
74 | * If there is a hash collision, we create a linked list. | ||
75 | */ | ||
76 | struct SmallMapEntry *next; | ||
77 | |||
78 | /** | ||
79 | * Key for the entry. | ||
80 | */ | ||
81 | const struct GNUNET_Uuid *key; | ||
82 | }; | ||
83 | |||
84 | |||
85 | /** | ||
86 | * Entry in the map. | ||
87 | */ | ||
88 | union MapEntry | ||
89 | { | ||
90 | /** | ||
91 | * Variant used if map entries only contain a pointer to the key. | ||
92 | */ | ||
93 | struct SmallMapEntry *sme; | ||
94 | |||
95 | /** | ||
96 | * Variant used if map entries contain the full key. | ||
97 | */ | ||
98 | struct BigMapEntry *bme; | ||
99 | }; | ||
100 | |||
101 | |||
102 | /** | ||
103 | * Internal representation of the hash map. | ||
104 | */ | ||
105 | struct GNUNET_CONTAINER_MultiUuidmap | ||
106 | { | ||
107 | /** | ||
108 | * All of our buckets. | ||
109 | */ | ||
110 | union MapEntry *map; | ||
111 | |||
112 | /** | ||
113 | * Number of entries in the map. | ||
114 | */ | ||
115 | unsigned int size; | ||
116 | |||
117 | /** | ||
118 | * Length of the "map" array. | ||
119 | */ | ||
120 | unsigned int map_length; | ||
121 | |||
122 | /** | ||
123 | * #GNUNET_NO if the map entries are of type 'struct BigMapEntry', | ||
124 | * #GNUNET_YES if the map entries are of type 'struct SmallMapEntry'. | ||
125 | */ | ||
126 | int use_small_entries; | ||
127 | |||
128 | /** | ||
129 | * Counts the destructive modifications (grow, remove) | ||
130 | * to the map, so that iterators can check if they are still valid. | ||
131 | */ | ||
132 | unsigned int modification_counter; | ||
133 | |||
134 | /** | ||
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. | ||
138 | */ | ||
139 | union MapEntry next_cache[NEXT_CACHE_SIZE]; | ||
140 | |||
141 | /** | ||
142 | * Offset of @e next_cache entries in use, must be smaller | ||
143 | * than #NEXT_CACHE_SIZE. | ||
144 | */ | ||
145 | unsigned int next_cache_off; | ||
146 | }; | ||
147 | |||
148 | |||
149 | /** | ||
150 | * Cursor into a multiuuidmap. | ||
151 | * Allows to enumerate elements asynchronously. | ||
152 | */ | ||
153 | struct GNUNET_CONTAINER_MultiUuidmapIterator | ||
154 | { | ||
155 | /** | ||
156 | * Position in the bucket 'idx' | ||
157 | */ | ||
158 | union MapEntry me; | ||
159 | |||
160 | /** | ||
161 | * Current bucket index. | ||
162 | */ | ||
163 | unsigned int idx; | ||
164 | |||
165 | /** | ||
166 | * Modification counter as observed on the map when the iterator | ||
167 | * was created. | ||
168 | */ | ||
169 | unsigned int modification_counter; | ||
170 | |||
171 | /** | ||
172 | * Map that we are iterating over. | ||
173 | */ | ||
174 | const struct GNUNET_CONTAINER_MultiUuidmap *map; | ||
175 | }; | ||
176 | |||
177 | |||
178 | /** | ||
179 | * Create a multi hash map. | ||
180 | * | ||
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 recipe 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 | ||
192 | */ | ||
193 | struct GNUNET_CONTAINER_MultiUuidmap * | ||
194 | GNUNET_CONTAINER_multiuuidmap_create (unsigned int len, int do_not_copy_keys) | ||
195 | { | ||
196 | struct GNUNET_CONTAINER_MultiUuidmap *map; | ||
197 | |||
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) | ||
202 | { | ||
203 | GNUNET_free (map); | ||
204 | return NULL; | ||
205 | } | ||
206 | map->map_length = len; | ||
207 | map->use_small_entries = do_not_copy_keys; | ||
208 | return map; | ||
209 | } | ||
210 | |||
211 | |||
212 | /** | ||
213 | * Destroy a hash map. Will not free any values | ||
214 | * stored in the hash map! | ||
215 | * | ||
216 | * @param map the map | ||
217 | */ | ||
218 | void | ||
219 | GNUNET_CONTAINER_multiuuidmap_destroy ( | ||
220 | struct GNUNET_CONTAINER_MultiUuidmap *map) | ||
221 | { | ||
222 | GNUNET_assert (0 == map->next_cache_off); | ||
223 | for (unsigned int i = 0; i < map->map_length; i++) | ||
224 | { | ||
225 | union MapEntry me; | ||
226 | |||
227 | me = map->map[i]; | ||
228 | if (map->use_small_entries) | ||
229 | { | ||
230 | struct SmallMapEntry *sme; | ||
231 | struct SmallMapEntry *nxt; | ||
232 | |||
233 | nxt = me.sme; | ||
234 | while (NULL != (sme = nxt)) | ||
235 | { | ||
236 | nxt = sme->next; | ||
237 | GNUNET_free (sme); | ||
238 | } | ||
239 | me.sme = NULL; | ||
240 | } | ||
241 | else | ||
242 | { | ||
243 | struct BigMapEntry *bme; | ||
244 | struct BigMapEntry *nxt; | ||
245 | |||
246 | nxt = me.bme; | ||
247 | while (NULL != (bme = nxt)) | ||
248 | { | ||
249 | nxt = bme->next; | ||
250 | GNUNET_free (bme); | ||
251 | } | ||
252 | me.bme = NULL; | ||
253 | } | ||
254 | } | ||
255 | GNUNET_free (map->map); | ||
256 | GNUNET_free (map); | ||
257 | } | ||
258 | |||
259 | |||
260 | /** | ||
261 | * Compute the index of the bucket for the given key. | ||
262 | * | ||
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" | ||
266 | */ | ||
267 | static unsigned int | ||
268 | idx_of (const struct GNUNET_CONTAINER_MultiUuidmap *map, | ||
269 | const struct GNUNET_Uuid *key) | ||
270 | { | ||
271 | unsigned int kx; | ||
272 | |||
273 | GNUNET_assert (NULL != map); | ||
274 | GNUNET_memcpy (&kx, key, sizeof(kx)); | ||
275 | return kx % map->map_length; | ||
276 | } | ||
277 | |||
278 | |||
279 | /** | ||
280 | * Get the number of key-value pairs in the map. | ||
281 | * | ||
282 | * @param map the map | ||
283 | * @return the number of key value pairs | ||
284 | */ | ||
285 | unsigned int | ||
286 | GNUNET_CONTAINER_multiuuidmap_size ( | ||
287 | const struct GNUNET_CONTAINER_MultiUuidmap *map) | ||
288 | { | ||
289 | return map->size; | ||
290 | } | ||
291 | |||
292 | |||
293 | /** | ||
294 | * Given a key find a value in the map matching the key. | ||
295 | * | ||
296 | * @param map the map | ||
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 | ||
302 | */ | ||
303 | void * | ||
304 | GNUNET_CONTAINER_multiuuidmap_get ( | ||
305 | const struct GNUNET_CONTAINER_MultiUuidmap *map, | ||
306 | const struct GNUNET_Uuid *key) | ||
307 | { | ||
308 | union MapEntry me; | ||
309 | |||
310 | me = map->map[idx_of (map, key)]; | ||
311 | if (map->use_small_entries) | ||
312 | { | ||
313 | for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) | ||
314 | if (0 == GNUNET_memcmp (key, sme->key)) | ||
315 | return sme->value; | ||
316 | } | ||
317 | else | ||
318 | { | ||
319 | for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) | ||
320 | if (0 == GNUNET_memcmp (key, &bme->key)) | ||
321 | return bme->value; | ||
322 | } | ||
323 | return NULL; | ||
324 | } | ||
325 | |||
326 | |||
327 | /** | ||
328 | * Iterate over all entries in the map. | ||
329 | * | ||
330 | * @param map 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 | ||
335 | */ | ||
336 | int | ||
337 | GNUNET_CONTAINER_multiuuidmap_iterate ( | ||
338 | struct GNUNET_CONTAINER_MultiUuidmap *map, | ||
339 | GNUNET_CONTAINER_MultiUuidmapIteratorCallback it, | ||
340 | void *it_cls) | ||
341 | { | ||
342 | int count; | ||
343 | union MapEntry me; | ||
344 | union MapEntry *ce; | ||
345 | struct GNUNET_Uuid kc; | ||
346 | |||
347 | count = 0; | ||
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++) | ||
352 | { | ||
353 | me = map->map[i]; | ||
354 | if (map->use_small_entries) | ||
355 | { | ||
356 | struct SmallMapEntry *sme; | ||
357 | |||
358 | ce->sme = me.sme; | ||
359 | while (NULL != (sme = ce->sme)) | ||
360 | { | ||
361 | ce->sme = sme->next; | ||
362 | if ((NULL != it) && (GNUNET_OK != it (it_cls, sme->key, sme->value))) | ||
363 | { | ||
364 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); | ||
365 | return GNUNET_SYSERR; | ||
366 | } | ||
367 | count++; | ||
368 | } | ||
369 | } | ||
370 | else | ||
371 | { | ||
372 | struct BigMapEntry *bme; | ||
373 | |||
374 | ce->bme = me.bme; | ||
375 | while (NULL != (bme = ce->bme)) | ||
376 | { | ||
377 | ce->bme = bme->next; | ||
378 | if (NULL != it) | ||
379 | { | ||
380 | kc = bme->key; | ||
381 | if (GNUNET_OK != it (it_cls, &kc, bme->value)) | ||
382 | { | ||
383 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); | ||
384 | return GNUNET_SYSERR; | ||
385 | } | ||
386 | } | ||
387 | count++; | ||
388 | } | ||
389 | } | ||
390 | } | ||
391 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); | ||
392 | return count; | ||
393 | } | ||
394 | |||
395 | |||
396 | /** | ||
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. | ||
399 | * | ||
400 | * @param map the map to check | ||
401 | * @param bme the entry that is about to be free'd | ||
402 | */ | ||
403 | static void | ||
404 | update_next_cache_bme (struct GNUNET_CONTAINER_MultiUuidmap *map, | ||
405 | const struct BigMapEntry *bme) | ||
406 | { | ||
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; | ||
410 | } | ||
411 | |||
412 | |||
413 | /** | ||
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. | ||
416 | * | ||
417 | * @param map the map to check | ||
418 | * @param sme the entry that is about to be free'd | ||
419 | */ | ||
420 | static void | ||
421 | update_next_cache_sme (struct GNUNET_CONTAINER_MultiUuidmap *map, | ||
422 | const struct SmallMapEntry *sme) | ||
423 | { | ||
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; | ||
427 | } | ||
428 | |||
429 | |||
430 | /** | ||
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 | ||
433 | * will be removed. | ||
434 | * | ||
435 | * @param map the map | ||
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 | ||
439 | * is not in the map | ||
440 | */ | ||
441 | int | ||
442 | GNUNET_CONTAINER_multiuuidmap_remove (struct GNUNET_CONTAINER_MultiUuidmap *map, | ||
443 | const struct GNUNET_Uuid *key, | ||
444 | const void *value) | ||
445 | { | ||
446 | union MapEntry me; | ||
447 | unsigned int i; | ||
448 | |||
449 | map->modification_counter++; | ||
450 | i = idx_of (map, key); | ||
451 | me = map->map[i]; | ||
452 | if (map->use_small_entries) | ||
453 | { | ||
454 | struct SmallMapEntry *p = NULL; | ||
455 | |||
456 | for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) | ||
457 | { | ||
458 | if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value)) | ||
459 | { | ||
460 | if (NULL == p) | ||
461 | map->map[i].sme = sme->next; | ||
462 | else | ||
463 | p->next = sme->next; | ||
464 | update_next_cache_sme (map, sme); | ||
465 | GNUNET_free (sme); | ||
466 | map->size--; | ||
467 | return GNUNET_YES; | ||
468 | } | ||
469 | p = sme; | ||
470 | } | ||
471 | } | ||
472 | else | ||
473 | { | ||
474 | struct BigMapEntry *p = NULL; | ||
475 | |||
476 | for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) | ||
477 | { | ||
478 | if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value)) | ||
479 | { | ||
480 | if (NULL == p) | ||
481 | map->map[i].bme = bme->next; | ||
482 | else | ||
483 | p->next = bme->next; | ||
484 | update_next_cache_bme (map, bme); | ||
485 | GNUNET_free (bme); | ||
486 | map->size--; | ||
487 | return GNUNET_YES; | ||
488 | } | ||
489 | p = bme; | ||
490 | } | ||
491 | } | ||
492 | return GNUNET_NO; | ||
493 | } | ||
494 | |||
495 | |||
496 | /** | ||
497 | * Remove all entries for the given key from the map. | ||
498 | * Note that the values would not be "freed". | ||
499 | * | ||
500 | * @param map the map | ||
501 | * @param key identifies values to be removed | ||
502 | * @return number of values removed | ||
503 | */ | ||
504 | int | ||
505 | GNUNET_CONTAINER_multiuuidmap_remove_all ( | ||
506 | struct GNUNET_CONTAINER_MultiUuidmap *map, | ||
507 | const struct GNUNET_Uuid *key) | ||
508 | { | ||
509 | union MapEntry me; | ||
510 | unsigned int i; | ||
511 | int ret; | ||
512 | |||
513 | map->modification_counter++; | ||
514 | |||
515 | ret = 0; | ||
516 | i = idx_of (map, key); | ||
517 | me = map->map[i]; | ||
518 | if (map->use_small_entries) | ||
519 | { | ||
520 | struct SmallMapEntry *sme; | ||
521 | struct SmallMapEntry *p; | ||
522 | |||
523 | p = NULL; | ||
524 | sme = me.sme; | ||
525 | while (NULL != sme) | ||
526 | { | ||
527 | if (0 == GNUNET_memcmp (key, sme->key)) | ||
528 | { | ||
529 | if (NULL == p) | ||
530 | map->map[i].sme = sme->next; | ||
531 | else | ||
532 | p->next = sme->next; | ||
533 | update_next_cache_sme (map, sme); | ||
534 | GNUNET_free (sme); | ||
535 | map->size--; | ||
536 | if (NULL == p) | ||
537 | sme = map->map[i].sme; | ||
538 | else | ||
539 | sme = p->next; | ||
540 | ret++; | ||
541 | } | ||
542 | else | ||
543 | { | ||
544 | p = sme; | ||
545 | sme = sme->next; | ||
546 | } | ||
547 | } | ||
548 | } | ||
549 | else | ||
550 | { | ||
551 | struct BigMapEntry *bme; | ||
552 | struct BigMapEntry *p; | ||
553 | |||
554 | p = NULL; | ||
555 | bme = me.bme; | ||
556 | while (NULL != bme) | ||
557 | { | ||
558 | if (0 == GNUNET_memcmp (key, &bme->key)) | ||
559 | { | ||
560 | if (NULL == p) | ||
561 | map->map[i].bme = bme->next; | ||
562 | else | ||
563 | p->next = bme->next; | ||
564 | update_next_cache_bme (map, bme); | ||
565 | GNUNET_free (bme); | ||
566 | map->size--; | ||
567 | if (NULL == p) | ||
568 | bme = map->map[i].bme; | ||
569 | else | ||
570 | bme = p->next; | ||
571 | ret++; | ||
572 | } | ||
573 | else | ||
574 | { | ||
575 | p = bme; | ||
576 | bme = bme->next; | ||
577 | } | ||
578 | } | ||
579 | } | ||
580 | return ret; | ||
581 | } | ||
582 | |||
583 | |||
584 | /** | ||
585 | * Check if the map contains any value under the given | ||
586 | * key (including values that are NULL). | ||
587 | * | ||
588 | * @param map the map | ||
589 | * @param key the key to test if a value exists for it | ||
590 | * @return #GNUNET_YES if such a value exists, | ||
591 | * #GNUNET_NO if not | ||
592 | */ | ||
593 | int | ||
594 | GNUNET_CONTAINER_multiuuidmap_contains ( | ||
595 | const struct GNUNET_CONTAINER_MultiUuidmap *map, | ||
596 | const struct GNUNET_Uuid *key) | ||
597 | { | ||
598 | union MapEntry me; | ||
599 | |||
600 | me = map->map[idx_of (map, key)]; | ||
601 | if (map->use_small_entries) | ||
602 | { | ||
603 | for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) | ||
604 | if (0 == GNUNET_memcmp (key, sme->key)) | ||
605 | return GNUNET_YES; | ||
606 | } | ||
607 | else | ||
608 | { | ||
609 | for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) | ||
610 | if (0 == GNUNET_memcmp (key, &bme->key)) | ||
611 | return GNUNET_YES; | ||
612 | } | ||
613 | return GNUNET_NO; | ||
614 | } | ||
615 | |||
616 | |||
617 | /** | ||
618 | * Check if the map contains the given value under the given | ||
619 | * key. | ||
620 | * | ||
621 | * @param map the map | ||
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, | ||
625 | * #GNUNET_NO if not | ||
626 | */ | ||
627 | int | ||
628 | GNUNET_CONTAINER_multiuuidmap_contains_value ( | ||
629 | const struct GNUNET_CONTAINER_MultiUuidmap *map, | ||
630 | const struct GNUNET_Uuid *key, | ||
631 | const void *value) | ||
632 | { | ||
633 | union MapEntry me; | ||
634 | |||
635 | me = map->map[idx_of (map, key)]; | ||
636 | if (map->use_small_entries) | ||
637 | { | ||
638 | for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) | ||
639 | if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value)) | ||
640 | return GNUNET_YES; | ||
641 | } | ||
642 | else | ||
643 | { | ||
644 | for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) | ||
645 | if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value)) | ||
646 | return GNUNET_YES; | ||
647 | } | ||
648 | return GNUNET_NO; | ||
649 | } | ||
650 | |||
651 | |||
652 | /** | ||
653 | * Grow the given map to a more appropriate size. | ||
654 | * | ||
655 | * @param map the hash map to grow | ||
656 | */ | ||
657 | static void | ||
658 | grow (struct GNUNET_CONTAINER_MultiUuidmap *map) | ||
659 | { | ||
660 | union MapEntry *old_map; | ||
661 | union MapEntry *new_map; | ||
662 | unsigned int old_len; | ||
663 | unsigned int new_len; | ||
664 | unsigned int idx; | ||
665 | |||
666 | old_map = map->map; | ||
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)); | ||
674 | if (NULL == new_map) | ||
675 | return; /* grow not possible */ | ||
676 | map->modification_counter++; | ||
677 | map->map_length = new_len; | ||
678 | map->map = new_map; | ||
679 | for (unsigned int i = 0; i < old_len; i++) | ||
680 | { | ||
681 | if (map->use_small_entries) | ||
682 | { | ||
683 | struct SmallMapEntry *sme; | ||
684 | |||
685 | while (NULL != (sme = old_map[i].sme)) | ||
686 | { | ||
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; | ||
691 | } | ||
692 | } | ||
693 | else | ||
694 | { | ||
695 | struct BigMapEntry *bme; | ||
696 | |||
697 | while (NULL != (bme = old_map[i].bme)) | ||
698 | { | ||
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; | ||
703 | } | ||
704 | } | ||
705 | } | ||
706 | GNUNET_free (old_map); | ||
707 | } | ||
708 | |||
709 | |||
710 | /** | ||
711 | * Store a key-value pair in the map. | ||
712 | * | ||
713 | * @param map 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 | ||
721 | */ | ||
722 | int | ||
723 | GNUNET_CONTAINER_multiuuidmap_put (struct GNUNET_CONTAINER_MultiUuidmap *map, | ||
724 | const struct GNUNET_Uuid *key, | ||
725 | void *value, | ||
726 | enum GNUNET_CONTAINER_MultiHashMapOption opt) | ||
727 | { | ||
728 | union MapEntry me; | ||
729 | unsigned int i; | ||
730 | |||
731 | i = idx_of (map, key); | ||
732 | if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) && | ||
733 | (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) | ||
734 | { | ||
735 | me = map->map[i]; | ||
736 | if (map->use_small_entries) | ||
737 | { | ||
738 | for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) | ||
739 | if (0 == GNUNET_memcmp (key, sme->key)) | ||
740 | { | ||
741 | if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) | ||
742 | return GNUNET_SYSERR; | ||
743 | sme->value = value; | ||
744 | return GNUNET_NO; | ||
745 | } | ||
746 | } | ||
747 | else | ||
748 | { | ||
749 | for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) | ||
750 | if (0 == GNUNET_memcmp (key, &bme->key)) | ||
751 | { | ||
752 | if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) | ||
753 | return GNUNET_SYSERR; | ||
754 | bme->value = value; | ||
755 | return GNUNET_NO; | ||
756 | } | ||
757 | } | ||
758 | } | ||
759 | if (map->size / 3 >= map->map_length / 4) | ||
760 | { | ||
761 | grow (map); | ||
762 | i = idx_of (map, key); | ||
763 | } | ||
764 | if (map->use_small_entries) | ||
765 | { | ||
766 | struct SmallMapEntry *sme; | ||
767 | |||
768 | sme = GNUNET_new (struct SmallMapEntry); | ||
769 | sme->key = key; | ||
770 | sme->value = value; | ||
771 | sme->next = map->map[i].sme; | ||
772 | map->map[i].sme = sme; | ||
773 | } | ||
774 | else | ||
775 | { | ||
776 | struct BigMapEntry *bme; | ||
777 | |||
778 | bme = GNUNET_new (struct BigMapEntry); | ||
779 | bme->key = *key; | ||
780 | bme->value = value; | ||
781 | bme->next = map->map[i].bme; | ||
782 | map->map[i].bme = bme; | ||
783 | } | ||
784 | map->size++; | ||
785 | return GNUNET_OK; | ||
786 | } | ||
787 | |||
788 | |||
789 | /** | ||
790 | * Iterate over all entries in the map that match a particular key. | ||
791 | * | ||
792 | * @param map the map | ||
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 | ||
798 | */ | ||
799 | int | ||
800 | GNUNET_CONTAINER_multiuuidmap_get_multiple ( | ||
801 | struct GNUNET_CONTAINER_MultiUuidmap *map, | ||
802 | const struct GNUNET_Uuid *key, | ||
803 | GNUNET_CONTAINER_MultiUuidmapIteratorCallback it, | ||
804 | void *it_cls) | ||
805 | { | ||
806 | int count; | ||
807 | union MapEntry me; | ||
808 | union MapEntry *ce; | ||
809 | |||
810 | ce = &map->next_cache[map->next_cache_off]; | ||
811 | GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE); | ||
812 | count = 0; | ||
813 | me = map->map[idx_of (map, key)]; | ||
814 | if (map->use_small_entries) | ||
815 | { | ||
816 | struct SmallMapEntry *sme; | ||
817 | |||
818 | ce->sme = me.sme; | ||
819 | while (NULL != (sme = ce->sme)) | ||
820 | { | ||
821 | ce->sme = sme->next; | ||
822 | if (0 != GNUNET_memcmp (key, sme->key)) | ||
823 | continue; | ||
824 | if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value))) | ||
825 | { | ||
826 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); | ||
827 | return GNUNET_SYSERR; | ||
828 | } | ||
829 | count++; | ||
830 | } | ||
831 | } | ||
832 | else | ||
833 | { | ||
834 | struct BigMapEntry *bme; | ||
835 | |||
836 | ce->bme = me.bme; | ||
837 | while (NULL != (bme = ce->bme)) | ||
838 | { | ||
839 | ce->bme = bme->next; | ||
840 | if (0 != GNUNET_memcmp (key, &bme->key)) | ||
841 | continue; | ||
842 | if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value))) | ||
843 | { | ||
844 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); | ||
845 | return GNUNET_SYSERR; | ||
846 | } | ||
847 | count++; | ||
848 | } | ||
849 | } | ||
850 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); | ||
851 | return count; | ||
852 | } | ||
853 | |||
854 | |||
855 | /** | ||
856 | * @ingroup hashmap | ||
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). | ||
860 | * | ||
861 | * @param map 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. | ||
865 | */ | ||
866 | unsigned int | ||
867 | GNUNET_CONTAINER_multiuuidmap_get_random ( | ||
868 | const struct GNUNET_CONTAINER_MultiUuidmap *map, | ||
869 | GNUNET_CONTAINER_MultiUuidmapIteratorCallback it, | ||
870 | void *it_cls) | ||
871 | { | ||
872 | unsigned int off; | ||
873 | union MapEntry me; | ||
874 | |||
875 | if (0 == map->size) | ||
876 | return 0; | ||
877 | if (NULL == it) | ||
878 | return 1; | ||
879 | off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size); | ||
880 | for (unsigned int idx = 0; idx < map->map_length; idx++) | ||
881 | { | ||
882 | me = map->map[idx]; | ||
883 | if (map->use_small_entries) | ||
884 | { | ||
885 | for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) | ||
886 | { | ||
887 | if (0 == off) | ||
888 | { | ||
889 | if (GNUNET_OK != it (it_cls, sme->key, sme->value)) | ||
890 | return GNUNET_SYSERR; | ||
891 | return 1; | ||
892 | } | ||
893 | off--; | ||
894 | } | ||
895 | } | ||
896 | else | ||
897 | { | ||
898 | for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) | ||
899 | { | ||
900 | if (0 == off) | ||
901 | { | ||
902 | if (GNUNET_OK != it (it_cls, &bme->key, bme->value)) | ||
903 | return GNUNET_SYSERR; | ||
904 | return 1; | ||
905 | } | ||
906 | off--; | ||
907 | } | ||
908 | } | ||
909 | } | ||
910 | GNUNET_break (0); | ||
911 | return GNUNET_SYSERR; | ||
912 | } | ||
913 | |||
914 | |||
915 | /** | ||
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. | ||
923 | * | ||
924 | * @param map the map to create an iterator for | ||
925 | * @return an iterator over the given multiuuidmap 'map' | ||
926 | */ | ||
927 | struct GNUNET_CONTAINER_MultiUuidmapIterator * | ||
928 | GNUNET_CONTAINER_multiuuidmap_iterator_create ( | ||
929 | const struct GNUNET_CONTAINER_MultiUuidmap *map) | ||
930 | { | ||
931 | struct GNUNET_CONTAINER_MultiUuidmapIterator *iter; | ||
932 | |||
933 | iter = GNUNET_new (struct GNUNET_CONTAINER_MultiUuidmapIterator); | ||
934 | iter->map = map; | ||
935 | iter->modification_counter = map->modification_counter; | ||
936 | iter->me = map->map[0]; | ||
937 | return iter; | ||
938 | } | ||
939 | |||
940 | |||
941 | /** | ||
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' | ||
944 | * are not modified. | ||
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. | ||
948 | * | ||
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 | ||
954 | */ | ||
955 | int | ||
956 | GNUNET_CONTAINER_multiuuidmap_iterator_next ( | ||
957 | struct GNUNET_CONTAINER_MultiUuidmapIterator *iter, | ||
958 | struct GNUNET_Uuid *key, | ||
959 | const void **value) | ||
960 | { | ||
961 | /* make sure the map has not been modified */ | ||
962 | GNUNET_assert (iter->modification_counter == iter->map->modification_counter); | ||
963 | |||
964 | /* look for the next entry, skipping empty buckets */ | ||
965 | while (1) | ||
966 | { | ||
967 | if (iter->idx >= iter->map->map_length) | ||
968 | return GNUNET_NO; | ||
969 | if (GNUNET_YES == iter->map->use_small_entries) | ||
970 | { | ||
971 | if (NULL != iter->me.sme) | ||
972 | { | ||
973 | if (NULL != key) | ||
974 | *key = *iter->me.sme->key; | ||
975 | if (NULL != value) | ||
976 | *value = iter->me.sme->value; | ||
977 | iter->me.sme = iter->me.sme->next; | ||
978 | return GNUNET_YES; | ||
979 | } | ||
980 | } | ||
981 | else | ||
982 | { | ||
983 | if (NULL != iter->me.bme) | ||
984 | { | ||
985 | if (NULL != key) | ||
986 | *key = iter->me.bme->key; | ||
987 | if (NULL != value) | ||
988 | *value = iter->me.bme->value; | ||
989 | iter->me.bme = iter->me.bme->next; | ||
990 | return GNUNET_YES; | ||
991 | } | ||
992 | } | ||
993 | iter->idx += 1; | ||
994 | if (iter->idx < iter->map->map_length) | ||
995 | iter->me = iter->map->map[iter->idx]; | ||
996 | } | ||
997 | } | ||
998 | |||
999 | |||
1000 | /** | ||
1001 | * Destroy a multiuuidmap iterator. | ||
1002 | * | ||
1003 | * @param iter the iterator to destroy | ||
1004 | */ | ||
1005 | void | ||
1006 | GNUNET_CONTAINER_multiuuidmap_iterator_destroy ( | ||
1007 | struct GNUNET_CONTAINER_MultiUuidmapIterator *iter) | ||
1008 | { | ||
1009 | GNUNET_free (iter); | ||
1010 | } | ||
1011 | |||
1012 | |||
1013 | /* end of container_multiuuidmap.c */ | ||