summaryrefslogtreecommitdiff
path: root/src/util/container_multiuuidmap.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/container_multiuuidmap.c')
-rw-r--r--src/util/container_multiuuidmap.c795
1 files changed, 394 insertions, 401 deletions
diff --git a/src/util/container_multiuuidmap.c b/src/util/container_multiuuidmap.c
index dd21fd128..78ed7f2b7 100644
--- a/src/util/container_multiuuidmap.c
+++ b/src/util/container_multiuuidmap.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file util/container_multiuuidmap.c 21 * @file util/container_multiuuidmap.c
22 * @brief hash map for UUIDs where the same key may be present multiple times 22 * @brief hash map for UUIDs where the same key may be present multiple times
@@ -27,7 +27,7 @@
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28 28
29#define LOG(kind, ...) \ 29#define LOG(kind, ...) \
30 GNUNET_log_from (kind, "util-container-multiuuidmap", __VA_ARGS__) 30 GNUNET_log_from(kind, "util-container-multiuuidmap", __VA_ARGS__)
31 31
32/** 32/**
33 * Maximum recursion depth for callbacks of 33 * Maximum recursion depth for callbacks of
@@ -41,9 +41,7 @@
41/** 41/**
42 * An entry in the hash map with the full key. 42 * An entry in the hash map with the full key.
43 */ 43 */
44struct BigMapEntry 44struct BigMapEntry {
45{
46
47 /** 45 /**
48 * Value of the entry. 46 * Value of the entry.
49 */ 47 */
@@ -64,9 +62,7 @@ struct BigMapEntry
64/** 62/**
65 * An entry in the hash map with just a pointer to the key. 63 * An entry in the hash map with just a pointer to the key.
66 */ 64 */
67struct SmallMapEntry 65struct SmallMapEntry {
68{
69
70 /** 66 /**
71 * Value of the entry. 67 * Value of the entry.
72 */ 68 */
@@ -87,8 +83,7 @@ struct SmallMapEntry
87/** 83/**
88 * Entry in the map. 84 * Entry in the map.
89 */ 85 */
90union MapEntry 86union MapEntry {
91{
92 /** 87 /**
93 * Variant used if map entries only contain a pointer to the key. 88 * Variant used if map entries only contain a pointer to the key.
94 */ 89 */
@@ -104,8 +99,7 @@ union MapEntry
104/** 99/**
105 * Internal representation of the hash map. 100 * Internal representation of the hash map.
106 */ 101 */
107struct GNUNET_CONTAINER_MultiUuidmap 102struct GNUNET_CONTAINER_MultiUuidmap {
108{
109 /** 103 /**
110 * All of our buckets. 104 * All of our buckets.
111 */ 105 */
@@ -152,8 +146,7 @@ struct GNUNET_CONTAINER_MultiUuidmap
152 * Cursor into a multiuuidmap. 146 * Cursor into a multiuuidmap.
153 * Allows to enumerate elements asynchronously. 147 * Allows to enumerate elements asynchronously.
154 */ 148 */
155struct GNUNET_CONTAINER_MultiUuidmapIterator 149struct GNUNET_CONTAINER_MultiUuidmapIterator {
156{
157 /** 150 /**
158 * Position in the bucket 'idx' 151 * Position in the bucket 'idx'
159 */ 152 */
@@ -193,18 +186,18 @@ struct GNUNET_CONTAINER_MultiUuidmapIterator
193 * @return NULL on error 186 * @return NULL on error
194 */ 187 */
195struct GNUNET_CONTAINER_MultiUuidmap * 188struct GNUNET_CONTAINER_MultiUuidmap *
196GNUNET_CONTAINER_multiuuidmap_create (unsigned int len, int do_not_copy_keys) 189GNUNET_CONTAINER_multiuuidmap_create(unsigned int len, int do_not_copy_keys)
197{ 190{
198 struct GNUNET_CONTAINER_MultiUuidmap *map; 191 struct GNUNET_CONTAINER_MultiUuidmap *map;
199 192
200 GNUNET_assert (len > 0); 193 GNUNET_assert(len > 0);
201 map = GNUNET_new (struct GNUNET_CONTAINER_MultiUuidmap); 194 map = GNUNET_new(struct GNUNET_CONTAINER_MultiUuidmap);
202 map->map = GNUNET_malloc_large (len * sizeof (union MapEntry)); 195 map->map = GNUNET_malloc_large(len * sizeof(union MapEntry));
203 if (NULL == map->map) 196 if (NULL == map->map)
204 { 197 {
205 GNUNET_free (map); 198 GNUNET_free(map);
206 return NULL; 199 return NULL;
207 } 200 }
208 map->map_length = len; 201 map->map_length = len;
209 map->use_small_entries = do_not_copy_keys; 202 map->use_small_entries = do_not_copy_keys;
210 return map; 203 return map;
@@ -218,44 +211,44 @@ GNUNET_CONTAINER_multiuuidmap_create (unsigned int len, int do_not_copy_keys)
218 * @param map the map 211 * @param map the map
219 */ 212 */
220void 213void
221GNUNET_CONTAINER_multiuuidmap_destroy ( 214GNUNET_CONTAINER_multiuuidmap_destroy(
222 struct GNUNET_CONTAINER_MultiUuidmap *map) 215 struct GNUNET_CONTAINER_MultiUuidmap *map)
223{ 216{
224 GNUNET_assert (0 == map->next_cache_off); 217 GNUNET_assert(0 == map->next_cache_off);
225 for (unsigned int i = 0; i < map->map_length; i++) 218 for (unsigned int i = 0; i < map->map_length; i++)
226 {
227 union MapEntry me;
228
229 me = map->map[i];
230 if (map->use_small_entries)
231 {
232 struct SmallMapEntry *sme;
233 struct SmallMapEntry *nxt;
234
235 nxt = me.sme;
236 while (NULL != (sme = nxt))
237 {
238 nxt = sme->next;
239 GNUNET_free (sme);
240 }
241 me.sme = NULL;
242 }
243 else
244 { 219 {
245 struct BigMapEntry *bme; 220 union MapEntry me;
246 struct BigMapEntry *nxt; 221
247 222 me = map->map[i];
248 nxt = me.bme; 223 if (map->use_small_entries)
249 while (NULL != (bme = nxt)) 224 {
250 { 225 struct SmallMapEntry *sme;
251 nxt = bme->next; 226 struct SmallMapEntry *nxt;
252 GNUNET_free (bme); 227
253 } 228 nxt = me.sme;
254 me.bme = NULL; 229 while (NULL != (sme = nxt))
230 {
231 nxt = sme->next;
232 GNUNET_free(sme);
233 }
234 me.sme = NULL;
235 }
236 else
237 {
238 struct BigMapEntry *bme;
239 struct BigMapEntry *nxt;
240
241 nxt = me.bme;
242 while (NULL != (bme = nxt))
243 {
244 nxt = bme->next;
245 GNUNET_free(bme);
246 }
247 me.bme = NULL;
248 }
255 } 249 }
256 } 250 GNUNET_free(map->map);
257 GNUNET_free (map->map); 251 GNUNET_free(map);
258 GNUNET_free (map);
259} 252}
260 253
261 254
@@ -267,13 +260,13 @@ GNUNET_CONTAINER_multiuuidmap_destroy (
267 * @return offset into the "map" array of "map" 260 * @return offset into the "map" array of "map"
268 */ 261 */
269static unsigned int 262static unsigned int
270idx_of (const struct GNUNET_CONTAINER_MultiUuidmap *map, 263idx_of(const struct GNUNET_CONTAINER_MultiUuidmap *map,
271 const struct GNUNET_Uuid *key) 264 const struct GNUNET_Uuid *key)
272{ 265{
273 unsigned int kx; 266 unsigned int kx;
274 267
275 GNUNET_assert (NULL != map); 268 GNUNET_assert(NULL != map);
276 GNUNET_memcpy (&kx, key, sizeof (kx)); 269 GNUNET_memcpy(&kx, key, sizeof(kx));
277 return kx % map->map_length; 270 return kx % map->map_length;
278} 271}
279 272
@@ -285,7 +278,7 @@ idx_of (const struct GNUNET_CONTAINER_MultiUuidmap *map,
285 * @return the number of key value pairs 278 * @return the number of key value pairs
286 */ 279 */
287unsigned int 280unsigned int
288GNUNET_CONTAINER_multiuuidmap_size ( 281GNUNET_CONTAINER_multiuuidmap_size(
289 const struct GNUNET_CONTAINER_MultiUuidmap *map) 282 const struct GNUNET_CONTAINER_MultiUuidmap *map)
290{ 283{
291 return map->size; 284 return map->size;
@@ -303,25 +296,25 @@ GNUNET_CONTAINER_multiuuidmap_size (
303 * key-value pairs with value NULL 296 * key-value pairs with value NULL
304 */ 297 */
305void * 298void *
306GNUNET_CONTAINER_multiuuidmap_get ( 299GNUNET_CONTAINER_multiuuidmap_get(
307 const struct GNUNET_CONTAINER_MultiUuidmap *map, 300 const struct GNUNET_CONTAINER_MultiUuidmap *map,
308 const struct GNUNET_Uuid *key) 301 const struct GNUNET_Uuid *key)
309{ 302{
310 union MapEntry me; 303 union MapEntry me;
311 304
312 me = map->map[idx_of (map, key)]; 305 me = map->map[idx_of(map, key)];
313 if (map->use_small_entries) 306 if (map->use_small_entries)
314 { 307 {
315 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 308 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
316 if (0 == GNUNET_memcmp (key, sme->key)) 309 if (0 == GNUNET_memcmp(key, sme->key))
317 return sme->value; 310 return sme->value;
318 } 311 }
319 else 312 else
320 { 313 {
321 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 314 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
322 if (0 == GNUNET_memcmp (key, &bme->key)) 315 if (0 == GNUNET_memcmp(key, &bme->key))
323 return bme->value; 316 return bme->value;
324 } 317 }
325 return NULL; 318 return NULL;
326} 319}
327 320
@@ -336,7 +329,7 @@ GNUNET_CONTAINER_multiuuidmap_get (
336 * #GNUNET_SYSERR if it aborted iteration 329 * #GNUNET_SYSERR if it aborted iteration
337 */ 330 */
338int 331int
339GNUNET_CONTAINER_multiuuidmap_iterate ( 332GNUNET_CONTAINER_multiuuidmap_iterate(
340 struct GNUNET_CONTAINER_MultiUuidmap *map, 333 struct GNUNET_CONTAINER_MultiUuidmap *map,
341 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it, 334 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it,
342 void *it_cls) 335 void *it_cls)
@@ -347,50 +340,50 @@ GNUNET_CONTAINER_multiuuidmap_iterate (
347 struct GNUNET_Uuid kc; 340 struct GNUNET_Uuid kc;
348 341
349 count = 0; 342 count = 0;
350 GNUNET_assert (NULL != map); 343 GNUNET_assert(NULL != map);
351 ce = &map->next_cache[map->next_cache_off]; 344 ce = &map->next_cache[map->next_cache_off];
352 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE); 345 GNUNET_assert(++map->next_cache_off < NEXT_CACHE_SIZE);
353 for (unsigned int i = 0; i < map->map_length; i++) 346 for (unsigned int i = 0; i < map->map_length; i++)
354 {
355 me = map->map[i];
356 if (map->use_small_entries)
357 { 347 {
358 struct SmallMapEntry *sme; 348 me = map->map[i];
359 349 if (map->use_small_entries)
360 ce->sme = me.sme;
361 while (NULL != (sme = ce->sme))
362 {
363 ce->sme = sme->next;
364 if ((NULL != it) && (GNUNET_OK != it (it_cls, sme->key, sme->value)))
365 { 350 {
366 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); 351 struct SmallMapEntry *sme;
367 return GNUNET_SYSERR; 352
353 ce->sme = me.sme;
354 while (NULL != (sme = ce->sme))
355 {
356 ce->sme = sme->next;
357 if ((NULL != it) && (GNUNET_OK != it(it_cls, sme->key, sme->value)))
358 {
359 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
360 return GNUNET_SYSERR;
361 }
362 count++;
363 }
368 } 364 }
369 count++; 365 else
370 }
371 }
372 else
373 {
374 struct BigMapEntry *bme;
375
376 ce->bme = me.bme;
377 while (NULL != (bme = ce->bme))
378 {
379 ce->bme = bme->next;
380 if (NULL != it)
381 { 366 {
382 kc = bme->key; 367 struct BigMapEntry *bme;
383 if (GNUNET_OK != it (it_cls, &kc, bme->value)) 368
384 { 369 ce->bme = me.bme;
385 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); 370 while (NULL != (bme = ce->bme))
386 return GNUNET_SYSERR; 371 {
387 } 372 ce->bme = bme->next;
373 if (NULL != it)
374 {
375 kc = bme->key;
376 if (GNUNET_OK != it(it_cls, &kc, bme->value))
377 {
378 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
379 return GNUNET_SYSERR;
380 }
381 }
382 count++;
383 }
388 } 384 }
389 count++;
390 }
391 } 385 }
392 } 386 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
393 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
394 return count; 387 return count;
395} 388}
396 389
@@ -403,8 +396,8 @@ GNUNET_CONTAINER_multiuuidmap_iterate (
403 * @param bme the entry that is about to be free'd 396 * @param bme the entry that is about to be free'd
404 */ 397 */
405static void 398static void
406update_next_cache_bme (struct GNUNET_CONTAINER_MultiUuidmap *map, 399update_next_cache_bme(struct GNUNET_CONTAINER_MultiUuidmap *map,
407 const struct BigMapEntry *bme) 400 const struct BigMapEntry *bme)
408{ 401{
409 for (unsigned int i = 0; i < map->next_cache_off; i++) 402 for (unsigned int i = 0; i < map->next_cache_off; i++)
410 if (map->next_cache[i].bme == bme) 403 if (map->next_cache[i].bme == bme)
@@ -420,8 +413,8 @@ update_next_cache_bme (struct GNUNET_CONTAINER_MultiUuidmap *map,
420 * @param sme the entry that is about to be free'd 413 * @param sme the entry that is about to be free'd
421 */ 414 */
422static void 415static void
423update_next_cache_sme (struct GNUNET_CONTAINER_MultiUuidmap *map, 416update_next_cache_sme(struct GNUNET_CONTAINER_MultiUuidmap *map,
424 const struct SmallMapEntry *sme) 417 const struct SmallMapEntry *sme)
425{ 418{
426 for (unsigned int i = 0; i < map->next_cache_off; i++) 419 for (unsigned int i = 0; i < map->next_cache_off; i++)
427 if (map->next_cache[i].sme == sme) 420 if (map->next_cache[i].sme == sme)
@@ -441,56 +434,56 @@ update_next_cache_sme (struct GNUNET_CONTAINER_MultiUuidmap *map,
441 * is not in the map 434 * is not in the map
442 */ 435 */
443int 436int
444GNUNET_CONTAINER_multiuuidmap_remove (struct GNUNET_CONTAINER_MultiUuidmap *map, 437GNUNET_CONTAINER_multiuuidmap_remove(struct GNUNET_CONTAINER_MultiUuidmap *map,
445 const struct GNUNET_Uuid *key, 438 const struct GNUNET_Uuid *key,
446 const void *value) 439 const void *value)
447{ 440{
448 union MapEntry me; 441 union MapEntry me;
449 unsigned int i; 442 unsigned int i;
450 443
451 map->modification_counter++; 444 map->modification_counter++;
452 i = idx_of (map, key); 445 i = idx_of(map, key);
453 me = map->map[i]; 446 me = map->map[i];
454 if (map->use_small_entries) 447 if (map->use_small_entries)
455 {
456 struct SmallMapEntry *p = NULL;
457
458 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
459 { 448 {
460 if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value)) 449 struct SmallMapEntry *p = NULL;
461 { 450
462 if (NULL == p) 451 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
463 map->map[i].sme = sme->next; 452 {
464 else 453 if ((0 == GNUNET_memcmp(key, sme->key)) && (value == sme->value))
465 p->next = sme->next; 454 {
466 update_next_cache_sme (map, sme); 455 if (NULL == p)
467 GNUNET_free (sme); 456 map->map[i].sme = sme->next;
468 map->size--; 457 else
469 return GNUNET_YES; 458 p->next = sme->next;
470 } 459 update_next_cache_sme(map, sme);
471 p = sme; 460 GNUNET_free(sme);
461 map->size--;
462 return GNUNET_YES;
463 }
464 p = sme;
465 }
472 } 466 }
473 }
474 else 467 else
475 {
476 struct BigMapEntry *p = NULL;
477
478 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
479 { 468 {
480 if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value)) 469 struct BigMapEntry *p = NULL;
481 { 470
482 if (NULL == p) 471 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
483 map->map[i].bme = bme->next; 472 {
484 else 473 if ((0 == GNUNET_memcmp(key, &bme->key)) && (value == bme->value))
485 p->next = bme->next; 474 {
486 update_next_cache_bme (map, bme); 475 if (NULL == p)
487 GNUNET_free (bme); 476 map->map[i].bme = bme->next;
488 map->size--; 477 else
489 return GNUNET_YES; 478 p->next = bme->next;
490 } 479 update_next_cache_bme(map, bme);
491 p = bme; 480 GNUNET_free(bme);
481 map->size--;
482 return GNUNET_YES;
483 }
484 p = bme;
485 }
492 } 486 }
493 }
494 return GNUNET_NO; 487 return GNUNET_NO;
495} 488}
496 489
@@ -504,7 +497,7 @@ GNUNET_CONTAINER_multiuuidmap_remove (struct GNUNET_CONTAINER_MultiUuidmap *map,
504 * @return number of values removed 497 * @return number of values removed
505 */ 498 */
506int 499int
507GNUNET_CONTAINER_multiuuidmap_remove_all ( 500GNUNET_CONTAINER_multiuuidmap_remove_all(
508 struct GNUNET_CONTAINER_MultiUuidmap *map, 501 struct GNUNET_CONTAINER_MultiUuidmap *map,
509 const struct GNUNET_Uuid *key) 502 const struct GNUNET_Uuid *key)
510{ 503{
@@ -515,70 +508,70 @@ GNUNET_CONTAINER_multiuuidmap_remove_all (
515 map->modification_counter++; 508 map->modification_counter++;
516 509
517 ret = 0; 510 ret = 0;
518 i = idx_of (map, key); 511 i = idx_of(map, key);
519 me = map->map[i]; 512 me = map->map[i];
520 if (map->use_small_entries) 513 if (map->use_small_entries)
521 {
522 struct SmallMapEntry *sme;
523 struct SmallMapEntry *p;
524
525 p = NULL;
526 sme = me.sme;
527 while (NULL != sme)
528 { 514 {
529 if (0 == GNUNET_memcmp (key, sme->key)) 515 struct SmallMapEntry *sme;
530 { 516 struct SmallMapEntry *p;
531 if (NULL == p) 517
532 map->map[i].sme = sme->next; 518 p = NULL;
533 else 519 sme = me.sme;
534 p->next = sme->next; 520 while (NULL != sme)
535 update_next_cache_sme (map, sme); 521 {
536 GNUNET_free (sme); 522 if (0 == GNUNET_memcmp(key, sme->key))
537 map->size--; 523 {
538 if (NULL == p) 524 if (NULL == p)
539 sme = map->map[i].sme; 525 map->map[i].sme = sme->next;
540 else 526 else
541 sme = p->next; 527 p->next = sme->next;
542 ret++; 528 update_next_cache_sme(map, sme);
543 } 529 GNUNET_free(sme);
544 else 530 map->size--;
545 { 531 if (NULL == p)
546 p = sme; 532 sme = map->map[i].sme;
547 sme = sme->next; 533 else
548 } 534 sme = p->next;
535 ret++;
536 }
537 else
538 {
539 p = sme;
540 sme = sme->next;
541 }
542 }
549 } 543 }
550 }
551 else 544 else
552 {
553 struct BigMapEntry *bme;
554 struct BigMapEntry *p;
555
556 p = NULL;
557 bme = me.bme;
558 while (NULL != bme)
559 { 545 {
560 if (0 == GNUNET_memcmp (key, &bme->key)) 546 struct BigMapEntry *bme;
561 { 547 struct BigMapEntry *p;
562 if (NULL == p) 548
563 map->map[i].bme = bme->next; 549 p = NULL;
564 else 550 bme = me.bme;
565 p->next = bme->next; 551 while (NULL != bme)
566 update_next_cache_bme (map, bme); 552 {
567 GNUNET_free (bme); 553 if (0 == GNUNET_memcmp(key, &bme->key))
568 map->size--; 554 {
569 if (NULL == p) 555 if (NULL == p)
570 bme = map->map[i].bme; 556 map->map[i].bme = bme->next;
571 else 557 else
572 bme = p->next; 558 p->next = bme->next;
573 ret++; 559 update_next_cache_bme(map, bme);
574 } 560 GNUNET_free(bme);
575 else 561 map->size--;
576 { 562 if (NULL == p)
577 p = bme; 563 bme = map->map[i].bme;
578 bme = bme->next; 564 else
579 } 565 bme = p->next;
566 ret++;
567 }
568 else
569 {
570 p = bme;
571 bme = bme->next;
572 }
573 }
580 } 574 }
581 }
582 return ret; 575 return ret;
583} 576}
584 577
@@ -593,25 +586,25 @@ GNUNET_CONTAINER_multiuuidmap_remove_all (
593 * #GNUNET_NO if not 586 * #GNUNET_NO if not
594 */ 587 */
595int 588int
596GNUNET_CONTAINER_multiuuidmap_contains ( 589GNUNET_CONTAINER_multiuuidmap_contains(
597 const struct GNUNET_CONTAINER_MultiUuidmap *map, 590 const struct GNUNET_CONTAINER_MultiUuidmap *map,
598 const struct GNUNET_Uuid *key) 591 const struct GNUNET_Uuid *key)
599{ 592{
600 union MapEntry me; 593 union MapEntry me;
601 594
602 me = map->map[idx_of (map, key)]; 595 me = map->map[idx_of(map, key)];
603 if (map->use_small_entries) 596 if (map->use_small_entries)
604 { 597 {
605 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 598 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
606 if (0 == GNUNET_memcmp (key, sme->key)) 599 if (0 == GNUNET_memcmp(key, sme->key))
607 return GNUNET_YES; 600 return GNUNET_YES;
608 } 601 }
609 else 602 else
610 { 603 {
611 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 604 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
612 if (0 == GNUNET_memcmp (key, &bme->key)) 605 if (0 == GNUNET_memcmp(key, &bme->key))
613 return GNUNET_YES; 606 return GNUNET_YES;
614 } 607 }
615 return GNUNET_NO; 608 return GNUNET_NO;
616} 609}
617 610
@@ -627,26 +620,26 @@ GNUNET_CONTAINER_multiuuidmap_contains (
627 * #GNUNET_NO if not 620 * #GNUNET_NO if not
628 */ 621 */
629int 622int
630GNUNET_CONTAINER_multiuuidmap_contains_value ( 623GNUNET_CONTAINER_multiuuidmap_contains_value(
631 const struct GNUNET_CONTAINER_MultiUuidmap *map, 624 const struct GNUNET_CONTAINER_MultiUuidmap *map,
632 const struct GNUNET_Uuid *key, 625 const struct GNUNET_Uuid *key,
633 const void *value) 626 const void *value)
634{ 627{
635 union MapEntry me; 628 union MapEntry me;
636 629
637 me = map->map[idx_of (map, key)]; 630 me = map->map[idx_of(map, key)];
638 if (map->use_small_entries) 631 if (map->use_small_entries)
639 { 632 {
640 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 633 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
641 if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value)) 634 if ((0 == GNUNET_memcmp(key, sme->key)) && (sme->value == value))
642 return GNUNET_YES; 635 return GNUNET_YES;
643 } 636 }
644 else 637 else
645 { 638 {
646 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next) 639 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
647 if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value)) 640 if ((0 == GNUNET_memcmp(key, &bme->key)) && (bme->value == value))
648 return GNUNET_YES; 641 return GNUNET_YES;
649 } 642 }
650 return GNUNET_NO; 643 return GNUNET_NO;
651} 644}
652 645
@@ -657,7 +650,7 @@ GNUNET_CONTAINER_multiuuidmap_contains_value (
657 * @param map the hash map to grow 650 * @param map the hash map to grow
658 */ 651 */
659static void 652static void
660grow (struct GNUNET_CONTAINER_MultiUuidmap *map) 653grow(struct GNUNET_CONTAINER_MultiUuidmap *map)
661{ 654{
662 union MapEntry *old_map; 655 union MapEntry *old_map;
663 union MapEntry *new_map; 656 union MapEntry *new_map;
@@ -672,40 +665,40 @@ grow (struct GNUNET_CONTAINER_MultiUuidmap *map)
672 new_len = old_len; /* never use 0 */ 665 new_len = old_len; /* never use 0 */
673 if (new_len == old_len) 666 if (new_len == old_len)
674 return; /* nothing changed */ 667 return; /* nothing changed */
675 new_map = GNUNET_malloc_large (new_len * sizeof (union MapEntry)); 668 new_map = GNUNET_malloc_large(new_len * sizeof(union MapEntry));
676 if (NULL == new_map) 669 if (NULL == new_map)
677 return; /* grow not possible */ 670 return; /* grow not possible */
678 map->modification_counter++; 671 map->modification_counter++;
679 map->map_length = new_len; 672 map->map_length = new_len;
680 map->map = new_map; 673 map->map = new_map;
681 for (unsigned int i = 0; i < old_len; i++) 674 for (unsigned int i = 0; i < old_len; i++)
682 {
683 if (map->use_small_entries)
684 {
685 struct SmallMapEntry *sme;
686
687 while (NULL != (sme = old_map[i].sme))
688 {
689 old_map[i].sme = sme->next;
690 idx = idx_of (map, sme->key);
691 sme->next = new_map[idx].sme;
692 new_map[idx].sme = sme;
693 }
694 }
695 else
696 { 675 {
697 struct BigMapEntry *bme; 676 if (map->use_small_entries)
698 677 {
699 while (NULL != (bme = old_map[i].bme)) 678 struct SmallMapEntry *sme;
700 { 679
701 old_map[i].bme = bme->next; 680 while (NULL != (sme = old_map[i].sme))
702 idx = idx_of (map, &bme->key); 681 {
703 bme->next = new_map[idx].bme; 682 old_map[i].sme = sme->next;
704 new_map[idx].bme = bme; 683 idx = idx_of(map, sme->key);
705 } 684 sme->next = new_map[idx].sme;
685 new_map[idx].sme = sme;
686 }
687 }
688 else
689 {
690 struct BigMapEntry *bme;
691
692 while (NULL != (bme = old_map[i].bme))
693 {
694 old_map[i].bme = bme->next;
695 idx = idx_of(map, &bme->key);
696 bme->next = new_map[idx].bme;
697 new_map[idx].bme = bme;
698 }
699 }
706 } 700 }
707 } 701 GNUNET_free(old_map);
708 GNUNET_free (old_map);
709} 702}
710 703
711 704
@@ -722,67 +715,67 @@ grow (struct GNUNET_CONTAINER_MultiUuidmap *map)
722 * value already exists 715 * value already exists
723 */ 716 */
724int 717int
725GNUNET_CONTAINER_multiuuidmap_put (struct GNUNET_CONTAINER_MultiUuidmap *map, 718GNUNET_CONTAINER_multiuuidmap_put(struct GNUNET_CONTAINER_MultiUuidmap *map,
726 const struct GNUNET_Uuid *key, 719 const struct GNUNET_Uuid *key,
727 void *value, 720 void *value,
728 enum GNUNET_CONTAINER_MultiHashMapOption opt) 721 enum GNUNET_CONTAINER_MultiHashMapOption opt)
729{ 722{
730 union MapEntry me; 723 union MapEntry me;
731 unsigned int i; 724 unsigned int i;
732 725
733 i = idx_of (map, key); 726 i = idx_of(map, key);
734 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) && 727 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
735 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) 728 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
736 {
737 me = map->map[i];
738 if (map->use_small_entries)
739 { 729 {
740 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 730 me = map->map[i];
741 if (0 == GNUNET_memcmp (key, sme->key)) 731 if (map->use_small_entries)
742 { 732 {
743 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) 733 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
744 return GNUNET_SYSERR; 734 if (0 == GNUNET_memcmp(key, sme->key))
745 sme->value = value; 735 {
746 return GNUNET_NO; 736 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
737 return GNUNET_SYSERR;
738 sme->value = value;
739 return GNUNET_NO;
740 }
747 } 741 }
748 } 742 else
749 else
750 {
751 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
752 if (0 == GNUNET_memcmp (key, &bme->key))
753 { 743 {
754 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) 744 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
755 return GNUNET_SYSERR; 745 if (0 == GNUNET_memcmp(key, &bme->key))
756 bme->value = value; 746 {
757 return GNUNET_NO; 747 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
748 return GNUNET_SYSERR;
749 bme->value = value;
750 return GNUNET_NO;
751 }
758 } 752 }
759 } 753 }
760 }
761 if (map->size / 3 >= map->map_length / 4) 754 if (map->size / 3 >= map->map_length / 4)
762 { 755 {
763 grow (map); 756 grow(map);
764 i = idx_of (map, key); 757 i = idx_of(map, key);
765 } 758 }
766 if (map->use_small_entries) 759 if (map->use_small_entries)
767 { 760 {
768 struct SmallMapEntry *sme; 761 struct SmallMapEntry *sme;
769 762
770 sme = GNUNET_new (struct SmallMapEntry); 763 sme = GNUNET_new(struct SmallMapEntry);
771 sme->key = key; 764 sme->key = key;
772 sme->value = value; 765 sme->value = value;
773 sme->next = map->map[i].sme; 766 sme->next = map->map[i].sme;
774 map->map[i].sme = sme; 767 map->map[i].sme = sme;
775 } 768 }
776 else 769 else
777 { 770 {
778 struct BigMapEntry *bme; 771 struct BigMapEntry *bme;
779 772
780 bme = GNUNET_new (struct BigMapEntry); 773 bme = GNUNET_new(struct BigMapEntry);
781 bme->key = *key; 774 bme->key = *key;
782 bme->value = value; 775 bme->value = value;
783 bme->next = map->map[i].bme; 776 bme->next = map->map[i].bme;
784 map->map[i].bme = bme; 777 map->map[i].bme = bme;
785 } 778 }
786 map->size++; 779 map->size++;
787 return GNUNET_OK; 780 return GNUNET_OK;
788} 781}
@@ -799,7 +792,7 @@ GNUNET_CONTAINER_multiuuidmap_put (struct GNUNET_CONTAINER_MultiUuidmap *map,
799 * #GNUNET_SYSERR if it aborted iteration 792 * #GNUNET_SYSERR if it aborted iteration
800 */ 793 */
801int 794int
802GNUNET_CONTAINER_multiuuidmap_get_multiple ( 795GNUNET_CONTAINER_multiuuidmap_get_multiple(
803 struct GNUNET_CONTAINER_MultiUuidmap *map, 796 struct GNUNET_CONTAINER_MultiUuidmap *map,
804 const struct GNUNET_Uuid *key, 797 const struct GNUNET_Uuid *key,
805 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it, 798 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it,
@@ -810,46 +803,46 @@ GNUNET_CONTAINER_multiuuidmap_get_multiple (
810 union MapEntry *ce; 803 union MapEntry *ce;
811 804
812 ce = &map->next_cache[map->next_cache_off]; 805 ce = &map->next_cache[map->next_cache_off];
813 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE); 806 GNUNET_assert(++map->next_cache_off < NEXT_CACHE_SIZE);
814 count = 0; 807 count = 0;
815 me = map->map[idx_of (map, key)]; 808 me = map->map[idx_of(map, key)];
816 if (map->use_small_entries) 809 if (map->use_small_entries)
817 {
818 struct SmallMapEntry *sme;
819
820 ce->sme = me.sme;
821 while (NULL != (sme = ce->sme))
822 { 810 {
823 ce->sme = sme->next; 811 struct SmallMapEntry *sme;
824 if (0 != GNUNET_memcmp (key, sme->key)) 812
825 continue; 813 ce->sme = me.sme;
826 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value))) 814 while (NULL != (sme = ce->sme))
827 { 815 {
828 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); 816 ce->sme = sme->next;
829 return GNUNET_SYSERR; 817 if (0 != GNUNET_memcmp(key, sme->key))
830 } 818 continue;
831 count++; 819 if ((NULL != it) && (GNUNET_OK != it(it_cls, key, sme->value)))
820 {
821 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
822 return GNUNET_SYSERR;
823 }
824 count++;
825 }
832 } 826 }
833 }
834 else 827 else
835 {
836 struct BigMapEntry *bme;
837
838 ce->bme = me.bme;
839 while (NULL != (bme = ce->bme))
840 { 828 {
841 ce->bme = bme->next; 829 struct BigMapEntry *bme;
842 if (0 != GNUNET_memcmp (key, &bme->key)) 830
843 continue; 831 ce->bme = me.bme;
844 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value))) 832 while (NULL != (bme = ce->bme))
845 { 833 {
846 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE); 834 ce->bme = bme->next;
847 return GNUNET_SYSERR; 835 if (0 != GNUNET_memcmp(key, &bme->key))
848 } 836 continue;
849 count++; 837 if ((NULL != it) && (GNUNET_OK != it(it_cls, key, bme->value)))
838 {
839 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
840 return GNUNET_SYSERR;
841 }
842 count++;
843 }
850 } 844 }
851 } 845 GNUNET_assert(--map->next_cache_off < NEXT_CACHE_SIZE);
852 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
853 return count; 846 return count;
854} 847}
855 848
@@ -866,7 +859,7 @@ GNUNET_CONTAINER_multiuuidmap_get_multiple (
866 * @return the number of key value pairs processed, zero or one. 859 * @return the number of key value pairs processed, zero or one.
867 */ 860 */
868unsigned int 861unsigned int
869GNUNET_CONTAINER_multiuuidmap_get_random ( 862GNUNET_CONTAINER_multiuuidmap_get_random(
870 const struct GNUNET_CONTAINER_MultiUuidmap *map, 863 const struct GNUNET_CONTAINER_MultiUuidmap *map,
871 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it, 864 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it,
872 void *it_cls) 865 void *it_cls)
@@ -878,38 +871,38 @@ GNUNET_CONTAINER_multiuuidmap_get_random (
878 return 0; 871 return 0;
879 if (NULL == it) 872 if (NULL == it)
880 return 1; 873 return 1;
881 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size); 874 off = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, map->size);
882 for (unsigned int idx = 0; idx < map->map_length; idx++) 875 for (unsigned int idx = 0; idx < map->map_length; idx++)
883 {
884 me = map->map[idx];
885 if (map->use_small_entries)
886 { 876 {
887 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next) 877 me = map->map[idx];
888 { 878 if (map->use_small_entries)
889 if (0 == off)
890 { 879 {
891 if (GNUNET_OK != it (it_cls, sme->key, sme->value)) 880 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
892 return GNUNET_SYSERR; 881 {
893 return 1; 882 if (0 == off)
883 {
884 if (GNUNET_OK != it(it_cls, sme->key, sme->value))
885 return GNUNET_SYSERR;
886 return 1;
887 }
888 off--;
889 }
894 } 890 }
895 off--; 891 else
896 }
897 }
898 else
899 {
900 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
901 {
902 if (0 == off)
903 { 892 {
904 if (GNUNET_OK != it (it_cls, &bme->key, bme->value)) 893 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
905 return GNUNET_SYSERR; 894 {
906 return 1; 895 if (0 == off)
896 {
897 if (GNUNET_OK != it(it_cls, &bme->key, bme->value))
898 return GNUNET_SYSERR;
899 return 1;
900 }
901 off--;
902 }
907 } 903 }
908 off--;
909 }
910 } 904 }
911 } 905 GNUNET_break(0);
912 GNUNET_break (0);
913 return GNUNET_SYSERR; 906 return GNUNET_SYSERR;
914} 907}
915 908
@@ -927,12 +920,12 @@ GNUNET_CONTAINER_multiuuidmap_get_random (
927 * @return an iterator over the given multiuuidmap 'map' 920 * @return an iterator over the given multiuuidmap 'map'
928 */ 921 */
929struct GNUNET_CONTAINER_MultiUuidmapIterator * 922struct GNUNET_CONTAINER_MultiUuidmapIterator *
930GNUNET_CONTAINER_multiuuidmap_iterator_create ( 923GNUNET_CONTAINER_multiuuidmap_iterator_create(
931 const struct GNUNET_CONTAINER_MultiUuidmap *map) 924 const struct GNUNET_CONTAINER_MultiUuidmap *map)
932{ 925{
933 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter; 926 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter;
934 927
935 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiUuidmapIterator); 928 iter = GNUNET_new(struct GNUNET_CONTAINER_MultiUuidmapIterator);
936 iter->map = map; 929 iter->map = map;
937 iter->modification_counter = map->modification_counter; 930 iter->modification_counter = map->modification_counter;
938 iter->me = map->map[0]; 931 iter->me = map->map[0];
@@ -955,47 +948,47 @@ GNUNET_CONTAINER_multiuuidmap_iterator_create (
955 * #GNUNET_NO if we are out of elements 948 * #GNUNET_NO if we are out of elements
956 */ 949 */
957int 950int
958GNUNET_CONTAINER_multiuuidmap_iterator_next ( 951GNUNET_CONTAINER_multiuuidmap_iterator_next(
959 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter, 952 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter,
960 struct GNUNET_Uuid *key, 953 struct GNUNET_Uuid *key,
961 const void **value) 954 const void **value)
962{ 955{
963 /* make sure the map has not been modified */ 956 /* make sure the map has not been modified */
964 GNUNET_assert (iter->modification_counter == iter->map->modification_counter); 957 GNUNET_assert(iter->modification_counter == iter->map->modification_counter);
965 958
966 /* look for the next entry, skipping empty buckets */ 959 /* look for the next entry, skipping empty buckets */
967 while (1) 960 while (1)
968 {
969 if (iter->idx >= iter->map->map_length)
970 return GNUNET_NO;
971 if (GNUNET_YES == iter->map->use_small_entries)
972 { 961 {
973 if (NULL != iter->me.sme) 962 if (iter->idx >= iter->map->map_length)
974 { 963 return GNUNET_NO;
975 if (NULL != key) 964 if (GNUNET_YES == iter->map->use_small_entries)
976 *key = *iter->me.sme->key; 965 {
977 if (NULL != value) 966 if (NULL != iter->me.sme)
978 *value = iter->me.sme->value; 967 {
979 iter->me.sme = iter->me.sme->next; 968 if (NULL != key)
980 return GNUNET_YES; 969 *key = *iter->me.sme->key;
981 } 970 if (NULL != value)
982 } 971 *value = iter->me.sme->value;
983 else 972 iter->me.sme = iter->me.sme->next;
984 { 973 return GNUNET_YES;
985 if (NULL != iter->me.bme) 974 }
986 { 975 }
987 if (NULL != key) 976 else
988 *key = iter->me.bme->key; 977 {
989 if (NULL != value) 978 if (NULL != iter->me.bme)
990 *value = iter->me.bme->value; 979 {
991 iter->me.bme = iter->me.bme->next; 980 if (NULL != key)
992 return GNUNET_YES; 981 *key = iter->me.bme->key;
993 } 982 if (NULL != value)
983 *value = iter->me.bme->value;
984 iter->me.bme = iter->me.bme->next;
985 return GNUNET_YES;
986 }
987 }
988 iter->idx += 1;
989 if (iter->idx < iter->map->map_length)
990 iter->me = iter->map->map[iter->idx];
994 } 991 }
995 iter->idx += 1;
996 if (iter->idx < iter->map->map_length)
997 iter->me = iter->map->map[iter->idx];
998 }
999} 992}
1000 993
1001 994
@@ -1005,10 +998,10 @@ GNUNET_CONTAINER_multiuuidmap_iterator_next (
1005 * @param iter the iterator to destroy 998 * @param iter the iterator to destroy
1006 */ 999 */
1007void 1000void
1008GNUNET_CONTAINER_multiuuidmap_iterator_destroy ( 1001GNUNET_CONTAINER_multiuuidmap_iterator_destroy(
1009 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter) 1002 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter)
1010{ 1003{
1011 GNUNET_free (iter); 1004 GNUNET_free(iter);
1012} 1005}
1013 1006
1014 1007