proc/ports: fix port_augment
@@ -67,34 +67,34 @@ static int ports_gapcmp(rbnode_t *n1, rbnode_t *n2)
static void ports_augment(rbnode_t *node)
{
rbnode_t *it;
- rbnode_t *parent = node->parent;
port_t *n = lib_treeof(port_t, linkage, node);
- port_t *p = lib_treeof(port_t, linkage, parent);
- port_t *pp = (parent != NULL) ? lib_treeof(port_t, linkage, parent->parent) : NULL;
+ port_t *p = n, *r, *l;
if (node->left == NULL) {
- if (parent != NULL && parent->right == node)
- n->lmaxgap = n->id - p->id - 1;
- else if (parent != NULL && parent->parent != NULL && parent->parent->right == parent)
- n->lmaxgap = n->id - pp->id - 1;
- else
- n->lmaxgap = n->id - 0;
+ for (it = node; it->parent != NULL; it = it->parent) {
+ p = lib_treeof(port_t, linkage, it->parent);
+ if (it->parent->right == it)
+ break;
+ }
+
+ n->lmaxgap = (n->id <= p->id) ? n->id : n->id - p->id - 1;
}
else {
- port_t *l = lib_treeof(port_t, linkage, node->left);
+ l = lib_treeof(port_t, linkage, node->left);
n->lmaxgap = max(l->lmaxgap, l->rmaxgap);
}
if (node->right == NULL) {
- if (parent != NULL && parent->left == node)
- n->rmaxgap = p->id - n->id - 1;
- else if (parent != NULL && parent->parent != NULL && parent->parent->left == parent)
- n->rmaxgap = pp->id - n->id - 1;
- else
- n->rmaxgap = (u32)-1 - n->id - 1;
+ for (it = node; it->parent != NULL; it = it->parent) {
+ p = lib_treeof(port_t, linkage, it->parent);
+ if (it->parent->left == it)
+ break;
+ }
+
+ n->rmaxgap = (n->id >= p->id) ? (unsigned)-1 - n->id - 1 : p->id - n->id - 1;
}
else {
- port_t *r = lib_treeof(port_t, linkage, node->right);
+ r = lib_treeof(port_t, linkage, node->right);
n->rmaxgap = max(r->lmaxgap, r->rmaxgap);
}
