blob: 626c2d48a453a09233cf54990569ebf8fd65114e [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"
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010025#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "xml.h"
Radek Krejci19a96102018-11-15 13:38:09 +010027#include "xpath.h"
28
Radek Krejci2a9fc652021-01-22 17:44:34 +010029void lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp);
Michal Vasko5fe75f12020-03-02 13:52:37 +010030void lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010031static void lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node);
Radek Krejci19a96102018-11-15 13:38:09 +010032
33static void
34lysp_stmt_free(struct ly_ctx *ctx, struct lysp_stmt *stmt)
35{
36 struct lysp_stmt *child, *next;
37
Michal Vaskoe180ed02021-02-05 16:31:20 +010038 lydict_remove(ctx, stmt->stmt);
39 lydict_remove(ctx, stmt->arg);
Radek Krejci19a96102018-11-15 13:38:09 +010040
41 LY_LIST_FOR_SAFE(stmt->child, next, child) {
42 lysp_stmt_free(ctx, child);
43 }
44
45 free(stmt);
46}
47
Radek Krejci2d7a47b2019-05-16 13:34:10 +020048void
Radek Krejci19a96102018-11-15 13:38:09 +010049lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext)
50{
51 struct lysp_stmt *stmt, *next;
52
Michal Vaskoe180ed02021-02-05 16:31:20 +010053 lydict_remove(ctx, ext->name);
54 lydict_remove(ctx, ext->argument);
Radek Krejci19a96102018-11-15 13:38:09 +010055
56 LY_LIST_FOR_SAFE(ext->child, next, stmt) {
57 lysp_stmt_free(ctx, stmt);
58 }
59}
60
David Sedlák298ff6d2019-07-26 14:29:03 +020061void
Radek Krejci19a96102018-11-15 13:38:09 +010062lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import)
63{
64 /* imported module is freed directly from the context's list */
Michal Vaskoe180ed02021-02-05 16:31:20 +010065 lydict_remove(ctx, import->name);
66 lydict_remove(ctx, import->prefix);
67 lydict_remove(ctx, import->dsc);
68 lydict_remove(ctx, import->ref);
Radek Krejci19a96102018-11-15 13:38:09 +010069 FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free);
70}
71
Radek Krejci771928a2021-01-19 13:42:36 +010072/**
73 * @brief Common function to erase include record in main module and submodule.
74 *
75 * There is a difference since the main module is expected to have the complete list if the included submodules and
76 * the parsed submodule is shared with any include in a submodule. Therefore, the referenced submodules in the include
77 * record are freed only from main module's records.
78 *
79 * @param[in] ctx libyang context
80 * @param[in] include The include record to be erased, the record itself is not freed.
81 * @param[in] main_module Flag to get know if the include record is placed in main module so also the referenced submodule
82 * is supposed to be freed.
83 */
84static void
85lysp_include_free_(struct ly_ctx *ctx, struct lysp_include *include, ly_bool main_module)
Radek Krejci19a96102018-11-15 13:38:09 +010086{
Radek Krejci771928a2021-01-19 13:42:36 +010087 if (main_module && include->submodule) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +020088 lysp_module_free((struct lysp_module *)include->submodule);
Radek Krejci19a96102018-11-15 13:38:09 +010089 }
Michal Vaskoe180ed02021-02-05 16:31:20 +010090 lydict_remove(ctx, include->name);
91 lydict_remove(ctx, include->dsc);
92 lydict_remove(ctx, include->ref);
Radek Krejci19a96102018-11-15 13:38:09 +010093 FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free);
94}
95
David Sedlákaa854b02019-07-22 14:17:10 +020096void
Radek Krejci771928a2021-01-19 13:42:36 +010097lysp_include_free_submodule(struct ly_ctx *ctx, struct lysp_include *include)
98{
99 return lysp_include_free_(ctx, include, 0);
100}
101
102void
103lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include)
104{
105 return lysp_include_free_(ctx, include, 1);
106}
107
108void
Radek Krejci19a96102018-11-15 13:38:09 +0100109lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev)
110{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100111 lydict_remove(ctx, rev->dsc);
112 lydict_remove(ctx, rev->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100113 FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free);
114}
115
David Sedlák986cb412019-07-04 13:10:11 +0200116void
Radek Krejci19a96102018-11-15 13:38:09 +0100117lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext)
118{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100119 lydict_remove(ctx, ext->name);
120 lydict_remove(ctx, ext->argument);
121 lydict_remove(ctx, ext->dsc);
122 lydict_remove(ctx, ext->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100123 FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free);
Michal Vasko5fe75f12020-03-02 13:52:37 +0100124 if (ext->compiled) {
125 lysc_extension_free(ctx, &ext->compiled);
126 }
Radek Krejci19a96102018-11-15 13:38:09 +0100127}
128
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200129void
Radek Krejci19a96102018-11-15 13:38:09 +0100130lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat)
131{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100132 lydict_remove(ctx, feat->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200133 FREE_ARRAY(ctx, feat->iffeatures, lysp_qname_free);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100134 FREE_ARRAY(ctx, feat->iffeatures_c, lysc_iffeature_free);
135 LY_ARRAY_FREE(feat->depfeatures);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100136 lydict_remove(ctx, feat->dsc);
137 lydict_remove(ctx, feat->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100138 FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
139}
140
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200141void
Radek Krejci19a96102018-11-15 13:38:09 +0100142lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident)
143{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100144 lydict_remove(ctx, ident->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200145 FREE_ARRAY(ctx, ident->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100146 FREE_STRINGS(ctx, ident->bases);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100147 lydict_remove(ctx, ident->dsc);
148 lydict_remove(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100149 FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
150}
151
Michal Vasko7f45cf22020-10-01 12:49:44 +0200152void
Radek Krejci19a96102018-11-15 13:38:09 +0100153lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
154{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100155 lydict_remove(ctx, restr->arg.str);
156 lydict_remove(ctx, restr->emsg);
157 lydict_remove(ctx, restr->eapptag);
158 lydict_remove(ctx, restr->dsc);
159 lydict_remove(ctx, restr->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100160 FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
161}
162
163static void
164lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item)
165{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100166 lydict_remove(ctx, item->name);
167 lydict_remove(ctx, item->dsc);
168 lydict_remove(ctx, item->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200169 FREE_ARRAY(ctx, item->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100170 FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
171}
172
Radek Krejcicdfecd92018-11-26 11:27:32 +0100173void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type);
Michal Vasko004d3152020-06-11 19:59:22 +0200174
David Sedlák32488102019-07-15 17:44:10 +0200175void
Radek Krejci19a96102018-11-15 13:38:09 +0100176lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type)
177{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100178 lydict_remove(ctx, type->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100179 FREE_MEMBER(ctx, type->range, lysp_restr_free);
180 FREE_MEMBER(ctx, type->length, lysp_restr_free);
181 FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
182 FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
183 FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
Michal Vasko004d3152020-06-11 19:59:22 +0200184 lyxp_expr_free(ctx, type->path);
Radek Krejci19a96102018-11-15 13:38:09 +0100185 FREE_STRINGS(ctx, type->bases);
186 FREE_ARRAY(ctx, type->types, lysp_type_free);
187 FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
188 if (type->compiled) {
189 lysc_type_free(ctx, type->compiled);
190 }
191}
192
David Sedlák04e17b22019-07-19 15:29:48 +0200193void
Radek Krejci19a96102018-11-15 13:38:09 +0100194lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
195{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100196 lydict_remove(ctx, tpdf->name);
197 lydict_remove(ctx, tpdf->units);
198 lydict_remove(ctx, tpdf->dflt.str);
199 lydict_remove(ctx, tpdf->dsc);
200 lydict_remove(ctx, tpdf->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100201 FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
202
203 lysp_type_free(ctx, &tpdf->type);
204
205}
206
Radek Krejcif538ce52019-03-05 10:46:14 +0100207void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100208lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp)
Radek Krejci19a96102018-11-15 13:38:09 +0100209{
210 struct lysp_node *node, *next;
211
Radek Krejci19a96102018-11-15 13:38:09 +0100212 FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100213 LY_LIST_FOR_SAFE((struct lysp_node *)grp->groupings, next, node) {
214 lysp_node_free(ctx, node);
215 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100216 LY_LIST_FOR_SAFE(grp->child, next, node) {
Radek Krejci19a96102018-11-15 13:38:09 +0100217 lysp_node_free(ctx, node);
218 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100219 LY_LIST_FOR_SAFE((struct lysp_node *)grp->actions, next, node) {
220 lysp_node_free(ctx, node);
221 }
222 LY_LIST_FOR_SAFE((struct lysp_node *)grp->notifs, next, node) {
223 lysp_node_free(ctx, node);
224 }
Radek Krejci19a96102018-11-15 13:38:09 +0100225}
226
Radek Krejcif09e4e82019-06-14 15:08:11 +0200227void
Radek Krejci19a96102018-11-15 13:38:09 +0100228lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
229{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100230 lydict_remove(ctx, when->cond);
231 lydict_remove(ctx, when->dsc);
232 lydict_remove(ctx, when->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100233 FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
234}
235
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200236void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100237lysp_augment_free(struct ly_ctx *ctx, struct lysp_node_augment *augment)
Radek Krejci19a96102018-11-15 13:38:09 +0100238{
239 struct lysp_node *node, *next;
240
Radek Krejci19a96102018-11-15 13:38:09 +0100241 LY_LIST_FOR_SAFE(augment->child, next, node) {
242 lysp_node_free(ctx, node);
243 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100244 LY_LIST_FOR_SAFE((struct lysp_node *)augment->actions, next, node) {
245 lysp_node_free(ctx, node);
246 }
247 LY_LIST_FOR_SAFE((struct lysp_node *)augment->notifs, next, node) {
248 lysp_node_free(ctx, node);
249 }
Radek Krejci19a96102018-11-15 13:38:09 +0100250}
251
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200252void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200253lysp_qname_free(struct ly_ctx *ctx, struct lysp_qname *qname)
254{
255 if (qname) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100256 lydict_remove(ctx, qname->str);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200257 }
258}
259
260void
Radek Krejci19a96102018-11-15 13:38:09 +0100261lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
262{
Michal Vasko22df3f02020-08-24 13:29:22 +0200263 struct lysp_deviate_add *add = (struct lysp_deviate_add *)d;
264 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl *)d;
Radek Krejci19a96102018-11-15 13:38:09 +0100265
266 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
Michal Vaskod989ba02020-08-24 10:59:24 +0200267 switch (d->mod) {
Radek Krejci19a96102018-11-15 13:38:09 +0100268 case LYS_DEV_NOT_SUPPORTED:
269 /* nothing to do */
270 break;
271 case LYS_DEV_ADD:
272 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100273 lydict_remove(ctx, add->units);
Radek Krejci19a96102018-11-15 13:38:09 +0100274 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200275 FREE_ARRAY(ctx, add->uniques, lysp_qname_free);
276 FREE_ARRAY(ctx, add->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100277 break;
278 case LYS_DEV_REPLACE:
279 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100280 lydict_remove(ctx, rpl->units);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200281 lysp_qname_free(ctx, &rpl->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +0100282 break;
283 default:
284 LOGINT(ctx);
285 break;
286 }
287}
288
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200289void
Radek Krejci19a96102018-11-15 13:38:09 +0100290lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
291{
292 struct lysp_deviate *next, *iter;
293
Michal Vaskoe180ed02021-02-05 16:31:20 +0100294 lydict_remove(ctx, dev->nodeid);
295 lydict_remove(ctx, dev->dsc);
296 lydict_remove(ctx, dev->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100297 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
298 lysp_deviate_free(ctx, iter);
299 free(iter);
300 }
301 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
302}
303
David Sedlákd2d676a2019-07-22 11:28:19 +0200304void
Radek Krejci19a96102018-11-15 13:38:09 +0100305lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
306{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100307 lydict_remove(ctx, ref->nodeid);
308 lydict_remove(ctx, ref->dsc);
309 lydict_remove(ctx, ref->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200310 FREE_ARRAY(ctx, ref->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100311 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100312 lydict_remove(ctx, ref->presence);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200313 FREE_ARRAY(ctx, ref->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100314 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
315}
316
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200317void
Radek Krejci19a96102018-11-15 13:38:09 +0100318lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
319{
320 struct lysp_node *child, *next;
Radek Krejci9a3823e2021-01-27 20:26:46 +0100321 struct lysp_restr *musts = lysp_node_musts(node);
322 struct lysp_when *when = lysp_node_when(node);
Radek Krejci19a96102018-11-15 13:38:09 +0100323
Michal Vaskoe180ed02021-02-05 16:31:20 +0100324 lydict_remove(ctx, node->name);
325 lydict_remove(ctx, node->dsc);
326 lydict_remove(ctx, node->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200327 FREE_ARRAY(ctx, node->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100328 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
329
Radek Krejci9a3823e2021-01-27 20:26:46 +0100330 FREE_MEMBER(ctx, when, lysp_when_free);
331 FREE_ARRAY(ctx, musts, lysp_restr_free);
332
Michal Vaskod989ba02020-08-24 10:59:24 +0200333 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100334 case LYS_CONTAINER:
Michal Vaskoe180ed02021-02-05 16:31:20 +0100335 lydict_remove(ctx, ((struct lysp_node_container *)node)->presence);
Michal Vasko22df3f02020-08-24 13:29:22 +0200336 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100337 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100338 lysp_node_free(ctx, child);
339 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200340 LY_LIST_FOR_SAFE(((struct lysp_node_container *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100341 lysp_node_free(ctx, child);
342 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100343 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->actions->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100344 lysp_node_free(ctx, child);
345 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100346 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->notifs->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100347 lysp_node_free(ctx, child);
348 }
Radek Krejci19a96102018-11-15 13:38:09 +0100349 break;
350 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200351 lysp_type_free(ctx, &((struct lysp_node_leaf *)node)->type);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100352 lydict_remove(ctx, ((struct lysp_node_leaf *)node)->units);
353 lydict_remove(ctx, ((struct lysp_node_leaf *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100354 break;
355 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200356 lysp_type_free(ctx, &((struct lysp_node_leaflist *)node)->type);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100357 lydict_remove(ctx, ((struct lysp_node_leaflist *)node)->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200358 FREE_ARRAY(ctx, ((struct lysp_node_leaflist *)node)->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100359 break;
360 case LYS_LIST:
Michal Vaskoe180ed02021-02-05 16:31:20 +0100361 lydict_remove(ctx, ((struct lysp_node_list *)node)->key);
Michal Vasko22df3f02020-08-24 13:29:22 +0200362 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100363 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100364 lysp_node_free(ctx, child);
365 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200366 LY_LIST_FOR_SAFE(((struct lysp_node_list *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100367 lysp_node_free(ctx, child);
368 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100369 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->actions->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100370 lysp_node_free(ctx, child);
371 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100372 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->notifs->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100373 lysp_node_free(ctx, child);
374 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200375 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->uniques, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100376 break;
377 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200378 LY_LIST_FOR_SAFE(((struct lysp_node_choice *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100379 lysp_node_free(ctx, child);
380 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100381 lydict_remove(ctx, ((struct lysp_node_choice *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100382 break;
383 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200384 LY_LIST_FOR_SAFE(((struct lysp_node_case *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100385 lysp_node_free(ctx, child);
386 }
387 break;
388 case LYS_ANYDATA:
389 case LYS_ANYXML:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100390 /* nothing special to do */
Radek Krejci19a96102018-11-15 13:38:09 +0100391 break;
392 case LYS_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +0200393 FREE_ARRAY(ctx, ((struct lysp_node_uses *)node)->refines, lysp_refine_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100394 LY_LIST_FOR_SAFE(&((struct lysp_node_uses *)node)->augments->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100395 lysp_node_free(ctx, child);
396 }
397 break;
398 case LYS_RPC:
399 case LYS_ACTION:
400 FREE_ARRAY(ctx, ((struct lysp_node_action *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100401 LY_LIST_FOR_SAFE(&((struct lysp_node_action *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100402 lysp_node_free(ctx, child);
403 }
404 if (((struct lysp_node_action *)node)->input.nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100405 lysp_node_free(ctx, &((struct lysp_node_action *)node)->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100406 }
407 if (((struct lysp_node_action *)node)->output.nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100408 lysp_node_free(ctx, &((struct lysp_node_action *)node)->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100409 }
410 break;
411 case LYS_INPUT:
412 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100413 FREE_ARRAY(ctx, ((struct lysp_node_action_inout *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100414 LY_LIST_FOR_SAFE(&((struct lysp_node_action_inout *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100415 lysp_node_free(ctx, child);
416 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100417 LY_LIST_FOR_SAFE(((struct lysp_node_action_inout *)node)->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100418 lysp_node_free(ctx, child);
419 }
420 /* do not free the node, it is never standalone but part of the action node */
421 return;
422 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100423 FREE_ARRAY(ctx, ((struct lysp_node_notif *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100424 LY_LIST_FOR_SAFE(&((struct lysp_node_notif *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100425 lysp_node_free(ctx, child);
426 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100427 LY_LIST_FOR_SAFE(((struct lysp_node_notif *)node)->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100428 lysp_node_free(ctx, child);
429 }
430 break;
431 case LYS_GROUPING:
432 lysp_grp_free(ctx, (struct lysp_node_grp *)node);
433 break;
434 case LYS_AUGMENT:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100435 lysp_augment_free(ctx, ((struct lysp_node_augment *)node));
Radek Krejci19a96102018-11-15 13:38:09 +0100436 break;
437 default:
438 LOGINT(ctx);
439 }
440
441 free(node);
442}
443
Radek Krejci15f10ab2020-11-03 14:14:14 +0100444void
Radek Krejci19a96102018-11-15 13:38:09 +0100445lysp_module_free(struct lysp_module *module)
446{
447 struct ly_ctx *ctx;
448 struct lysp_node *node, *next;
449
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100450 if (!module) {
451 return;
452 }
453 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100454
455 FREE_ARRAY(ctx, module->imports, lysp_import_free);
Radek Krejci771928a2021-01-19 13:42:36 +0100456 FREE_ARRAY(ctx, module->includes, module->is_submod ? lysp_include_free_submodule : lysp_include_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100457
Radek Krejci19a96102018-11-15 13:38:09 +0100458 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
459 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
460 FREE_ARRAY(ctx, module->features, lysp_feature_free);
461 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
462 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100463 LY_LIST_FOR_SAFE((struct lysp_node *)module->groupings, next, node) {
464 lysp_node_free(ctx, node);
465 }
Radek Krejci19a96102018-11-15 13:38:09 +0100466 LY_LIST_FOR_SAFE(module->data, next, node) {
467 lysp_node_free(ctx, node);
468 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100469 LY_LIST_FOR_SAFE((struct lysp_node *)module->augments, next, node) {
470 lysp_node_free(ctx, node);
471 }
472 LY_LIST_FOR_SAFE((struct lysp_node *)module->rpcs, next, node) {
473 lysp_node_free(ctx, node);
474 }
475 LY_LIST_FOR_SAFE((struct lysp_node *)module->notifs, next, node) {
476 lysp_node_free(ctx, node);
477 }
Radek Krejci19a96102018-11-15 13:38:09 +0100478 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
479 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
480
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200481 if (module->is_submod) {
482 struct lysp_submodule *submod = (struct lysp_submodule *)module;
483
Michal Vaskoe180ed02021-02-05 16:31:20 +0100484 lydict_remove(ctx, submod->name);
485 lydict_remove(ctx, submod->filepath);
486 lydict_remove(ctx, submod->prefix);
487 lydict_remove(ctx, submod->org);
488 lydict_remove(ctx, submod->contact);
489 lydict_remove(ctx, submod->dsc);
490 lydict_remove(ctx, submod->ref);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200491 }
492
Radek Krejci19a96102018-11-15 13:38:09 +0100493 free(module);
494}
495
Radek Krejci0af46292019-01-11 16:02:31 +0100496void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100497lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
498{
499 if (--(*ext)->refcount) {
500 return;
501 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100502 lydict_remove(ctx, (*ext)->name);
503 lydict_remove(ctx, (*ext)->argument);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100504 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
505 free(*ext);
506}
507
508void
Radek Krejci19a96102018-11-15 13:38:09 +0100509lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
510{
fredganebc50572019-10-31 15:39:23 +0800511 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200512 ext->def->plugin->free(ctx, ext);
513 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100514 if (ext->def) {
515 lysc_extension_free(ctx, &ext->def);
516 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100517 lydict_remove(ctx, ext->argument);
Radek Krejci0935f412019-08-20 16:15:18 +0200518 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
519}
520
521void
Radek Krejci19a96102018-11-15 13:38:09 +0100522lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
523{
524 LY_ARRAY_FREE(iff->features);
525 free(iff->expr);
526}
527
528static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100529lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100530{
Radek Krejci00b874b2019-02-12 10:54:50 +0100531 if (--(*w)->refcount) {
532 return;
533 }
534 lyxp_expr_free(ctx, (*w)->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200535 lysc_prefixes_free((*w)->prefixes);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100536 lydict_remove(ctx, (*w)->dsc);
537 lydict_remove(ctx, (*w)->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +0100538 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
539 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100540}
541
Radek Krejciccd20f12019-02-15 14:12:27 +0100542void
Radek Krejci58d171e2018-11-23 13:50:55 +0100543lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
544{
545 lyxp_expr_free(ctx, must->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200546 lysc_prefixes_free(must->prefixes);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100547 lydict_remove(ctx, must->emsg);
548 lydict_remove(ctx, must->eapptag);
549 lydict_remove(ctx, must->dsc);
550 lydict_remove(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100551 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
552}
553
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100554void
Radek Krejci19a96102018-11-15 13:38:09 +0100555lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
556{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100557 lydict_remove(ctx, ident->name);
558 lydict_remove(ctx, ident->dsc);
559 lydict_remove(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100560 LY_ARRAY_FREE(ident->derived);
561 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
562}
563
Radek Krejci19a96102018-11-15 13:38:09 +0100564static void
565lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
566{
567 LY_ARRAY_FREE(range->parts);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100568 lydict_remove(ctx, range->eapptag);
569 lydict_remove(ctx, range->emsg);
570 lydict_remove(ctx, range->dsc);
571 lydict_remove(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100572 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
573}
574
575static void
576lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
577{
578 if (--(*pattern)->refcount) {
579 return;
580 }
Radek Krejci54579462019-04-30 12:47:06 +0200581 pcre2_code_free((*pattern)->code);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100582 lydict_remove(ctx, (*pattern)->expr);
583 lydict_remove(ctx, (*pattern)->eapptag);
584 lydict_remove(ctx, (*pattern)->emsg);
585 lydict_remove(ctx, (*pattern)->dsc);
586 lydict_remove(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100587 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
588 free(*pattern);
589}
590
591static void
Radek Krejci693262f2019-04-29 15:23:20 +0200592lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100593{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100594 lydict_remove(ctx, item->name);
595 lydict_remove(ctx, item->dsc);
596 lydict_remove(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100597 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
598}
599
Radek Krejcia3045382018-11-22 14:30:31 +0100600static void
601lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
602{
603 lysc_type_free(ctx, *type);
604}
Radek Krejci0f969882020-08-21 16:56:47 +0200605
Radek Krejcicdfecd92018-11-26 11:27:32 +0100606void
Radek Krejci19a96102018-11-15 13:38:09 +0100607lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
608{
609 if (--type->refcount) {
610 return;
611 }
Radek Krejcib915ac92020-08-14 23:31:04 +0200612
Michal Vaskod989ba02020-08-24 10:59:24 +0200613 switch (type->basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100614 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +0200615 FREE_MEMBER(ctx, ((struct lysc_type_bin *)type)->length, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100616 break;
617 case LY_TYPE_BITS:
Michal Vasko22df3f02020-08-24 13:29:22 +0200618 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 +0100619 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100620 case LY_TYPE_DEC64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200621 FREE_MEMBER(ctx, ((struct lysc_type_dec *)type)->range, lysc_range_free);
Radek Krejci6cba4292018-11-15 17:33:29 +0100622 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100623 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +0200624 FREE_MEMBER(ctx, ((struct lysc_type_str *)type)->length, lysc_range_free);
625 FREE_ARRAY(ctx, ((struct lysc_type_str *)type)->patterns, lysc_pattern_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100626 break;
627 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +0200628 FREE_ARRAY(ctx, ((struct lysc_type_enum *)type)->enums, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100629 break;
630 case LY_TYPE_INT8:
631 case LY_TYPE_UINT8:
632 case LY_TYPE_INT16:
633 case LY_TYPE_UINT16:
634 case LY_TYPE_INT32:
635 case LY_TYPE_UINT32:
636 case LY_TYPE_INT64:
637 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200638 FREE_MEMBER(ctx, ((struct lysc_type_num *)type)->range, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100639 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100640 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +0200641 LY_ARRAY_FREE(((struct lysc_type_identityref *)type)->bases);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100642 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100643 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +0200644 FREE_ARRAY(ctx, ((struct lysc_type_union *)type)->types, lysc_type2_free);
Radek Krejcia3045382018-11-22 14:30:31 +0100645 break;
646 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200647 lyxp_expr_free(ctx, ((struct lysc_type_leafref *)type)->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200648 lysc_prefixes_free(((struct lysc_type_leafref *)type)->prefixes);
Radek Krejcia3045382018-11-22 14:30:31 +0100649 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100650 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100651 case LY_TYPE_BOOL:
652 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100653 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100654 /* nothing to do */
655 break;
656 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200657
658 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100659 free(type);
660}
661
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100662void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100663lysc_node_action_inout_free(struct ly_ctx *ctx, struct lysc_node_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100664{
665 struct lysc_node *child, *child_next;
666
Radek Krejcif538ce52019-03-05 10:46:14 +0100667 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100668 LY_LIST_FOR_SAFE(inout->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100669 lysc_node_free_(ctx, child);
Radek Krejcif538ce52019-03-05 10:46:14 +0100670 }
671}
672
673void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100674lysc_node_action_free(struct ly_ctx *ctx, struct lysc_node_action *action)
Radek Krejcif538ce52019-03-05 10:46:14 +0100675{
Michal Vasko37a0fe62021-02-03 09:53:04 +0100676 FREE_ARRAY(ctx, action->when, lysc_when_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100677 if (action->input.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100678 lysc_node_free_(ctx, &action->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100679 }
680 if (action->output.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100681 lysc_node_free_(ctx, &action->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100682 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100683}
684
Radek Krejcifc11bd72019-04-11 16:00:05 +0200685void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100686lysc_node_notif_free(struct ly_ctx *ctx, struct lysc_node_notif *notif)
Radek Krejcifc11bd72019-04-11 16:00:05 +0200687{
688 struct lysc_node *child, *child_next;
689
Michal Vasko37a0fe62021-02-03 09:53:04 +0100690 FREE_ARRAY(ctx, notif->when, lysc_when_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200691 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100692 LY_LIST_FOR_SAFE(notif->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100693 lysc_node_free_(ctx, child);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200694 }
695}
696
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200697void
Radek Krejci19a96102018-11-15 13:38:09 +0100698lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
699{
700 struct lysc_node *child, *child_next;
701
702 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100703 lysc_node_free_(ctx, child);
Radek Krejci19a96102018-11-15 13:38:09 +0100704 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100705 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
706 lysc_node_free_(ctx, child);
707 }
708 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
709 lysc_node_free_(ctx, child);
710 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100711 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100712 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100713}
714
715static void
716lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
717{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100718 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100719 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100720 if (node->type) {
721 lysc_type_free(ctx, node->type);
722 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100723 lydict_remove(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200724 if (node->dflt) {
725 node->dflt->realtype->plugin->free(ctx, node->dflt);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200726 lysc_type_free(ctx, (struct lysc_type *)node->dflt->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200727 free(node->dflt);
728 }
Radek Krejci19a96102018-11-15 13:38:09 +0100729}
730
Radek Krejci42452ac2018-11-28 17:09:52 +0100731static void
732lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
733{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200734 LY_ARRAY_COUNT_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100735
Radek Krejci9a3823e2021-01-27 20:26:46 +0100736 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci42452ac2018-11-28 17:09:52 +0100737 FREE_ARRAY(ctx, node->musts, lysc_must_free);
738 if (node->type) {
739 lysc_type_free(ctx, node->type);
740 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100741 lydict_remove(ctx, node->units);
Radek Krejci42452ac2018-11-28 17:09:52 +0100742 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200743 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200744 lysc_type_free(ctx, (struct lysc_type *)node->dflts[u]->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200745 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100746 }
747 LY_ARRAY_FREE(node->dflts);
748}
749
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100750static void
751lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
752{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200753 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100754 struct lysc_node *child, *child_next;
755
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100756 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100757 lysc_node_free_(ctx, child);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100758 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100759 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100760 FREE_ARRAY(ctx, node->musts, lysc_must_free);
761
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100762 LY_ARRAY_FOR(node->uniques, u) {
763 LY_ARRAY_FREE(node->uniques[u]);
764 }
765 LY_ARRAY_FREE(node->uniques);
766
Radek Krejci2a9fc652021-01-22 17:44:34 +0100767 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
768 lysc_node_free_(ctx, child);
769 }
770 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
771 lysc_node_free_(ctx, child);
772 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100773}
774
Radek Krejci056d0a82018-12-06 16:57:25 +0100775static void
776lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
777{
778 struct lysc_node *child, *child_next;
779
Radek Krejci9a3823e2021-01-27 20:26:46 +0100780 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200781 LY_LIST_FOR_SAFE((struct lysc_node *)node->cases, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100782 lysc_node_free_(ctx, child);
Michal Vasko20424b42020-08-31 12:29:38 +0200783 }
784}
785
786static void
787lysc_node_case_free(struct ly_ctx *ctx, struct lysc_node_case *node)
788{
789 struct lysc_node *child, *child_next;
790
Radek Krejci9a3823e2021-01-27 20:26:46 +0100791 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200792 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100793 lysc_node_free_(ctx, child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100794 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100795}
Radek Krejci056d0a82018-12-06 16:57:25 +0100796
Radek Krejci9800fb82018-12-13 14:26:23 +0100797static void
798lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
799{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100800 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9800fb82018-12-13 14:26:23 +0100801 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100802}
803
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100804static void
805lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +0100806{
Radek Krejci2a9fc652021-01-22 17:44:34 +0100807 ly_bool inout = 0;
808
Radek Krejci19a96102018-11-15 13:38:09 +0100809 /* common part */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100810 lydict_remove(ctx, node->name);
811 lydict_remove(ctx, node->dsc);
812 lydict_remove(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100813
814 /* nodetype-specific part */
Michal Vaskod989ba02020-08-24 10:59:24 +0200815 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100816 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200817 lysc_node_container_free(ctx, (struct lysc_node_container *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100818 break;
819 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200820 lysc_node_leaf_free(ctx, (struct lysc_node_leaf *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100821 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100822 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200823 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist *)node);
Radek Krejci42452ac2018-11-28 17:09:52 +0100824 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100825 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200826 lysc_node_list_free(ctx, (struct lysc_node_list *)node);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100827 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100828 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200829 lysc_node_choice_free(ctx, (struct lysc_node_choice *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100830 break;
831 case LYS_CASE:
Michal Vasko20424b42020-08-31 12:29:38 +0200832 lysc_node_case_free(ctx, (struct lysc_node_case *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100833 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100834 case LYS_ANYDATA:
835 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200836 lysc_node_anydata_free(ctx, (struct lysc_node_anydata *)node);
Radek Krejci9800fb82018-12-13 14:26:23 +0100837 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100838 case LYS_RPC:
839 case LYS_ACTION:
840 lysc_node_action_free(ctx, (struct lysc_node_action *)node);
841 break;
842 case LYS_INPUT:
843 case LYS_OUTPUT:
844 lysc_node_action_inout_free(ctx, (struct lysc_node_action_inout *)node);
845 inout = 1;
846 break;
847 case LYS_NOTIF:
848 lysc_node_notif_free(ctx, (struct lysc_node_notif *)node);
849 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100850 default:
851 LOGINT(ctx);
852 }
853
Radek Krejci056d0a82018-12-06 16:57:25 +0100854 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100855
856 if (!inout) {
857 free(node);
858 }
Radek Krejci19a96102018-11-15 13:38:09 +0100859}
860
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100861void
862lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node, ly_bool unlink)
863{
864 struct lysc_node *iter, **child_p;
865
Radek Krejci2a9fc652021-01-22 17:44:34 +0100866 if (node->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
867 /* nothing to do - inouts are part of actions and cannot be unlinked/freed separately */
868 return;
869 }
870
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100871 if (unlink) {
872 /* unlink from siblings */
873 if (node->prev->next) {
874 node->prev->next = node->next;
875 }
876 if (node->next) {
877 node->next->prev = node->prev;
878 } else {
879 /* unlinking the last node */
880 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100881 if (node->nodetype == LYS_ACTION) {
882 iter = (struct lysc_node *)lysc_node_actions(node->parent);
883 } else if (node->nodetype == LYS_NOTIF) {
884 iter = (struct lysc_node *)lysc_node_notifs(node->parent);
885 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +0100886 iter = (struct lysc_node *)lysc_node_child(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100887 }
Michal Vasko539c4a62020-11-03 17:21:34 +0100888 LY_CHECK_ERR_RET(!iter, LOGINT(ctx), );
Radek Krejci2a9fc652021-01-22 17:44:34 +0100889 } else if (node->nodetype == LYS_RPC) {
890 iter = (struct lysc_node *)node->module->compiled->rpcs;
891 } else if (node->nodetype == LYS_NOTIF) {
892 iter = (struct lysc_node *)node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100893 } else {
894 iter = node->module->compiled->data;
895 }
896 /* update the "last" pointer from the first node */
897 iter->prev = node->prev;
898 }
899
900 /* unlink from parent */
901 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100902 if (node->nodetype == LYS_ACTION) {
903 child_p = (struct lysc_node **)lysc_node_actions_p(node->parent);
904 } else if (node->nodetype == LYS_NOTIF) {
905 child_p = (struct lysc_node **)lysc_node_notifs_p(node->parent);
906 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +0100907 child_p = lysc_node_child_p(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100908 }
909 } else if (node->nodetype == LYS_RPC) {
910 child_p = (struct lysc_node **)&node->module->compiled->rpcs;
911 } else if (node->nodetype == LYS_NOTIF) {
912 child_p = (struct lysc_node **)&node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100913 } else {
914 child_p = &node->module->compiled->data;
915 }
916 if (child_p && (*child_p == node)) {
917 /* the node is the first child */
918 *child_p = node->next;
919 }
920 }
921
922 lysc_node_free_(ctx, node);
923}
924
Radek Krejci19a96102018-11-15 13:38:09 +0100925static void
926lysc_module_free_(struct lysc_module *module)
927{
928 struct ly_ctx *ctx;
929 struct lysc_node *node, *node_next;
930
Michal Vaskod989ba02020-08-24 10:59:24 +0200931 LY_CHECK_ARG_RET(NULL, module, );
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100932 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100933
Radek Krejci19a96102018-11-15 13:38:09 +0100934 LY_LIST_FOR_SAFE(module->data, node_next, node) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100935 lysc_node_free_(ctx, node);
Radek Krejci19a96102018-11-15 13:38:09 +0100936 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100937 LY_LIST_FOR_SAFE((struct lysc_node *)module->rpcs, node_next, node) {
938 lysc_node_free_(ctx, node);
939 }
940 LY_LIST_FOR_SAFE((struct lysc_node *)module->notifs, node_next, node) {
941 lysc_node_free_(ctx, node);
942 }
Radek Krejci19a96102018-11-15 13:38:09 +0100943 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
944
945 free(module);
946}
947
948void
949lysc_module_free(struct lysc_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
950{
Radek Krejci9b042892019-02-13 14:28:44 +0100951 /* TODO use the destructor, this just suppress warning about unused parameter */
952 (void) private_destructor;
953
Radek Krejci19a96102018-11-15 13:38:09 +0100954 if (module) {
955 lysc_module_free_(module);
956 }
957}
958
959void
960lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
961{
962 if (!module) {
963 return;
964 }
965
966 lysc_module_free(module->compiled, private_destructor);
Radek Krejci80d281e2020-09-14 17:42:54 +0200967 FREE_ARRAY(module->ctx, module->identities, lysc_ident_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100968 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100969
Michal Vasko7f45cf22020-10-01 12:49:44 +0200970 LY_ARRAY_FREE(module->augmented_by);
971 LY_ARRAY_FREE(module->deviated_by);
972
Michal Vaskoe180ed02021-02-05 16:31:20 +0100973 lydict_remove(module->ctx, module->name);
974 lydict_remove(module->ctx, module->revision);
975 lydict_remove(module->ctx, module->ns);
976 lydict_remove(module->ctx, module->prefix);
977 lydict_remove(module->ctx, module->filepath);
978 lydict_remove(module->ctx, module->org);
979 lydict_remove(module->ctx, module->contact);
980 lydict_remove(module->ctx, module->dsc);
981 lydict_remove(module->ctx, module->ref);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100982
Radek Krejci19a96102018-11-15 13:38:09 +0100983 free(module);
984}
Michal Vasko33ff9422020-07-03 09:50:39 +0200985
Radek Krejci38d85362019-09-05 16:26:38 +0200986API void
987lysc_extension_instance_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
988{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200989 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
Radek Krejci38d85362019-09-05 16:26:38 +0200990 if (!substmts[u].storage) {
991 continue;
992 }
993
Michal Vaskod989ba02020-08-24 10:59:24 +0200994 switch (substmts[u].stmt) {
Radek Krejci38d85362019-09-05 16:26:38 +0200995 case LY_STMT_TYPE:
996 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
997 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +0200998 struct lysc_type *type = *((struct lysc_type **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200999 if (!type) {
1000 break;
1001 }
1002 lysc_type_free(ctx, type);
1003 } else {
1004 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +02001005 struct lysc_type **types = *((struct lysc_type ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001006 if (!types) {
1007 break;
1008 }
1009 FREE_ARRAY(ctx, types, lysc_type2_free);
1010 }
1011 break;
1012 case LY_STMT_UNITS:
1013 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1014 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02001015 const char *str = *((const char **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001016 if (!str) {
1017 break;
1018 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001019 lydict_remove(ctx, str);
Radek Krejci38d85362019-09-05 16:26:38 +02001020 } else {
1021 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +02001022 const char **strs = *((const char ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001023 if (!strs) {
1024 break;
1025 }
1026 FREE_STRINGS(ctx, strs);
1027 }
1028 break;
1029 case LY_STMT_STATUS:
Radek Krejciad5963b2019-09-06 16:03:05 +02001030 case LY_STMT_CONFIG:
Radek Krejci38d85362019-09-05 16:26:38 +02001031 /* nothing to do */
1032 break;
1033 case LY_STMT_IF_FEATURE: {
Michal Vasko22df3f02020-08-24 13:29:22 +02001034 struct lysc_iffeature *iff = *((struct lysc_iffeature **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001035 if (!iff) {
1036 break;
1037 }
1038 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1039 /* single item */
1040 lysc_iffeature_free(ctx, iff);
1041 free(iff);
1042 } else {
1043 /* multiple items */
1044 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
1045 }
1046 break;
1047 }
1048
Radek Krejci0f969882020-08-21 16:56:47 +02001049 /* TODO other statements */
Radek Krejci38d85362019-09-05 16:26:38 +02001050 default:
1051 LOGINT(ctx);
1052 }
1053 }
1054}
David Sedlákebd3acf2019-07-26 15:04:32 +02001055
1056void
Michal Vaskob36053d2020-03-26 15:49:30 +01001057yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001058{
1059 if (ctx) {
1060 free(ctx);
1061 }
1062}
1063
1064void
Michal Vaskob36053d2020-03-26 15:49:30 +01001065yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001066{
1067 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001068 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +02001069 free(ctx);
1070 }
1071}