gpu: nvgpu: utils: improve CCM for rbtree

Improve the code complexity for the rbtree function delete_fixup().
Create smaller functions, delete_fixup_right_child() and
delete_left_child() to handle those parts of the agorithm. Also, create
helper function has_no_red_children() to handle a common check in these
new functions.

This brings the TCC metric to 7.

JIRA NVGPU-4094

Change-Id: If34167be308093ae3b597e02bbd3da8b4e9d27aa
Signed-off-by: Philip Elcan <pelcan@nvidia.com>
Reviewed-on: https://git-master.nvidia.com/r/2205844
Reviewed-by: svc-mobile-coverity <svc-mobile-coverity@nvidia.com>
Reviewed-by: svc-mobile-misra <svc-mobile-misra@nvidia.com>
Reviewed-by: Nicolas Benech <nbenech@nvidia.com>
GVS: Gerrit_Virtual_Submit
Reviewed-by: Alex Waterman <alexw@nvidia.com>
Reviewed-by: mobile promotions <svcmobile_promotions@nvidia.com>
Tested-by: mobile promotions <svcmobile_promotions@nvidia.com>
This commit is contained in:
Philip Elcan
2019-09-25 15:07:13 -04:00
committed by Alex Waterman
parent e7e0879217
commit 5e0bf2bb7a

View File

@@ -183,6 +183,85 @@ void nvgpu_rbtree_insert(struct nvgpu_rbtree_node *new_node,
insert_fixup(root, new_node);
}
/*
* helper function for delete_fixup_*_child to test if node has no red
* children
*/
static bool has_no_red_children(struct nvgpu_rbtree_node *w)
{
return (w == NULL) ||
(((w->left == NULL) || (!w->left->is_red)) &&
((w->right == NULL) || (!w->right->is_red)));
}
/* delete_fixup handling if x is the left child */
static void delete_fixup_left_child(struct nvgpu_rbtree_node **root,
struct nvgpu_rbtree_node *parent_of_x,
struct nvgpu_rbtree_node **x)
{
struct nvgpu_rbtree_node *w = parent_of_x->right;
if ((w != NULL) && (w->is_red)) {
w->is_red = false;
parent_of_x->is_red = true;
rotate_left(root, parent_of_x);
w = parent_of_x->right;
}
if (has_no_red_children(w)) {
if (w != NULL) {
w->is_red = true;
}
*x = parent_of_x;
} else {
if ((w->right == NULL) || (!w->right->is_red)) {
w->left->is_red = false;
w->is_red = true;
rotate_right(root, w);
w = parent_of_x->right;
}
w->is_red = parent_of_x->is_red;
parent_of_x->is_red = false;
w->right->is_red = false;
rotate_left(root, parent_of_x);
*x = *root;
}
}
/* delete_fixup handling if x is the right child */
static void delete_fixup_right_child(struct nvgpu_rbtree_node **root,
struct nvgpu_rbtree_node *parent_of_x,
struct nvgpu_rbtree_node **x)
{
struct nvgpu_rbtree_node *w = parent_of_x->left;
if ((w != NULL) && (w->is_red)) {
w->is_red = false;
parent_of_x->is_red = true;
rotate_right(root, parent_of_x);
w = parent_of_x->left;
}
if (has_no_red_children(w)) {
if (w != NULL) {
w->is_red = true;
}
*x = parent_of_x;
} else {
if ((w->left == NULL) || (!w->left->is_red)) {
w->right->is_red = false;
w->is_red = true;
rotate_left(root, w);
w = parent_of_x->left;
}
w->is_red = parent_of_x->is_red;
parent_of_x->is_red = false;
w->left->is_red = false;
rotate_right(root, parent_of_x);
*x = *root;
}
}
/*
* maintain red-black tree balance after deleting node x
*/
@@ -204,65 +283,9 @@ static void delete_fixup(struct nvgpu_rbtree_node **root,
}
if (x == parent_of_x->left) {
struct nvgpu_rbtree_node *w = parent_of_x->right;
if ((w != NULL) && (w->is_red)) {
w->is_red = false;
parent_of_x->is_red = true;
rotate_left(root, parent_of_x);
w = parent_of_x->right;
}
if ((w == NULL) ||
(((w->left == NULL) || (!w->left->is_red)) &&
((w->right == NULL) || (!w->right->is_red)))) {
if (w != NULL) {
w->is_red = true;
}
x = parent_of_x;
} else {
if ((w->right == NULL) || (!w->right->is_red)) {
w->left->is_red = false;
w->is_red = true;
rotate_right(root, w);
w = parent_of_x->right;
}
w->is_red = parent_of_x->is_red;
parent_of_x->is_red = false;
w->right->is_red = false;
rotate_left(root, parent_of_x);
x = *root;
}
delete_fixup_left_child(root, parent_of_x, &x);
} else {
struct nvgpu_rbtree_node *w = parent_of_x->left;
if ((w != NULL) && (w->is_red)) {
w->is_red = false;
parent_of_x->is_red = true;
rotate_right(root, parent_of_x);
w = parent_of_x->left;
}
if ((w == NULL) ||
(((w->right == NULL) || (!w->right->is_red)) &&
((w->left == NULL) || (!w->left->is_red)))) {
if (w != NULL) {
w->is_red = true;
}
x = parent_of_x;
} else {
if ((w->left == NULL) || (!w->left->is_red)) {
w->right->is_red = false;
w->is_red = true;
rotate_left(root, w);
w = parent_of_x->left;
}
w->is_red = parent_of_x->is_red;
parent_of_x->is_red = false;
w->left->is_red = false;
rotate_right(root, parent_of_x);
x = *root;
}
delete_fixup_right_child(root, parent_of_x, &x);
}
parent_of_x = x->parent;
}