523#define MAX_WORK 10 524staticstruct rt_trie_node *resize(struct trie *t, struct tnode *tn) 525 { 526int i; 527structtnode *old_tn; 528int inflate_threshold_use; 529int halve_threshold_use; 530int max_work; 531 532if (!tn) 533returnNULL; 534 535 pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n", 536 tn, inflate_threshold, halve_threshold); 537 538/* No children */ 539if (tn->empty_children == tnode_child_length(tn)) { 540 tnode_free_safe(tn); 541returnNULL; 542 } 543/* One child */ 544if (tn->empty_children == tnode_child_length(tn) - 1) 545goto one_child; 546/* 547 * Double as long as the resulting node has a number of 548 * nonempty nodes that are above the threshold. 549 */ 550 551/* 552 * From "Implementing a dynamic compressed trie" by Stefan Nilsson of 553 * the Helsinki University of Technology and Matti Tikkanen of Nokia 554 * Telecommunications, page 6: 555 * "A node is doubled if the ratio of non-empty children to all 556 * children in the *doubled* node is at least 'high'."
根据 "Implementing a dynamic compressed trie"的第6页 "如果想要一个节点的, 其非空子孩子在所有孩子中的比例必须不少于 ‘high'"
557 * 558 * 'high' in this instance is the variable 'inflate_threshold'. It 559 * is expressed as a percentage, so we multiply it with 560 * tnode_child_length() and instead of multiplying by 2 (since the 561 * child array will be doubled by inflate()) and multiplying 562 * the left-hand side by 100 (to handle the percentage thing) we 563 * multiply the left-hand side by 50. 564 * 'high'在这里有变量'inflate_threshold'表示。它有一个百分比的形式。 100 * tn_noempty_children/(tnode_child_length() *2) = 'inflate_threshold' 避免除法: 100 * tn_noempty_children = 'inflate_threshold' * (tnode_child_length() *2) 100和2 抵消: 50 * tn_noempty_children = 'inflate_threshold' * tnode_child_length() 展开tn_noempty_children 50 * (tnode_child_length(tn) - tn->empty_children) = 'inflate_threshold' * tnode_child_length()
565 * The left-hand side may look a bit weird: tnode_child_length(tn) 566 * - tn->empty_children is of course the number of non-null children 567 * in the current node. tn->full_children is the number of "full" 568 * children, that is non-null tnodes with a skip value of 0. 569 * All of those will be doubled in the resulting inflated tnode, so 570 * we just count them one extra time here. 571 * 左边看起来有些怪异, tnode_child_length(tn) - tn->empty_children 指的是当前节点的非空孩子节点个数。 tn->full_children 是 'full'孩子节点的个数。即那些非空节点并且节点的 skip值为0(即 节点的pos == 父节点的pos+父节点的bits) 在最终膨胀的节点里,这些节点将会膨胀原来的两倍。
/* * Add a child at position i overwriting the old value. * Update the value of full_children and empty_children. */
staticvoidtnode_put_child_reorg(struct tnode *tn, int i, struct rt_trie_node *n, int wasfull) { structrt_trie_node *chi = rtnl_dereference(tn->child[i]); int isfull;
BUG_ON(i >= 1<<tn->bits);
/* update emptyChildren */ if (n == NULL && chi != NULL) tn->empty_children++; elseif (n != NULL && chi == NULL) tn->empty_children--;