]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Refactor find_subtree_prefix()
authorIgor Putovny <igor.putovny@nic.cz>
Mon, 28 Apr 2025 15:40:13 +0000 (17:40 +0200)
committerIgor Putovny <igor.putovny@nic.cz>
Tue, 29 Apr 2025 13:01:15 +0000 (15:01 +0200)
proto/aggregator/trie.c

index 2966e7f75658f71ca60e875edb0104d679af8e9c..e18bb116760acc152e7b865f880b1211c6a3071e 100644 (file)
@@ -548,49 +548,38 @@ aggregator_trie_remove_prefix(struct aggregator_proto *p, ip_addr prefix, u32 px
  * and save result into @prefix and @pxlen.
  */
 static void
-aggregator_find_subtree_prefix(const struct trie_node *target, ip_addr *prefix, u32 *pxlen, u32 type)
+aggregator_find_subtree_prefix(const struct trie_node *target, ip_addr *prefix, u32 *pxlen, u32 addr_type)
 {
   ASSERT_DIE(target != NULL);
   ASSERT_DIE(prefix != NULL);
   ASSERT_DIE(pxlen != NULL);
 
-  int path[IP6_MAX_PREFIX_LENGTH] = { 0 };
-  int pos = 0;
-  u32 len = 0;
-
   const struct trie_node *node = target;
   const struct trie_node *parent = node->parent;
 
+  u32 len = 0;
+
   /* Ascend to the root node */
   while (parent)
   {
     if (node == node->parent->child[0])
-      path[pos++] = 0;
+      ip6_clrbit(prefix, node->depth + ipa_shift[addr_type] - 1);
     else if (node == node->parent->child[1])
-      path[pos++] = 1;
+      ip6_setbit(prefix, node->depth + ipa_shift[addr_type] - 1);
     else
       bug("Corrupted memory (node is not its parent's child)");
 
-    ASSERT_DIE(pos < IP6_MAX_PREFIX_LENGTH);
     node = parent;
     parent = node->parent;
+    len++;
   }
 
-  ASSERT_DIE(node->parent == NULL);
-
-  /* Descend to the target node */
-  for (int i = pos - 1; i >= 0; i--)
+  /* Descend back to target node */
+  for (u32 i = 0; i < len; i++)
   {
-    if (path[i] == 0)
-      ip6_clrbit(prefix, node->depth + ipa_shift[type]);
-    else if (path[i] == 1)
-      ip6_setbit(prefix, node->depth + ipa_shift[type]);
-
-    ASSERT_DIE(node->child[path[i]] != NULL);
-    node = node->child[path[i]];
-
-    len++;
-    ASSERT_DIE((u32)node->depth == len);
+    u32 bit = ip6_getbit(*prefix, node->depth + ipa_shift[addr_type]);
+    node = node->child[bit];
+    ASSERT_DIE(node != NULL);
   }
 
   ASSERT_DIE(node == target);