From: Remi Gacogne Date: Tue, 2 Feb 2021 13:44:07 +0000 (+0100) Subject: Make NetmaskTree::fork() a bit easier to understand X-Git-Tag: dnsdist-1.6.0-alpha2~68^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7846373d04f54f401c288bd37a7101448616a331;p=thirdparty%2Fpdns.git Make NetmaskTree::fork() a bit easier to understand --- diff --git a/pdns/iputils.hh b/pdns/iputils.hh index 3fd37a83d1..dcc9af8c3b 100644 --- a/pdns/iputils.hh +++ b/pdns/iputils.hh @@ -775,9 +775,11 @@ private: TreeNode* branch_node = new TreeNode(node.first.getSuper(bits)); branch_node->d_bits = bits; + // the current node will now be a child of the new branch node + // (hereafter new_child1 points to "this") + unique_ptr new_child1 = std::move(parent_ref); // attach the branch node under our former parent - unique_ptr new_child1(branch_node); - std::swap(parent_ref, new_child1); // hereafter new_child1 points to "this" + parent_ref = std::unique_ptr(branch_node); branch_node->parent = parent; // create second new leaf node for the new key @@ -789,12 +791,16 @@ private: new_child1->parent = branch_node; new_child2->parent = branch_node; if (new_child1->node.first.getBit(-1-bits)) { - std::swap(branch_node->right, new_child1); - std::swap(branch_node->left, new_child2); + branch_node->right = std::move(new_child1); + branch_node->left = std::move(new_child2); } else { - std::swap(branch_node->right, new_child2); - std::swap(branch_node->left, new_child1); + branch_node->right = std::move(new_child2); + branch_node->left = std::move(new_child1); } + // now we have attached the new unique pointers to the tree: + // - branch_node is below its parent + // - new_child1 (ourselves) is below branch_node + // - new_child2, the new leaf node, is below branch_node as well return new_node; }