From b5f1135ba8d0983d2d7acb5450060f46daf4c054 Mon Sep 17 00:00:00 2001 From: Philip Elcan Date: Wed, 25 Sep 2019 17:12:38 -0400 Subject: [PATCH] gpu: nvgpu: utils: improve CCM for rbtree This improves the code complexity of the rbtree function nvgpu_rbtree_unlink() by creating helper functions swap_in_new_child() and adopt_children(). This reduces the TCC metric to 9. JIRA NVGPU-4097 Change-Id: I2eb9ddf9a74478600874c71dab2b1267b5148b7b Signed-off-by: Philip Elcan Reviewed-on: https://git-master.nvidia.com/r/2205845 Reviewed-by: svc-mobile-coverity Reviewed-by: svc-mobile-misra Reviewed-by: Nicolas Benech GVS: Gerrit_Virtual_Submit Reviewed-by: Alex Waterman Reviewed-by: mobile promotions Tested-by: mobile promotions --- drivers/gpu/nvgpu/common/utils/rbtree.c | 61 +++++++++++++------------ 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/drivers/gpu/nvgpu/common/utils/rbtree.c b/drivers/gpu/nvgpu/common/utils/rbtree.c index b17d17048..86979043b 100644 --- a/drivers/gpu/nvgpu/common/utils/rbtree.c +++ b/drivers/gpu/nvgpu/common/utils/rbtree.c @@ -295,6 +295,35 @@ static void delete_fixup(struct nvgpu_rbtree_node **root, } } +static void swap_in_new_child(struct nvgpu_rbtree_node *old, + struct nvgpu_rbtree_node *new, + struct nvgpu_rbtree_node **root) +{ + if (old->parent != NULL) { + if (old == old->parent->left) { + old->parent->left = new; + } else { + old->parent->right = new; + } + } else { + *root = new; + } +} + +static void adopt_children(struct nvgpu_rbtree_node *old, + struct nvgpu_rbtree_node *new) +{ + new->left = old->left; + if (old->left != NULL) { + old->left->parent = new; + } + + new->right = old->right; + if (old->right != NULL) { + old->right->parent = new; + } +} + void nvgpu_rbtree_unlink(struct nvgpu_rbtree_node *node, struct nvgpu_rbtree_node **root) { @@ -330,16 +359,8 @@ void nvgpu_rbtree_unlink(struct nvgpu_rbtree_node *node, if (x != NULL) { x->parent = parent_of_x; } - - if (y->parent != NULL) { - if (y == y->parent->left) { - y->parent->left = x; - } else { - y->parent->right = x; - } - } else { - *root = x; - } + /* update the parent's links */ + swap_in_new_child(y, x, root); y_was_black = !y->is_red; if (y != z) { @@ -347,27 +368,11 @@ void nvgpu_rbtree_unlink(struct nvgpu_rbtree_node *node, * the memory for z can be freed */ y->parent = z->parent; - if (z->parent != NULL) { - if (z == z->parent->left) { - z->parent->left = y; - } else { - z->parent->right = y; - } - } else { - *root = y; - } + swap_in_new_child(z, y, root); y->is_red = z->is_red; - y->left = z->left; - if (z->left != NULL) { - z->left->parent = y; - } - - y->right = z->right; - if (z->right != NULL) { - z->right->parent = y; - } + adopt_children(z, y); if (parent_of_x == z) { parent_of_x = y;