Skip to content
Snippets Groups Projects
Commit 52ea845b authored by Jan Kadlec's avatar Jan Kadlec
Browse files

Changes to dname table.

Refs #793
parent 1335040e
No related branches found
No related tags found
No related merge requests found
......@@ -19,7 +19,10 @@ static int compare_dname_table_nodes(struct dname_table_node *n1,
static void delete_dname_table_node(struct dname_table_node *node, void *data)
{
UNUSED(data);
if (data) {
dnslib_dname_free(&node->dname);
}
/*!< \todo it would be nice to set pointers to NULL, too. */
free(node);
}
......@@ -37,12 +40,14 @@ dnslib_dname_table_t *dnslib_dname_table_new()
}
TREE_INIT(ret->tree, compare_dname_table_nodes);
ret->id_counter = 1;
return ret;
}
const dnslib_dname_t *dnslib_dname_table_find_dname(
const dnslib_dname_table_t *table,
const dnslib_dname_t *dname)
dnslib_dname_t *dnslib_dname_table_find_dname(const dnslib_dname_table_t *table,
dnslib_dname_t *dname)
{
struct dname_table_node *node = NULL;
struct dname_table_node sought;
......@@ -57,8 +62,8 @@ const dnslib_dname_t *dnslib_dname_table_find_dname(
}
}
int dnslib_dname_table_add_dname(const dnslib_dname_table_t *table,
const dnslib_dname_t *dname)
int dnslib_dname_table_add_dname(dnslib_dname_table_t *table,
dnslib_dname_t *dname)
{
/* Node for insertion has to be created */
struct dname_table_node *node =
......@@ -66,6 +71,11 @@ int dnslib_dname_table_add_dname(const dnslib_dname_table_t *table,
CHECK_ALLOC_LOG(node, DNSLIB_ENOMEM);
node->dname = dname;
node->avl.avl_height = 0;
node->avl.avl_left = NULL;
node->avl.avl_right = NULL;
node->dname->id = table->id_counter++;
TREE_INSERT(table->tree, dname_table_node, avl, node);
return DNSLIB_EOK;
......@@ -78,8 +88,22 @@ void dnslib_dname_table_free(dnslib_dname_table_t **table)
}
/* Walk the tree and free each node, but not the dnames. */
TREE_FORWARD_APPLY((*table)->tree, dname_table_node, avl,
delete_dname_table_node, NULL);
TREE_POST_ORDER_APPLY((*table)->tree, dname_table_node, avl,
delete_dname_table_node, 0);
free(*table);
*table = NULL;
}
void dnslib_dname_table_deep_free(dnslib_dname_table_t **table)
{
if (table == NULL || *table == NULL) {
return;
}
/* Walk the tree and free each node, but not the dnames. */
TREE_POST_ORDER_APPLY((*table)->tree, dname_table_node, avl,
delete_dname_table_node, (void *) 1);
free(*table);
*table = NULL;
......
......@@ -10,7 +10,7 @@
struct dname_table_node {
const dnslib_dname_t *dname;
dnslib_dname_t *dname;
TREE_ENTRY(dname_table_node) avl;
};
......@@ -18,6 +18,7 @@ typedef TREE_HEAD(avl, dname_table_node) table_tree_t;
/*!< \note contains only tree now, but might change in the future. */
struct dnslib_dname_table {
unsigned int id_counter;
table_tree_t *tree;
};
......@@ -25,13 +26,13 @@ typedef struct dnslib_dname_table dnslib_dname_table_t;
dnslib_dname_table_t *dnslib_dname_table_new();
const dnslib_dname_t *dnslib_dname_table_find_dname(
const dnslib_dname_table_t *table,
const dnslib_dname_t *dname);
dnslib_dname_t *dnslib_dname_table_find_dname(const dnslib_dname_table_t *table,
dnslib_dname_t *dname);
int dnslib_dname_table_add_dname(const dnslib_dname_table_t *table,
const dnslib_dname_t *dname);
int dnslib_dname_table_add_dname(dnslib_dname_table_t *table,
dnslib_dname_t *dname);
void dnslib_dname_table_free(dnslib_dname_table_t **table);
void dnslib_dname_table_deep_free(dnslib_dname_table_t **table);
#endif // DNAMETABLE_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