Skip to content
Snippets Groups Projects
Commit 24198ba7 authored by Marek Vavruša's avatar Marek Vavruša
Browse files

zone-tree: treat NULL tree as empty

parent 07317eb3
No related branches found
No related tags found
1 merge request!207No Default Nsec3 Tree
......@@ -51,12 +51,12 @@ knot_zone_tree_t* knot_zone_tree_create()
/*----------------------------------------------------------------------------*/
size_t knot_zone_tree_weight(knot_zone_tree_t* tree)
size_t knot_zone_tree_weight(const knot_zone_tree_t* tree)
{
return hattrie_weight(tree);
}
int knot_zone_tree_is_empty(knot_zone_tree_t *tree)
int knot_zone_tree_is_empty(const knot_zone_tree_t *tree)
{
return knot_zone_tree_weight(tree) == 0;
}
......@@ -65,6 +65,10 @@ int knot_zone_tree_is_empty(knot_zone_tree_t *tree)
int knot_zone_tree_insert(knot_zone_tree_t *tree, knot_node_t *node)
{
if (tree == NULL) {
return KNOT_EINVAL;
}
assert(tree && node && node->owner);
uint8_t lf[KNOT_DNAME_MAXLEN];
knot_dname_lf(lf, node->owner, NULL);
......@@ -78,7 +82,7 @@ int knot_zone_tree_insert(knot_zone_tree_t *tree, knot_node_t *node)
int knot_zone_tree_find(knot_zone_tree_t *tree, const knot_dname_t *owner,
const knot_node_t **found)
{
if (tree == NULL || owner == NULL || found == NULL) {
if (owner == NULL || found == NULL) {
return KNOT_EINVAL;
}
......@@ -90,10 +94,14 @@ int knot_zone_tree_find(knot_zone_tree_t *tree, const knot_dname_t *owner,
int knot_zone_tree_get(knot_zone_tree_t *tree, const knot_dname_t *owner,
knot_node_t **found)
{
if (tree == NULL || owner == NULL) {
if (owner == NULL) {
return KNOT_EINVAL;
}
if (knot_zone_tree_is_empty(tree)) {
return KNOT_ENONODE;
}
uint8_t lf[KNOT_DNAME_MAXLEN];
knot_dname_lf(lf, owner, NULL);
......@@ -114,7 +122,7 @@ int knot_zone_tree_find_less_or_equal(knot_zone_tree_t *tree,
const knot_node_t **found,
const knot_node_t **previous)
{
if (tree == NULL || owner == NULL || found == NULL || previous == NULL) {
if (owner == NULL || found == NULL || previous == NULL) {
return KNOT_EINVAL;
}
......@@ -134,11 +142,14 @@ int knot_zone_tree_get_less_or_equal(knot_zone_tree_t *tree,
knot_node_t **found,
knot_node_t **previous)
{
if (tree == NULL || owner == NULL || found == NULL
|| previous == NULL) {
if (owner == NULL || found == NULL || previous == NULL) {
return KNOT_EINVAL;
}
if (knot_zone_tree_is_empty(tree)) {
return KNOT_ENONODE;
}
uint8_t lf[KNOT_DNAME_MAXLEN];
knot_dname_lf(lf, owner, NULL);
......@@ -197,10 +208,14 @@ int knot_zone_tree_remove(knot_zone_tree_t *tree,
const knot_dname_t *owner,
knot_node_t **removed)
{
if (tree == NULL || owner == NULL) {
if (owner == NULL) {
return KNOT_EINVAL;
}
if (knot_zone_tree_is_empty(tree)) {
return KNOT_ENONODE;
}
uint8_t lf[KNOT_DNAME_MAXLEN];
knot_dname_lf(lf, owner, NULL);
......@@ -222,10 +237,14 @@ int knot_zone_tree_apply_inorder(knot_zone_tree_t *tree,
knot_zone_tree_apply_cb_t function,
void *data)
{
if (tree == NULL || function == NULL) {
if (function == NULL) {
return KNOT_EINVAL;
}
if (knot_zone_tree_is_empty(tree)) {
return KNOT_EOK;
}
int result = KNOT_EOK;
hattrie_iter_t *i = hattrie_iter_begin(tree, 1);
......@@ -247,10 +266,14 @@ int knot_zone_tree_apply(knot_zone_tree_t *tree,
knot_zone_tree_apply_cb_t function,
void *data)
{
if (tree == NULL || function == NULL) {
if (function == NULL) {
return KNOT_EINVAL;
}
if (knot_zone_tree_is_empty(tree)) {
return KNOT_EOK;
}
return hattrie_apply_rev(tree, (int (*)(value_t*,void*))function, data);
}
......@@ -259,11 +282,15 @@ int knot_zone_tree_apply(knot_zone_tree_t *tree,
int knot_zone_tree_shallow_copy(knot_zone_tree_t *from,
knot_zone_tree_t **to)
{
if (to == NULL || from == NULL) {
if (to == NULL) {
return KNOT_EINVAL;
}
*to = hattrie_dup(from, knot_zone_node_copy);
if (from != NULL) {
*to = hattrie_dup(from, knot_zone_node_copy);
} else {
*to = NULL;
}
return KNOT_EOK;
}
......@@ -273,11 +300,15 @@ int knot_zone_tree_shallow_copy(knot_zone_tree_t *from,
int knot_zone_tree_deep_copy(knot_zone_tree_t *from,
knot_zone_tree_t **to)
{
if (to == NULL || from == NULL) {
if (to == NULL) {
return KNOT_EINVAL;
}
*to = hattrie_dup(from, knot_zone_node_deep_copy);
if (from != NULL) {
*to = hattrie_dup(from, knot_zone_node_deep_copy);
} else {
*to = NULL;
}
return KNOT_EOK;
}
......@@ -313,31 +344,3 @@ void knot_zone_tree_deep_free(knot_zone_tree_t **tree)
knot_zone_tree_apply(*tree, knot_zone_tree_free_node, NULL);
knot_zone_tree_free(tree);
}
/*----------------------------------------------------------------------------*/
void hattrie_insert_dname(hattrie_t *tr, knot_dname_t *dname)
{
uint8_t lf[KNOT_DNAME_MAXLEN];
knot_dname_lf(lf, dname, NULL);
*hattrie_get(tr, (char*)lf+1, *lf) = dname;
}
/*----------------------------------------------------------------------------*/
knot_dname_t *hattrie_get_dname(hattrie_t *tr, knot_dname_t *dname)
{
if (tr == NULL || dname == NULL) {
return NULL;
}
uint8_t lf[KNOT_DNAME_MAXLEN];
knot_dname_lf(lf, dname, NULL);
value_t *val = hattrie_tryget(tr, (char*)lf+1, *lf);
if (val == NULL) {
return NULL;
} else {
return (knot_dname_t *)(*val);
}
}
......@@ -54,7 +54,7 @@ knot_zone_tree_t* knot_zone_tree_create();
* \param tree Zone tree.
* \return number of nodes in tree.
*/
size_t knot_zone_tree_weight(knot_zone_tree_t* tree);
size_t knot_zone_tree_weight(const knot_zone_tree_t* tree);
/*!
* \brief Checks if the zone tree is empty.
......@@ -63,7 +63,7 @@ size_t knot_zone_tree_weight(knot_zone_tree_t* tree);
*
* \return Nonzero if the zone tree is empty.
*/
int knot_zone_tree_is_empty(knot_zone_tree_t *tree);
int knot_zone_tree_is_empty(const knot_zone_tree_t *tree);
/*!
* \brief Inserts the given node into the zone tree.
......@@ -242,9 +242,6 @@ void knot_zone_tree_free(knot_zone_tree_t **tree);
*/
void knot_zone_tree_deep_free(knot_zone_tree_t **tree);
void hattrie_insert_dname(hattrie_t *tr, knot_dname_t *dname);
knot_dname_t *hattrie_get_dname(hattrie_t *tr, knot_dname_t *dname);
/*----------------------------------------------------------------------------*/
#endif // _KNOT_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