Skip to content
Snippets Groups Projects
Commit b3f728b5 authored by Lubos Slovak's avatar Lubos Slovak Committed by Jan Kadlec
Browse files

Zone tree copying.

refs #994 @30m
parent 5249f184
No related branches found
No related tags found
No related merge requests found
......@@ -315,3 +315,67 @@ int dnslib_zone_tree_reverse_apply_inorder(dnslib_zone_tree_t *tree,
}
/*----------------------------------------------------------------------------*/
static void dnslib_zone_tree_delete_subtree(dnslib_zone_tree_node_t *root)
{
if (root == NULL) {
return;
}
dnslib_zone_tree_delete_subtree(root->avl.avl_left);
dnslib_zone_tree_delete_subtree(root->avl.avl_right);
free(root);
}
/*----------------------------------------------------------------------------*/
static int dnslib_zone_tree_copy_node(dnslib_zone_tree_node_t *from,
dnslib_zone_tree_node_t **to)
{
if (from == NULL) {
*to = NULL;
return DNSLIB_EOK;
}
*to = (dnslib_zone_tree_node_t *)
malloc(sizeof(dnslib_zone_tree_node_t));
if (*to == NULL) {
return DNSLIB_ENOMEM;
}
(*to)->node = from->node;
(*to)->avl.avl_height = from->avl.avl_height;
int ret = dnslib_zone_tree_copy_node(from->avl.avl_left,
&(*to)->avl.avl_left);
if (ret != DNSLIB_EOK) {
return ret;
}
ret = dnslib_zone_tree_copy_node(from->avl.avl_right,
&(*to)->avl.avl_right);
if (ret != DNSLIB_EOK) {
dnslib_zone_tree_delete_subtree((*to)->avl.avl_left);
(*to)->avl.avl_left = NULL;
return ret;
}
return DNSLIB_EOK;
}
/*----------------------------------------------------------------------------*/
int dnslib_zone_tree_copy(dnslib_zone_tree_t *from, dnslib_zone_tree_t *to)
{
/*
* This function will copy the tree by hand, so that the nodes
* do not have to be inserted the normal way. It should be substantially
* faster.
*/
to->th_cmp = from->th_cmp;
return dnslib_zone_tree_copy_node(from->th_root, &to->th_root);
}
/*----------------------------------------------------------------------------*/
......@@ -217,6 +217,21 @@ int dnslib_zone_tree_reverse_apply_inorder(dnslib_zone_tree_t *tree,
dnslib_node_t *node, void *data),
void *data);
/*!
* \brief Copies the whole zone tree structure (but not the data contained
* within).
*
* \warning This function does not check if the target zone tree is empty,
* it just replaces the root pointer.
*
* \param from Original zone tree.
* \param to Zone tree to copy the original one into.
*
* \retval DNSLIB_EOK
* \retval DNSLIB_ENOMEM
*/
int dnslib_zone_tree_copy(dnslib_zone_tree_t *from, dnslib_zone_tree_t *to);
/*----------------------------------------------------------------------------*/
#endif // _KNOT_DNSLIB_ZONE_TREE_H_
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment