void print_task_list() { int i = 0; list_node_t *node = NULL; list_foreach(task_list, node) { task_t *task = node->key; kprintf("task #%d:\n", i); kprintf(" PID = %d\n", task->pid); kprintf(" status = %d\n", task->status); kprintf(" ustack = %x\n", task->mm->ustack); kprintf(" kstack_start = %x\n", task->mm->kstack_start); kprintf(" kstack_current = %x\n", task->mm->kstack_current); kprintf(" page_directory = %x\n", task->mm->page_directory); print_preorder(rbtree_root(task->mm->rb_vmallocs)); i++; }
/* * Returns the successor of node, or nil if there is none. */ static struct rbnode *rbsuccessor(struct rbtree *tree, struct rbnode *node) { struct rbnode *succ; if ((succ = node->right) != rbnil(tree)) { while (succ->left != rbnil(tree)) { succ = succ->left; } } else { /* No right child, move up until we find it or hit the root */ for (succ = node->parent; node == succ->right; succ = succ->parent) { node = succ; } if (succ == rbtree_root(tree)) { succ = rbnil(tree); } } return (succ); }
/* * Delete node 'z' from the tree and return its data pointer. */ void *rbtree_delete(struct rbtree *tree, struct rbnode *z) { struct rbnode *x, *y; void *data = z->data; if (z->left == rbnil(tree) || z->right == rbnil(tree)) { y = z; } else { y = rbsuccessor(tree, z); } x = (y->left == rbnil(tree)) ? y->right : y->left; if ((x->parent = y->parent) == rbtree_root(tree)) { rbtree_first(tree) = x; } else { if (y == y->parent->left) { y->parent->left = x; } else { y->parent->right = x; } } if (y->color == black) { rbrepair(tree, x); } if (y != z) { y->left = z->left; y->right = z->right; y->parent = z->parent; y->color = z->color; z->left->parent = z->right->parent = y; if (z == z->parent->left) { z->parent->left = y; } else { z->parent->right = y; } } free(z); tree->num_nodes--; return (data); }
/* * If a node matching "data" already exists, a pointer to * the existant node is returned. Otherwise we return NULL. */ struct rbnode *rbtree_insert(struct rbtree *tree, void *data) { struct rbnode *node = rbtree_first(tree); struct rbnode *parent = rbtree_root(tree); int res; /* Find correct insertion point. */ while (node != rbnil(tree)) { parent = node; if ((res = tree->compar(data, node->data)) == 0) { return (node); } node = res < 0 ? node->left : node->right; } node = (struct rbnode *) malloc(sizeof(*node)); node->data = data; node->left = node->right = rbnil(tree); node->parent = parent; if (parent == rbtree_root(tree) || tree->compar(data, parent->data) < 0) { parent->left = node; } else { parent->right = node; } node->color = red; /* * If the parent node is black we are all set, if it is red we have * the following possible cases to deal with. We iterate through * the rest of the tree to make sure none of the required properties * is violated. * * 1) The uncle is red. We repaint both the parent and uncle black * and repaint the grandparent node red. * * 2) The uncle is black and the new node is the right child of its * parent, and the parent in turn is the left child of its parent. * We do a left rotation to switch the roles of the parent and * child, relying on further iterations to fixup the old parent. * * 3) The uncle is black and the new node is the left child of its * parent, and the parent in turn is the left child of its parent. * We switch the colors of the parent and grandparent and perform * a right rotation around the grandparent. This makes the former * parent the parent of the new node and the former grandparent. * * Note that because we use a sentinel for the root node we never * need to worry about replacing the root. */ while (node->parent->color == red) { struct rbnode *uncle; if (node->parent == node->parent->parent->left) { uncle = node->parent->parent->right; if (uncle->color == red) { node->parent->color = black; uncle->color = black; node->parent->parent->color = red; node = node->parent->parent; } else { /* if (uncle->color == black) */ if (node == node->parent->right) { node = node->parent; rotate_left(tree, node); } node->parent->color = black; node->parent->parent->color = red; rotate_right(tree, node->parent->parent); } } else { /* if (node->parent == node->parent->parent->right) */ uncle = node->parent->parent->left; if (uncle->color == red) { node->parent->color = black; uncle->color = black; node->parent->parent->color = red; node = node->parent->parent; } else { /* if (uncle->color == black) */ if (node == node->parent->left) { node = node->parent; rotate_right(tree, node); } node->parent->color = black; node->parent->parent->color = red; rotate_left(tree, node->parent->parent); } } } tree->num_nodes++; rbtree_first(tree)->color = black; /* first node is always black */ return (NULL); }