diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-02-08 15:20:15 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-02-08 15:20:15 +0000 |
commit | 23818fb1b0be92a7c840fbc24eb6c4efa44b98cd (patch) | |
tree | 5cee6d08c632ca6714495760b9b89227bd3b8e56 /src/ats/gnunet-service-ats_preferences.c | |
parent | 632fa6c46a8ea3595be1f10f69ef72a5d051b049 (diff) | |
download | gnunet-23818fb1b0be92a7c840fbc24eb6c4efa44b98cd.tar.gz gnunet-23818fb1b0be92a7c840fbc24eb6c4efa44b98cd.zip |
cleaning up preference_peers as well once they are no longer needed
Diffstat (limited to 'src/ats/gnunet-service-ats_preferences.c')
-rw-r--r-- | src/ats/gnunet-service-ats_preferences.c | 158 |
1 files changed, 88 insertions, 70 deletions
diff --git a/src/ats/gnunet-service-ats_preferences.c b/src/ats/gnunet-service-ats_preferences.c index 46c2073f4..3be6d3525 100644 --- a/src/ats/gnunet-service-ats_preferences.c +++ b/src/ats/gnunet-service-ats_preferences.c | |||
@@ -64,6 +64,12 @@ struct PeerRelative | |||
64 | */ | 64 | */ |
65 | double f_rel[GNUNET_ATS_PreferenceCount]; | 65 | double f_rel[GNUNET_ATS_PreferenceCount]; |
66 | 66 | ||
67 | /** | ||
68 | * Number of clients that are expressing a preference for | ||
69 | * this peer. When this counter reaches zero, this entry | ||
70 | * is freed. | ||
71 | */ | ||
72 | unsigned int num_clients; | ||
67 | }; | 73 | }; |
68 | 74 | ||
69 | 75 | ||
@@ -249,6 +255,67 @@ update_relative_values_for_peer (const struct GNUNET_PeerIdentity *id, | |||
249 | 255 | ||
250 | 256 | ||
251 | /** | 257 | /** |
258 | * Free a peer's `struct PeerRelative`. | ||
259 | * | ||
260 | * @param cls unused | ||
261 | * @param key the key | ||
262 | * @param value the `struct PeerRelative` to free. | ||
263 | * @return #GNUNET_OK to continue | ||
264 | */ | ||
265 | static int | ||
266 | free_peer (void *cls, | ||
267 | const struct GNUNET_PeerIdentity *key, | ||
268 | void *value) | ||
269 | { | ||
270 | struct PeerRelative *rp = value; | ||
271 | |||
272 | GNUNET_assert (GNUNET_YES == | ||
273 | GNUNET_CONTAINER_multipeermap_remove (preference_peers, | ||
274 | key, | ||
275 | value)); | ||
276 | GNUNET_free (rp); | ||
277 | return GNUNET_OK; | ||
278 | } | ||
279 | |||
280 | |||
281 | /** | ||
282 | * Free `struct PreferencePeer` entry in map. | ||
283 | * | ||
284 | * @param cls the `struct PreferenceClient` with the map | ||
285 | * @param key the peer the entry is for | ||
286 | * @param value the `struct PreferencePeer` entry to free | ||
287 | * @return #GNUNET_OK (continue to iterate) | ||
288 | */ | ||
289 | static int | ||
290 | free_preference (void *cls, | ||
291 | const struct GNUNET_PeerIdentity *key, | ||
292 | void *value) | ||
293 | { | ||
294 | struct PreferenceClient *pc = cls; | ||
295 | struct PreferencePeer *p = value; | ||
296 | struct PeerRelative *pr; | ||
297 | |||
298 | GNUNET_assert (GNUNET_OK == | ||
299 | GNUNET_CONTAINER_multipeermap_remove (pc->peer2pref, | ||
300 | key, | ||
301 | p)); | ||
302 | GNUNET_free (p); | ||
303 | pr = GNUNET_CONTAINER_multipeermap_get (preference_peers, | ||
304 | key); | ||
305 | GNUNET_assert (NULL != pr); | ||
306 | GNUNET_assert (pr->num_clients > 0); | ||
307 | pr->num_clients--; | ||
308 | if (0 == pr->num_clients) | ||
309 | { | ||
310 | free_peer (NULL, | ||
311 | key, | ||
312 | pr); | ||
313 | } | ||
314 | return GNUNET_OK; | ||
315 | } | ||
316 | |||
317 | |||
318 | /** | ||
252 | * Closure for #age_values(). | 319 | * Closure for #age_values(). |
253 | */ | 320 | */ |
254 | struct AgeContext | 321 | struct AgeContext |
@@ -309,12 +376,9 @@ age_values (void *cls, | |||
309 | if (GNUNET_YES == dead) | 376 | if (GNUNET_YES == dead) |
310 | { | 377 | { |
311 | /* all preferences are zero, remove this entry */ | 378 | /* all preferences are zero, remove this entry */ |
312 | GNUNET_CONTAINER_multipeermap_remove (ac->cur_client->peer2pref, | 379 | free_preference (ac->cur_client, |
313 | peer, | 380 | peer, |
314 | p); | 381 | p); |
315 | GNUNET_free (p); | ||
316 | /* FIXME: Consider destroying the `struct PeerRelative` | ||
317 | as well here... */ | ||
318 | } | 382 | } |
319 | return GNUNET_OK; | 383 | return GNUNET_OK; |
320 | } | 384 | } |
@@ -526,6 +590,23 @@ update_preference (struct GNUNET_SERVER_Client *client, | |||
526 | pc_tail, | 590 | pc_tail, |
527 | c_cur); | 591 | c_cur); |
528 | } | 592 | } |
593 | |||
594 | /* check global peer entry exists */ | ||
595 | if (NULL == | ||
596 | (r_cur = GNUNET_CONTAINER_multipeermap_get (preference_peers, | ||
597 | peer))) | ||
598 | { | ||
599 | /* Create struct for peer */ | ||
600 | r_cur = GNUNET_new (struct PeerRelative); | ||
601 | for (i = 0; i < GNUNET_ATS_PreferenceCount; i++) | ||
602 | r_cur->f_rel[i] = DEFAULT_REL_PREFERENCE; | ||
603 | GNUNET_assert (GNUNET_OK == | ||
604 | GNUNET_CONTAINER_multipeermap_put (preference_peers, | ||
605 | peer, | ||
606 | r_cur, | ||
607 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | ||
608 | } | ||
609 | |||
529 | /* Find entry for peer */ | 610 | /* Find entry for peer */ |
530 | p_cur = GNUNET_CONTAINER_multipeermap_get (c_cur->peer2pref, | 611 | p_cur = GNUNET_CONTAINER_multipeermap_get (c_cur->peer2pref, |
531 | peer); | 612 | peer); |
@@ -545,21 +626,7 @@ update_preference (struct GNUNET_SERVER_Client *client, | |||
545 | peer, | 626 | peer, |
546 | p_cur, | 627 | p_cur, |
547 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | 628 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); |
548 | } | 629 | r_cur->num_clients++; |
549 | |||
550 | if (NULL == | ||
551 | GNUNET_CONTAINER_multipeermap_get (preference_peers, | ||
552 | peer)) | ||
553 | { | ||
554 | /* Create struct for peer */ | ||
555 | r_cur = GNUNET_new (struct PeerRelative); | ||
556 | for (i = 0; i < GNUNET_ATS_PreferenceCount; i++) | ||
557 | r_cur->f_rel[i] = DEFAULT_REL_PREFERENCE; | ||
558 | GNUNET_assert (GNUNET_OK == | ||
559 | GNUNET_CONTAINER_multipeermap_put (preference_peers, | ||
560 | peer, | ||
561 | r_cur, | ||
562 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | ||
563 | } | 630 | } |
564 | 631 | ||
565 | p_cur->f_abs[kind] += score_abs; | 632 | p_cur->f_abs[kind] += score_abs; |
@@ -649,55 +716,6 @@ GAS_preference_init () | |||
649 | 716 | ||
650 | 717 | ||
651 | /** | 718 | /** |
652 | * Free a peer's `struct PeerRelative`. | ||
653 | * | ||
654 | * @param cls unused | ||
655 | * @param key the key | ||
656 | * @param value the `struct PeerRelative` to free. | ||
657 | * @return #GNUNET_OK to continue | ||
658 | */ | ||
659 | static int | ||
660 | free_peer (void *cls, | ||
661 | const struct GNUNET_PeerIdentity *key, | ||
662 | void *value) | ||
663 | { | ||
664 | struct PeerRelative *rp = value; | ||
665 | |||
666 | GNUNET_assert (GNUNET_YES == | ||
667 | GNUNET_CONTAINER_multipeermap_remove (preference_peers, | ||
668 | key, | ||
669 | value)); | ||
670 | GNUNET_free (rp); | ||
671 | return GNUNET_OK; | ||
672 | } | ||
673 | |||
674 | |||
675 | /** | ||
676 | * Free `struct PreferencePeer` entry in map. | ||
677 | * | ||
678 | * @param cls the `struct PreferenceClient` with the map | ||
679 | * @param key the peer the entry is for | ||
680 | * @param value the `struct PreferencePeer` entry to free | ||
681 | * @return #GNUNET_OK (continue to iterate) | ||
682 | */ | ||
683 | static int | ||
684 | free_preference (void *cls, | ||
685 | const struct GNUNET_PeerIdentity *key, | ||
686 | void *value) | ||
687 | { | ||
688 | struct PreferenceClient *pc = cls; | ||
689 | struct PreferencePeer *p = value; | ||
690 | |||
691 | GNUNET_assert (GNUNET_OK == | ||
692 | GNUNET_CONTAINER_multipeermap_remove (pc->peer2pref, | ||
693 | key, | ||
694 | p)); | ||
695 | GNUNET_free (p); | ||
696 | return GNUNET_OK; | ||
697 | } | ||
698 | |||
699 | |||
700 | /** | ||
701 | * Shutdown preferences subsystem. | 719 | * Shutdown preferences subsystem. |
702 | */ | 720 | */ |
703 | void | 721 | void |