aboutsummaryrefslogtreecommitdiff
path: root/src/mesh/mesh_tunnel_tree.c
diff options
context:
space:
mode:
authorBart Polot <bart@net.in.tum.de>2011-09-21 12:59:59 +0000
committerBart Polot <bart@net.in.tum.de>2011-09-21 12:59:59 +0000
commit99636d1589e9e311967dcdcba41bd51c3c1f5de8 (patch)
tree867e8cdaae9c917f10a9024a4f1a53cb819b265e /src/mesh/mesh_tunnel_tree.c
parentad9322433829c2609909005208b9a2f195745f44 (diff)
downloadgnunet-99636d1589e9e311967dcdcba41bd51c3c1f5de8.tar.gz
gnunet-99636d1589e9e311967dcdcba41bd51c3c1f5de8.zip
Renamed functions, make valgrind stop complaining about memory leaks by explicitly freeing stuff before exit
Diffstat (limited to 'src/mesh/mesh_tunnel_tree.c')
-rw-r--r--src/mesh/mesh_tunnel_tree.c74
1 files changed, 63 insertions, 11 deletions
diff --git a/src/mesh/mesh_tunnel_tree.c b/src/mesh/mesh_tunnel_tree.c
index ad9e9032a..30aaeadb3 100644
--- a/src/mesh/mesh_tunnel_tree.c
+++ b/src/mesh/mesh_tunnel_tree.c
@@ -129,7 +129,7 @@ path_get_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
129 * @return Pointer to the node of the peer. NULL if not found. 129 * @return Pointer to the node of the peer. NULL if not found.
130 */ 130 */
131struct MeshTunnelTreeNode * 131struct MeshTunnelTreeNode *
132tunnel_find_peer (struct MeshTunnelTreeNode *root, GNUNET_PEER_Id peer_id) 132tree_find_peer (struct MeshTunnelTreeNode *root, GNUNET_PEER_Id peer_id)
133{ 133{
134 struct MeshTunnelTreeNode *n; 134 struct MeshTunnelTreeNode *n;
135 unsigned int i; 135 unsigned int i;
@@ -138,7 +138,7 @@ tunnel_find_peer (struct MeshTunnelTreeNode *root, GNUNET_PEER_Id peer_id)
138 return root; 138 return root;
139 for (i = 0; i < root->nchildren; i++) 139 for (i = 0; i < root->nchildren; i++)
140 { 140 {
141 n = tunnel_find_peer (&root->children[i], peer_id); 141 n = tree_find_peer (&root->children[i], peer_id);
142 if (NULL != n) 142 if (NULL != n)
143 return n; 143 return n;
144 } 144 }
@@ -153,7 +153,7 @@ tunnel_find_peer (struct MeshTunnelTreeNode *root, GNUNET_PEER_Id peer_id)
153 * @param cb Callback to use to notify about disconnected peers. 153 * @param cb Callback to use to notify about disconnected peers.
154 */ 154 */
155void 155void
156tunnel_mark_peers_disconnected (struct MeshTunnelTreeNode *parent, 156tree_mark_peers_disconnected (struct MeshTunnelTreeNode *parent,
157 MeshNodeDisconnectCB cb) 157 MeshNodeDisconnectCB cb)
158{ 158{
159 unsigned int i; 159 unsigned int i;
@@ -165,7 +165,7 @@ tunnel_mark_peers_disconnected (struct MeshTunnelTreeNode *parent,
165 parent->status = MESH_PEER_RECONNECTING; 165 parent->status = MESH_PEER_RECONNECTING;
166 for (i = 0; i < parent->nchildren; i++) 166 for (i = 0; i < parent->nchildren; i++)
167 { 167 {
168 tunnel_mark_peers_disconnected (&parent->children[i], cb); 168 tree_mark_peers_disconnected (&parent->children[i], cb);
169 } 169 }
170// struct GNUNET_MESH_PeerControl msg; 170// struct GNUNET_MESH_PeerControl msg;
171// if (NULL == parent->t->client) 171// if (NULL == parent->t->client)
@@ -195,7 +195,7 @@ tunnel_mark_peers_disconnected (struct MeshTunnelTreeNode *parent,
195 * @return pointer to the pathless node, NULL on error 195 * @return pointer to the pathless node, NULL on error
196 */ 196 */
197struct MeshTunnelTreeNode * 197struct MeshTunnelTreeNode *
198tunnel_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id, 198tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
199 MeshNodeDisconnectCB cb) 199 MeshNodeDisconnectCB cb)
200{ 200{
201 struct MeshTunnelTreeNode *parent; 201 struct MeshTunnelTreeNode *parent;
@@ -204,7 +204,7 @@ tunnel_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
204 204
205 if (peer_id == t->root->peer) 205 if (peer_id == t->root->peer)
206 return NULL; 206 return NULL;
207 node = n = tunnel_find_peer (t->me, peer_id); 207 node = n = tree_find_peer (t->me, peer_id);
208 if (NULL == n) 208 if (NULL == n)
209 return NULL; 209 return NULL;
210 parent = n->parent; 210 parent = n->parent;
@@ -222,7 +222,7 @@ tunnel_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
222 parent->nchildren--; 222 parent->nchildren--;
223 parent->children = GNUNET_realloc (parent->children, parent->nchildren); 223 parent->children = GNUNET_realloc (parent->children, parent->nchildren);
224 224
225 tunnel_mark_peers_disconnected (node, cb); 225 tree_mark_peers_disconnected (node, cb);
226 226
227 return node; 227 return node;
228} 228}
@@ -239,13 +239,13 @@ tunnel_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
239 * Path must be destroyed afterwards. 239 * Path must be destroyed afterwards.
240 */ 240 */
241struct MeshPeerPath * 241struct MeshPeerPath *
242tunnel_get_path_to_peer(struct MeshTunnelTree *t, GNUNET_PEER_Id peer) 242tree_get_path_to_peer(struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
243{ 243{
244 struct MeshTunnelTreeNode *n; 244 struct MeshTunnelTreeNode *n;
245 struct MeshPeerPath *p; 245 struct MeshPeerPath *p;
246 GNUNET_PEER_Id myid = t->me->peer; 246 GNUNET_PEER_Id myid = t->me->peer;
247 247
248 n = tunnel_find_peer(t->me, peer); 248 n = tree_find_peer(t->me, peer);
249 p = GNUNET_malloc(sizeof(struct MeshPeerPath)); 249 p = GNUNET_malloc(sizeof(struct MeshPeerPath));
250 250
251 /* Building the path (inverted!) */ 251 /* Building the path (inverted!) */
@@ -280,7 +280,7 @@ tunnel_get_path_to_peer(struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
280 * - do not disconnect peers until new path is created & connected 280 * - do not disconnect peers until new path is created & connected
281 */ 281 */
282int 282int
283tunnel_add_path (struct MeshTunnelTree *t, const struct MeshPeerPath *p, 283tree_add_path (struct MeshTunnelTree *t, const struct MeshPeerPath *p,
284 MeshNodeDisconnectCB cb) 284 MeshNodeDisconnectCB cb)
285{ 285{
286 struct MeshTunnelTreeNode *parent; 286 struct MeshTunnelTreeNode *parent;
@@ -302,7 +302,7 @@ tunnel_add_path (struct MeshTunnelTree *t, const struct MeshPeerPath *p,
302 } 302 }
303 if (1 == p->length) 303 if (1 == p->length)
304 return GNUNET_OK; 304 return GNUNET_OK;
305 oldnode = tunnel_del_path (t, p->peers[p->length - 1], cb); 305 oldnode = tree_del_path (t, p->peers[p->length - 1], cb);
306 /* Look for the first node that is not already present in the tree 306 /* Look for the first node that is not already present in the tree
307 * 307 *
308 * Assuming that the tree is somewhat balanced, O(log n * log N). 308 * Assuming that the tree is somewhat balanced, O(log n * log N).
@@ -371,3 +371,55 @@ tunnel_add_path (struct MeshTunnelTree *t, const struct MeshPeerPath *p,
371 } 371 }
372 return GNUNET_OK; 372 return GNUNET_OK;
373} 373}
374
375
376/**
377 * Destroy the node and all children
378 *
379 * @param n Parent node to be destroyed
380 */
381void
382tree_node_destroy (struct MeshTunnelTreeNode *n)
383{
384 unsigned int i;
385
386 if (n->nchildren == 0) return;
387 for (i = 0; i < n->nchildren; i++)
388 {
389 tree_node_destroy(&n->children[i]);
390 }
391 GNUNET_free(n->children);
392}
393
394
395/**
396 * Iterator over hash map peer entries and frees all data in it.
397 * Used prior to destroying a hashmap. Makes you miss anonymous functions in C.
398 *
399 * @param cls closure
400 * @param key current key code (will no longer contain valid data!!)
401 * @param value value in the hash map (treated as void *)
402 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
403 */
404static int
405iterate_free (void *cls, const GNUNET_HashCode * key, void *value)
406{
407 GNUNET_free(value);
408 return GNUNET_YES;
409}
410
411
412/**
413 * Destroy the whole tree and free all used memory and Peer_Ids
414 *
415 * @param t Tree to be destroyed
416 */
417void
418tree_destroy (struct MeshTunnelTree *t)
419{
420 tree_node_destroy(t->root);
421 GNUNET_free(t->root);
422 GNUNET_CONTAINER_multihashmap_iterate(t->first_hops, &iterate_free, NULL);
423 GNUNET_CONTAINER_multihashmap_destroy(t->first_hops);
424
425} \ No newline at end of file