From: Igor Putovny Date: Mon, 28 Apr 2025 15:40:13 +0000 (+0200) Subject: Refactor find_subtree_prefix() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=08f0a0aba54e1ee5ce0422e47beab2d3bd4bffe3;p=thirdparty%2Fbird.git Refactor find_subtree_prefix() --- diff --git a/proto/aggregator/trie.c b/proto/aggregator/trie.c index 2966e7f75..e18bb1167 100644 --- a/proto/aggregator/trie.c +++ b/proto/aggregator/trie.c @@ -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);