[PATCH v3 1/2] Staging: lustre: Refactor the function interval_erase_color() in /lustre/ldlm/interval_tree.c

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The function interval_erase_color() in lustre/ldlm/interval_tree.c is
too long and can be refactored. Most of the statements are same for if
and else conditions. I am passing a new variable n based on which the
differences are recognised.

This fixes the following checkpatch.pl warning in
lustre/ldlm/interval_tree.c
WARNING: line over 80 characters in the file

Signed-off-by: Monam Agarwal <monamagarwal123@xxxxxxxxx>
---

Changes since version 1:
* Incorrect fixing
* Incorrect Signed-off-by line

Changes since version 2:
* Removed new line character

 drivers/staging/lustre/lustre/ldlm/interval_tree.c |  128 +++++++++++---------
 1 file changed, 69 insertions(+), 59 deletions(-)

diff --git a/drivers/staging/lustre/lustre/ldlm/interval_tree.c b/drivers/staging/lustre/lustre/ldlm/interval_tree.c
index 1de1d8e..7c956de 100644
--- a/drivers/staging/lustre/lustre/ldlm/interval_tree.c
+++ b/drivers/staging/lustre/lustre/ldlm/interval_tree.c
@@ -404,75 +404,85 @@ static inline int node_is_black_or_0(struct interval_node *node)
 	return !node || node_is_black(node);
 }
 
+static int interval_set_color(struct interval_node *node,
+			      struct interval_node *parent,
+			      struct interval_node *root,
+			      int n)
+{
+	struct interval_node *tmp;
+	if (n == 0)
+		tmp = parent->in_right;
+	else
+		tmp = parent->in_left;
+	if (node_is_red(tmp)) {
+		tmp->in_color = INTERVAL_BLACK;
+		parent->in_color = INTERVAL_RED;
+		if (n == 0) {
+			__rotate_left(parent, root);
+			tmp = parent->in_right;
+		} else {
+			__rotate_right(parent, root);
+			tmp = parent->in_left;
+		}
+	}
+	if (node_is_black_or_0(tmp->in_left) &&
+	    node_is_black_or_0(tmp->in_right)) {
+		tmp->in_color = INTERVAL_RED;
+		node = parent;
+		parent = node->in_parent;
+	} else {
+		if (n == 0) {
+			if (node_is_black_or_0(tmp->in_right)) {
+				struct interval_node *o_left;
+				o_left = tmp->in_left;
+				if (o_left)
+					o_left->in_color = INTERVAL_BLACK;
+				tmp->in_color = INTERVAL_RED;
+				__rotate_right(tmp, root);
+				tmp = parent->in_right;
+			}
+			tmp->in_color = parent->in_color;
+			parent->in_color = INTERVAL_BLACK;
+			if (tmp->in_right)
+				tmp->in_right->in_color = INTERVAL_BLACK;
+			__rotate_left(parent, root);
+			node = *root;
+			return 1;
+		} else {
+			if (node_is_black_or_0(tmp->in_left)) {
+				struct interval_node *o_right;
+				o_right = tmp->in_right;
+				if (o_right)
+					o_right->in_color = INTERVAL_BLACK;
+				tmp->in_color = INTERVAL_RED;
+				__rotate_left(tmp, root);
+				tmp = parent->in_left;
+			}
+			tmp->in_color = parent->in_color;
+			parent->in_color = INTERVAL_BLACK;
+			if (tmp->in_left)
+				tmp->in_left->in_color = INTERVAL_BLACK;
+			__rotate_right(parent, root);
+			node = *root;
+			return 1;
+		}
+	}
+	return 0;
+}
+
 static void interval_erase_color(struct interval_node *node,
 				 struct interval_node *parent,
 				 struct interval_node **root)
 {
-	struct interval_node *tmp;
 
 	while (node_is_black_or_0(node) && node != *root) {
 		if (parent->in_left == node) {
-			tmp = parent->in_right;
-			if (node_is_red(tmp)) {
-				tmp->in_color = INTERVAL_BLACK;
-				parent->in_color = INTERVAL_RED;
-				__rotate_left(parent, root);
-				tmp = parent->in_right;
-			}
-			if (node_is_black_or_0(tmp->in_left) &&
-			    node_is_black_or_0(tmp->in_right)) {
-				tmp->in_color = INTERVAL_RED;
-				node = parent;
-				parent = node->in_parent;
-			} else {
-				if (node_is_black_or_0(tmp->in_right)) {
-					struct interval_node *o_left;
-					o_left = tmp->in_left;
-					if (o_left)
-						o_left->in_color = INTERVAL_BLACK;
-					tmp->in_color = INTERVAL_RED;
-					__rotate_right(tmp, root);
-					tmp = parent->in_right;
-				}
-				tmp->in_color = parent->in_color;
-				parent->in_color = INTERVAL_BLACK;
-				if (tmp->in_right)
-					tmp->in_right->in_color = INTERVAL_BLACK;
-				__rotate_left(parent, root);
-				node = *root;
+			if (interval_set_color(node, parent, root, 0))
 				break;
-			}
 		} else {
-			tmp = parent->in_left;
-			if (node_is_red(tmp)) {
-				tmp->in_color = INTERVAL_BLACK;
-				parent->in_color = INTERVAL_RED;
-				__rotate_right(parent, root);
-				tmp = parent->in_left;
-			}
-			if (node_is_black_or_0(tmp->in_left) &&
-			    node_is_black_or_0(tmp->in_right)) {
-				tmp->in_color = INTERVAL_RED;
-				node = parent;
-				parent = node->in_parent;
-			} else {
-				if (node_is_black_or_0(tmp->in_left)) {
-					struct interval_node *o_right;
-					o_right = tmp->in_right;
-					if (o_right)
-						o_right->in_color = INTERVAL_BLACK;
-					tmp->in_color = INTERVAL_RED;
-					__rotate_left(tmp, root);
-					tmp = parent->in_left;
-				}
-				tmp->in_color = parent->in_color;
-				parent->in_color = INTERVAL_BLACK;
-				if (tmp->in_left)
-					tmp->in_left->in_color = INTERVAL_BLACK;
-				__rotate_right(parent, root);
-				node = *root;
+
+			if (interval_set_color(node, parent, root, 1))
 				break;
-			}
 		}
 	}
 	if (node)
-- 
1.7.9.5

_______________________________________________
devel mailing list
devel@xxxxxxxxxxxxxxxxxxxxxx
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel




[Index of Archives]     [Linux Driver Backports]     [DMA Engine]     [Linux GPIO]     [Linux SPI]     [Video for Linux]     [Linux USB Devel]     [Linux Coverity]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]
  Powered by Linux