blob: 234daf8b3075301f065c2263105b158a05d1fc4c [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
Michal Vaskoee757602021-06-10 14:38:19 +020015#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <stdlib.h>
17
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020019#include "compat.h"
Radek Krejci77114102021-03-10 15:21:57 +010020#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010021#include "log.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include "plugins_exts.h"
23#include "plugins_types.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "tree_data.h"
Michal Vaskofc2cd072021-02-24 13:17:17 +010026#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010027#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010029#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "xml.h"
Radek Krejci19a96102018-11-15 13:38:09 +010031#include "xpath.h"
32
Radek Krejci2a9fc652021-01-22 17:44:34 +010033void lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010034static void lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node);
Radek Krejci19a96102018-11-15 13:38:09 +010035
36static void
37lysp_stmt_free(struct ly_ctx *ctx, struct lysp_stmt *stmt)
38{
39 struct lysp_stmt *child, *next;
40
Michal Vaskoe180ed02021-02-05 16:31:20 +010041 lydict_remove(ctx, stmt->stmt);
42 lydict_remove(ctx, stmt->arg);
Michal Vaskofc2cd072021-02-24 13:17:17 +010043 ly_free_prefix_data(stmt->format, stmt->prefix_data);
Radek Krejci19a96102018-11-15 13:38:09 +010044
45 LY_LIST_FOR_SAFE(stmt->child, next, child) {
46 lysp_stmt_free(ctx, child);
47 }
48
49 free(stmt);
50}
51
Radek Krejci2d7a47b2019-05-16 13:34:10 +020052void
Radek Krejci19a96102018-11-15 13:38:09 +010053lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext)
54{
55 struct lysp_stmt *stmt, *next;
aPiecek60d9d672021-04-27 15:49:57 +020056 struct lysp_node *node, *next_node;
Radek Krejci19a96102018-11-15 13:38:09 +010057
Michal Vaskoe180ed02021-02-05 16:31:20 +010058 lydict_remove(ctx, ext->name);
59 lydict_remove(ctx, ext->argument);
Michal Vaskofc2cd072021-02-24 13:17:17 +010060 ly_free_prefix_data(ext->format, ext->prefix_data);
aPiecek60d9d672021-04-27 15:49:57 +020061 LY_LIST_FOR_SAFE(ext->parsed, next_node, node) {
62 lysp_node_free(ctx, node);
63 }
Radek Krejci19a96102018-11-15 13:38:09 +010064
65 LY_LIST_FOR_SAFE(ext->child, next, stmt) {
66 lysp_stmt_free(ctx, stmt);
67 }
68}
69
David Sedlák298ff6d2019-07-26 14:29:03 +020070void
Radek Krejci19a96102018-11-15 13:38:09 +010071lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import)
72{
73 /* imported module is freed directly from the context's list */
Michal Vaskoe180ed02021-02-05 16:31:20 +010074 lydict_remove(ctx, import->name);
75 lydict_remove(ctx, import->prefix);
76 lydict_remove(ctx, import->dsc);
77 lydict_remove(ctx, import->ref);
Radek Krejci19a96102018-11-15 13:38:09 +010078 FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free);
79}
80
Radek Krejci771928a2021-01-19 13:42:36 +010081/**
82 * @brief Common function to erase include record in main module and submodule.
83 *
84 * There is a difference since the main module is expected to have the complete list if the included submodules and
85 * the parsed submodule is shared with any include in a submodule. Therefore, the referenced submodules in the include
86 * record are freed only from main module's records.
87 *
88 * @param[in] ctx libyang context
89 * @param[in] include The include record to be erased, the record itself is not freed.
90 * @param[in] main_module Flag to get know if the include record is placed in main module so also the referenced submodule
91 * is supposed to be freed.
92 */
93static void
94lysp_include_free_(struct ly_ctx *ctx, struct lysp_include *include, ly_bool main_module)
Radek Krejci19a96102018-11-15 13:38:09 +010095{
Radek Krejci771928a2021-01-19 13:42:36 +010096 if (main_module && include->submodule) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +020097 lysp_module_free((struct lysp_module *)include->submodule);
Radek Krejci19a96102018-11-15 13:38:09 +010098 }
Michal Vaskoe180ed02021-02-05 16:31:20 +010099 lydict_remove(ctx, include->name);
100 lydict_remove(ctx, include->dsc);
101 lydict_remove(ctx, include->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100102 FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free);
103}
104
David Sedlákaa854b02019-07-22 14:17:10 +0200105void
Radek Krejci771928a2021-01-19 13:42:36 +0100106lysp_include_free_submodule(struct ly_ctx *ctx, struct lysp_include *include)
107{
108 return lysp_include_free_(ctx, include, 0);
109}
110
111void
112lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include)
113{
114 return lysp_include_free_(ctx, include, 1);
115}
116
117void
Radek Krejci19a96102018-11-15 13:38:09 +0100118lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev)
119{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100120 lydict_remove(ctx, rev->dsc);
121 lydict_remove(ctx, rev->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100122 FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free);
123}
124
David Sedlák986cb412019-07-04 13:10:11 +0200125void
Radek Krejci19a96102018-11-15 13:38:09 +0100126lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext)
127{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100128 lydict_remove(ctx, ext->name);
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100129 lydict_remove(ctx, ext->argname);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100130 lydict_remove(ctx, ext->dsc);
131 lydict_remove(ctx, ext->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100132 FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free);
Michal Vasko5fe75f12020-03-02 13:52:37 +0100133 if (ext->compiled) {
134 lysc_extension_free(ctx, &ext->compiled);
135 }
Radek Krejci19a96102018-11-15 13:38:09 +0100136}
137
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200138void
Radek Krejci19a96102018-11-15 13:38:09 +0100139lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat)
140{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100141 lydict_remove(ctx, feat->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200142 FREE_ARRAY(ctx, feat->iffeatures, lysp_qname_free);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100143 FREE_ARRAY(ctx, feat->iffeatures_c, lysc_iffeature_free);
144 LY_ARRAY_FREE(feat->depfeatures);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100145 lydict_remove(ctx, feat->dsc);
146 lydict_remove(ctx, feat->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100147 FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
148}
149
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200150void
Radek Krejci19a96102018-11-15 13:38:09 +0100151lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident)
152{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100153 lydict_remove(ctx, ident->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200154 FREE_ARRAY(ctx, ident->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100155 FREE_STRINGS(ctx, ident->bases);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100156 lydict_remove(ctx, ident->dsc);
157 lydict_remove(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100158 FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
159}
160
Michal Vasko7f45cf22020-10-01 12:49:44 +0200161void
Radek Krejci19a96102018-11-15 13:38:09 +0100162lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
163{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100164 lydict_remove(ctx, restr->arg.str);
165 lydict_remove(ctx, restr->emsg);
166 lydict_remove(ctx, restr->eapptag);
167 lydict_remove(ctx, restr->dsc);
168 lydict_remove(ctx, restr->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100169 FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
170}
171
172static void
173lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item)
174{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100175 lydict_remove(ctx, item->name);
176 lydict_remove(ctx, item->dsc);
177 lydict_remove(ctx, item->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200178 FREE_ARRAY(ctx, item->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100179 FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
180}
181
Radek Krejcicdfecd92018-11-26 11:27:32 +0100182void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type);
Michal Vasko004d3152020-06-11 19:59:22 +0200183
David Sedlák32488102019-07-15 17:44:10 +0200184void
Radek Krejci19a96102018-11-15 13:38:09 +0100185lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type)
186{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100187 lydict_remove(ctx, type->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100188 FREE_MEMBER(ctx, type->range, lysp_restr_free);
189 FREE_MEMBER(ctx, type->length, lysp_restr_free);
190 FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
191 FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
192 FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
Michal Vasko004d3152020-06-11 19:59:22 +0200193 lyxp_expr_free(ctx, type->path);
Radek Krejci19a96102018-11-15 13:38:09 +0100194 FREE_STRINGS(ctx, type->bases);
195 FREE_ARRAY(ctx, type->types, lysp_type_free);
196 FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
197 if (type->compiled) {
198 lysc_type_free(ctx, type->compiled);
199 }
200}
201
David Sedlák04e17b22019-07-19 15:29:48 +0200202void
Radek Krejci19a96102018-11-15 13:38:09 +0100203lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
204{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100205 lydict_remove(ctx, tpdf->name);
206 lydict_remove(ctx, tpdf->units);
207 lydict_remove(ctx, tpdf->dflt.str);
208 lydict_remove(ctx, tpdf->dsc);
209 lydict_remove(ctx, tpdf->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100210 FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
211
212 lysp_type_free(ctx, &tpdf->type);
213
214}
215
Radek Krejcif538ce52019-03-05 10:46:14 +0100216void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100217lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp)
Radek Krejci19a96102018-11-15 13:38:09 +0100218{
219 struct lysp_node *node, *next;
220
Radek Krejci19a96102018-11-15 13:38:09 +0100221 FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100222 LY_LIST_FOR_SAFE((struct lysp_node *)grp->groupings, next, node) {
223 lysp_node_free(ctx, node);
224 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100225 LY_LIST_FOR_SAFE(grp->child, next, node) {
Radek Krejci19a96102018-11-15 13:38:09 +0100226 lysp_node_free(ctx, node);
227 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100228 LY_LIST_FOR_SAFE((struct lysp_node *)grp->actions, next, node) {
229 lysp_node_free(ctx, node);
230 }
231 LY_LIST_FOR_SAFE((struct lysp_node *)grp->notifs, next, node) {
232 lysp_node_free(ctx, node);
233 }
Radek Krejci19a96102018-11-15 13:38:09 +0100234}
235
Radek Krejcif09e4e82019-06-14 15:08:11 +0200236void
Radek Krejci19a96102018-11-15 13:38:09 +0100237lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
238{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100239 lydict_remove(ctx, when->cond);
240 lydict_remove(ctx, when->dsc);
241 lydict_remove(ctx, when->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100242 FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
243}
244
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200245void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100246lysp_augment_free(struct ly_ctx *ctx, struct lysp_node_augment *augment)
Radek Krejci19a96102018-11-15 13:38:09 +0100247{
248 struct lysp_node *node, *next;
249
Radek Krejci19a96102018-11-15 13:38:09 +0100250 LY_LIST_FOR_SAFE(augment->child, next, node) {
251 lysp_node_free(ctx, node);
252 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100253 LY_LIST_FOR_SAFE((struct lysp_node *)augment->actions, next, node) {
254 lysp_node_free(ctx, node);
255 }
256 LY_LIST_FOR_SAFE((struct lysp_node *)augment->notifs, next, node) {
257 lysp_node_free(ctx, node);
258 }
Radek Krejci19a96102018-11-15 13:38:09 +0100259}
260
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200261void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200262lysp_qname_free(struct ly_ctx *ctx, struct lysp_qname *qname)
263{
264 if (qname) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100265 lydict_remove(ctx, qname->str);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200266 }
267}
268
269void
Radek Krejci19a96102018-11-15 13:38:09 +0100270lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
271{
Michal Vasko22df3f02020-08-24 13:29:22 +0200272 struct lysp_deviate_add *add = (struct lysp_deviate_add *)d;
273 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl *)d;
Radek Krejci19a96102018-11-15 13:38:09 +0100274
275 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
Michal Vaskod989ba02020-08-24 10:59:24 +0200276 switch (d->mod) {
Radek Krejci19a96102018-11-15 13:38:09 +0100277 case LYS_DEV_NOT_SUPPORTED:
278 /* nothing to do */
279 break;
280 case LYS_DEV_ADD:
281 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100282 lydict_remove(ctx, add->units);
Radek Krejci19a96102018-11-15 13:38:09 +0100283 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200284 FREE_ARRAY(ctx, add->uniques, lysp_qname_free);
285 FREE_ARRAY(ctx, add->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100286 break;
287 case LYS_DEV_REPLACE:
288 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100289 lydict_remove(ctx, rpl->units);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200290 lysp_qname_free(ctx, &rpl->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +0100291 break;
292 default:
293 LOGINT(ctx);
294 break;
295 }
296}
297
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200298void
Radek Krejci19a96102018-11-15 13:38:09 +0100299lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
300{
301 struct lysp_deviate *next, *iter;
302
Michal Vaskoe180ed02021-02-05 16:31:20 +0100303 lydict_remove(ctx, dev->nodeid);
304 lydict_remove(ctx, dev->dsc);
305 lydict_remove(ctx, dev->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100306 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
307 lysp_deviate_free(ctx, iter);
308 free(iter);
309 }
310 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
311}
312
David Sedlákd2d676a2019-07-22 11:28:19 +0200313void
Radek Krejci19a96102018-11-15 13:38:09 +0100314lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
315{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100316 lydict_remove(ctx, ref->nodeid);
317 lydict_remove(ctx, ref->dsc);
318 lydict_remove(ctx, ref->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200319 FREE_ARRAY(ctx, ref->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100320 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100321 lydict_remove(ctx, ref->presence);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200322 FREE_ARRAY(ctx, ref->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100323 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
324}
325
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200326void
Radek Krejci19a96102018-11-15 13:38:09 +0100327lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
328{
329 struct lysp_node *child, *next;
Michal Vasko856ea972021-06-09 09:44:30 +0200330 struct lysp_node_container *cont;
331 struct lysp_node_leaf *leaf;
332 struct lysp_node_leaflist *llist;
333 struct lysp_node_list *list;
334 struct lysp_node_choice *choice;
335 struct lysp_node_case *cas;
336 struct lysp_node_uses *uses;
337 struct lysp_node_action *act;
338 struct lysp_node_action_inout *inout;
339 struct lysp_node_notif *notif;
Radek Krejci9a3823e2021-01-27 20:26:46 +0100340 struct lysp_restr *musts = lysp_node_musts(node);
341 struct lysp_when *when = lysp_node_when(node);
Radek Krejci19a96102018-11-15 13:38:09 +0100342
Michal Vaskoe180ed02021-02-05 16:31:20 +0100343 lydict_remove(ctx, node->name);
344 lydict_remove(ctx, node->dsc);
345 lydict_remove(ctx, node->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200346 FREE_ARRAY(ctx, node->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100347 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
348
Radek Krejci9a3823e2021-01-27 20:26:46 +0100349 FREE_MEMBER(ctx, when, lysp_when_free);
350 FREE_ARRAY(ctx, musts, lysp_restr_free);
351
Michal Vaskod989ba02020-08-24 10:59:24 +0200352 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100353 case LYS_CONTAINER:
Michal Vasko856ea972021-06-09 09:44:30 +0200354 cont = (struct lysp_node_container *)node;
355
356 lydict_remove(ctx, cont->presence);
357 FREE_ARRAY(ctx, cont->typedefs, lysp_tpdf_free);
358 if (cont->groupings) {
359 LY_LIST_FOR_SAFE(&cont->groupings->node, next, child) {
360 lysp_node_free(ctx, child);
361 }
362 }
363 LY_LIST_FOR_SAFE(cont->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100364 lysp_node_free(ctx, child);
365 }
Michal Vasko856ea972021-06-09 09:44:30 +0200366 if (cont->actions) {
367 LY_LIST_FOR_SAFE(&cont->actions->node, next, child) {
368 lysp_node_free(ctx, child);
369 }
Radek Krejci19a96102018-11-15 13:38:09 +0100370 }
Michal Vasko856ea972021-06-09 09:44:30 +0200371 if (cont->notifs) {
372 LY_LIST_FOR_SAFE(&cont->notifs->node, next, child) {
373 lysp_node_free(ctx, child);
374 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100375 }
Radek Krejci19a96102018-11-15 13:38:09 +0100376 break;
377 case LYS_LEAF:
Michal Vasko856ea972021-06-09 09:44:30 +0200378 leaf = (struct lysp_node_leaf *)node;
379
380 lysp_type_free(ctx, &leaf->type);
381 lydict_remove(ctx, leaf->units);
382 lydict_remove(ctx, leaf->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100383 break;
384 case LYS_LEAFLIST:
Michal Vasko856ea972021-06-09 09:44:30 +0200385 llist = (struct lysp_node_leaflist *)node;
386
387 lysp_type_free(ctx, &llist->type);
388 lydict_remove(ctx, llist->units);
389 FREE_ARRAY(ctx, llist->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100390 break;
391 case LYS_LIST:
Michal Vasko856ea972021-06-09 09:44:30 +0200392 list = (struct lysp_node_list *)node;
393
394 lydict_remove(ctx, list->key);
395 FREE_ARRAY(ctx, list->typedefs, lysp_tpdf_free);
396 if (list->groupings) {
397 LY_LIST_FOR_SAFE(&list->groupings->node, next, child) {
398 lysp_node_free(ctx, child);
399 }
400 }
401 LY_LIST_FOR_SAFE(list->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100402 lysp_node_free(ctx, child);
403 }
Michal Vasko856ea972021-06-09 09:44:30 +0200404 if (list->actions) {
405 LY_LIST_FOR_SAFE(&list->actions->node, next, child) {
406 lysp_node_free(ctx, child);
407 }
Radek Krejci19a96102018-11-15 13:38:09 +0100408 }
Michal Vasko856ea972021-06-09 09:44:30 +0200409 if (list->notifs) {
410 LY_LIST_FOR_SAFE(&list->notifs->node, next, child) {
411 lysp_node_free(ctx, child);
412 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100413 }
Michal Vasko856ea972021-06-09 09:44:30 +0200414 FREE_ARRAY(ctx, list->uniques, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100415 break;
416 case LYS_CHOICE:
Michal Vasko856ea972021-06-09 09:44:30 +0200417 choice = (struct lysp_node_choice *)node;
418
419 LY_LIST_FOR_SAFE(choice->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100420 lysp_node_free(ctx, child);
421 }
Michal Vasko856ea972021-06-09 09:44:30 +0200422 lydict_remove(ctx, choice->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100423 break;
424 case LYS_CASE:
Michal Vasko856ea972021-06-09 09:44:30 +0200425 cas = (struct lysp_node_case *)node;
426
427 LY_LIST_FOR_SAFE(cas->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100428 lysp_node_free(ctx, child);
429 }
430 break;
431 case LYS_ANYDATA:
432 case LYS_ANYXML:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100433 /* nothing special to do */
Radek Krejci19a96102018-11-15 13:38:09 +0100434 break;
435 case LYS_USES:
Michal Vasko856ea972021-06-09 09:44:30 +0200436 uses = (struct lysp_node_uses *)node;
437
438 FREE_ARRAY(ctx, uses->refines, lysp_refine_free);
439 if (uses->augments) {
440 LY_LIST_FOR_SAFE(&uses->augments->node, next, child) {
441 lysp_node_free(ctx, child);
442 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100443 }
444 break;
445 case LYS_RPC:
446 case LYS_ACTION:
Michal Vasko856ea972021-06-09 09:44:30 +0200447 act = (struct lysp_node_action *)node;
448
449 FREE_ARRAY(ctx, act->typedefs, lysp_tpdf_free);
450 if (act->groupings) {
451 LY_LIST_FOR_SAFE(&act->groupings->node, next, child) {
452 lysp_node_free(ctx, child);
453 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100454 }
Michal Vasko856ea972021-06-09 09:44:30 +0200455 if (act->input.nodetype) {
456 lysp_node_free(ctx, &act->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100457 }
Michal Vasko856ea972021-06-09 09:44:30 +0200458 if (act->output.nodetype) {
459 lysp_node_free(ctx, &act->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100460 }
461 break;
462 case LYS_INPUT:
463 case LYS_OUTPUT:
Michal Vasko856ea972021-06-09 09:44:30 +0200464 inout = (struct lysp_node_action_inout *)node;
465
466 FREE_ARRAY(ctx, inout->typedefs, lysp_tpdf_free);
467 if (inout->groupings) {
468 LY_LIST_FOR_SAFE(&inout->groupings->node, next, child) {
469 lysp_node_free(ctx, child);
470 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100471 }
Michal Vasko856ea972021-06-09 09:44:30 +0200472 LY_LIST_FOR_SAFE(inout->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100473 lysp_node_free(ctx, child);
474 }
475 /* do not free the node, it is never standalone but part of the action node */
476 return;
477 case LYS_NOTIF:
Michal Vasko856ea972021-06-09 09:44:30 +0200478 notif = (struct lysp_node_notif *)node;
479
480 FREE_ARRAY(ctx, notif->typedefs, lysp_tpdf_free);
481 if (notif->groupings) {
482 LY_LIST_FOR_SAFE(&notif->groupings->node, next, child) {
483 lysp_node_free(ctx, child);
484 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100485 }
Michal Vasko856ea972021-06-09 09:44:30 +0200486 LY_LIST_FOR_SAFE(notif->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100487 lysp_node_free(ctx, child);
488 }
489 break;
490 case LYS_GROUPING:
491 lysp_grp_free(ctx, (struct lysp_node_grp *)node);
492 break;
493 case LYS_AUGMENT:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100494 lysp_augment_free(ctx, ((struct lysp_node_augment *)node));
Radek Krejci19a96102018-11-15 13:38:09 +0100495 break;
496 default:
497 LOGINT(ctx);
498 }
499
500 free(node);
501}
502
Radek Krejci15f10ab2020-11-03 14:14:14 +0100503void
Radek Krejci19a96102018-11-15 13:38:09 +0100504lysp_module_free(struct lysp_module *module)
505{
506 struct ly_ctx *ctx;
507 struct lysp_node *node, *next;
508
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100509 if (!module) {
510 return;
511 }
512 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100513
514 FREE_ARRAY(ctx, module->imports, lysp_import_free);
Radek Krejci771928a2021-01-19 13:42:36 +0100515 FREE_ARRAY(ctx, module->includes, module->is_submod ? lysp_include_free_submodule : lysp_include_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100516
Radek Krejci19a96102018-11-15 13:38:09 +0100517 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
518 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
519 FREE_ARRAY(ctx, module->features, lysp_feature_free);
520 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
521 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100522 LY_LIST_FOR_SAFE((struct lysp_node *)module->groupings, next, node) {
523 lysp_node_free(ctx, node);
524 }
Radek Krejci19a96102018-11-15 13:38:09 +0100525 LY_LIST_FOR_SAFE(module->data, next, node) {
526 lysp_node_free(ctx, node);
527 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100528 LY_LIST_FOR_SAFE((struct lysp_node *)module->augments, next, node) {
529 lysp_node_free(ctx, node);
530 }
531 LY_LIST_FOR_SAFE((struct lysp_node *)module->rpcs, next, node) {
532 lysp_node_free(ctx, node);
533 }
534 LY_LIST_FOR_SAFE((struct lysp_node *)module->notifs, next, node) {
535 lysp_node_free(ctx, node);
536 }
Radek Krejci19a96102018-11-15 13:38:09 +0100537 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
538 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
539
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200540 if (module->is_submod) {
541 struct lysp_submodule *submod = (struct lysp_submodule *)module;
542
Michal Vaskoe180ed02021-02-05 16:31:20 +0100543 lydict_remove(ctx, submod->name);
544 lydict_remove(ctx, submod->filepath);
545 lydict_remove(ctx, submod->prefix);
546 lydict_remove(ctx, submod->org);
547 lydict_remove(ctx, submod->contact);
548 lydict_remove(ctx, submod->dsc);
549 lydict_remove(ctx, submod->ref);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200550 }
551
Radek Krejci19a96102018-11-15 13:38:09 +0100552 free(module);
553}
554
Radek Krejci0af46292019-01-11 16:02:31 +0100555void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100556lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
557{
558 if (--(*ext)->refcount) {
559 return;
560 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100561 lydict_remove(ctx, (*ext)->name);
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100562 lydict_remove(ctx, (*ext)->argname);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100563 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
564 free(*ext);
Radek Krejci720d2612021-03-03 19:44:22 +0100565 *ext = NULL;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100566}
567
568void
Radek Krejci19a96102018-11-15 13:38:09 +0100569lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
570{
fredganebc50572019-10-31 15:39:23 +0800571 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200572 ext->def->plugin->free(ctx, ext);
573 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100574 if (ext->def) {
575 lysc_extension_free(ctx, &ext->def);
576 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100577 lydict_remove(ctx, ext->argument);
Radek Krejci0935f412019-08-20 16:15:18 +0200578 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
579}
580
581void
Radek Krejci19a96102018-11-15 13:38:09 +0100582lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
583{
584 LY_ARRAY_FREE(iff->features);
585 free(iff->expr);
586}
587
588static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100589lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100590{
Radek Krejci00b874b2019-02-12 10:54:50 +0100591 if (--(*w)->refcount) {
592 return;
593 }
594 lyxp_expr_free(ctx, (*w)->cond);
Radek Krejci8df109d2021-04-23 12:19:08 +0200595 ly_free_prefix_data(LY_VALUE_SCHEMA_RESOLVED, (*w)->prefixes);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100596 lydict_remove(ctx, (*w)->dsc);
597 lydict_remove(ctx, (*w)->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +0100598 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
599 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100600}
601
Radek Krejciccd20f12019-02-15 14:12:27 +0100602void
Radek Krejci58d171e2018-11-23 13:50:55 +0100603lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
604{
605 lyxp_expr_free(ctx, must->cond);
Radek Krejci8df109d2021-04-23 12:19:08 +0200606 ly_free_prefix_data(LY_VALUE_SCHEMA_RESOLVED, must->prefixes);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100607 lydict_remove(ctx, must->emsg);
608 lydict_remove(ctx, must->eapptag);
609 lydict_remove(ctx, must->dsc);
610 lydict_remove(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100611 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
612}
613
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100614void
Radek Krejci19a96102018-11-15 13:38:09 +0100615lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
616{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100617 lydict_remove(ctx, ident->name);
618 lydict_remove(ctx, ident->dsc);
619 lydict_remove(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100620 LY_ARRAY_FREE(ident->derived);
621 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
622}
623
Radek Krejci19a96102018-11-15 13:38:09 +0100624static void
625lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
626{
627 LY_ARRAY_FREE(range->parts);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100628 lydict_remove(ctx, range->eapptag);
629 lydict_remove(ctx, range->emsg);
630 lydict_remove(ctx, range->dsc);
631 lydict_remove(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100632 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
633}
634
635static void
636lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
637{
638 if (--(*pattern)->refcount) {
639 return;
640 }
Radek Krejci54579462019-04-30 12:47:06 +0200641 pcre2_code_free((*pattern)->code);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100642 lydict_remove(ctx, (*pattern)->expr);
643 lydict_remove(ctx, (*pattern)->eapptag);
644 lydict_remove(ctx, (*pattern)->emsg);
645 lydict_remove(ctx, (*pattern)->dsc);
646 lydict_remove(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100647 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
648 free(*pattern);
649}
650
651static void
Radek Krejci693262f2019-04-29 15:23:20 +0200652lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100653{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100654 lydict_remove(ctx, item->name);
655 lydict_remove(ctx, item->dsc);
656 lydict_remove(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100657 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
658}
659
Radek Krejcia3045382018-11-22 14:30:31 +0100660static void
661lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
662{
663 lysc_type_free(ctx, *type);
664}
Radek Krejci0f969882020-08-21 16:56:47 +0200665
Radek Krejcicdfecd92018-11-26 11:27:32 +0100666void
Radek Krejci19a96102018-11-15 13:38:09 +0100667lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
668{
669 if (--type->refcount) {
670 return;
671 }
Radek Krejcib915ac92020-08-14 23:31:04 +0200672
Michal Vaskod989ba02020-08-24 10:59:24 +0200673 switch (type->basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100674 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +0200675 FREE_MEMBER(ctx, ((struct lysc_type_bin *)type)->length, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100676 break;
677 case LY_TYPE_BITS:
Michal Vasko22df3f02020-08-24 13:29:22 +0200678 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 +0100679 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100680 case LY_TYPE_DEC64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200681 FREE_MEMBER(ctx, ((struct lysc_type_dec *)type)->range, lysc_range_free);
Radek Krejci6cba4292018-11-15 17:33:29 +0100682 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100683 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +0200684 FREE_MEMBER(ctx, ((struct lysc_type_str *)type)->length, lysc_range_free);
685 FREE_ARRAY(ctx, ((struct lysc_type_str *)type)->patterns, lysc_pattern_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100686 break;
687 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +0200688 FREE_ARRAY(ctx, ((struct lysc_type_enum *)type)->enums, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100689 break;
690 case LY_TYPE_INT8:
691 case LY_TYPE_UINT8:
692 case LY_TYPE_INT16:
693 case LY_TYPE_UINT16:
694 case LY_TYPE_INT32:
695 case LY_TYPE_UINT32:
696 case LY_TYPE_INT64:
697 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200698 FREE_MEMBER(ctx, ((struct lysc_type_num *)type)->range, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100699 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100700 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +0200701 LY_ARRAY_FREE(((struct lysc_type_identityref *)type)->bases);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100702 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100703 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +0200704 FREE_ARRAY(ctx, ((struct lysc_type_union *)type)->types, lysc_type2_free);
Radek Krejcia3045382018-11-22 14:30:31 +0100705 break;
706 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200707 lyxp_expr_free(ctx, ((struct lysc_type_leafref *)type)->path);
Radek Krejci8df109d2021-04-23 12:19:08 +0200708 ly_free_prefix_data(LY_VALUE_SCHEMA_RESOLVED, ((struct lysc_type_leafref *)type)->prefixes);
Radek Krejcia3045382018-11-22 14:30:31 +0100709 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100710 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100711 case LY_TYPE_BOOL:
712 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100713 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100714 /* nothing to do */
715 break;
716 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200717
718 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100719 free(type);
720}
721
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100722void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100723lysc_node_action_inout_free(struct ly_ctx *ctx, struct lysc_node_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100724{
725 struct lysc_node *child, *child_next;
726
Radek Krejcif538ce52019-03-05 10:46:14 +0100727 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100728 LY_LIST_FOR_SAFE(inout->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100729 lysc_node_free_(ctx, child);
Radek Krejcif538ce52019-03-05 10:46:14 +0100730 }
731}
732
733void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100734lysc_node_action_free(struct ly_ctx *ctx, struct lysc_node_action *action)
Radek Krejcif538ce52019-03-05 10:46:14 +0100735{
Michal Vasko37a0fe62021-02-03 09:53:04 +0100736 FREE_ARRAY(ctx, action->when, lysc_when_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100737 if (action->input.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100738 lysc_node_free_(ctx, &action->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100739 }
740 if (action->output.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100741 lysc_node_free_(ctx, &action->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100742 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100743}
744
Radek Krejcifc11bd72019-04-11 16:00:05 +0200745void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100746lysc_node_notif_free(struct ly_ctx *ctx, struct lysc_node_notif *notif)
Radek Krejcifc11bd72019-04-11 16:00:05 +0200747{
748 struct lysc_node *child, *child_next;
749
Michal Vasko37a0fe62021-02-03 09:53:04 +0100750 FREE_ARRAY(ctx, notif->when, lysc_when_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200751 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100752 LY_LIST_FOR_SAFE(notif->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100753 lysc_node_free_(ctx, child);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200754 }
755}
756
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200757void
Radek Krejci19a96102018-11-15 13:38:09 +0100758lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
759{
760 struct lysc_node *child, *child_next;
761
762 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100763 lysc_node_free_(ctx, child);
Radek Krejci19a96102018-11-15 13:38:09 +0100764 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100765 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
766 lysc_node_free_(ctx, child);
767 }
768 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
769 lysc_node_free_(ctx, child);
770 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100771 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100772 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100773}
774
775static void
776lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
777{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100778 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100779 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100780 if (node->type) {
781 lysc_type_free(ctx, node->type);
782 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100783 lydict_remove(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200784 if (node->dflt) {
785 node->dflt->realtype->plugin->free(ctx, node->dflt);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200786 lysc_type_free(ctx, (struct lysc_type *)node->dflt->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200787 free(node->dflt);
788 }
Radek Krejci19a96102018-11-15 13:38:09 +0100789}
790
Radek Krejci42452ac2018-11-28 17:09:52 +0100791static void
792lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
793{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200794 LY_ARRAY_COUNT_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100795
Radek Krejci9a3823e2021-01-27 20:26:46 +0100796 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci42452ac2018-11-28 17:09:52 +0100797 FREE_ARRAY(ctx, node->musts, lysc_must_free);
798 if (node->type) {
799 lysc_type_free(ctx, node->type);
800 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100801 lydict_remove(ctx, node->units);
Radek Krejci42452ac2018-11-28 17:09:52 +0100802 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200803 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200804 lysc_type_free(ctx, (struct lysc_type *)node->dflts[u]->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200805 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100806 }
807 LY_ARRAY_FREE(node->dflts);
808}
809
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100810static void
811lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
812{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200813 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100814 struct lysc_node *child, *child_next;
815
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100816 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100817 lysc_node_free_(ctx, child);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100818 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100819 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100820 FREE_ARRAY(ctx, node->musts, lysc_must_free);
821
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100822 LY_ARRAY_FOR(node->uniques, u) {
823 LY_ARRAY_FREE(node->uniques[u]);
824 }
825 LY_ARRAY_FREE(node->uniques);
826
Radek Krejci2a9fc652021-01-22 17:44:34 +0100827 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
828 lysc_node_free_(ctx, child);
829 }
830 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
831 lysc_node_free_(ctx, child);
832 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100833}
834
Radek Krejci056d0a82018-12-06 16:57:25 +0100835static void
836lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
837{
838 struct lysc_node *child, *child_next;
839
Radek Krejci9a3823e2021-01-27 20:26:46 +0100840 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200841 LY_LIST_FOR_SAFE((struct lysc_node *)node->cases, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100842 lysc_node_free_(ctx, child);
Michal Vasko20424b42020-08-31 12:29:38 +0200843 }
844}
845
846static void
847lysc_node_case_free(struct ly_ctx *ctx, struct lysc_node_case *node)
848{
849 struct lysc_node *child, *child_next;
850
Radek Krejci9a3823e2021-01-27 20:26:46 +0100851 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200852 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100853 lysc_node_free_(ctx, child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100854 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100855}
Radek Krejci056d0a82018-12-06 16:57:25 +0100856
Radek Krejci9800fb82018-12-13 14:26:23 +0100857static void
858lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
859{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100860 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9800fb82018-12-13 14:26:23 +0100861 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100862}
863
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100864static void
865lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +0100866{
Radek Krejci2a9fc652021-01-22 17:44:34 +0100867 ly_bool inout = 0;
868
Radek Krejci19a96102018-11-15 13:38:09 +0100869 /* common part */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100870 lydict_remove(ctx, node->name);
871 lydict_remove(ctx, node->dsc);
872 lydict_remove(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100873
874 /* nodetype-specific part */
Michal Vaskod989ba02020-08-24 10:59:24 +0200875 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100876 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200877 lysc_node_container_free(ctx, (struct lysc_node_container *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100878 break;
879 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200880 lysc_node_leaf_free(ctx, (struct lysc_node_leaf *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100881 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100882 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200883 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist *)node);
Radek Krejci42452ac2018-11-28 17:09:52 +0100884 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100885 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200886 lysc_node_list_free(ctx, (struct lysc_node_list *)node);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100887 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100888 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200889 lysc_node_choice_free(ctx, (struct lysc_node_choice *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100890 break;
891 case LYS_CASE:
Michal Vasko20424b42020-08-31 12:29:38 +0200892 lysc_node_case_free(ctx, (struct lysc_node_case *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100893 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100894 case LYS_ANYDATA:
895 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200896 lysc_node_anydata_free(ctx, (struct lysc_node_anydata *)node);
Radek Krejci9800fb82018-12-13 14:26:23 +0100897 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100898 case LYS_RPC:
899 case LYS_ACTION:
900 lysc_node_action_free(ctx, (struct lysc_node_action *)node);
901 break;
902 case LYS_INPUT:
903 case LYS_OUTPUT:
904 lysc_node_action_inout_free(ctx, (struct lysc_node_action_inout *)node);
905 inout = 1;
906 break;
907 case LYS_NOTIF:
908 lysc_node_notif_free(ctx, (struct lysc_node_notif *)node);
909 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100910 default:
911 LOGINT(ctx);
912 }
913
Radek Krejci056d0a82018-12-06 16:57:25 +0100914 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100915
916 if (!inout) {
917 free(node);
918 }
Radek Krejci19a96102018-11-15 13:38:09 +0100919}
920
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100921void
922lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node, ly_bool unlink)
923{
924 struct lysc_node *iter, **child_p;
925
Radek Krejci2a9fc652021-01-22 17:44:34 +0100926 if (node->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
927 /* nothing to do - inouts are part of actions and cannot be unlinked/freed separately */
928 return;
929 }
930
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100931 if (unlink) {
932 /* unlink from siblings */
933 if (node->prev->next) {
934 node->prev->next = node->next;
935 }
936 if (node->next) {
937 node->next->prev = node->prev;
938 } else {
939 /* unlinking the last node */
940 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100941 if (node->nodetype == LYS_ACTION) {
942 iter = (struct lysc_node *)lysc_node_actions(node->parent);
943 } else if (node->nodetype == LYS_NOTIF) {
944 iter = (struct lysc_node *)lysc_node_notifs(node->parent);
945 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +0100946 iter = (struct lysc_node *)lysc_node_child(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100947 }
Michal Vasko539c4a62020-11-03 17:21:34 +0100948 LY_CHECK_ERR_RET(!iter, LOGINT(ctx), );
Radek Krejci2a9fc652021-01-22 17:44:34 +0100949 } else if (node->nodetype == LYS_RPC) {
950 iter = (struct lysc_node *)node->module->compiled->rpcs;
951 } else if (node->nodetype == LYS_NOTIF) {
952 iter = (struct lysc_node *)node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100953 } else {
954 iter = node->module->compiled->data;
955 }
956 /* update the "last" pointer from the first node */
957 iter->prev = node->prev;
958 }
959
960 /* unlink from parent */
961 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100962 if (node->nodetype == LYS_ACTION) {
963 child_p = (struct lysc_node **)lysc_node_actions_p(node->parent);
964 } else if (node->nodetype == LYS_NOTIF) {
965 child_p = (struct lysc_node **)lysc_node_notifs_p(node->parent);
966 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +0100967 child_p = lysc_node_child_p(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100968 }
969 } else if (node->nodetype == LYS_RPC) {
970 child_p = (struct lysc_node **)&node->module->compiled->rpcs;
971 } else if (node->nodetype == LYS_NOTIF) {
972 child_p = (struct lysc_node **)&node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100973 } else {
974 child_p = &node->module->compiled->data;
975 }
976 if (child_p && (*child_p == node)) {
977 /* the node is the first child */
978 *child_p = node->next;
979 }
980 }
981
982 lysc_node_free_(ctx, node);
983}
984
Radek Krejci90ed21e2021-04-12 14:47:46 +0200985void
986lysc_module_free(struct lysc_module *module)
Radek Krejci19a96102018-11-15 13:38:09 +0100987{
988 struct ly_ctx *ctx;
989 struct lysc_node *node, *node_next;
990
Radek Krejci90ed21e2021-04-12 14:47:46 +0200991 if (!module) {
992 return;
993 }
994
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100995 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100996
Radek Krejci19a96102018-11-15 13:38:09 +0100997 LY_LIST_FOR_SAFE(module->data, node_next, node) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100998 lysc_node_free_(ctx, node);
Radek Krejci19a96102018-11-15 13:38:09 +0100999 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001000 LY_LIST_FOR_SAFE((struct lysc_node *)module->rpcs, node_next, node) {
1001 lysc_node_free_(ctx, node);
1002 }
1003 LY_LIST_FOR_SAFE((struct lysc_node *)module->notifs, node_next, node) {
1004 lysc_node_free_(ctx, node);
1005 }
Radek Krejci19a96102018-11-15 13:38:09 +01001006 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
1007
1008 free(module);
1009}
1010
1011void
Radek Krejci90ed21e2021-04-12 14:47:46 +02001012lys_module_free(struct lys_module *module)
Radek Krejci19a96102018-11-15 13:38:09 +01001013{
1014 if (!module) {
1015 return;
1016 }
Michal Vaskoee757602021-06-10 14:38:19 +02001017 assert(!module->implemented);
1018 assert(!module->compiled);
Radek Krejci19a96102018-11-15 13:38:09 +01001019
Radek Krejci80d281e2020-09-14 17:42:54 +02001020 FREE_ARRAY(module->ctx, module->identities, lysc_ident_free);
Radek Krejci19a96102018-11-15 13:38:09 +01001021 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001022
Michal Vasko7f45cf22020-10-01 12:49:44 +02001023 LY_ARRAY_FREE(module->augmented_by);
1024 LY_ARRAY_FREE(module->deviated_by);
1025
Michal Vaskoe180ed02021-02-05 16:31:20 +01001026 lydict_remove(module->ctx, module->name);
1027 lydict_remove(module->ctx, module->revision);
1028 lydict_remove(module->ctx, module->ns);
1029 lydict_remove(module->ctx, module->prefix);
1030 lydict_remove(module->ctx, module->filepath);
1031 lydict_remove(module->ctx, module->org);
1032 lydict_remove(module->ctx, module->contact);
1033 lydict_remove(module->ctx, module->dsc);
1034 lydict_remove(module->ctx, module->ref);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001035
Radek Krejci19a96102018-11-15 13:38:09 +01001036 free(module);
1037}
Michal Vasko33ff9422020-07-03 09:50:39 +02001038
Radek Krejci38d85362019-09-05 16:26:38 +02001039API void
Radek Krejci0b013302021-03-29 15:22:32 +02001040lyplg_ext_instance_substatements_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
Radek Krejci38d85362019-09-05 16:26:38 +02001041{
Radek Krejci1b2eef82021-02-17 11:17:27 +01001042 LY_ARRAY_COUNT_TYPE u;
1043
1044 LY_ARRAY_FOR(substmts, u) {
Radek Krejci38d85362019-09-05 16:26:38 +02001045 if (!substmts[u].storage) {
1046 continue;
1047 }
1048
Michal Vaskod989ba02020-08-24 10:59:24 +02001049 switch (substmts[u].stmt) {
Radek Krejci6b88a462021-02-17 12:39:34 +01001050 case LY_STMT_ACTION:
1051 case LY_STMT_ANYDATA:
1052 case LY_STMT_ANYXML:
1053 case LY_STMT_CONTAINER:
1054 case LY_STMT_CHOICE:
1055 case LY_STMT_LEAF:
1056 case LY_STMT_LEAF_LIST:
1057 case LY_STMT_LIST:
1058 case LY_STMT_NOTIFICATION:
1059 case LY_STMT_RPC:
1060 case LY_STMT_USES: {
1061 struct lysc_node *child, *child_next;
1062
1063 LY_LIST_FOR_SAFE(*((struct lysc_node **)substmts[u].storage), child_next, child) {
1064 lysc_node_free_(ctx, child);
Radek Krejci38d85362019-09-05 16:26:38 +02001065 }
1066 break;
Radek Krejci6b88a462021-02-17 12:39:34 +01001067 }
1068 case LY_STMT_CONFIG:
1069 case LY_STMT_STATUS:
1070 /* nothing to do */
1071 break;
Radek Krejci1b2eef82021-02-17 11:17:27 +01001072 case LY_STMT_DESCRIPTION:
1073 case LY_STMT_REFERENCE:
Radek Krejci38d85362019-09-05 16:26:38 +02001074 case LY_STMT_UNITS:
1075 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1076 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02001077 const char *str = *((const char **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001078 if (!str) {
1079 break;
1080 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001081 lydict_remove(ctx, str);
Radek Krejci38d85362019-09-05 16:26:38 +02001082 } else {
1083 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +02001084 const char **strs = *((const char ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001085 if (!strs) {
1086 break;
1087 }
1088 FREE_STRINGS(ctx, strs);
1089 }
1090 break;
Radek Krejci38d85362019-09-05 16:26:38 +02001091 case LY_STMT_IF_FEATURE: {
Michal Vasko22df3f02020-08-24 13:29:22 +02001092 struct lysc_iffeature *iff = *((struct lysc_iffeature **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001093 if (!iff) {
1094 break;
1095 }
1096 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1097 /* single item */
1098 lysc_iffeature_free(ctx, iff);
1099 free(iff);
1100 } else {
1101 /* multiple items */
1102 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
1103 }
1104 break;
Radek Krejci6b88a462021-02-17 12:39:34 +01001105 case LY_STMT_TYPE:
1106 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1107 /* single item */
1108 struct lysc_type *type = *((struct lysc_type **)substmts[u].storage);
1109 if (!type) {
1110 break;
1111 }
1112 lysc_type_free(ctx, type);
1113 } else {
1114 /* multiple items */
1115 struct lysc_type **types = *((struct lysc_type ***)substmts[u].storage);
1116 if (!types) {
1117 break;
1118 }
1119 FREE_ARRAY(ctx, types, lysc_type2_free);
1120 }
1121 break;
Radek Krejci38d85362019-09-05 16:26:38 +02001122 }
1123
Radek Krejci0f969882020-08-21 16:56:47 +02001124 /* TODO other statements */
Radek Krejci38d85362019-09-05 16:26:38 +02001125 default:
1126 LOGINT(ctx);
1127 }
1128 }
Radek Krejci1b2eef82021-02-17 11:17:27 +01001129
1130 LY_ARRAY_FREE(substmts);
Radek Krejci38d85362019-09-05 16:26:38 +02001131}
David Sedlákebd3acf2019-07-26 15:04:32 +02001132
1133void
Michal Vaskob36053d2020-03-26 15:49:30 +01001134yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001135{
1136 if (ctx) {
aPiecek8d4e75d2021-06-24 14:47:06 +02001137 if (ctx->main_ctx == (struct lys_parser_ctx *)ctx) {
1138 ly_set_erase(&ctx->tpdfs_nodes, NULL);
1139 ly_set_erase(&ctx->grps_nodes, NULL);
1140 }
David Sedlákebd3acf2019-07-26 15:04:32 +02001141 free(ctx);
1142 }
1143}
1144
1145void
Michal Vaskob36053d2020-03-26 15:49:30 +01001146yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001147{
1148 if (ctx) {
aPiecek8d4e75d2021-06-24 14:47:06 +02001149 if (ctx->main_ctx == (struct lys_parser_ctx *)ctx) {
1150 ly_set_erase(&ctx->tpdfs_nodes, NULL);
1151 ly_set_erase(&ctx->grps_nodes, NULL);
1152 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001153 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +02001154 free(ctx);
1155 }
1156}