[CRIU] [PATCH] pstree: Simplify pstree_item_next

Cyrill Gorcunov gorcunov at openvz.org
Wed Oct 3 08:54:18 EDT 2012


It's a deep first search used here so the
code can be shrinked.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 pstree.c |   25 ++++++++-----------------
 1 files changed, 8 insertions(+), 17 deletions(-)

diff --git a/pstree.c b/pstree.c
index 6e91eff..ddd7488 100644
--- a/pstree.c
+++ b/pstree.c
@@ -47,28 +47,19 @@ struct pstree_item *__alloc_pstree_item(bool rst)
 	return item;
 }
 
+/* Deep first search on children */
 struct pstree_item *pstree_item_next(struct pstree_item *item)
 {
-	if (!list_empty(&item->children)) {
-		item = list_first_entry(&item->children, struct pstree_item, list);
-		return item;
-	}
+	if (!list_empty(&item->children))
+		return list_first_entry(&item->children, struct pstree_item, list);
 
-	while (1) {
-		if (item->parent == NULL) {
-			item = NULL;
-			break;
-		}
-		if (item->list.next == &item->parent->children) {
-			item = item->parent;
-			continue;
-		} else {
-			item = list_entry(item->list.next, struct pstree_item, list);
-			break;
-		}
+	while (item->parent) {
+		if (item->list.next != &item->parent->children)
+			return list_entry(item->list.next, struct pstree_item, list);
+		item = item->parent;
 	}
 
-	return item;
+	return NULL;
 }
 
 int dump_pstree(struct pstree_item *root_item)
-- 
1.7.7.6



More information about the CRIU mailing list