blob: 09ce99d04409e612cf55eb5e11a2e82f588209df [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
Radek Krejcie7b95092019-05-15 11:03:07 +02002 * @file tree_schema_free.c
Radek Krejci19a96102018-11-15 13:38:09 +01003 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejcie7b95092019-05-15 11:03:07 +02004 * @brief Freeing functions for schema tree structures.
Radek Krejci19a96102018-11-15 13:38:09 +01005 *
Radek Krejcie7b95092019-05-15 11:03:07 +02006 * Copyright (c) 2019 CESNET, z.s.p.o.
Radek Krejci19a96102018-11-15 13:38:09 +01007 *
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 <stdlib.h>
16
Radek Krejci535ea9f2020-05-29 16:01:05 +020017#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020018#include "compat.h"
Radek Krejci47fab892020-11-05 17:02:41 +010019#include "log.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include "plugins_exts.h"
21#include "plugins_types.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "tree_data.h"
Michal Vaskofc2cd072021-02-24 13:17:17 +010024#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010025#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010027#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "xml.h"
Radek Krejci19a96102018-11-15 13:38:09 +010029#include "xpath.h"
30
Radek Krejci2a9fc652021-01-22 17:44:34 +010031void lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010032static void lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node);
Radek Krejci19a96102018-11-15 13:38:09 +010033
34static void
35lysp_stmt_free(struct ly_ctx *ctx, struct lysp_stmt *stmt)
36{
37 struct lysp_stmt *child, *next;
38
Michal Vaskoe180ed02021-02-05 16:31:20 +010039 lydict_remove(ctx, stmt->stmt);
40 lydict_remove(ctx, stmt->arg);
Michal Vaskofc2cd072021-02-24 13:17:17 +010041 ly_free_prefix_data(stmt->format, stmt->prefix_data);
Radek Krejci19a96102018-11-15 13:38:09 +010042
43 LY_LIST_FOR_SAFE(stmt->child, next, child) {
44 lysp_stmt_free(ctx, child);
45 }
46
47 free(stmt);
48}
49
Radek Krejci2d7a47b2019-05-16 13:34:10 +020050void
Radek Krejci19a96102018-11-15 13:38:09 +010051lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext)
52{
53 struct lysp_stmt *stmt, *next;
54
Michal Vaskoe180ed02021-02-05 16:31:20 +010055 lydict_remove(ctx, ext->name);
56 lydict_remove(ctx, ext->argument);
Michal Vaskofc2cd072021-02-24 13:17:17 +010057 ly_free_prefix_data(ext->format, ext->prefix_data);
Radek Krejci19a96102018-11-15 13:38:09 +010058
59 LY_LIST_FOR_SAFE(ext->child, next, stmt) {
60 lysp_stmt_free(ctx, stmt);
61 }
62}
63
David Sedlák298ff6d2019-07-26 14:29:03 +020064void
Radek Krejci19a96102018-11-15 13:38:09 +010065lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import)
66{
67 /* imported module is freed directly from the context's list */
Michal Vaskoe180ed02021-02-05 16:31:20 +010068 lydict_remove(ctx, import->name);
69 lydict_remove(ctx, import->prefix);
70 lydict_remove(ctx, import->dsc);
71 lydict_remove(ctx, import->ref);
Radek Krejci19a96102018-11-15 13:38:09 +010072 FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free);
73}
74
Radek Krejci771928a2021-01-19 13:42:36 +010075/**
76 * @brief Common function to erase include record in main module and submodule.
77 *
78 * There is a difference since the main module is expected to have the complete list if the included submodules and
79 * the parsed submodule is shared with any include in a submodule. Therefore, the referenced submodules in the include
80 * record are freed only from main module's records.
81 *
82 * @param[in] ctx libyang context
83 * @param[in] include The include record to be erased, the record itself is not freed.
84 * @param[in] main_module Flag to get know if the include record is placed in main module so also the referenced submodule
85 * is supposed to be freed.
86 */
87static void
88lysp_include_free_(struct ly_ctx *ctx, struct lysp_include *include, ly_bool main_module)
Radek Krejci19a96102018-11-15 13:38:09 +010089{
Radek Krejci771928a2021-01-19 13:42:36 +010090 if (main_module && include->submodule) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +020091 lysp_module_free((struct lysp_module *)include->submodule);
Radek Krejci19a96102018-11-15 13:38:09 +010092 }
Michal Vaskoe180ed02021-02-05 16:31:20 +010093 lydict_remove(ctx, include->name);
94 lydict_remove(ctx, include->dsc);
95 lydict_remove(ctx, include->ref);
Radek Krejci19a96102018-11-15 13:38:09 +010096 FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free);
97}
98
David Sedlákaa854b02019-07-22 14:17:10 +020099void
Radek Krejci771928a2021-01-19 13:42:36 +0100100lysp_include_free_submodule(struct ly_ctx *ctx, struct lysp_include *include)
101{
102 return lysp_include_free_(ctx, include, 0);
103}
104
105void
106lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include)
107{
108 return lysp_include_free_(ctx, include, 1);
109}
110
111void
Radek Krejci19a96102018-11-15 13:38:09 +0100112lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev)
113{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100114 lydict_remove(ctx, rev->dsc);
115 lydict_remove(ctx, rev->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100116 FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free);
117}
118
David Sedlák986cb412019-07-04 13:10:11 +0200119void
Radek Krejci19a96102018-11-15 13:38:09 +0100120lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext)
121{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100122 lydict_remove(ctx, ext->name);
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100123 lydict_remove(ctx, ext->argname);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100124 lydict_remove(ctx, ext->dsc);
125 lydict_remove(ctx, ext->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100126 FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free);
Michal Vasko5fe75f12020-03-02 13:52:37 +0100127 if (ext->compiled) {
128 lysc_extension_free(ctx, &ext->compiled);
129 }
Radek Krejci19a96102018-11-15 13:38:09 +0100130}
131
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200132void
Radek Krejci19a96102018-11-15 13:38:09 +0100133lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat)
134{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100135 lydict_remove(ctx, feat->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200136 FREE_ARRAY(ctx, feat->iffeatures, lysp_qname_free);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100137 FREE_ARRAY(ctx, feat->iffeatures_c, lysc_iffeature_free);
138 LY_ARRAY_FREE(feat->depfeatures);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100139 lydict_remove(ctx, feat->dsc);
140 lydict_remove(ctx, feat->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100141 FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
142}
143
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200144void
Radek Krejci19a96102018-11-15 13:38:09 +0100145lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident)
146{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100147 lydict_remove(ctx, ident->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200148 FREE_ARRAY(ctx, ident->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100149 FREE_STRINGS(ctx, ident->bases);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100150 lydict_remove(ctx, ident->dsc);
151 lydict_remove(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100152 FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
153}
154
Michal Vasko7f45cf22020-10-01 12:49:44 +0200155void
Radek Krejci19a96102018-11-15 13:38:09 +0100156lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
157{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100158 lydict_remove(ctx, restr->arg.str);
159 lydict_remove(ctx, restr->emsg);
160 lydict_remove(ctx, restr->eapptag);
161 lydict_remove(ctx, restr->dsc);
162 lydict_remove(ctx, restr->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100163 FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
164}
165
166static void
167lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item)
168{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100169 lydict_remove(ctx, item->name);
170 lydict_remove(ctx, item->dsc);
171 lydict_remove(ctx, item->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200172 FREE_ARRAY(ctx, item->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100173 FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
174}
175
Radek Krejcicdfecd92018-11-26 11:27:32 +0100176void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type);
Michal Vasko004d3152020-06-11 19:59:22 +0200177
David Sedlák32488102019-07-15 17:44:10 +0200178void
Radek Krejci19a96102018-11-15 13:38:09 +0100179lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type)
180{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100181 lydict_remove(ctx, type->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100182 FREE_MEMBER(ctx, type->range, lysp_restr_free);
183 FREE_MEMBER(ctx, type->length, lysp_restr_free);
184 FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
185 FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
186 FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
Michal Vasko004d3152020-06-11 19:59:22 +0200187 lyxp_expr_free(ctx, type->path);
Radek Krejci19a96102018-11-15 13:38:09 +0100188 FREE_STRINGS(ctx, type->bases);
189 FREE_ARRAY(ctx, type->types, lysp_type_free);
190 FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
191 if (type->compiled) {
192 lysc_type_free(ctx, type->compiled);
193 }
194}
195
David Sedlák04e17b22019-07-19 15:29:48 +0200196void
Radek Krejci19a96102018-11-15 13:38:09 +0100197lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
198{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100199 lydict_remove(ctx, tpdf->name);
200 lydict_remove(ctx, tpdf->units);
201 lydict_remove(ctx, tpdf->dflt.str);
202 lydict_remove(ctx, tpdf->dsc);
203 lydict_remove(ctx, tpdf->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100204 FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
205
206 lysp_type_free(ctx, &tpdf->type);
207
208}
209
Radek Krejcif538ce52019-03-05 10:46:14 +0100210void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100211lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp)
Radek Krejci19a96102018-11-15 13:38:09 +0100212{
213 struct lysp_node *node, *next;
214
Radek Krejci19a96102018-11-15 13:38:09 +0100215 FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100216 LY_LIST_FOR_SAFE((struct lysp_node *)grp->groupings, next, node) {
217 lysp_node_free(ctx, node);
218 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100219 LY_LIST_FOR_SAFE(grp->child, next, node) {
Radek Krejci19a96102018-11-15 13:38:09 +0100220 lysp_node_free(ctx, node);
221 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100222 LY_LIST_FOR_SAFE((struct lysp_node *)grp->actions, next, node) {
223 lysp_node_free(ctx, node);
224 }
225 LY_LIST_FOR_SAFE((struct lysp_node *)grp->notifs, next, node) {
226 lysp_node_free(ctx, node);
227 }
Radek Krejci19a96102018-11-15 13:38:09 +0100228}
229
Radek Krejcif09e4e82019-06-14 15:08:11 +0200230void
Radek Krejci19a96102018-11-15 13:38:09 +0100231lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
232{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100233 lydict_remove(ctx, when->cond);
234 lydict_remove(ctx, when->dsc);
235 lydict_remove(ctx, when->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100236 FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
237}
238
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200239void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100240lysp_augment_free(struct ly_ctx *ctx, struct lysp_node_augment *augment)
Radek Krejci19a96102018-11-15 13:38:09 +0100241{
242 struct lysp_node *node, *next;
243
Radek Krejci19a96102018-11-15 13:38:09 +0100244 LY_LIST_FOR_SAFE(augment->child, next, node) {
245 lysp_node_free(ctx, node);
246 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100247 LY_LIST_FOR_SAFE((struct lysp_node *)augment->actions, next, node) {
248 lysp_node_free(ctx, node);
249 }
250 LY_LIST_FOR_SAFE((struct lysp_node *)augment->notifs, next, node) {
251 lysp_node_free(ctx, node);
252 }
Radek Krejci19a96102018-11-15 13:38:09 +0100253}
254
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200255void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200256lysp_qname_free(struct ly_ctx *ctx, struct lysp_qname *qname)
257{
258 if (qname) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100259 lydict_remove(ctx, qname->str);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200260 }
261}
262
263void
Radek Krejci19a96102018-11-15 13:38:09 +0100264lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
265{
Michal Vasko22df3f02020-08-24 13:29:22 +0200266 struct lysp_deviate_add *add = (struct lysp_deviate_add *)d;
267 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl *)d;
Radek Krejci19a96102018-11-15 13:38:09 +0100268
269 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
Michal Vaskod989ba02020-08-24 10:59:24 +0200270 switch (d->mod) {
Radek Krejci19a96102018-11-15 13:38:09 +0100271 case LYS_DEV_NOT_SUPPORTED:
272 /* nothing to do */
273 break;
274 case LYS_DEV_ADD:
275 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100276 lydict_remove(ctx, add->units);
Radek Krejci19a96102018-11-15 13:38:09 +0100277 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200278 FREE_ARRAY(ctx, add->uniques, lysp_qname_free);
279 FREE_ARRAY(ctx, add->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100280 break;
281 case LYS_DEV_REPLACE:
282 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100283 lydict_remove(ctx, rpl->units);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200284 lysp_qname_free(ctx, &rpl->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +0100285 break;
286 default:
287 LOGINT(ctx);
288 break;
289 }
290}
291
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200292void
Radek Krejci19a96102018-11-15 13:38:09 +0100293lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
294{
295 struct lysp_deviate *next, *iter;
296
Michal Vaskoe180ed02021-02-05 16:31:20 +0100297 lydict_remove(ctx, dev->nodeid);
298 lydict_remove(ctx, dev->dsc);
299 lydict_remove(ctx, dev->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100300 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
301 lysp_deviate_free(ctx, iter);
302 free(iter);
303 }
304 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
305}
306
David Sedlákd2d676a2019-07-22 11:28:19 +0200307void
Radek Krejci19a96102018-11-15 13:38:09 +0100308lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
309{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100310 lydict_remove(ctx, ref->nodeid);
311 lydict_remove(ctx, ref->dsc);
312 lydict_remove(ctx, ref->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200313 FREE_ARRAY(ctx, ref->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100314 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100315 lydict_remove(ctx, ref->presence);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200316 FREE_ARRAY(ctx, ref->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100317 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
318}
319
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200320void
Radek Krejci19a96102018-11-15 13:38:09 +0100321lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
322{
323 struct lysp_node *child, *next;
Radek Krejci9a3823e2021-01-27 20:26:46 +0100324 struct lysp_restr *musts = lysp_node_musts(node);
325 struct lysp_when *when = lysp_node_when(node);
Radek Krejci19a96102018-11-15 13:38:09 +0100326
Michal Vaskoe180ed02021-02-05 16:31:20 +0100327 lydict_remove(ctx, node->name);
328 lydict_remove(ctx, node->dsc);
329 lydict_remove(ctx, node->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200330 FREE_ARRAY(ctx, node->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100331 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
332
Radek Krejci9a3823e2021-01-27 20:26:46 +0100333 FREE_MEMBER(ctx, when, lysp_when_free);
334 FREE_ARRAY(ctx, musts, lysp_restr_free);
335
Michal Vaskod989ba02020-08-24 10:59:24 +0200336 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100337 case LYS_CONTAINER:
Michal Vaskoe180ed02021-02-05 16:31:20 +0100338 lydict_remove(ctx, ((struct lysp_node_container *)node)->presence);
Michal Vasko22df3f02020-08-24 13:29:22 +0200339 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100340 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100341 lysp_node_free(ctx, child);
342 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200343 LY_LIST_FOR_SAFE(((struct lysp_node_container *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100344 lysp_node_free(ctx, child);
345 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100346 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->actions->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100347 lysp_node_free(ctx, child);
348 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100349 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->notifs->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100350 lysp_node_free(ctx, child);
351 }
Radek Krejci19a96102018-11-15 13:38:09 +0100352 break;
353 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200354 lysp_type_free(ctx, &((struct lysp_node_leaf *)node)->type);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100355 lydict_remove(ctx, ((struct lysp_node_leaf *)node)->units);
356 lydict_remove(ctx, ((struct lysp_node_leaf *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100357 break;
358 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200359 lysp_type_free(ctx, &((struct lysp_node_leaflist *)node)->type);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100360 lydict_remove(ctx, ((struct lysp_node_leaflist *)node)->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200361 FREE_ARRAY(ctx, ((struct lysp_node_leaflist *)node)->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100362 break;
363 case LYS_LIST:
Michal Vaskoe180ed02021-02-05 16:31:20 +0100364 lydict_remove(ctx, ((struct lysp_node_list *)node)->key);
Michal Vasko22df3f02020-08-24 13:29:22 +0200365 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100366 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100367 lysp_node_free(ctx, child);
368 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200369 LY_LIST_FOR_SAFE(((struct lysp_node_list *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100370 lysp_node_free(ctx, child);
371 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100372 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->actions->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100373 lysp_node_free(ctx, child);
374 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100375 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->notifs->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100376 lysp_node_free(ctx, child);
377 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200378 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->uniques, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100379 break;
380 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200381 LY_LIST_FOR_SAFE(((struct lysp_node_choice *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100382 lysp_node_free(ctx, child);
383 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100384 lydict_remove(ctx, ((struct lysp_node_choice *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100385 break;
386 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200387 LY_LIST_FOR_SAFE(((struct lysp_node_case *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100388 lysp_node_free(ctx, child);
389 }
390 break;
391 case LYS_ANYDATA:
392 case LYS_ANYXML:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100393 /* nothing special to do */
Radek Krejci19a96102018-11-15 13:38:09 +0100394 break;
395 case LYS_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +0200396 FREE_ARRAY(ctx, ((struct lysp_node_uses *)node)->refines, lysp_refine_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100397 LY_LIST_FOR_SAFE(&((struct lysp_node_uses *)node)->augments->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100398 lysp_node_free(ctx, child);
399 }
400 break;
401 case LYS_RPC:
402 case LYS_ACTION:
403 FREE_ARRAY(ctx, ((struct lysp_node_action *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100404 LY_LIST_FOR_SAFE(&((struct lysp_node_action *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100405 lysp_node_free(ctx, child);
406 }
407 if (((struct lysp_node_action *)node)->input.nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100408 lysp_node_free(ctx, &((struct lysp_node_action *)node)->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100409 }
410 if (((struct lysp_node_action *)node)->output.nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100411 lysp_node_free(ctx, &((struct lysp_node_action *)node)->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100412 }
413 break;
414 case LYS_INPUT:
415 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100416 FREE_ARRAY(ctx, ((struct lysp_node_action_inout *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100417 LY_LIST_FOR_SAFE(&((struct lysp_node_action_inout *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100418 lysp_node_free(ctx, child);
419 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100420 LY_LIST_FOR_SAFE(((struct lysp_node_action_inout *)node)->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100421 lysp_node_free(ctx, child);
422 }
423 /* do not free the node, it is never standalone but part of the action node */
424 return;
425 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100426 FREE_ARRAY(ctx, ((struct lysp_node_notif *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100427 LY_LIST_FOR_SAFE(&((struct lysp_node_notif *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100428 lysp_node_free(ctx, child);
429 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100430 LY_LIST_FOR_SAFE(((struct lysp_node_notif *)node)->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100431 lysp_node_free(ctx, child);
432 }
433 break;
434 case LYS_GROUPING:
435 lysp_grp_free(ctx, (struct lysp_node_grp *)node);
436 break;
437 case LYS_AUGMENT:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100438 lysp_augment_free(ctx, ((struct lysp_node_augment *)node));
Radek Krejci19a96102018-11-15 13:38:09 +0100439 break;
440 default:
441 LOGINT(ctx);
442 }
443
444 free(node);
445}
446
Radek Krejci15f10ab2020-11-03 14:14:14 +0100447void
Radek Krejci19a96102018-11-15 13:38:09 +0100448lysp_module_free(struct lysp_module *module)
449{
450 struct ly_ctx *ctx;
451 struct lysp_node *node, *next;
452
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100453 if (!module) {
454 return;
455 }
456 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100457
458 FREE_ARRAY(ctx, module->imports, lysp_import_free);
Radek Krejci771928a2021-01-19 13:42:36 +0100459 FREE_ARRAY(ctx, module->includes, module->is_submod ? lysp_include_free_submodule : lysp_include_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100460
Radek Krejci19a96102018-11-15 13:38:09 +0100461 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
462 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
463 FREE_ARRAY(ctx, module->features, lysp_feature_free);
464 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
465 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100466 LY_LIST_FOR_SAFE((struct lysp_node *)module->groupings, next, node) {
467 lysp_node_free(ctx, node);
468 }
Radek Krejci19a96102018-11-15 13:38:09 +0100469 LY_LIST_FOR_SAFE(module->data, next, node) {
470 lysp_node_free(ctx, node);
471 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100472 LY_LIST_FOR_SAFE((struct lysp_node *)module->augments, next, node) {
473 lysp_node_free(ctx, node);
474 }
475 LY_LIST_FOR_SAFE((struct lysp_node *)module->rpcs, next, node) {
476 lysp_node_free(ctx, node);
477 }
478 LY_LIST_FOR_SAFE((struct lysp_node *)module->notifs, next, node) {
479 lysp_node_free(ctx, node);
480 }
Radek Krejci19a96102018-11-15 13:38:09 +0100481 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
482 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
483
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200484 if (module->is_submod) {
485 struct lysp_submodule *submod = (struct lysp_submodule *)module;
486
Michal Vaskoe180ed02021-02-05 16:31:20 +0100487 lydict_remove(ctx, submod->name);
488 lydict_remove(ctx, submod->filepath);
489 lydict_remove(ctx, submod->prefix);
490 lydict_remove(ctx, submod->org);
491 lydict_remove(ctx, submod->contact);
492 lydict_remove(ctx, submod->dsc);
493 lydict_remove(ctx, submod->ref);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200494 }
495
Radek Krejci19a96102018-11-15 13:38:09 +0100496 free(module);
497}
498
Radek Krejci0af46292019-01-11 16:02:31 +0100499void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100500lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
501{
502 if (--(*ext)->refcount) {
503 return;
504 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100505 lydict_remove(ctx, (*ext)->name);
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100506 lydict_remove(ctx, (*ext)->argname);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100507 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
508 free(*ext);
Radek Krejci720d2612021-03-03 19:44:22 +0100509 *ext = NULL;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100510}
511
512void
Radek Krejci19a96102018-11-15 13:38:09 +0100513lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
514{
fredganebc50572019-10-31 15:39:23 +0800515 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200516 ext->def->plugin->free(ctx, ext);
517 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100518 if (ext->def) {
519 lysc_extension_free(ctx, &ext->def);
520 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100521 lydict_remove(ctx, ext->argument);
Radek Krejci0935f412019-08-20 16:15:18 +0200522 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
523}
524
525void
Radek Krejci19a96102018-11-15 13:38:09 +0100526lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
527{
528 LY_ARRAY_FREE(iff->features);
529 free(iff->expr);
530}
531
532static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100533lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100534{
Radek Krejci00b874b2019-02-12 10:54:50 +0100535 if (--(*w)->refcount) {
536 return;
537 }
538 lyxp_expr_free(ctx, (*w)->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200539 lysc_prefixes_free((*w)->prefixes);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100540 lydict_remove(ctx, (*w)->dsc);
541 lydict_remove(ctx, (*w)->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +0100542 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
543 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100544}
545
Radek Krejciccd20f12019-02-15 14:12:27 +0100546void
Radek Krejci58d171e2018-11-23 13:50:55 +0100547lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
548{
549 lyxp_expr_free(ctx, must->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200550 lysc_prefixes_free(must->prefixes);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100551 lydict_remove(ctx, must->emsg);
552 lydict_remove(ctx, must->eapptag);
553 lydict_remove(ctx, must->dsc);
554 lydict_remove(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100555 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
556}
557
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100558void
Radek Krejci19a96102018-11-15 13:38:09 +0100559lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
560{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100561 lydict_remove(ctx, ident->name);
562 lydict_remove(ctx, ident->dsc);
563 lydict_remove(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100564 LY_ARRAY_FREE(ident->derived);
565 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
566}
567
Radek Krejci19a96102018-11-15 13:38:09 +0100568static void
569lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
570{
571 LY_ARRAY_FREE(range->parts);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100572 lydict_remove(ctx, range->eapptag);
573 lydict_remove(ctx, range->emsg);
574 lydict_remove(ctx, range->dsc);
575 lydict_remove(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100576 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
577}
578
579static void
580lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
581{
582 if (--(*pattern)->refcount) {
583 return;
584 }
Radek Krejci54579462019-04-30 12:47:06 +0200585 pcre2_code_free((*pattern)->code);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100586 lydict_remove(ctx, (*pattern)->expr);
587 lydict_remove(ctx, (*pattern)->eapptag);
588 lydict_remove(ctx, (*pattern)->emsg);
589 lydict_remove(ctx, (*pattern)->dsc);
590 lydict_remove(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100591 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
592 free(*pattern);
593}
594
595static void
Radek Krejci693262f2019-04-29 15:23:20 +0200596lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100597{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100598 lydict_remove(ctx, item->name);
599 lydict_remove(ctx, item->dsc);
600 lydict_remove(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100601 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
602}
603
Radek Krejcia3045382018-11-22 14:30:31 +0100604static void
605lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
606{
607 lysc_type_free(ctx, *type);
608}
Radek Krejci0f969882020-08-21 16:56:47 +0200609
Radek Krejcicdfecd92018-11-26 11:27:32 +0100610void
Radek Krejci19a96102018-11-15 13:38:09 +0100611lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
612{
613 if (--type->refcount) {
614 return;
615 }
Radek Krejcib915ac92020-08-14 23:31:04 +0200616
Michal Vaskod989ba02020-08-24 10:59:24 +0200617 switch (type->basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100618 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +0200619 FREE_MEMBER(ctx, ((struct lysc_type_bin *)type)->length, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100620 break;
621 case LY_TYPE_BITS:
Michal Vasko22df3f02020-08-24 13:29:22 +0200622 FREE_ARRAY(ctx, (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)type)->bits, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100623 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100624 case LY_TYPE_DEC64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200625 FREE_MEMBER(ctx, ((struct lysc_type_dec *)type)->range, lysc_range_free);
Radek Krejci6cba4292018-11-15 17:33:29 +0100626 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100627 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +0200628 FREE_MEMBER(ctx, ((struct lysc_type_str *)type)->length, lysc_range_free);
629 FREE_ARRAY(ctx, ((struct lysc_type_str *)type)->patterns, lysc_pattern_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100630 break;
631 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +0200632 FREE_ARRAY(ctx, ((struct lysc_type_enum *)type)->enums, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100633 break;
634 case LY_TYPE_INT8:
635 case LY_TYPE_UINT8:
636 case LY_TYPE_INT16:
637 case LY_TYPE_UINT16:
638 case LY_TYPE_INT32:
639 case LY_TYPE_UINT32:
640 case LY_TYPE_INT64:
641 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200642 FREE_MEMBER(ctx, ((struct lysc_type_num *)type)->range, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100643 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100644 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +0200645 LY_ARRAY_FREE(((struct lysc_type_identityref *)type)->bases);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100646 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100647 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +0200648 FREE_ARRAY(ctx, ((struct lysc_type_union *)type)->types, lysc_type2_free);
Radek Krejcia3045382018-11-22 14:30:31 +0100649 break;
650 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200651 lyxp_expr_free(ctx, ((struct lysc_type_leafref *)type)->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200652 lysc_prefixes_free(((struct lysc_type_leafref *)type)->prefixes);
Radek Krejcia3045382018-11-22 14:30:31 +0100653 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100654 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100655 case LY_TYPE_BOOL:
656 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100657 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100658 /* nothing to do */
659 break;
660 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200661
662 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100663 free(type);
664}
665
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100666void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100667lysc_node_action_inout_free(struct ly_ctx *ctx, struct lysc_node_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100668{
669 struct lysc_node *child, *child_next;
670
Radek Krejcif538ce52019-03-05 10:46:14 +0100671 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100672 LY_LIST_FOR_SAFE(inout->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100673 lysc_node_free_(ctx, child);
Radek Krejcif538ce52019-03-05 10:46:14 +0100674 }
675}
676
677void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100678lysc_node_action_free(struct ly_ctx *ctx, struct lysc_node_action *action)
Radek Krejcif538ce52019-03-05 10:46:14 +0100679{
Michal Vasko37a0fe62021-02-03 09:53:04 +0100680 FREE_ARRAY(ctx, action->when, lysc_when_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100681 if (action->input.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100682 lysc_node_free_(ctx, &action->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100683 }
684 if (action->output.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100685 lysc_node_free_(ctx, &action->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100686 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100687}
688
Radek Krejcifc11bd72019-04-11 16:00:05 +0200689void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100690lysc_node_notif_free(struct ly_ctx *ctx, struct lysc_node_notif *notif)
Radek Krejcifc11bd72019-04-11 16:00:05 +0200691{
692 struct lysc_node *child, *child_next;
693
Michal Vasko37a0fe62021-02-03 09:53:04 +0100694 FREE_ARRAY(ctx, notif->when, lysc_when_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200695 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100696 LY_LIST_FOR_SAFE(notif->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100697 lysc_node_free_(ctx, child);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200698 }
699}
700
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200701void
Radek Krejci19a96102018-11-15 13:38:09 +0100702lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
703{
704 struct lysc_node *child, *child_next;
705
706 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100707 lysc_node_free_(ctx, child);
Radek Krejci19a96102018-11-15 13:38:09 +0100708 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100709 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
710 lysc_node_free_(ctx, child);
711 }
712 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
713 lysc_node_free_(ctx, child);
714 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100715 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100716 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100717}
718
719static void
720lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
721{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100722 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100723 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100724 if (node->type) {
725 lysc_type_free(ctx, node->type);
726 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100727 lydict_remove(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200728 if (node->dflt) {
729 node->dflt->realtype->plugin->free(ctx, node->dflt);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200730 lysc_type_free(ctx, (struct lysc_type *)node->dflt->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200731 free(node->dflt);
732 }
Radek Krejci19a96102018-11-15 13:38:09 +0100733}
734
Radek Krejci42452ac2018-11-28 17:09:52 +0100735static void
736lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
737{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200738 LY_ARRAY_COUNT_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100739
Radek Krejci9a3823e2021-01-27 20:26:46 +0100740 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci42452ac2018-11-28 17:09:52 +0100741 FREE_ARRAY(ctx, node->musts, lysc_must_free);
742 if (node->type) {
743 lysc_type_free(ctx, node->type);
744 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100745 lydict_remove(ctx, node->units);
Radek Krejci42452ac2018-11-28 17:09:52 +0100746 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200747 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200748 lysc_type_free(ctx, (struct lysc_type *)node->dflts[u]->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200749 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100750 }
751 LY_ARRAY_FREE(node->dflts);
752}
753
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100754static void
755lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
756{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200757 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100758 struct lysc_node *child, *child_next;
759
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100760 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100761 lysc_node_free_(ctx, child);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100762 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100763 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100764 FREE_ARRAY(ctx, node->musts, lysc_must_free);
765
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100766 LY_ARRAY_FOR(node->uniques, u) {
767 LY_ARRAY_FREE(node->uniques[u]);
768 }
769 LY_ARRAY_FREE(node->uniques);
770
Radek Krejci2a9fc652021-01-22 17:44:34 +0100771 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
772 lysc_node_free_(ctx, child);
773 }
774 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
775 lysc_node_free_(ctx, child);
776 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100777}
778
Radek Krejci056d0a82018-12-06 16:57:25 +0100779static void
780lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
781{
782 struct lysc_node *child, *child_next;
783
Radek Krejci9a3823e2021-01-27 20:26:46 +0100784 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200785 LY_LIST_FOR_SAFE((struct lysc_node *)node->cases, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100786 lysc_node_free_(ctx, child);
Michal Vasko20424b42020-08-31 12:29:38 +0200787 }
788}
789
790static void
791lysc_node_case_free(struct ly_ctx *ctx, struct lysc_node_case *node)
792{
793 struct lysc_node *child, *child_next;
794
Radek Krejci9a3823e2021-01-27 20:26:46 +0100795 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200796 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100797 lysc_node_free_(ctx, child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100798 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100799}
Radek Krejci056d0a82018-12-06 16:57:25 +0100800
Radek Krejci9800fb82018-12-13 14:26:23 +0100801static void
802lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
803{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100804 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9800fb82018-12-13 14:26:23 +0100805 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100806}
807
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100808static void
809lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +0100810{
Radek Krejci2a9fc652021-01-22 17:44:34 +0100811 ly_bool inout = 0;
812
Radek Krejci19a96102018-11-15 13:38:09 +0100813 /* common part */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100814 lydict_remove(ctx, node->name);
815 lydict_remove(ctx, node->dsc);
816 lydict_remove(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100817
818 /* nodetype-specific part */
Michal Vaskod989ba02020-08-24 10:59:24 +0200819 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100820 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200821 lysc_node_container_free(ctx, (struct lysc_node_container *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100822 break;
823 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200824 lysc_node_leaf_free(ctx, (struct lysc_node_leaf *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100825 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100826 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200827 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist *)node);
Radek Krejci42452ac2018-11-28 17:09:52 +0100828 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100829 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200830 lysc_node_list_free(ctx, (struct lysc_node_list *)node);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100831 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100832 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200833 lysc_node_choice_free(ctx, (struct lysc_node_choice *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100834 break;
835 case LYS_CASE:
Michal Vasko20424b42020-08-31 12:29:38 +0200836 lysc_node_case_free(ctx, (struct lysc_node_case *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100837 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100838 case LYS_ANYDATA:
839 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200840 lysc_node_anydata_free(ctx, (struct lysc_node_anydata *)node);
Radek Krejci9800fb82018-12-13 14:26:23 +0100841 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100842 case LYS_RPC:
843 case LYS_ACTION:
844 lysc_node_action_free(ctx, (struct lysc_node_action *)node);
845 break;
846 case LYS_INPUT:
847 case LYS_OUTPUT:
848 lysc_node_action_inout_free(ctx, (struct lysc_node_action_inout *)node);
849 inout = 1;
850 break;
851 case LYS_NOTIF:
852 lysc_node_notif_free(ctx, (struct lysc_node_notif *)node);
853 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100854 default:
855 LOGINT(ctx);
856 }
857
Radek Krejci056d0a82018-12-06 16:57:25 +0100858 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100859
860 if (!inout) {
861 free(node);
862 }
Radek Krejci19a96102018-11-15 13:38:09 +0100863}
864
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100865void
866lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node, ly_bool unlink)
867{
868 struct lysc_node *iter, **child_p;
869
Radek Krejci2a9fc652021-01-22 17:44:34 +0100870 if (node->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
871 /* nothing to do - inouts are part of actions and cannot be unlinked/freed separately */
872 return;
873 }
874
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100875 if (unlink) {
876 /* unlink from siblings */
877 if (node->prev->next) {
878 node->prev->next = node->next;
879 }
880 if (node->next) {
881 node->next->prev = node->prev;
882 } else {
883 /* unlinking the last node */
884 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100885 if (node->nodetype == LYS_ACTION) {
886 iter = (struct lysc_node *)lysc_node_actions(node->parent);
887 } else if (node->nodetype == LYS_NOTIF) {
888 iter = (struct lysc_node *)lysc_node_notifs(node->parent);
889 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +0100890 iter = (struct lysc_node *)lysc_node_child(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100891 }
Michal Vasko539c4a62020-11-03 17:21:34 +0100892 LY_CHECK_ERR_RET(!iter, LOGINT(ctx), );
Radek Krejci2a9fc652021-01-22 17:44:34 +0100893 } else if (node->nodetype == LYS_RPC) {
894 iter = (struct lysc_node *)node->module->compiled->rpcs;
895 } else if (node->nodetype == LYS_NOTIF) {
896 iter = (struct lysc_node *)node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100897 } else {
898 iter = node->module->compiled->data;
899 }
900 /* update the "last" pointer from the first node */
901 iter->prev = node->prev;
902 }
903
904 /* unlink from parent */
905 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100906 if (node->nodetype == LYS_ACTION) {
907 child_p = (struct lysc_node **)lysc_node_actions_p(node->parent);
908 } else if (node->nodetype == LYS_NOTIF) {
909 child_p = (struct lysc_node **)lysc_node_notifs_p(node->parent);
910 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +0100911 child_p = lysc_node_child_p(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100912 }
913 } else if (node->nodetype == LYS_RPC) {
914 child_p = (struct lysc_node **)&node->module->compiled->rpcs;
915 } else if (node->nodetype == LYS_NOTIF) {
916 child_p = (struct lysc_node **)&node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100917 } else {
918 child_p = &node->module->compiled->data;
919 }
920 if (child_p && (*child_p == node)) {
921 /* the node is the first child */
922 *child_p = node->next;
923 }
924 }
925
926 lysc_node_free_(ctx, node);
927}
928
Radek Krejci19a96102018-11-15 13:38:09 +0100929static void
930lysc_module_free_(struct lysc_module *module)
931{
932 struct ly_ctx *ctx;
933 struct lysc_node *node, *node_next;
934
Michal Vaskod989ba02020-08-24 10:59:24 +0200935 LY_CHECK_ARG_RET(NULL, module, );
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100936 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100937
Radek Krejci19a96102018-11-15 13:38:09 +0100938 LY_LIST_FOR_SAFE(module->data, node_next, node) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100939 lysc_node_free_(ctx, node);
Radek Krejci19a96102018-11-15 13:38:09 +0100940 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100941 LY_LIST_FOR_SAFE((struct lysc_node *)module->rpcs, node_next, node) {
942 lysc_node_free_(ctx, node);
943 }
944 LY_LIST_FOR_SAFE((struct lysc_node *)module->notifs, node_next, node) {
945 lysc_node_free_(ctx, node);
946 }
Radek Krejci19a96102018-11-15 13:38:09 +0100947 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
948
949 free(module);
950}
951
952void
953lysc_module_free(struct lysc_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
954{
Radek Krejci9b042892019-02-13 14:28:44 +0100955 /* TODO use the destructor, this just suppress warning about unused parameter */
956 (void) private_destructor;
957
Radek Krejci19a96102018-11-15 13:38:09 +0100958 if (module) {
959 lysc_module_free_(module);
960 }
961}
962
963void
964lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
965{
966 if (!module) {
967 return;
968 }
969
970 lysc_module_free(module->compiled, private_destructor);
Radek Krejci80d281e2020-09-14 17:42:54 +0200971 FREE_ARRAY(module->ctx, module->identities, lysc_ident_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100972 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100973
Michal Vasko7f45cf22020-10-01 12:49:44 +0200974 LY_ARRAY_FREE(module->augmented_by);
975 LY_ARRAY_FREE(module->deviated_by);
976
Michal Vaskoe180ed02021-02-05 16:31:20 +0100977 lydict_remove(module->ctx, module->name);
978 lydict_remove(module->ctx, module->revision);
979 lydict_remove(module->ctx, module->ns);
980 lydict_remove(module->ctx, module->prefix);
981 lydict_remove(module->ctx, module->filepath);
982 lydict_remove(module->ctx, module->org);
983 lydict_remove(module->ctx, module->contact);
984 lydict_remove(module->ctx, module->dsc);
985 lydict_remove(module->ctx, module->ref);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100986
Radek Krejci19a96102018-11-15 13:38:09 +0100987 free(module);
988}
Michal Vasko33ff9422020-07-03 09:50:39 +0200989
Radek Krejci38d85362019-09-05 16:26:38 +0200990API void
Radek Krejci1b2eef82021-02-17 11:17:27 +0100991lysc_extension_instance_substatements_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
Radek Krejci38d85362019-09-05 16:26:38 +0200992{
Radek Krejci1b2eef82021-02-17 11:17:27 +0100993 LY_ARRAY_COUNT_TYPE u;
994
995 LY_ARRAY_FOR(substmts, u) {
Radek Krejci38d85362019-09-05 16:26:38 +0200996 if (!substmts[u].storage) {
997 continue;
998 }
999
Michal Vaskod989ba02020-08-24 10:59:24 +02001000 switch (substmts[u].stmt) {
Radek Krejci6b88a462021-02-17 12:39:34 +01001001 case LY_STMT_ACTION:
1002 case LY_STMT_ANYDATA:
1003 case LY_STMT_ANYXML:
1004 case LY_STMT_CONTAINER:
1005 case LY_STMT_CHOICE:
1006 case LY_STMT_LEAF:
1007 case LY_STMT_LEAF_LIST:
1008 case LY_STMT_LIST:
1009 case LY_STMT_NOTIFICATION:
1010 case LY_STMT_RPC:
1011 case LY_STMT_USES: {
1012 struct lysc_node *child, *child_next;
1013
1014 LY_LIST_FOR_SAFE(*((struct lysc_node **)substmts[u].storage), child_next, child) {
1015 lysc_node_free_(ctx, child);
Radek Krejci38d85362019-09-05 16:26:38 +02001016 }
1017 break;
Radek Krejci6b88a462021-02-17 12:39:34 +01001018 }
1019 case LY_STMT_CONFIG:
1020 case LY_STMT_STATUS:
1021 /* nothing to do */
1022 break;
Radek Krejci1b2eef82021-02-17 11:17:27 +01001023 case LY_STMT_DESCRIPTION:
1024 case LY_STMT_REFERENCE:
Radek Krejci38d85362019-09-05 16:26:38 +02001025 case LY_STMT_UNITS:
1026 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1027 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02001028 const char *str = *((const char **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001029 if (!str) {
1030 break;
1031 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001032 lydict_remove(ctx, str);
Radek Krejci38d85362019-09-05 16:26:38 +02001033 } else {
1034 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +02001035 const char **strs = *((const char ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001036 if (!strs) {
1037 break;
1038 }
1039 FREE_STRINGS(ctx, strs);
1040 }
1041 break;
Radek Krejci38d85362019-09-05 16:26:38 +02001042 case LY_STMT_IF_FEATURE: {
Michal Vasko22df3f02020-08-24 13:29:22 +02001043 struct lysc_iffeature *iff = *((struct lysc_iffeature **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001044 if (!iff) {
1045 break;
1046 }
1047 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1048 /* single item */
1049 lysc_iffeature_free(ctx, iff);
1050 free(iff);
1051 } else {
1052 /* multiple items */
1053 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
1054 }
1055 break;
Radek Krejci6b88a462021-02-17 12:39:34 +01001056 case LY_STMT_TYPE:
1057 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1058 /* single item */
1059 struct lysc_type *type = *((struct lysc_type **)substmts[u].storage);
1060 if (!type) {
1061 break;
1062 }
1063 lysc_type_free(ctx, type);
1064 } else {
1065 /* multiple items */
1066 struct lysc_type **types = *((struct lysc_type ***)substmts[u].storage);
1067 if (!types) {
1068 break;
1069 }
1070 FREE_ARRAY(ctx, types, lysc_type2_free);
1071 }
1072 break;
Radek Krejci38d85362019-09-05 16:26:38 +02001073 }
1074
Radek Krejci0f969882020-08-21 16:56:47 +02001075 /* TODO other statements */
Radek Krejci38d85362019-09-05 16:26:38 +02001076 default:
1077 LOGINT(ctx);
1078 }
1079 }
Radek Krejci1b2eef82021-02-17 11:17:27 +01001080
1081 LY_ARRAY_FREE(substmts);
Radek Krejci38d85362019-09-05 16:26:38 +02001082}
David Sedlákebd3acf2019-07-26 15:04:32 +02001083
1084void
Michal Vaskob36053d2020-03-26 15:49:30 +01001085yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001086{
1087 if (ctx) {
1088 free(ctx);
1089 }
1090}
1091
1092void
Michal Vaskob36053d2020-03-26 15:49:30 +01001093yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001094{
1095 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001096 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +02001097 free(ctx);
1098 }
1099}