Re-wrote broken codeblock_tree_delete() function.

This commit is contained in:
SarahW 2016-05-12 21:21:49 +01:00
commit 2909742567

View file

@ -109,14 +109,14 @@ static inline void codeblock_tree_add(codeblock_t *new_block)
static inline void codeblock_tree_delete(codeblock_t *block) static inline void codeblock_tree_delete(codeblock_t *block)
{ {
while (1)
{
codeblock_t *parent = block->parent; codeblock_t *parent = block->parent;
if (!block->left && !block->right) if (!block->left && !block->right)
{ {
/*Easy case - remove from parent*/ /*Easy case - remove from parent*/
if (parent) if (!parent)
pages[block->phys >> 12].head = NULL;
else
{ {
if (parent->left == block) if (parent->left == block)
parent->left = NULL; parent->left = NULL;
@ -127,40 +127,59 @@ static inline void codeblock_tree_delete(codeblock_t *block)
} }
else if (!block->left) else if (!block->left)
{ {
/*Only left node*/ /*Only right node*/
if (!parent) if (!parent)
pages[block->phys >> 12].head = block->left; {
pages[block->phys >> 12].head = block->right;
pages[block->phys >> 12].head->parent = NULL;
}
else else
{ {
if (parent->left == block) if (parent->left == block)
parent->left = block->left; {
parent->left = block->right;
parent->left->parent = parent;
}
if (parent->right == block) if (parent->right == block)
parent->right = block->left; {
parent->right = block->right;
parent->right->parent = parent;
}
} }
return; return;
} }
else if (!block->right) else if (!block->right)
{ {
/*Only right node*/ /*Only left node*/
if (!parent) if (!parent)
pages[block->phys >> 12].head = block->right; {
pages[block->phys >> 12].head = block->left;
pages[block->phys >> 12].head->parent = NULL;
}
else else
{ {
if (parent->left == block) if (parent->left == block)
parent->left = block->right; {
parent->left = block->left;
parent->left->parent = parent;
}
if (parent->right == block) if (parent->right == block)
parent->right = block->right; {
parent->right = block->left;
parent->right->parent = parent;
}
} }
return; return;
} }
else else
{ {
/*Difficult case - node has two children. Walk right child to find lowest node*/ /*Difficult case - node has two children. Walk right child to find lowest node*/
codeblock_t *lowest = block->right; codeblock_t *lowest = block->right, *highest;
codeblock_t *old_parent; codeblock_t *old_parent;
while (lowest->left) while (lowest->left)
lowest = lowest->left; lowest = lowest->left;
old_parent = lowest->parent; old_parent = lowest->parent;
/*Replace deleted node with lowest node*/ /*Replace deleted node with lowest node*/
@ -173,21 +192,32 @@ static inline void codeblock_tree_delete(codeblock_t *block)
if (parent->right == block) if (parent->right == block)
parent->right = lowest; parent->right = lowest;
} }
lowest->parent = parent;
if (old_parent = block) lowest->parent = parent;
lowest->left = block->left;
if (lowest->left)
lowest->left->parent = lowest;
old_parent->left = NULL;
highest = lowest->right;
if (!highest)
{ {
if (lowest != block->right)
{
lowest->right = block->right;
block->right->parent = lowest;
}
return; return;
} }
else
while (highest->right)
highest = highest->right;
if (block->right && block->right != lowest)
{ {
/*Replace old lowest node with deleted node, and loop*/ highest->right = block->right;
if (old_parent->left == lowest) block->right->parent = highest;
old_parent->left = block->left;
if (old_parent->right == lowest)
old_parent->right = block->right;
block->parent = old_parent;
}
} }
} }
} }