blob: dc888cdf1a6262cd42b102a25d4103f79ab26ea3 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file tree_data_free.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Freeing functions for data tree structures
5 *
6 * Copyright (c) 2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejcie7b95092019-05-15 11:03:07 +020015#include <assert.h>
16#include <stdlib.h>
17
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include "common.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include "hash_table.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include "tree.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include "tree_schema.h"
22#include "tree_data_internal.h"
Radek Krejci849a62a2019-05-22 15:29:05 +020023#include "plugins_types.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020024
Radek Krejcid46e46a2020-09-15 14:22:42 +020025void
26ly_free_val_prefs(const struct ly_ctx *ctx, struct ly_prefix *val_prefs)
27{
28 LY_ARRAY_COUNT_TYPE u;
29
30 LY_ARRAY_FOR(val_prefs, u) {
31 FREE_STRING(ctx, val_prefs[u].id);
32 FREE_STRING(ctx, val_prefs[u].module_ns);
33 }
34 LY_ARRAY_FREE(val_prefs);
35}
36
Michal Vasko3a41dff2020-07-15 14:30:28 +020037static void
Radek Krejci857189e2020-09-01 13:26:36 +020038lyd_free_meta(struct lyd_meta *meta, ly_bool siblings)
Radek Krejcie7b95092019-05-15 11:03:07 +020039{
Michal Vasko9f96a052020-03-10 09:41:45 +010040 struct lyd_meta *iter;
Radek Krejcie7b95092019-05-15 11:03:07 +020041
Michal Vasko9f96a052020-03-10 09:41:45 +010042 if (!meta) {
Radek Krejcie7b95092019-05-15 11:03:07 +020043 return;
44 }
45
Michal Vasko9f96a052020-03-10 09:41:45 +010046 if (meta->parent) {
47 if (meta->parent->meta == meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +020048 if (siblings) {
Michal Vasko9f96a052020-03-10 09:41:45 +010049 meta->parent->meta = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +020050 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +010051 meta->parent->meta = meta->next;
Radek Krejcie7b95092019-05-15 11:03:07 +020052 }
53 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +020054 for (iter = meta->parent->meta; iter->next != meta; iter = iter->next) {}
Radek Krejcie7b95092019-05-15 11:03:07 +020055 if (iter->next) {
Michal Vasko3a41dff2020-07-15 14:30:28 +020056 if (siblings) {
Radek Krejcie7b95092019-05-15 11:03:07 +020057 iter->next = NULL;
58 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +010059 iter->next = meta->next;
Radek Krejcie7b95092019-05-15 11:03:07 +020060 }
61 }
62 }
63 }
64
Michal Vasko3a41dff2020-07-15 14:30:28 +020065 if (!siblings) {
Michal Vasko9f96a052020-03-10 09:41:45 +010066 meta->next = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +020067 }
68
Michal Vasko52927e22020-03-16 17:26:14 +010069 for (iter = meta; iter; ) {
Michal Vasko9f96a052020-03-10 09:41:45 +010070 meta = iter;
Radek Krejcie7b95092019-05-15 11:03:07 +020071 iter = iter->next;
72
Michal Vasko3a41dff2020-07-15 14:30:28 +020073 FREE_STRING(meta->annotation->module->ctx, meta->name);
74 meta->value.realtype->plugin->free(meta->annotation->module->ctx, &meta->value);
Michal Vasko9f96a052020-03-10 09:41:45 +010075 free(meta);
Radek Krejcie7b95092019-05-15 11:03:07 +020076 }
77}
78
Michal Vasko52927e22020-03-16 17:26:14 +010079API void
Michal Vasko3a41dff2020-07-15 14:30:28 +020080lyd_free_meta_single(struct lyd_meta *meta)
81{
82 lyd_free_meta(meta, 0);
83}
84
85API void
86lyd_free_meta_siblings(struct lyd_meta *meta)
87{
88 lyd_free_meta(meta, 1);
89}
90
91static void
Radek Krejci011e4aa2020-09-04 15:22:31 +020092lyd_free_attr(const struct ly_ctx *ctx, struct lyd_attr *attr, ly_bool siblings)
Michal Vasko52927e22020-03-16 17:26:14 +010093{
Radek Krejci1798aae2020-07-14 13:26:06 +020094 struct lyd_attr *iter;
Michal Vasko52927e22020-03-16 17:26:14 +010095
96 LY_CHECK_ARG_RET(NULL, ctx, );
97 if (!attr) {
98 return;
99 }
100
101 if (attr->parent) {
102 if (attr->parent->attr == attr) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200103 if (siblings) {
Michal Vasko52927e22020-03-16 17:26:14 +0100104 attr->parent->attr = NULL;
105 } else {
106 attr->parent->attr = attr->next;
107 }
108 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200109 for (iter = attr->parent->attr; iter->next != attr; iter = iter->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +0100110 if (iter->next) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200111 if (siblings) {
Michal Vasko52927e22020-03-16 17:26:14 +0100112 iter->next = NULL;
113 } else {
114 iter->next = attr->next;
115 }
116 }
117 }
118 }
119
Michal Vasko3a41dff2020-07-15 14:30:28 +0200120 if (!siblings) {
Michal Vasko52927e22020-03-16 17:26:14 +0100121 attr->next = NULL;
122 }
123
124 for (iter = attr; iter; ) {
125 attr = iter;
126 iter = iter->next;
127
Radek Krejcid46e46a2020-09-15 14:22:42 +0200128 ly_free_val_prefs(ctx, attr->val_prefs);
Michal Vasko52927e22020-03-16 17:26:14 +0100129 FREE_STRING(ctx, attr->name);
130 FREE_STRING(ctx, attr->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200131 FREE_STRING(ctx, attr->prefix.id);
132 FREE_STRING(ctx, attr->prefix.module_ns);
Michal Vasko52927e22020-03-16 17:26:14 +0100133 free(attr);
134 }
135}
136
Michal Vasko3a41dff2020-07-15 14:30:28 +0200137API void
Radek Krejci011e4aa2020-09-04 15:22:31 +0200138lyd_free_attr_single(const struct ly_ctx *ctx, struct lyd_attr *attr)
Michal Vasko3a41dff2020-07-15 14:30:28 +0200139{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200140 lyd_free_attr(ctx, attr, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200141}
142
143API void
Radek Krejci011e4aa2020-09-04 15:22:31 +0200144lyd_free_attr_siblings(const struct ly_ctx *ctx, struct lyd_attr *attr)
Michal Vasko3a41dff2020-07-15 14:30:28 +0200145{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200146 lyd_free_attr(ctx, attr, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200147}
148
Radek Krejcie7b95092019-05-15 11:03:07 +0200149/**
150 * @brief Free Data (sub)tree.
Radek Krejcie7b95092019-05-15 11:03:07 +0200151 * @param[in] node Data node to be freed.
152 * @param[in] top Recursion flag to unlink the root of the subtree being freed.
153 */
154static void
Radek Krejci857189e2020-09-01 13:26:36 +0200155lyd_free_subtree(struct lyd_node *node, ly_bool top)
Radek Krejcie7b95092019-05-15 11:03:07 +0200156{
157 struct lyd_node *iter, *next;
158 struct lyd_node *children;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +0200159 struct lyd_node_opaq *opaq = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +0200160
161 assert(node);
162
Michal Vasko52927e22020-03-16 17:26:14 +0100163 if (!node->schema) {
164 opaq = (struct lyd_node_opaq *)node;
Radek Krejcie7b95092019-05-15 11:03:07 +0200165
166 /* free the children */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200167 children = lyd_child(node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200168 LY_LIST_FOR_SAFE(children, next, iter) {
Michal Vasko52927e22020-03-16 17:26:14 +0100169 lyd_free_subtree(iter, 0);
170 }
171
Michal Vaskob7be7a82020-08-20 09:09:04 +0200172 FREE_STRING(LYD_CTX(opaq), opaq->name);
173 FREE_STRING(LYD_CTX(opaq), opaq->prefix.id);
174 FREE_STRING(LYD_CTX(opaq), opaq->prefix.module_ns);
175 ly_free_val_prefs(LYD_CTX(opaq), opaq->val_prefs);
176 FREE_STRING(LYD_CTX(opaq), opaq->value);
Michal Vasko52927e22020-03-16 17:26:14 +0100177 } else if (node->schema->nodetype & LYD_NODE_INNER) {
178 /* remove children hash table in case of inner data node */
179 lyht_free(((struct lyd_node_inner *)node)->children_ht);
180 ((struct lyd_node_inner *)node)->children_ht = NULL;
181
182 /* free the children */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200183 children = lyd_child(node);
Michal Vasko52927e22020-03-16 17:26:14 +0100184 LY_LIST_FOR_SAFE(children, next, iter) {
185 lyd_free_subtree(iter, 0);
Radek Krejcie7b95092019-05-15 11:03:07 +0200186 }
187 } else if (node->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +0200188 /* only frees the value this way */
189 lyd_any_copy_value(node, NULL, 0);
Radek Krejcie7b95092019-05-15 11:03:07 +0200190 } else if (node->schema->nodetype & LYD_NODE_TERM) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200191 ((struct lysc_node_leaf *)node->schema)->type->plugin->free(LYD_CTX(node), &((struct lyd_node_term *)node)->value);
Radek Krejcie7b95092019-05-15 11:03:07 +0200192 }
193
Michal Vasko52927e22020-03-16 17:26:14 +0100194 if (!node->schema) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200195 lyd_free_attr_siblings(LYD_CTX(node), opaq->attr);
Michal Vasko52927e22020-03-16 17:26:14 +0100196 } else {
197 /* free the node's metadata */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200198 lyd_free_meta_siblings(node->meta);
Michal Vasko52927e22020-03-16 17:26:14 +0100199 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200200
201 /* unlink only the nodes from the first level, nodes in subtree are freed all, so no unlink is needed */
202 if (top) {
203 lyd_unlink_tree(node);
204 }
205
206 free(node);
207}
208
209API void
210lyd_free_tree(struct lyd_node *node)
211{
212 if (!node) {
213 return;
214 }
215
Michal Vasko52927e22020-03-16 17:26:14 +0100216 lyd_free_subtree(node, 1);
Radek Krejcie7b95092019-05-15 11:03:07 +0200217}
218
Radek Krejcib0849a22019-07-25 12:31:04 +0200219static void
Radek Krejci857189e2020-09-01 13:26:36 +0200220lyd_free_(struct lyd_node *node, ly_bool top)
Radek Krejcie7b95092019-05-15 11:03:07 +0200221{
222 struct lyd_node *iter, *next;
223
224 if (!node) {
225 return;
226 }
227
Radek Krejcib0849a22019-07-25 12:31:04 +0200228 /* get the first (top-level) sibling */
229 if (top) {
Michal Vaskod989ba02020-08-24 10:59:24 +0200230 for ( ; node->parent; node = (struct lyd_node *)node->parent) {}
Radek Krejcib0849a22019-07-25 12:31:04 +0200231 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200232 while (node->prev->next) {
233 node = node->prev;
234 }
235
236 LY_LIST_FOR_SAFE(node, next, iter) {
237 /* in case of the top-level nodes (node->parent is NULL), no unlinking needed */
Michal Vasko52927e22020-03-16 17:26:14 +0100238 lyd_free_subtree(iter, iter->parent ? 1 : 0);
Radek Krejcie7b95092019-05-15 11:03:07 +0200239 }
240}
Radek Krejcib0849a22019-07-25 12:31:04 +0200241
242API void
Michal Vaskof03ed032020-03-04 13:31:44 +0100243lyd_free_siblings(struct lyd_node *node)
Radek Krejcib0849a22019-07-25 12:31:04 +0200244{
245 lyd_free_(node, 0);
246}
247
248API void
249lyd_free_all(struct lyd_node *node)
250{
251 lyd_free_(node, 1);
252}