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_multipeermap.c | |
parent | ce4a47a9937aa30075b8cc8624bdb1332cd69a2d (diff) | |
download | gnunet-9ce956ea4c93f038995a21c6c1c0133eee6bff75.tar.gz gnunet-9ce956ea4c93f038995a21c6c1c0133eee6bff75.zip |
memcmp -> GNUNET_memcmp
Diffstat (limited to 'src/util/container_multipeermap.c')
-rw-r--r-- | src/util/container_multipeermap.c | 378 |
1 files changed, 172 insertions, 206 deletions
diff --git a/src/util/container_multipeermap.c b/src/util/container_multipeermap.c index 613efc0a9..d03221cb5 100644 --- a/src/util/container_multipeermap.c +++ b/src/util/container_multipeermap.c | |||
@@ -26,7 +26,8 @@ | |||
26 | #include "platform.h" | 26 | #include "platform.h" |
27 | #include "gnunet_util_lib.h" | 27 | #include "gnunet_util_lib.h" |
28 | 28 | ||
29 | #define LOG(kind,...) GNUNET_log_from (kind, "util-container-multipeermap", __VA_ARGS__) | 29 | #define LOG(kind, ...) \ |
30 | GNUNET_log_from (kind, "util-container-multipeermap", __VA_ARGS__) | ||
30 | 31 | ||
31 | /** | 32 | /** |
32 | * Maximum recursion depth for callbacks of | 33 | * Maximum recursion depth for callbacks of |
@@ -56,7 +57,6 @@ struct BigMapEntry | |||
56 | * Key for the entry. | 57 | * Key for the entry. |
57 | */ | 58 | */ |
58 | struct GNUNET_PeerIdentity key; | 59 | struct GNUNET_PeerIdentity key; |
59 | |||
60 | }; | 60 | }; |
61 | 61 | ||
62 | 62 | ||
@@ -80,7 +80,6 @@ struct SmallMapEntry | |||
80 | * Key for the entry. | 80 | * Key for the entry. |
81 | */ | 81 | */ |
82 | const struct GNUNET_PeerIdentity *key; | 82 | const struct GNUNET_PeerIdentity *key; |
83 | |||
84 | }; | 83 | }; |
85 | 84 | ||
86 | 85 | ||
@@ -193,15 +192,13 @@ struct GNUNET_CONTAINER_MultiPeerMapIterator | |||
193 | * @return NULL on error | 192 | * @return NULL on error |
194 | */ | 193 | */ |
195 | struct GNUNET_CONTAINER_MultiPeerMap * | 194 | struct GNUNET_CONTAINER_MultiPeerMap * |
196 | GNUNET_CONTAINER_multipeermap_create (unsigned int len, | 195 | GNUNET_CONTAINER_multipeermap_create (unsigned int len, int do_not_copy_keys) |
197 | int do_not_copy_keys) | ||
198 | { | 196 | { |
199 | struct GNUNET_CONTAINER_MultiPeerMap *map; | 197 | struct GNUNET_CONTAINER_MultiPeerMap *map; |
200 | 198 | ||
201 | GNUNET_assert (len > 0); | 199 | GNUNET_assert (len > 0); |
202 | map = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMap); | 200 | map = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMap); |
203 | map->map = GNUNET_malloc_large (len * | 201 | map->map = GNUNET_malloc_large (len * sizeof (union MapEntry)); |
204 | sizeof (union MapEntry)); | ||
205 | if (NULL == map->map) | 202 | if (NULL == map->map) |
206 | { | 203 | { |
207 | GNUNET_free (map); | 204 | GNUNET_free (map); |
@@ -220,7 +217,8 @@ GNUNET_CONTAINER_multipeermap_create (unsigned int len, | |||
220 | * @param map the map | 217 | * @param map the map |
221 | */ | 218 | */ |
222 | void | 219 | void |
223 | GNUNET_CONTAINER_multipeermap_destroy (struct GNUNET_CONTAINER_MultiPeerMap *map) | 220 | GNUNET_CONTAINER_multipeermap_destroy ( |
221 | struct GNUNET_CONTAINER_MultiPeerMap *map) | ||
224 | { | 222 | { |
225 | GNUNET_assert (0 == map->next_cache_off); | 223 | GNUNET_assert (0 == map->next_cache_off); |
226 | for (unsigned int i = 0; i < map->map_length; i++) | 224 | for (unsigned int i = 0; i < map->map_length; i++) |
@@ -236,8 +234,8 @@ GNUNET_CONTAINER_multipeermap_destroy (struct GNUNET_CONTAINER_MultiPeerMap *map | |||
236 | nxt = me.sme; | 234 | nxt = me.sme; |
237 | while (NULL != (sme = nxt)) | 235 | while (NULL != (sme = nxt)) |
238 | { | 236 | { |
239 | nxt = sme->next; | 237 | nxt = sme->next; |
240 | GNUNET_free (sme); | 238 | GNUNET_free (sme); |
241 | } | 239 | } |
242 | me.sme = NULL; | 240 | me.sme = NULL; |
243 | } | 241 | } |
@@ -249,8 +247,8 @@ GNUNET_CONTAINER_multipeermap_destroy (struct GNUNET_CONTAINER_MultiPeerMap *map | |||
249 | nxt = me.bme; | 247 | nxt = me.bme; |
250 | while (NULL != (bme = nxt)) | 248 | while (NULL != (bme = nxt)) |
251 | { | 249 | { |
252 | nxt = bme->next; | 250 | nxt = bme->next; |
253 | GNUNET_free (bme); | 251 | GNUNET_free (bme); |
254 | } | 252 | } |
255 | me.bme = NULL; | 253 | me.bme = NULL; |
256 | } | 254 | } |
@@ -274,9 +272,7 @@ idx_of (const struct GNUNET_CONTAINER_MultiPeerMap *map, | |||
274 | unsigned int kx; | 272 | unsigned int kx; |
275 | 273 | ||
276 | GNUNET_assert (NULL != map); | 274 | GNUNET_assert (NULL != map); |
277 | GNUNET_memcpy (&kx, | 275 | GNUNET_memcpy (&kx, key, sizeof (kx)); |
278 | key, | ||
279 | sizeof (kx)); | ||
280 | return kx % map->map_length; | 276 | return kx % map->map_length; |
281 | } | 277 | } |
282 | 278 | ||
@@ -288,7 +284,8 @@ idx_of (const struct GNUNET_CONTAINER_MultiPeerMap *map, | |||
288 | * @return the number of key value pairs | 284 | * @return the number of key value pairs |
289 | */ | 285 | */ |
290 | unsigned int | 286 | unsigned int |
291 | GNUNET_CONTAINER_multipeermap_size (const struct GNUNET_CONTAINER_MultiPeerMap *map) | 287 | GNUNET_CONTAINER_multipeermap_size ( |
288 | const struct GNUNET_CONTAINER_MultiPeerMap *map) | ||
292 | { | 289 | { |
293 | return map->size; | 290 | return map->size; |
294 | } | 291 | } |
@@ -305,8 +302,9 @@ GNUNET_CONTAINER_multipeermap_size (const struct GNUNET_CONTAINER_MultiPeerMap * | |||
305 | * key-value pairs with value NULL | 302 | * key-value pairs with value NULL |
306 | */ | 303 | */ |
307 | void * | 304 | void * |
308 | GNUNET_CONTAINER_multipeermap_get (const struct GNUNET_CONTAINER_MultiPeerMap *map, | 305 | GNUNET_CONTAINER_multipeermap_get ( |
309 | const struct GNUNET_PeerIdentity *key) | 306 | const struct GNUNET_CONTAINER_MultiPeerMap *map, |
307 | const struct GNUNET_PeerIdentity *key) | ||
310 | { | 308 | { |
311 | union MapEntry me; | 309 | union MapEntry me; |
312 | 310 | ||
@@ -314,18 +312,14 @@ GNUNET_CONTAINER_multipeermap_get (const struct GNUNET_CONTAINER_MultiPeerMap *m | |||
314 | if (map->use_small_entries) | 312 | if (map->use_small_entries) |
315 | { | 313 | { |
316 | for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) | 314 | for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) |
317 | if (0 == memcmp (key, | 315 | if (0 == GNUNET_memcmp (key, sme->key)) |
318 | sme->key, | 316 | return sme->value; |
319 | sizeof (struct GNUNET_PeerIdentity))) | ||
320 | return sme->value; | ||
321 | } | 317 | } |
322 | else | 318 | else |
323 | { | 319 | { |
324 | for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) | 320 | for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) |
325 | if (0 == memcmp (key, | 321 | if (0 == GNUNET_memcmp (key, &bme->key)) |
326 | &bme->key, | 322 | return bme->value; |
327 | sizeof (struct GNUNET_PeerIdentity))) | ||
328 | return bme->value; | ||
329 | } | 323 | } |
330 | return NULL; | 324 | return NULL; |
331 | } | 325 | } |
@@ -341,9 +335,10 @@ GNUNET_CONTAINER_multipeermap_get (const struct GNUNET_CONTAINER_MultiPeerMap *m | |||
341 | * #GNUNET_SYSERR if it aborted iteration | 335 | * #GNUNET_SYSERR if it aborted iteration |
342 | */ | 336 | */ |
343 | int | 337 | int |
344 | GNUNET_CONTAINER_multipeermap_iterate (struct GNUNET_CONTAINER_MultiPeerMap *map, | 338 | GNUNET_CONTAINER_multipeermap_iterate ( |
345 | GNUNET_CONTAINER_PeerMapIterator it, | 339 | struct GNUNET_CONTAINER_MultiPeerMap *map, |
346 | void *it_cls) | 340 | GNUNET_CONTAINER_PeerMapIterator it, |
341 | void *it_cls) | ||
347 | { | 342 | { |
348 | int count; | 343 | int count; |
349 | union MapEntry me; | 344 | union MapEntry me; |
@@ -364,18 +359,16 @@ GNUNET_CONTAINER_multipeermap_iterate (struct GNUNET_CONTAINER_MultiPeerMap *map | |||
364 | ce->sme = me.sme; | 359 | ce->sme = me.sme; |
365 | while (NULL != (sme = ce->sme)) | 360 | while (NULL != (sme = ce->sme)) |
366 | { | 361 | { |
367 | ce->sme = sme->next; | 362 | ce->sme = sme->next; |
368 | if (NULL != it) | 363 | if (NULL != it) |
369 | { | 364 | { |
370 | if (GNUNET_OK != it (it_cls, | 365 | if (GNUNET_OK != it (it_cls, sme->key, sme->value)) |
371 | sme->key, | 366 | { |
372 | sme->value)) | 367 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); |
373 | { | 368 | return GNUNET_SYSERR; |
374 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); | 369 | } |
375 | return GNUNET_SYSERR; | 370 | } |
376 | } | 371 | count++; |
377 | } | ||
378 | count++; | ||
379 | } | 372 | } |
380 | } | 373 | } |
381 | else | 374 | else |
@@ -385,19 +378,17 @@ GNUNET_CONTAINER_multipeermap_iterate (struct GNUNET_CONTAINER_MultiPeerMap *map | |||
385 | ce->bme = me.bme; | 378 | ce->bme = me.bme; |
386 | while (NULL != (bme = ce->bme)) | 379 | while (NULL != (bme = ce->bme)) |
387 | { | 380 | { |
388 | ce->bme = bme->next; | 381 | ce->bme = bme->next; |
389 | if (NULL != it) | 382 | if (NULL != it) |
390 | { | 383 | { |
391 | kc = bme->key; | 384 | kc = bme->key; |
392 | if (GNUNET_OK != it (it_cls, | 385 | if (GNUNET_OK != it (it_cls, &kc, bme->value)) |
393 | &kc, | 386 | { |
394 | bme->value)) | 387 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); |
395 | { | 388 | return GNUNET_SYSERR; |
396 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); | 389 | } |
397 | return GNUNET_SYSERR; | 390 | } |
398 | } | 391 | count++; |
399 | } | ||
400 | count++; | ||
401 | } | 392 | } |
402 | } | 393 | } |
403 | } | 394 | } |
@@ -415,9 +406,9 @@ GNUNET_CONTAINER_multipeermap_iterate (struct GNUNET_CONTAINER_MultiPeerMap *map | |||
415 | */ | 406 | */ |
416 | static void | 407 | static void |
417 | update_next_cache_bme (struct GNUNET_CONTAINER_MultiPeerMap *map, | 408 | update_next_cache_bme (struct GNUNET_CONTAINER_MultiPeerMap *map, |
418 | const struct BigMapEntry *bme) | 409 | const struct BigMapEntry *bme) |
419 | { | 410 | { |
420 | for (unsigned int i=0;i<map->next_cache_off;i++) | 411 | for (unsigned int i = 0; i < map->next_cache_off; i++) |
421 | if (map->next_cache[i].bme == bme) | 412 | if (map->next_cache[i].bme == bme) |
422 | map->next_cache[i].bme = bme->next; | 413 | map->next_cache[i].bme = bme->next; |
423 | } | 414 | } |
@@ -432,9 +423,9 @@ update_next_cache_bme (struct GNUNET_CONTAINER_MultiPeerMap *map, | |||
432 | */ | 423 | */ |
433 | static void | 424 | static void |
434 | update_next_cache_sme (struct GNUNET_CONTAINER_MultiPeerMap *map, | 425 | update_next_cache_sme (struct GNUNET_CONTAINER_MultiPeerMap *map, |
435 | const struct SmallMapEntry *sme) | 426 | const struct SmallMapEntry *sme) |
436 | { | 427 | { |
437 | for (unsigned int i=0;i<map->next_cache_off;i++) | 428 | for (unsigned int i = 0; i < map->next_cache_off; i++) |
438 | if (map->next_cache[i].sme == sme) | 429 | if (map->next_cache[i].sme == sme) |
439 | map->next_cache[i].sme = sme->next; | 430 | map->next_cache[i].sme = sme->next; |
440 | } | 431 | } |
@@ -454,7 +445,7 @@ update_next_cache_sme (struct GNUNET_CONTAINER_MultiPeerMap *map, | |||
454 | int | 445 | int |
455 | GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map, | 446 | GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map, |
456 | const struct GNUNET_PeerIdentity *key, | 447 | const struct GNUNET_PeerIdentity *key, |
457 | const void *value) | 448 | const void *value) |
458 | { | 449 | { |
459 | union MapEntry me; | 450 | union MapEntry me; |
460 | unsigned int i; | 451 | unsigned int i; |
@@ -468,18 +459,16 @@ GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map, | |||
468 | 459 | ||
469 | for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) | 460 | for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) |
470 | { | 461 | { |
471 | if ((0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity))) && | 462 | if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value)) |
472 | (value == sme->value)) | ||
473 | { | 463 | { |
474 | if (NULL == p) | 464 | if (NULL == p) |
475 | map->map[i].sme = sme->next; | 465 | map->map[i].sme = sme->next; |
476 | else | 466 | else |
477 | p->next = sme->next; | 467 | p->next = sme->next; |
478 | update_next_cache_sme (map, | 468 | update_next_cache_sme (map, sme); |
479 | sme); | 469 | GNUNET_free (sme); |
480 | GNUNET_free (sme); | 470 | map->size--; |
481 | map->size--; | 471 | return GNUNET_YES; |
482 | return GNUNET_YES; | ||
483 | } | 472 | } |
484 | p = sme; | 473 | p = sme; |
485 | } | 474 | } |
@@ -490,20 +479,16 @@ GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map, | |||
490 | 479 | ||
491 | for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) | 480 | for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) |
492 | { | 481 | { |
493 | if ((0 == memcmp (key, | 482 | if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value)) |
494 | &bme->key, | ||
495 | sizeof (struct GNUNET_PeerIdentity))) && | ||
496 | (value == bme->value)) | ||
497 | { | 483 | { |
498 | if (NULL == p) | 484 | if (NULL == p) |
499 | map->map[i].bme = bme->next; | 485 | map->map[i].bme = bme->next; |
500 | else | 486 | else |
501 | p->next = bme->next; | 487 | p->next = bme->next; |
502 | update_next_cache_bme (map, | 488 | update_next_cache_bme (map, bme); |
503 | bme); | 489 | GNUNET_free (bme); |
504 | GNUNET_free (bme); | 490 | map->size--; |
505 | map->size--; | 491 | return GNUNET_YES; |
506 | return GNUNET_YES; | ||
507 | } | 492 | } |
508 | p = bme; | 493 | p = bme; |
509 | } | 494 | } |
@@ -521,8 +506,9 @@ GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map, | |||
521 | * @return number of values removed | 506 | * @return number of values removed |
522 | */ | 507 | */ |
523 | int | 508 | int |
524 | GNUNET_CONTAINER_multipeermap_remove_all (struct GNUNET_CONTAINER_MultiPeerMap *map, | 509 | GNUNET_CONTAINER_multipeermap_remove_all ( |
525 | const struct GNUNET_PeerIdentity *key) | 510 | struct GNUNET_CONTAINER_MultiPeerMap *map, |
511 | const struct GNUNET_PeerIdentity *key) | ||
526 | { | 512 | { |
527 | union MapEntry me; | 513 | union MapEntry me; |
528 | unsigned int i; | 514 | unsigned int i; |
@@ -542,28 +528,25 @@ GNUNET_CONTAINER_multipeermap_remove_all (struct GNUNET_CONTAINER_MultiPeerMap * | |||
542 | sme = me.sme; | 528 | sme = me.sme; |
543 | while (NULL != sme) | 529 | while (NULL != sme) |
544 | { | 530 | { |
545 | if (0 == memcmp (key, | 531 | if (0 == GNUNET_memcmp (key, sme->key)) |
546 | sme->key, | ||
547 | sizeof (struct GNUNET_PeerIdentity))) | ||
548 | { | 532 | { |
549 | if (NULL == p) | 533 | if (NULL == p) |
550 | map->map[i].sme = sme->next; | 534 | map->map[i].sme = sme->next; |
551 | else | 535 | else |
552 | p->next = sme->next; | 536 | p->next = sme->next; |
553 | update_next_cache_sme (map, | 537 | update_next_cache_sme (map, sme); |
554 | sme); | 538 | GNUNET_free (sme); |
555 | GNUNET_free (sme); | 539 | map->size--; |
556 | map->size--; | 540 | if (NULL == p) |
557 | if (NULL == p) | 541 | sme = map->map[i].sme; |
558 | sme = map->map[i].sme; | 542 | else |
559 | else | 543 | sme = p->next; |
560 | sme = p->next; | 544 | ret++; |
561 | ret++; | ||
562 | } | 545 | } |
563 | else | 546 | else |
564 | { | 547 | { |
565 | p = sme; | 548 | p = sme; |
566 | sme = sme->next; | 549 | sme = sme->next; |
567 | } | 550 | } |
568 | } | 551 | } |
569 | } | 552 | } |
@@ -576,28 +559,25 @@ GNUNET_CONTAINER_multipeermap_remove_all (struct GNUNET_CONTAINER_MultiPeerMap * | |||
576 | bme = me.bme; | 559 | bme = me.bme; |
577 | while (NULL != bme) | 560 | while (NULL != bme) |
578 | { | 561 | { |
579 | if (0 == memcmp (key, | 562 | if (0 == GNUNET_memcmp (key, &bme->key)) |
580 | &bme->key, | ||
581 | sizeof (struct GNUNET_PeerIdentity))) | ||
582 | { | 563 | { |
583 | if (NULL == p) | 564 | if (NULL == p) |
584 | map->map[i].bme = bme->next; | 565 | map->map[i].bme = bme->next; |
585 | else | 566 | else |
586 | p->next = bme->next; | 567 | p->next = bme->next; |
587 | update_next_cache_bme (map, | 568 | update_next_cache_bme (map, bme); |
588 | bme); | 569 | GNUNET_free (bme); |
589 | GNUNET_free (bme); | 570 | map->size--; |
590 | map->size--; | 571 | if (NULL == p) |
591 | if (NULL == p) | 572 | bme = map->map[i].bme; |
592 | bme = map->map[i].bme; | 573 | else |
593 | else | 574 | bme = p->next; |
594 | bme = p->next; | 575 | ret++; |
595 | ret++; | ||
596 | } | 576 | } |
597 | else | 577 | else |
598 | { | 578 | { |
599 | p = bme; | 579 | p = bme; |
600 | bme = bme->next; | 580 | bme = bme->next; |
601 | } | 581 | } |
602 | } | 582 | } |
603 | } | 583 | } |
@@ -615,8 +595,9 @@ GNUNET_CONTAINER_multipeermap_remove_all (struct GNUNET_CONTAINER_MultiPeerMap * | |||
615 | * #GNUNET_NO if not | 595 | * #GNUNET_NO if not |
616 | */ | 596 | */ |
617 | int | 597 | int |
618 | GNUNET_CONTAINER_multipeermap_contains (const struct GNUNET_CONTAINER_MultiPeerMap *map, | 598 | GNUNET_CONTAINER_multipeermap_contains ( |
619 | const struct GNUNET_PeerIdentity *key) | 599 | const struct GNUNET_CONTAINER_MultiPeerMap *map, |
600 | const struct GNUNET_PeerIdentity *key) | ||
620 | { | 601 | { |
621 | union MapEntry me; | 602 | union MapEntry me; |
622 | 603 | ||
@@ -624,14 +605,14 @@ GNUNET_CONTAINER_multipeermap_contains (const struct GNUNET_CONTAINER_MultiPeerM | |||
624 | if (map->use_small_entries) | 605 | if (map->use_small_entries) |
625 | { | 606 | { |
626 | for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) | 607 | for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) |
627 | if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity))) | 608 | if (0 == GNUNET_memcmp (key, sme->key)) |
628 | return GNUNET_YES; | 609 | return GNUNET_YES; |
629 | } | 610 | } |
630 | else | 611 | else |
631 | { | 612 | { |
632 | for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) | 613 | for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) |
633 | if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity))) | 614 | if (0 == GNUNET_memcmp (key, &bme->key)) |
634 | return GNUNET_YES; | 615 | return GNUNET_YES; |
635 | } | 616 | } |
636 | return GNUNET_NO; | 617 | return GNUNET_NO; |
637 | } | 618 | } |
@@ -648,9 +629,10 @@ GNUNET_CONTAINER_multipeermap_contains (const struct GNUNET_CONTAINER_MultiPeerM | |||
648 | * #GNUNET_NO if not | 629 | * #GNUNET_NO if not |
649 | */ | 630 | */ |
650 | int | 631 | int |
651 | GNUNET_CONTAINER_multipeermap_contains_value (const struct GNUNET_CONTAINER_MultiPeerMap *map, | 632 | GNUNET_CONTAINER_multipeermap_contains_value ( |
652 | const struct GNUNET_PeerIdentity *key, | 633 | const struct GNUNET_CONTAINER_MultiPeerMap *map, |
653 | const void *value) | 634 | const struct GNUNET_PeerIdentity *key, |
635 | const void *value) | ||
654 | { | 636 | { |
655 | union MapEntry me; | 637 | union MapEntry me; |
656 | 638 | ||
@@ -658,20 +640,14 @@ GNUNET_CONTAINER_multipeermap_contains_value (const struct GNUNET_CONTAINER_Mult | |||
658 | if (map->use_small_entries) | 640 | if (map->use_small_entries) |
659 | { | 641 | { |
660 | for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) | 642 | for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) |
661 | if ( (0 == memcmp (key, | 643 | if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value)) |
662 | sme->key, | 644 | return GNUNET_YES; |
663 | sizeof (struct GNUNET_PeerIdentity))) && | ||
664 | (sme->value == value) ) | ||
665 | return GNUNET_YES; | ||
666 | } | 645 | } |
667 | else | 646 | else |
668 | { | 647 | { |
669 | for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) | 648 | for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) |
670 | if ( (0 == memcmp (key, | 649 | if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value)) |
671 | &bme->key, | 650 | return GNUNET_YES; |
672 | sizeof (struct GNUNET_PeerIdentity))) && | ||
673 | (bme->value == value) ) | ||
674 | return GNUNET_YES; | ||
675 | } | 651 | } |
676 | return GNUNET_NO; | 652 | return GNUNET_NO; |
677 | } | 653 | } |
@@ -699,8 +675,7 @@ grow (struct GNUNET_CONTAINER_MultiPeerMap *map) | |||
699 | new_len = old_len; /* never use 0 */ | 675 | new_len = old_len; /* never use 0 */ |
700 | if (new_len == old_len) | 676 | if (new_len == old_len) |
701 | return; /* nothing changed */ | 677 | return; /* nothing changed */ |
702 | new_map = GNUNET_malloc_large (new_len * | 678 | new_map = GNUNET_malloc_large (new_len * sizeof (union MapEntry)); |
703 | sizeof (union MapEntry)); | ||
704 | if (NULL == new_map) | 679 | if (NULL == new_map) |
705 | return; /* grow not possible */ | 680 | return; /* grow not possible */ |
706 | map->modification_counter++; | 681 | map->modification_counter++; |
@@ -714,10 +689,10 @@ grow (struct GNUNET_CONTAINER_MultiPeerMap *map) | |||
714 | 689 | ||
715 | while (NULL != (sme = old_map[i].sme)) | 690 | while (NULL != (sme = old_map[i].sme)) |
716 | { | 691 | { |
717 | old_map[i].sme = sme->next; | 692 | old_map[i].sme = sme->next; |
718 | idx = idx_of (map, sme->key); | 693 | idx = idx_of (map, sme->key); |
719 | sme->next = new_map[idx].sme; | 694 | sme->next = new_map[idx].sme; |
720 | new_map[idx].sme = sme; | 695 | new_map[idx].sme = sme; |
721 | } | 696 | } |
722 | } | 697 | } |
723 | else | 698 | else |
@@ -726,10 +701,10 @@ grow (struct GNUNET_CONTAINER_MultiPeerMap *map) | |||
726 | 701 | ||
727 | while (NULL != (bme = old_map[i].bme)) | 702 | while (NULL != (bme = old_map[i].bme)) |
728 | { | 703 | { |
729 | old_map[i].bme = bme->next; | 704 | old_map[i].bme = bme->next; |
730 | idx = idx_of (map, &bme->key); | 705 | idx = idx_of (map, &bme->key); |
731 | bme->next = new_map[idx].bme; | 706 | bme->next = new_map[idx].bme; |
732 | new_map[idx].bme = bme; | 707 | new_map[idx].bme = bme; |
733 | } | 708 | } |
734 | } | 709 | } |
735 | } | 710 | } |
@@ -752,7 +727,7 @@ grow (struct GNUNET_CONTAINER_MultiPeerMap *map) | |||
752 | int | 727 | int |
753 | GNUNET_CONTAINER_multipeermap_put (struct GNUNET_CONTAINER_MultiPeerMap *map, | 728 | GNUNET_CONTAINER_multipeermap_put (struct GNUNET_CONTAINER_MultiPeerMap *map, |
754 | const struct GNUNET_PeerIdentity *key, | 729 | const struct GNUNET_PeerIdentity *key, |
755 | void *value, | 730 | void *value, |
756 | enum GNUNET_CONTAINER_MultiHashMapOption opt) | 731 | enum GNUNET_CONTAINER_MultiHashMapOption opt) |
757 | { | 732 | { |
758 | union MapEntry me; | 733 | union MapEntry me; |
@@ -768,26 +743,26 @@ GNUNET_CONTAINER_multipeermap_put (struct GNUNET_CONTAINER_MultiPeerMap *map, | |||
768 | struct SmallMapEntry *sme; | 743 | struct SmallMapEntry *sme; |
769 | 744 | ||
770 | for (sme = me.sme; NULL != sme; sme = sme->next) | 745 | for (sme = me.sme; NULL != sme; sme = sme->next) |
771 | if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity))) | 746 | if (0 == GNUNET_memcmp (key, sme->key)) |
772 | { | 747 | { |
773 | if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) | 748 | if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) |
774 | return GNUNET_SYSERR; | 749 | return GNUNET_SYSERR; |
775 | sme->value = value; | 750 | sme->value = value; |
776 | return GNUNET_NO; | 751 | return GNUNET_NO; |
777 | } | 752 | } |
778 | } | 753 | } |
779 | else | 754 | else |
780 | { | 755 | { |
781 | struct BigMapEntry *bme; | 756 | struct BigMapEntry *bme; |
782 | 757 | ||
783 | for (bme = me.bme; NULL != bme; bme = bme->next) | 758 | for (bme = me.bme; NULL != bme; bme = bme->next) |
784 | if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity))) | 759 | if (0 == GNUNET_memcmp (key, &bme->key)) |
785 | { | 760 | { |
786 | if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) | 761 | if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) |
787 | return GNUNET_SYSERR; | 762 | return GNUNET_SYSERR; |
788 | bme->value = value; | 763 | bme->value = value; |
789 | return GNUNET_NO; | 764 | return GNUNET_NO; |
790 | } | 765 | } |
791 | } | 766 | } |
792 | } | 767 | } |
793 | if (map->size / 3 >= map->map_length / 4) | 768 | if (map->size / 3 >= map->map_length / 4) |
@@ -831,10 +806,11 @@ GNUNET_CONTAINER_multipeermap_put (struct GNUNET_CONTAINER_MultiPeerMap *map, | |||
831 | * #GNUNET_SYSERR if it aborted iteration | 806 | * #GNUNET_SYSERR if it aborted iteration |
832 | */ | 807 | */ |
833 | int | 808 | int |
834 | GNUNET_CONTAINER_multipeermap_get_multiple (struct GNUNET_CONTAINER_MultiPeerMap *map, | 809 | GNUNET_CONTAINER_multipeermap_get_multiple ( |
835 | const struct GNUNET_PeerIdentity *key, | 810 | struct GNUNET_CONTAINER_MultiPeerMap *map, |
836 | GNUNET_CONTAINER_PeerMapIterator it, | 811 | const struct GNUNET_PeerIdentity *key, |
837 | void *it_cls) | 812 | GNUNET_CONTAINER_PeerMapIterator it, |
813 | void *it_cls) | ||
838 | { | 814 | { |
839 | int count; | 815 | int count; |
840 | union MapEntry me; | 816 | union MapEntry me; |
@@ -852,17 +828,12 @@ GNUNET_CONTAINER_multipeermap_get_multiple (struct GNUNET_CONTAINER_MultiPeerMap | |||
852 | while (NULL != (sme = ce->sme)) | 828 | while (NULL != (sme = ce->sme)) |
853 | { | 829 | { |
854 | ce->sme = sme->next; | 830 | ce->sme = sme->next; |
855 | if (0 != memcmp (key, | 831 | if (0 != GNUNET_memcmp (key, sme->key)) |
856 | sme->key, | 832 | continue; |
857 | sizeof (struct GNUNET_PeerIdentity))) | 833 | if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value))) |
858 | continue; | ||
859 | if ( (NULL != it) && | ||
860 | (GNUNET_OK != it (it_cls, | ||
861 | key, | ||
862 | sme->value))) | ||
863 | { | 834 | { |
864 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); | 835 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); |
865 | return GNUNET_SYSERR; | 836 | return GNUNET_SYSERR; |
866 | } | 837 | } |
867 | count++; | 838 | count++; |
868 | } | 839 | } |
@@ -875,17 +846,12 @@ GNUNET_CONTAINER_multipeermap_get_multiple (struct GNUNET_CONTAINER_MultiPeerMap | |||
875 | while (NULL != (bme = ce->bme)) | 846 | while (NULL != (bme = ce->bme)) |
876 | { | 847 | { |
877 | ce->bme = bme->next; | 848 | ce->bme = bme->next; |
878 | if (0 != memcmp (key, | 849 | if (0 != GNUNET_memcmp (key, &bme->key)) |
879 | &bme->key, | 850 | continue; |
880 | sizeof (struct GNUNET_PeerIdentity))) | 851 | if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value))) |
881 | continue; | ||
882 | if ( (NULL != it) && | ||
883 | (GNUNET_OK != it (it_cls, | ||
884 | key, | ||
885 | bme->value))) | ||
886 | { | 852 | { |
887 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); | 853 | GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); |
888 | return GNUNET_SYSERR; | 854 | return GNUNET_SYSERR; |
889 | } | 855 | } |
890 | count++; | 856 | count++; |
891 | } | 857 | } |
@@ -907,9 +873,10 @@ GNUNET_CONTAINER_multipeermap_get_multiple (struct GNUNET_CONTAINER_MultiPeerMap | |||
907 | * @return the number of key value pairs processed, zero or one. | 873 | * @return the number of key value pairs processed, zero or one. |
908 | */ | 874 | */ |
909 | unsigned int | 875 | unsigned int |
910 | GNUNET_CONTAINER_multipeermap_get_random (const struct GNUNET_CONTAINER_MultiPeerMap *map, | 876 | GNUNET_CONTAINER_multipeermap_get_random ( |
911 | GNUNET_CONTAINER_PeerMapIterator it, | 877 | const struct GNUNET_CONTAINER_MultiPeerMap *map, |
912 | void *it_cls) | 878 | GNUNET_CONTAINER_PeerMapIterator it, |
879 | void *it_cls) | ||
913 | { | 880 | { |
914 | unsigned int off; | 881 | unsigned int off; |
915 | union MapEntry me; | 882 | union MapEntry me; |
@@ -918,8 +885,7 @@ GNUNET_CONTAINER_multipeermap_get_random (const struct GNUNET_CONTAINER_MultiPee | |||
918 | return 0; | 885 | return 0; |
919 | if (NULL == it) | 886 | if (NULL == it) |
920 | return 1; | 887 | return 1; |
921 | off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, | 888 | off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size); |
922 | map->size); | ||
923 | for (unsigned int idx = 0; idx < map->map_length; idx++) | 889 | for (unsigned int idx = 0; idx < map->map_length; idx++) |
924 | { | 890 | { |
925 | me = map->map[idx]; | 891 | me = map->map[idx]; |
@@ -934,9 +900,7 @@ GNUNET_CONTAINER_multipeermap_get_random (const struct GNUNET_CONTAINER_MultiPee | |||
934 | nxt = sme->next; | 900 | nxt = sme->next; |
935 | if (0 == off) | 901 | if (0 == off) |
936 | { | 902 | { |
937 | if (GNUNET_OK != it (it_cls, | 903 | if (GNUNET_OK != it (it_cls, sme->key, sme->value)) |
938 | sme->key, | ||
939 | sme->value)) | ||
940 | return GNUNET_SYSERR; | 904 | return GNUNET_SYSERR; |
941 | return 1; | 905 | return 1; |
942 | } | 906 | } |
@@ -954,11 +918,9 @@ GNUNET_CONTAINER_multipeermap_get_random (const struct GNUNET_CONTAINER_MultiPee | |||
954 | nxt = bme->next; | 918 | nxt = bme->next; |
955 | if (0 == off) | 919 | if (0 == off) |
956 | { | 920 | { |
957 | if (GNUNET_OK != it (it_cls, | 921 | if (GNUNET_OK != it (it_cls, &bme->key, bme->value)) |
958 | &bme->key, | 922 | return GNUNET_SYSERR; |
959 | bme->value)) | 923 | return 1; |
960 | return GNUNET_SYSERR; | ||
961 | return 1; | ||
962 | } | 924 | } |
963 | off--; | 925 | off--; |
964 | } | 926 | } |
@@ -982,7 +944,8 @@ GNUNET_CONTAINER_multipeermap_get_random (const struct GNUNET_CONTAINER_MultiPee | |||
982 | * @return an iterator over the given multipeermap 'map' | 944 | * @return an iterator over the given multipeermap 'map' |
983 | */ | 945 | */ |
984 | struct GNUNET_CONTAINER_MultiPeerMapIterator * | 946 | struct GNUNET_CONTAINER_MultiPeerMapIterator * |
985 | GNUNET_CONTAINER_multipeermap_iterator_create (const struct GNUNET_CONTAINER_MultiPeerMap *map) | 947 | GNUNET_CONTAINER_multipeermap_iterator_create ( |
948 | const struct GNUNET_CONTAINER_MultiPeerMap *map) | ||
986 | { | 949 | { |
987 | struct GNUNET_CONTAINER_MultiPeerMapIterator *iter; | 950 | struct GNUNET_CONTAINER_MultiPeerMapIterator *iter; |
988 | 951 | ||
@@ -1009,8 +972,10 @@ GNUNET_CONTAINER_multipeermap_iterator_create (const struct GNUNET_CONTAINER_Mul | |||
1009 | * #GNUNET_NO if we are out of elements | 972 | * #GNUNET_NO if we are out of elements |
1010 | */ | 973 | */ |
1011 | int | 974 | int |
1012 | GNUNET_CONTAINER_multipeermap_iterator_next (struct GNUNET_CONTAINER_MultiPeerMapIterator *iter, | 975 | GNUNET_CONTAINER_multipeermap_iterator_next ( |
1013 | struct GNUNET_PeerIdentity *key, const void **value) | 976 | struct GNUNET_CONTAINER_MultiPeerMapIterator *iter, |
977 | struct GNUNET_PeerIdentity *key, | ||
978 | const void **value) | ||
1014 | { | 979 | { |
1015 | /* make sure the map has not been modified */ | 980 | /* make sure the map has not been modified */ |
1016 | GNUNET_assert (iter->modification_counter == iter->map->modification_counter); | 981 | GNUNET_assert (iter->modification_counter == iter->map->modification_counter); |
@@ -1057,7 +1022,8 @@ GNUNET_CONTAINER_multipeermap_iterator_next (struct GNUNET_CONTAINER_MultiPeerMa | |||
1057 | * @param iter the iterator to destroy | 1022 | * @param iter the iterator to destroy |
1058 | */ | 1023 | */ |
1059 | void | 1024 | void |
1060 | GNUNET_CONTAINER_multipeermap_iterator_destroy (struct GNUNET_CONTAINER_MultiPeerMapIterator *iter) | 1025 | GNUNET_CONTAINER_multipeermap_iterator_destroy ( |
1026 | struct GNUNET_CONTAINER_MultiPeerMapIterator *iter) | ||
1061 | { | 1027 | { |
1062 | GNUNET_free (iter); | 1028 | GNUNET_free (iter); |
1063 | } | 1029 | } |