diff options
author | Christian Grothoff <christian@grothoff.org> | 2019-06-04 11:52:52 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2019-06-04 11:52:52 +0200 |
commit | 9ce956ea4c93f038995a21c6c1c0133eee6bff75 (patch) | |
tree | a77d69671a84ab7199bd3d6f8473011f13b87a49 /src/util/container_multihashmap.c | |
parent | ce4a47a9937aa30075b8cc8624bdb1332cd69a2d (diff) | |
download | gnunet-9ce956ea4c93f038995a21c6c1c0133eee6bff75.tar.gz gnunet-9ce956ea4c93f038995a21c6c1c0133eee6bff75.zip |
memcmp -> GNUNET_memcmp
Diffstat (limited to 'src/util/container_multihashmap.c')
-rw-r--r-- | src/util/container_multihashmap.c | 388 |
1 files changed, 174 insertions, 214 deletions
diff --git a/src/util/container_multihashmap.c b/src/util/container_multihashmap.c index 7605ea151..165f04149 100644 --- a/src/util/container_multihashmap.c +++ b/src/util/container_multihashmap.c | |||
@@ -26,7 +26,8 @@ | |||
26 | #include "platform.h" | 26 | #include "platform.h" |
27 | #include "gnunet_container_lib.h" | 27 | #include "gnunet_container_lib.h" |
28 | 28 | ||
29 | #define LOG(kind,...) GNUNET_log_from (kind, "util-container-multihashmap", __VA_ARGS__) | 29 | #define LOG(kind, ...) \ |
30 | GNUNET_log_from (kind, "util-container-multihashmap", __VA_ARGS__) | ||
30 | 31 | ||
31 | /** | 32 | /** |
32 | * Maximum recursion depth for callbacks of | 33 | * Maximum recursion depth for callbacks of |
@@ -57,7 +58,6 @@ struct BigMapEntry | |||
57 | * Key for the entry. | 58 | * Key for the entry. |
58 | */ | 59 | */ |
59 | struct GNUNET_HashCode key; | 60 | struct GNUNET_HashCode key; |
60 | |||
61 | }; | 61 | }; |
62 | 62 | ||
63 | 63 | ||
@@ -81,7 +81,6 @@ struct SmallMapEntry | |||
81 | * Key for the entry. | 81 | * Key for the entry. |
82 | */ | 82 | */ |
83 | const struct GNUNET_HashCode *key; | 83 | const struct GNUNET_HashCode *key; |
84 | |||
85 | }; | 84 | }; |
86 | 85 | ||
87 | 86 | ||
@@ -194,8 +193,7 @@ struct GNUNET_CONTAINER_MultiHashMapIterator | |||
194 | * @return NULL on error | 193 | * @return NULL on error |
195 | */ | 194 | */ |
196 | struct GNUNET_CONTAINER_MultiHashMap * | 195 | struct GNUNET_CONTAINER_MultiHashMap * |
197 | GNUNET_CONTAINER_multihashmap_create (unsigned int len, | 196 | GNUNET_CONTAINER_multihashmap_create (unsigned int len, int do_not_copy_keys) |
198 | int do_not_copy_keys) | ||
199 | { | 197 | { |
200 | struct GNUNET_CONTAINER_MultiHashMap *hm; | 198 | struct GNUNET_CONTAINER_MultiHashMap *hm; |
201 | 199 | ||
@@ -207,7 +205,7 @@ GNUNET_CONTAINER_multihashmap_create (unsigned int len, | |||
207 | /* application *explicitly* requested very large map, hopefully | 205 | /* application *explicitly* requested very large map, hopefully |
208 | it checks the return value... */ | 206 | it checks the return value... */ |
209 | s = len * sizeof (union MapEntry); | 207 | s = len * sizeof (union MapEntry); |
210 | if ( (s / sizeof (union MapEntry)) != len) | 208 | if ((s / sizeof (union MapEntry)) != len) |
211 | return NULL; /* integer overflow on multiplication */ | 209 | return NULL; /* integer overflow on multiplication */ |
212 | if (NULL == (hm->map = GNUNET_malloc_large (s))) | 210 | if (NULL == (hm->map = GNUNET_malloc_large (s))) |
213 | { | 211 | { |
@@ -221,8 +219,7 @@ GNUNET_CONTAINER_multihashmap_create (unsigned int len, | |||
221 | } | 219 | } |
222 | else | 220 | else |
223 | { | 221 | { |
224 | hm->map = GNUNET_new_array (len, | 222 | hm->map = GNUNET_new_array (len, union MapEntry); |
225 | union MapEntry); | ||
226 | } | 223 | } |
227 | hm->map_length = len; | 224 | hm->map_length = len; |
228 | hm->use_small_entries = do_not_copy_keys; | 225 | hm->use_small_entries = do_not_copy_keys; |
@@ -237,7 +234,8 @@ GNUNET_CONTAINER_multihashmap_create (unsigned int len, | |||
237 | * @param map the map | 234 | * @param map the map |
238 | */ | 235 | */ |
239 | void | 236 | void |
240 | GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap *map) | 237 | GNUNET_CONTAINER_multihashmap_destroy ( |
238 | struct GNUNET_CONTAINER_MultiHashMap *map) | ||
241 | { | 239 | { |
242 | GNUNET_assert (0 == map->next_cache_off); | 240 | GNUNET_assert (0 == map->next_cache_off); |
243 | for (unsigned int i = 0; i < map->map_length; i++) | 241 | for (unsigned int i = 0; i < map->map_length; i++) |
@@ -253,8 +251,8 @@ GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap *map | |||
253 | nxt = me.sme; | 251 | nxt = me.sme; |
254 | while (NULL != (sme = nxt)) | 252 | while (NULL != (sme = nxt)) |
255 | { | 253 | { |
256 | nxt = sme->next; | 254 | nxt = sme->next; |
257 | GNUNET_free (sme); | 255 | GNUNET_free (sme); |
258 | } | 256 | } |
259 | me.sme = NULL; | 257 | me.sme = NULL; |
260 | } | 258 | } |
@@ -266,8 +264,8 @@ GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap *map | |||
266 | nxt = me.bme; | 264 | nxt = me.bme; |
267 | while (NULL != (bme = nxt)) | 265 | while (NULL != (bme = nxt)) |
268 | { | 266 | { |
269 | nxt = bme->next; | 267 | nxt = bme->next; |
270 | GNUNET_free (bme); | 268 | GNUNET_free (bme); |
271 | } | 269 | } |
272 | me.bme = NULL; | 270 | me.bme = NULL; |
273 | } | 271 | } |
@@ -300,7 +298,8 @@ idx_of (const struct GNUNET_CONTAINER_MultiHashMap *map, | |||
300 | * @return the number of key value pairs | 298 | * @return the number of key value pairs |
301 | */ | 299 | */ |
302 | unsigned int | 300 | unsigned int |
303 | GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap *map) | 301 | GNUNET_CONTAINER_multihashmap_size ( |
302 | const struct GNUNET_CONTAINER_MultiHashMap *map) | ||
304 | { | 303 | { |
305 | return map->size; | 304 | return map->size; |
306 | } | 305 | } |
@@ -317,8 +316,9 @@ GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap * | |||
317 | * key-value pairs with value NULL | 316 | * key-value pairs with value NULL |
318 | */ | 317 | */ |
319 | void * | 318 | void * |
320 | GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap *map, | 319 | GNUNET_CONTAINER_multihashmap_get ( |
321 | const struct GNUNET_HashCode *key) | 320 | const struct GNUNET_CONTAINER_MultiHashMap *map, |
321 | const struct GNUNET_HashCode *key) | ||
322 | { | 322 | { |
323 | union MapEntry me; | 323 | union MapEntry me; |
324 | 324 | ||
@@ -328,20 +328,16 @@ GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap *m | |||
328 | struct SmallMapEntry *sme; | 328 | struct SmallMapEntry *sme; |
329 | 329 | ||
330 | for (sme = me.sme; NULL != sme; sme = sme->next) | 330 | for (sme = me.sme; NULL != sme; sme = sme->next) |
331 | if (0 == memcmp (key, | 331 | if (0 == GNUNET_memcmp (key, sme->key)) |
332 | sme->key, | 332 | return sme->value; |
333 | sizeof (struct GNUNET_HashCode))) | ||
334 | return sme->value; | ||
335 | } | 333 | } |
336 | else | 334 | else |
337 | { | 335 | { |
338 | struct BigMapEntry *bme; | 336 | struct BigMapEntry *bme; |
339 | 337 | ||
340 | for (bme = me.bme; NULL != bme; bme = bme->next) | 338 | for (bme = me.bme; NULL != bme; bme = bme->next) |
341 | if (0 == memcmp (key, | 339 | if (0 == GNUNET_memcmp (key, &bme->key)) |
342 | &bme->key, | 340 | return bme->value; |
343 | sizeof (struct GNUNET_HashCode))) | ||
344 | return bme->value; | ||
345 | } | 341 | } |
346 | return NULL; | 342 | return NULL; |
347 | } | 343 | } |
@@ -357,9 +353,10 @@ GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap *m | |||
357 | * #GNUNET_SYSERR if it aborted iteration | 353 | * #GNUNET_SYSERR if it aborted iteration |
358 | */ | 354 | */ |
359 | int | 355 | int |
360 | GNUNET_CONTAINER_multihashmap_iterate (struct GNUNET_CONTAINER_MultiHashMap *map, | 356 | GNUNET_CONTAINER_multihashmap_iterate ( |
361 | GNUNET_CONTAINER_HashMapIterator it, | 357 | struct GNUNET_CONTAINER_MultiHashMap *map, |
362 | void *it_cls) | 358 | GNUNET_CONTAINER_HashMapIterator it, |
359 | void *it_cls) | ||
363 | { | 360 | { |
364 | int count; | 361 | int count; |
365 | union MapEntry me; | 362 | union MapEntry me; |
@@ -380,18 +377,16 @@ GNUNET_CONTAINER_multihashmap_iterate (struct GNUNET_CONTAINER_MultiHashMap *map | |||
380 | ce->sme = me.sme; | 377 | ce->sme = me.sme; |
381 | while (NULL != (sme = ce->sme)) | 378 | while (NULL != (sme = ce->sme)) |
382 | { | 379 | { |
383 | ce->sme = sme->next; | 380 | ce->sme = sme->next; |
384 | if (NULL != it) | 381 | if (NULL != it) |
385 | { | 382 | { |
386 | if (GNUNET_OK != it (it_cls, | 383 | if (GNUNET_OK != it (it_cls, sme->key, sme->value)) |
387 | sme->key, | 384 | { |
388 | sme->value)) | 385 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); |
389 | { | 386 | return GNUNET_SYSERR; |
390 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); | 387 | } |
391 | return GNUNET_SYSERR; | 388 | } |
392 | } | 389 | count++; |
393 | } | ||
394 | count++; | ||
395 | } | 390 | } |
396 | } | 391 | } |
397 | else | 392 | else |
@@ -401,19 +396,17 @@ GNUNET_CONTAINER_multihashmap_iterate (struct GNUNET_CONTAINER_MultiHashMap *map | |||
401 | ce->bme = me.bme; | 396 | ce->bme = me.bme; |
402 | while (NULL != (bme = ce->bme)) | 397 | while (NULL != (bme = ce->bme)) |
403 | { | 398 | { |
404 | ce->bme = bme->next; | 399 | ce->bme = bme->next; |
405 | if (NULL != it) | 400 | if (NULL != it) |
406 | { | 401 | { |
407 | kc = bme->key; | 402 | kc = bme->key; |
408 | if (GNUNET_OK != it (it_cls, | 403 | if (GNUNET_OK != it (it_cls, &kc, bme->value)) |
409 | &kc, | 404 | { |
410 | bme->value)) | 405 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); |
411 | { | 406 | return GNUNET_SYSERR; |
412 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); | 407 | } |
413 | return GNUNET_SYSERR; | 408 | } |
414 | } | 409 | count++; |
415 | } | ||
416 | count++; | ||
417 | } | 410 | } |
418 | } | 411 | } |
419 | } | 412 | } |
@@ -431,9 +424,9 @@ GNUNET_CONTAINER_multihashmap_iterate (struct GNUNET_CONTAINER_MultiHashMap *map | |||
431 | */ | 424 | */ |
432 | static void | 425 | static void |
433 | update_next_cache_bme (struct GNUNET_CONTAINER_MultiHashMap *map, | 426 | update_next_cache_bme (struct GNUNET_CONTAINER_MultiHashMap *map, |
434 | const struct BigMapEntry *bme) | 427 | const struct BigMapEntry *bme) |
435 | { | 428 | { |
436 | for (unsigned int i=0;i<map->next_cache_off;i++) | 429 | for (unsigned int i = 0; i < map->next_cache_off; i++) |
437 | if (map->next_cache[i].bme == bme) | 430 | if (map->next_cache[i].bme == bme) |
438 | map->next_cache[i].bme = bme->next; | 431 | map->next_cache[i].bme = bme->next; |
439 | } | 432 | } |
@@ -448,9 +441,9 @@ update_next_cache_bme (struct GNUNET_CONTAINER_MultiHashMap *map, | |||
448 | */ | 441 | */ |
449 | static void | 442 | static void |
450 | update_next_cache_sme (struct GNUNET_CONTAINER_MultiHashMap *map, | 443 | update_next_cache_sme (struct GNUNET_CONTAINER_MultiHashMap *map, |
451 | const struct SmallMapEntry *sme) | 444 | const struct SmallMapEntry *sme) |
452 | { | 445 | { |
453 | for (unsigned int i=0;i<map->next_cache_off;i++) | 446 | for (unsigned int i = 0; i < map->next_cache_off; i++) |
454 | if (map->next_cache[i].sme == sme) | 447 | if (map->next_cache[i].sme == sme) |
455 | map->next_cache[i].sme = sme->next; | 448 | map->next_cache[i].sme = sme->next; |
456 | } | 449 | } |
@@ -470,7 +463,7 @@ update_next_cache_sme (struct GNUNET_CONTAINER_MultiHashMap *map, | |||
470 | int | 463 | int |
471 | GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map, | 464 | GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map, |
472 | const struct GNUNET_HashCode *key, | 465 | const struct GNUNET_HashCode *key, |
473 | const void *value) | 466 | const void *value) |
474 | { | 467 | { |
475 | union MapEntry me; | 468 | union MapEntry me; |
476 | unsigned int i; | 469 | unsigned int i; |
@@ -486,20 +479,16 @@ GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map, | |||
486 | p = NULL; | 479 | p = NULL; |
487 | for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) | 480 | for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) |
488 | { | 481 | { |
489 | if ( (0 == memcmp (key, | 482 | if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value)) |
490 | sme->key, | ||
491 | sizeof (struct GNUNET_HashCode))) && | ||
492 | (value == sme->value) ) | ||
493 | { | 483 | { |
494 | if (NULL == p) | 484 | if (NULL == p) |
495 | map->map[i].sme = sme->next; | 485 | map->map[i].sme = sme->next; |
496 | else | 486 | else |
497 | p->next = sme->next; | 487 | p->next = sme->next; |
498 | update_next_cache_sme (map, | 488 | update_next_cache_sme (map, sme); |
499 | sme); | 489 | GNUNET_free (sme); |
500 | GNUNET_free (sme); | 490 | map->size--; |
501 | map->size--; | 491 | return GNUNET_YES; |
502 | return GNUNET_YES; | ||
503 | } | 492 | } |
504 | p = sme; | 493 | p = sme; |
505 | } | 494 | } |
@@ -511,20 +500,16 @@ GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map, | |||
511 | p = NULL; | 500 | p = NULL; |
512 | for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) | 501 | for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) |
513 | { | 502 | { |
514 | if ( (0 == memcmp (key, | 503 | if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value)) |
515 | &bme->key, | ||
516 | sizeof (struct GNUNET_HashCode))) && | ||
517 | (value == bme->value) ) | ||
518 | { | 504 | { |
519 | if (NULL == p) | 505 | if (NULL == p) |
520 | map->map[i].bme = bme->next; | 506 | map->map[i].bme = bme->next; |
521 | else | 507 | else |
522 | p->next = bme->next; | 508 | p->next = bme->next; |
523 | update_next_cache_bme (map, | 509 | update_next_cache_bme (map, bme); |
524 | bme); | 510 | GNUNET_free (bme); |
525 | GNUNET_free (bme); | 511 | map->size--; |
526 | map->size--; | 512 | return GNUNET_YES; |
527 | return GNUNET_YES; | ||
528 | } | 513 | } |
529 | p = bme; | 514 | p = bme; |
530 | } | 515 | } |
@@ -542,8 +527,9 @@ GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map, | |||
542 | * @return number of values removed | 527 | * @return number of values removed |
543 | */ | 528 | */ |
544 | int | 529 | int |
545 | GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap *map, | 530 | GNUNET_CONTAINER_multihashmap_remove_all ( |
546 | const struct GNUNET_HashCode *key) | 531 | struct GNUNET_CONTAINER_MultiHashMap *map, |
532 | const struct GNUNET_HashCode *key) | ||
547 | { | 533 | { |
548 | union MapEntry me; | 534 | union MapEntry me; |
549 | unsigned int i; | 535 | unsigned int i; |
@@ -563,28 +549,25 @@ GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap * | |||
563 | sme = me.sme; | 549 | sme = me.sme; |
564 | while (NULL != sme) | 550 | while (NULL != sme) |
565 | { | 551 | { |
566 | if (0 == memcmp (key, | 552 | if (0 == GNUNET_memcmp (key, sme->key)) |
567 | sme->key, | ||
568 | sizeof (struct GNUNET_HashCode))) | ||
569 | { | 553 | { |
570 | if (NULL == p) | 554 | if (NULL == p) |
571 | map->map[i].sme = sme->next; | 555 | map->map[i].sme = sme->next; |
572 | else | 556 | else |
573 | p->next = sme->next; | 557 | p->next = sme->next; |
574 | update_next_cache_sme (map, | 558 | update_next_cache_sme (map, sme); |
575 | sme); | 559 | GNUNET_free (sme); |
576 | GNUNET_free (sme); | 560 | map->size--; |
577 | map->size--; | 561 | if (NULL == p) |
578 | if (NULL == p) | 562 | sme = map->map[i].sme; |
579 | sme = map->map[i].sme; | 563 | else |
580 | else | 564 | sme = p->next; |
581 | sme = p->next; | 565 | ret++; |
582 | ret++; | ||
583 | } | 566 | } |
584 | else | 567 | else |
585 | { | 568 | { |
586 | p = sme; | 569 | p = sme; |
587 | sme = sme->next; | 570 | sme = sme->next; |
588 | } | 571 | } |
589 | } | 572 | } |
590 | } | 573 | } |
@@ -597,28 +580,25 @@ GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap * | |||
597 | bme = me.bme; | 580 | bme = me.bme; |
598 | while (NULL != bme) | 581 | while (NULL != bme) |
599 | { | 582 | { |
600 | if (0 == memcmp (key, | 583 | if (0 == GNUNET_memcmp (key, &bme->key)) |
601 | &bme->key, | ||
602 | sizeof (struct GNUNET_HashCode))) | ||
603 | { | 584 | { |
604 | if (NULL == p) | 585 | if (NULL == p) |
605 | map->map[i].bme = bme->next; | 586 | map->map[i].bme = bme->next; |
606 | else | 587 | else |
607 | p->next = bme->next; | 588 | p->next = bme->next; |
608 | update_next_cache_bme (map, | 589 | update_next_cache_bme (map, bme); |
609 | bme); | 590 | GNUNET_free (bme); |
610 | GNUNET_free (bme); | 591 | map->size--; |
611 | map->size--; | 592 | if (NULL == p) |
612 | if (NULL == p) | 593 | bme = map->map[i].bme; |
613 | bme = map->map[i].bme; | 594 | else |
614 | else | 595 | bme = p->next; |
615 | bme = p->next; | 596 | ret++; |
616 | ret++; | ||
617 | } | 597 | } |
618 | else | 598 | else |
619 | { | 599 | { |
620 | p = bme; | 600 | p = bme; |
621 | bme = bme->next; | 601 | bme = bme->next; |
622 | } | 602 | } |
623 | } | 603 | } |
624 | } | 604 | } |
@@ -635,15 +615,12 @@ GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap * | |||
635 | * @return #GNUNET_OK (continue to iterate) | 615 | * @return #GNUNET_OK (continue to iterate) |
636 | */ | 616 | */ |
637 | static int | 617 | static int |
638 | remove_all (void *cls, | 618 | remove_all (void *cls, const struct GNUNET_HashCode *key, void *value) |
639 | const struct GNUNET_HashCode *key, | ||
640 | void *value) | ||
641 | { | 619 | { |
642 | struct GNUNET_CONTAINER_MultiHashMap *map = cls; | 620 | struct GNUNET_CONTAINER_MultiHashMap *map = cls; |
643 | 621 | ||
644 | GNUNET_CONTAINER_multihashmap_remove (map, | 622 | GNUNET_assert (GNUNET_YES == |
645 | key, | 623 | GNUNET_CONTAINER_multihashmap_remove (map, key, value)); |
646 | value); | ||
647 | return GNUNET_OK; | 624 | return GNUNET_OK; |
648 | } | 625 | } |
649 | 626 | ||
@@ -662,9 +639,7 @@ GNUNET_CONTAINER_multihashmap_clear (struct GNUNET_CONTAINER_MultiHashMap *map) | |||
662 | unsigned int ret; | 639 | unsigned int ret; |
663 | 640 | ||
664 | ret = map->size; | 641 | ret = map->size; |
665 | GNUNET_CONTAINER_multihashmap_iterate (map, | 642 | GNUNET_CONTAINER_multihashmap_iterate (map, &remove_all, map); |
666 | &remove_all, | ||
667 | map); | ||
668 | return ret; | 643 | return ret; |
669 | } | 644 | } |
670 | 645 | ||
@@ -679,9 +654,9 @@ GNUNET_CONTAINER_multihashmap_clear (struct GNUNET_CONTAINER_MultiHashMap *map) | |||
679 | * #GNUNET_NO if not | 654 | * #GNUNET_NO if not |
680 | */ | 655 | */ |
681 | int | 656 | int |
682 | GNUNET_CONTAINER_multihashmap_contains (const struct | 657 | GNUNET_CONTAINER_multihashmap_contains ( |
683 | GNUNET_CONTAINER_MultiHashMap *map, | 658 | const struct GNUNET_CONTAINER_MultiHashMap *map, |
684 | const struct GNUNET_HashCode *key) | 659 | const struct GNUNET_HashCode *key) |
685 | { | 660 | { |
686 | union MapEntry me; | 661 | union MapEntry me; |
687 | 662 | ||
@@ -691,16 +666,16 @@ GNUNET_CONTAINER_multihashmap_contains (const struct | |||
691 | struct SmallMapEntry *sme; | 666 | struct SmallMapEntry *sme; |
692 | 667 | ||
693 | for (sme = me.sme; NULL != sme; sme = sme->next) | 668 | for (sme = me.sme; NULL != sme; sme = sme->next) |
694 | if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode))) | 669 | if (0 == GNUNET_memcmp (key, sme->key)) |
695 | return GNUNET_YES; | 670 | return GNUNET_YES; |
696 | } | 671 | } |
697 | else | 672 | else |
698 | { | 673 | { |
699 | struct BigMapEntry *bme; | 674 | struct BigMapEntry *bme; |
700 | 675 | ||
701 | for (bme = me.bme; NULL != bme; bme = bme->next) | 676 | for (bme = me.bme; NULL != bme; bme = bme->next) |
702 | if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode))) | 677 | if (0 == GNUNET_memcmp (key, &bme->key)) |
703 | return GNUNET_YES; | 678 | return GNUNET_YES; |
704 | } | 679 | } |
705 | return GNUNET_NO; | 680 | return GNUNET_NO; |
706 | } | 681 | } |
@@ -717,9 +692,10 @@ GNUNET_CONTAINER_multihashmap_contains (const struct | |||
717 | * #GNUNET_NO if not | 692 | * #GNUNET_NO if not |
718 | */ | 693 | */ |
719 | int | 694 | int |
720 | GNUNET_CONTAINER_multihashmap_contains_value (const struct GNUNET_CONTAINER_MultiHashMap *map, | 695 | GNUNET_CONTAINER_multihashmap_contains_value ( |
721 | const struct GNUNET_HashCode *key, | 696 | const struct GNUNET_CONTAINER_MultiHashMap *map, |
722 | const void *value) | 697 | const struct GNUNET_HashCode *key, |
698 | const void *value) | ||
723 | { | 699 | { |
724 | union MapEntry me; | 700 | union MapEntry me; |
725 | 701 | ||
@@ -729,22 +705,16 @@ GNUNET_CONTAINER_multihashmap_contains_value (const struct GNUNET_CONTAINER_Mult | |||
729 | struct SmallMapEntry *sme; | 705 | struct SmallMapEntry *sme; |
730 | 706 | ||
731 | for (sme = me.sme; NULL != sme; sme = sme->next) | 707 | for (sme = me.sme; NULL != sme; sme = sme->next) |
732 | if ( (0 == memcmp (key, | 708 | if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value)) |
733 | sme->key, | 709 | return GNUNET_YES; |
734 | sizeof (struct GNUNET_HashCode))) && | ||
735 | (sme->value == value) ) | ||
736 | return GNUNET_YES; | ||
737 | } | 710 | } |
738 | else | 711 | else |
739 | { | 712 | { |
740 | struct BigMapEntry *bme; | 713 | struct BigMapEntry *bme; |
741 | 714 | ||
742 | for (bme = me.bme; NULL != bme; bme = bme->next) | 715 | for (bme = me.bme; NULL != bme; bme = bme->next) |
743 | if ( (0 == memcmp (key, | 716 | if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value)) |
744 | &bme->key, | 717 | return GNUNET_YES; |
745 | sizeof (struct GNUNET_HashCode))) && | ||
746 | (bme->value == value) ) | ||
747 | return GNUNET_YES; | ||
748 | } | 718 | } |
749 | return GNUNET_NO; | 719 | return GNUNET_NO; |
750 | } | 720 | } |
@@ -772,8 +742,7 @@ grow (struct GNUNET_CONTAINER_MultiHashMap *map) | |||
772 | new_len = old_len; /* never use 0 */ | 742 | new_len = old_len; /* never use 0 */ |
773 | if (new_len == old_len) | 743 | if (new_len == old_len) |
774 | return; /* nothing changed */ | 744 | return; /* nothing changed */ |
775 | new_map = GNUNET_malloc_large (new_len * | 745 | new_map = GNUNET_malloc_large (new_len * sizeof (union MapEntry)); |
776 | sizeof (union MapEntry)); | ||
777 | if (NULL == new_map) | 746 | if (NULL == new_map) |
778 | return; /* grow not possible */ | 747 | return; /* grow not possible */ |
779 | map->modification_counter++; | 748 | map->modification_counter++; |
@@ -787,10 +756,10 @@ grow (struct GNUNET_CONTAINER_MultiHashMap *map) | |||
787 | 756 | ||
788 | while (NULL != (sme = old_map[i].sme)) | 757 | while (NULL != (sme = old_map[i].sme)) |
789 | { | 758 | { |
790 | old_map[i].sme = sme->next; | 759 | old_map[i].sme = sme->next; |
791 | idx = idx_of (map, sme->key); | 760 | idx = idx_of (map, sme->key); |
792 | sme->next = new_map[idx].sme; | 761 | sme->next = new_map[idx].sme; |
793 | new_map[idx].sme = sme; | 762 | new_map[idx].sme = sme; |
794 | } | 763 | } |
795 | } | 764 | } |
796 | else | 765 | else |
@@ -799,10 +768,10 @@ grow (struct GNUNET_CONTAINER_MultiHashMap *map) | |||
799 | 768 | ||
800 | while (NULL != (bme = old_map[i].bme)) | 769 | while (NULL != (bme = old_map[i].bme)) |
801 | { | 770 | { |
802 | old_map[i].bme = bme->next; | 771 | old_map[i].bme = bme->next; |
803 | idx = idx_of (map, &bme->key); | 772 | idx = idx_of (map, &bme->key); |
804 | bme->next = new_map[idx].bme; | 773 | bme->next = new_map[idx].bme; |
805 | new_map[idx].bme = bme; | 774 | new_map[idx].bme = bme; |
806 | } | 775 | } |
807 | } | 776 | } |
808 | } | 777 | } |
@@ -825,7 +794,7 @@ grow (struct GNUNET_CONTAINER_MultiHashMap *map) | |||
825 | int | 794 | int |
826 | GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map, | 795 | GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map, |
827 | const struct GNUNET_HashCode *key, | 796 | const struct GNUNET_HashCode *key, |
828 | void *value, | 797 | void *value, |
829 | enum GNUNET_CONTAINER_MultiHashMapOption opt) | 798 | enum GNUNET_CONTAINER_MultiHashMapOption opt) |
830 | { | 799 | { |
831 | union MapEntry me; | 800 | union MapEntry me; |
@@ -841,26 +810,26 @@ GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map, | |||
841 | struct SmallMapEntry *sme; | 810 | struct SmallMapEntry *sme; |
842 | 811 | ||
843 | for (sme = me.sme; NULL != sme; sme = sme->next) | 812 | for (sme = me.sme; NULL != sme; sme = sme->next) |
844 | if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode))) | 813 | if (0 == GNUNET_memcmp (key, sme->key)) |
845 | { | 814 | { |
846 | if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) | 815 | if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) |
847 | return GNUNET_SYSERR; | 816 | return GNUNET_SYSERR; |
848 | sme->value = value; | 817 | sme->value = value; |
849 | return GNUNET_NO; | 818 | return GNUNET_NO; |
850 | } | 819 | } |
851 | } | 820 | } |
852 | else | 821 | else |
853 | { | 822 | { |
854 | struct BigMapEntry *bme; | 823 | struct BigMapEntry *bme; |
855 | 824 | ||
856 | for (bme = me.bme; NULL != bme; bme = bme->next) | 825 | for (bme = me.bme; NULL != bme; bme = bme->next) |
857 | if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode))) | 826 | if (0 == GNUNET_memcmp (key, &bme->key)) |
858 | { | 827 | { |
859 | if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) | 828 | if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) |
860 | return GNUNET_SYSERR; | 829 | return GNUNET_SYSERR; |
861 | bme->value = value; | 830 | bme->value = value; |
862 | return GNUNET_NO; | 831 | return GNUNET_NO; |
863 | } | 832 | } |
864 | } | 833 | } |
865 | } | 834 | } |
866 | if (map->size / 3 >= map->map_length / 4) | 835 | if (map->size / 3 >= map->map_length / 4) |
@@ -904,10 +873,11 @@ GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map, | |||
904 | * #GNUNET_SYSERR if it aborted iteration | 873 | * #GNUNET_SYSERR if it aborted iteration |
905 | */ | 874 | */ |
906 | int | 875 | int |
907 | GNUNET_CONTAINER_multihashmap_get_multiple (struct GNUNET_CONTAINER_MultiHashMap *map, | 876 | GNUNET_CONTAINER_multihashmap_get_multiple ( |
908 | const struct GNUNET_HashCode *key, | 877 | struct GNUNET_CONTAINER_MultiHashMap *map, |
909 | GNUNET_CONTAINER_HashMapIterator it, | 878 | const struct GNUNET_HashCode *key, |
910 | void *it_cls) | 879 | GNUNET_CONTAINER_HashMapIterator it, |
880 | void *it_cls) | ||
911 | { | 881 | { |
912 | int count; | 882 | int count; |
913 | union MapEntry *me; | 883 | union MapEntry *me; |
@@ -925,17 +895,12 @@ GNUNET_CONTAINER_multihashmap_get_multiple (struct GNUNET_CONTAINER_MultiHashMap | |||
925 | while (NULL != (sme = ce->sme)) | 895 | while (NULL != (sme = ce->sme)) |
926 | { | 896 | { |
927 | ce->sme = sme->next; | 897 | ce->sme = sme->next; |
928 | if (0 != memcmp (key, | 898 | if (0 != GNUNET_memcmp (key, sme->key)) |
929 | sme->key, | 899 | continue; |
930 | sizeof (struct GNUNET_HashCode))) | 900 | if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value))) |
931 | continue; | ||
932 | if ( (NULL != it) && | ||
933 | (GNUNET_OK != it (it_cls, | ||
934 | key, | ||
935 | sme->value))) | ||
936 | { | 901 | { |
937 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); | 902 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); |
938 | return GNUNET_SYSERR; | 903 | return GNUNET_SYSERR; |
939 | } | 904 | } |
940 | count++; | 905 | count++; |
941 | } | 906 | } |
@@ -948,17 +913,12 @@ GNUNET_CONTAINER_multihashmap_get_multiple (struct GNUNET_CONTAINER_MultiHashMap | |||
948 | while (NULL != (bme = ce->bme)) | 913 | while (NULL != (bme = ce->bme)) |
949 | { | 914 | { |
950 | ce->bme = bme->next; | 915 | ce->bme = bme->next; |
951 | if (0 != memcmp (key, | 916 | if (0 != GNUNET_memcmp (key, &bme->key)) |
952 | &bme->key, | 917 | continue; |
953 | sizeof (struct GNUNET_HashCode))) | 918 | if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value))) |
954 | continue; | ||
955 | if ( (NULL != it) && | ||
956 | (GNUNET_OK != it (it_cls, | ||
957 | key, | ||
958 | bme->value))) | ||
959 | { | 919 | { |
960 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); | 920 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); |
961 | return GNUNET_SYSERR; | 921 | return GNUNET_SYSERR; |
962 | } | 922 | } |
963 | count++; | 923 | count++; |
964 | } | 924 | } |
@@ -980,9 +940,10 @@ GNUNET_CONTAINER_multihashmap_get_multiple (struct GNUNET_CONTAINER_MultiHashMap | |||
980 | * @return the number of key value pairs processed, zero or one. | 940 | * @return the number of key value pairs processed, zero or one. |
981 | */ | 941 | */ |
982 | unsigned int | 942 | unsigned int |
983 | GNUNET_CONTAINER_multihashmap_get_random (const struct GNUNET_CONTAINER_MultiHashMap *map, | 943 | GNUNET_CONTAINER_multihashmap_get_random ( |
984 | GNUNET_CONTAINER_HashMapIterator it, | 944 | const struct GNUNET_CONTAINER_MultiHashMap *map, |
985 | void *it_cls) | 945 | GNUNET_CONTAINER_HashMapIterator it, |
946 | void *it_cls) | ||
986 | { | 947 | { |
987 | unsigned int off; | 948 | unsigned int off; |
988 | unsigned int idx; | 949 | unsigned int idx; |
@@ -992,8 +953,7 @@ GNUNET_CONTAINER_multihashmap_get_random (const struct GNUNET_CONTAINER_MultiHas | |||
992 | return 0; | 953 | return 0; |
993 | if (NULL == it) | 954 | if (NULL == it) |
994 | return 1; | 955 | return 1; |
995 | off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, | 956 | off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size); |
996 | map->size); | ||
997 | for (idx = 0; idx < map->map_length; idx++) | 957 | for (idx = 0; idx < map->map_length; idx++) |
998 | { | 958 | { |
999 | me = map->map[idx]; | 959 | me = map->map[idx]; |
@@ -1008,9 +968,7 @@ GNUNET_CONTAINER_multihashmap_get_random (const struct GNUNET_CONTAINER_MultiHas | |||
1008 | nxt = sme->next; | 968 | nxt = sme->next; |
1009 | if (0 == off) | 969 | if (0 == off) |
1010 | { | 970 | { |
1011 | if (GNUNET_OK != it (it_cls, | 971 | if (GNUNET_OK != it (it_cls, sme->key, sme->value)) |
1012 | sme->key, | ||
1013 | sme->value)) | ||
1014 | return GNUNET_SYSERR; | 972 | return GNUNET_SYSERR; |
1015 | return 1; | 973 | return 1; |
1016 | } | 974 | } |
@@ -1028,8 +986,7 @@ GNUNET_CONTAINER_multihashmap_get_random (const struct GNUNET_CONTAINER_MultiHas | |||
1028 | nxt = bme->next; | 986 | nxt = bme->next; |
1029 | if (0 == off) | 987 | if (0 == off) |
1030 | { | 988 | { |
1031 | if (GNUNET_OK != it (it_cls, | 989 | if (GNUNET_OK != it (it_cls, &bme->key, bme->value)) |
1032 | &bme->key, bme->value)) | ||
1033 | return GNUNET_SYSERR; | 990 | return GNUNET_SYSERR; |
1034 | return 1; | 991 | return 1; |
1035 | } | 992 | } |
@@ -1055,7 +1012,8 @@ GNUNET_CONTAINER_multihashmap_get_random (const struct GNUNET_CONTAINER_MultiHas | |||
1055 | * @return an iterator over the given multihashmap 'map' | 1012 | * @return an iterator over the given multihashmap 'map' |
1056 | */ | 1013 | */ |
1057 | struct GNUNET_CONTAINER_MultiHashMapIterator * | 1014 | struct GNUNET_CONTAINER_MultiHashMapIterator * |
1058 | GNUNET_CONTAINER_multihashmap_iterator_create (const struct GNUNET_CONTAINER_MultiHashMap *map) | 1015 | GNUNET_CONTAINER_multihashmap_iterator_create ( |
1016 | const struct GNUNET_CONTAINER_MultiHashMap *map) | ||
1059 | { | 1017 | { |
1060 | struct GNUNET_CONTAINER_MultiHashMapIterator *iter; | 1018 | struct GNUNET_CONTAINER_MultiHashMapIterator *iter; |
1061 | 1019 | ||
@@ -1082,9 +1040,10 @@ GNUNET_CONTAINER_multihashmap_iterator_create (const struct GNUNET_CONTAINER_Mul | |||
1082 | * #GNUNET_NO if we are out of elements | 1040 | * #GNUNET_NO if we are out of elements |
1083 | */ | 1041 | */ |
1084 | int | 1042 | int |
1085 | GNUNET_CONTAINER_multihashmap_iterator_next (struct GNUNET_CONTAINER_MultiHashMapIterator *iter, | 1043 | GNUNET_CONTAINER_multihashmap_iterator_next ( |
1086 | struct GNUNET_HashCode *key, | 1044 | struct GNUNET_CONTAINER_MultiHashMapIterator *iter, |
1087 | const void **value) | 1045 | struct GNUNET_HashCode *key, |
1046 | const void **value) | ||
1088 | { | 1047 | { |
1089 | /* make sure the map has not been modified */ | 1048 | /* make sure the map has not been modified */ |
1090 | GNUNET_assert (iter->modification_counter == iter->map->modification_counter); | 1049 | GNUNET_assert (iter->modification_counter == iter->map->modification_counter); |
@@ -1131,7 +1090,8 @@ GNUNET_CONTAINER_multihashmap_iterator_next (struct GNUNET_CONTAINER_MultiHashMa | |||
1131 | * @param iter the iterator to destroy | 1090 | * @param iter the iterator to destroy |
1132 | */ | 1091 | */ |
1133 | void | 1092 | void |
1134 | GNUNET_CONTAINER_multihashmap_iterator_destroy (struct GNUNET_CONTAINER_MultiHashMapIterator *iter) | 1093 | GNUNET_CONTAINER_multihashmap_iterator_destroy ( |
1094 | struct GNUNET_CONTAINER_MultiHashMapIterator *iter) | ||
1135 | { | 1095 | { |
1136 | GNUNET_free (iter); | 1096 | GNUNET_free (iter); |
1137 | } | 1097 | } |