[sheepdog] [PATCH 5/5] rbtree: add rb_destroy helper

Liu Yuan namei.unix at gmail.com
Thu Sep 5 06:38:13 CEST 2013


Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 dog/vdi.c        |    6 +-----
 include/rbtree.h |   10 ++++++++++
 sheep/group.c    |    6 +-----
 3 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/dog/vdi.c b/dog/vdi.c
index b4e8821..9219a9e 100644
--- a/dog/vdi.c
+++ b/dog/vdi.c
@@ -926,7 +926,6 @@ static int do_track_object(uint64_t oid, uint8_t nr_copies)
 	nr_logs = rsp->data_length / sizeof(struct epoch_log);
 	for (i = nr_logs - 1; i >= 0; i--) {
 		struct rb_root vroot;
-		struct sd_vnode *v;
 
 		printf("\nobj %"PRIx64" locations at epoch %d, copies = %d\n",
 		       oid, logs[i].epoch, nr_copies);
@@ -951,10 +950,7 @@ static int do_track_object(uint64_t oid, uint8_t nr_copies)
 
 			printf("%s\n", addr_to_str(n->addr, n->port));
 		}
-		rb_for_each_entry(v, &vroot, rb) {
-			rb_erase(&v->rb, &vroot);
-			free(v);
-		}
+		rb_destroy(&vroot, struct sd_vnode, rb);
 	}
 
 	free(logs);
diff --git a/include/rbtree.h b/include/rbtree.h
index c926cff..92c5389 100644
--- a/include/rbtree.h
+++ b/include/rbtree.h
@@ -175,4 +175,14 @@ static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
 		     ({ pos =  rb_entry(__pos, typeof(*pos), member); 1; }); \
 	     __pos = __n)
 
+/* Destroy the tree and free the memory */
+#define rb_destroy(root, type, member)                                  \
+{                                                                       \
+        type *__dummy;                                                  \
+        rb_for_each_entry(__dummy, root, member) {                      \
+                rb_erase(&__dummy->member, root);                       \
+                free(__dummy);                                          \
+        }                                                               \
+}
+
 #endif /* __RBTREE_H_ */
diff --git a/sheep/group.c b/sheep/group.c
index 378d084..899ce32 100644
--- a/sheep/group.c
+++ b/sheep/group.c
@@ -102,11 +102,7 @@ void put_vnode_info(struct vnode_info *vnode_info)
 {
 	if (vnode_info) {
 		if (refcount_dec(&vnode_info->refcnt) == 0) {
-			struct sd_vnode *v;
-			rb_for_each_entry(v, &vnode_info->vroot, rb) {
-				rb_erase(&v->rb, &vnode_info->vroot);
-				free(v);
-			}
+			rb_destroy(&vnode_info->vroot, struct sd_vnode, rb);
 			free(vnode_info);
 		}
 	}
-- 
1.7.9.5




More information about the sheepdog mailing list