blob: cea7784ab764444d25c94f5ca0468901daae731b [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
Radek Krejcie7b95092019-05-15 11:03:07 +02002 * @file tree_schema_free.c
Radek Krejci19a96102018-11-15 13:38:09 +01003 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejcie7b95092019-05-15 11:03:07 +02004 * @brief Freeing functions for schema tree structures.
Radek Krejci19a96102018-11-15 13:38:09 +01005 *
Radek Krejcie7b95092019-05-15 11:03:07 +02006 * Copyright (c) 2019 CESNET, z.s.p.o.
Radek Krejci19a96102018-11-15 13:38:09 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejcie7b95092019-05-15 11:03:07 +020015#include <stdlib.h>
16
Radek Krejci535ea9f2020-05-29 16:01:05 +020017#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020018#include "compat.h"
Radek Krejci47fab892020-11-05 17:02:41 +010019#include "log.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include "plugins_exts.h"
21#include "plugins_types.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "tree_data.h"
Michal Vaskofc2cd072021-02-24 13:17:17 +010024#include "tree_data_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010026#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "xml.h"
Radek Krejci19a96102018-11-15 13:38:09 +010028#include "xpath.h"
29
Radek Krejci2a9fc652021-01-22 17:44:34 +010030void lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp);
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);
Michal Vaskofc2cd072021-02-24 13:17:17 +010040 ly_free_prefix_data(stmt->format, stmt->prefix_data);
Radek Krejci19a96102018-11-15 13:38:09 +010041
42 LY_LIST_FOR_SAFE(stmt->child, next, child) {
43 lysp_stmt_free(ctx, child);
44 }
45
46 free(stmt);
47}
48
Radek Krejci2d7a47b2019-05-16 13:34:10 +020049void
Radek Krejci19a96102018-11-15 13:38:09 +010050lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext)
51{
52 struct lysp_stmt *stmt, *next;
53
Michal Vaskoe180ed02021-02-05 16:31:20 +010054 lydict_remove(ctx, ext->name);
55 lydict_remove(ctx, ext->argument);
Michal Vaskofc2cd072021-02-24 13:17:17 +010056 ly_free_prefix_data(ext->format, ext->prefix_data);
Radek Krejci19a96102018-11-15 13:38:09 +010057
58 LY_LIST_FOR_SAFE(ext->child, next, stmt) {
59 lysp_stmt_free(ctx, stmt);
60 }
61}
62
David Sedlák298ff6d2019-07-26 14:29:03 +020063void
Radek Krejci19a96102018-11-15 13:38:09 +010064lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import)
65{
66 /* imported module is freed directly from the context's list */
Michal Vaskoe180ed02021-02-05 16:31:20 +010067 lydict_remove(ctx, import->name);
68 lydict_remove(ctx, import->prefix);
69 lydict_remove(ctx, import->dsc);
70 lydict_remove(ctx, import->ref);
Radek Krejci19a96102018-11-15 13:38:09 +010071 FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free);
72}
73
Radek Krejci771928a2021-01-19 13:42:36 +010074/**
75 * @brief Common function to erase include record in main module and submodule.
76 *
77 * There is a difference since the main module is expected to have the complete list if the included submodules and
78 * the parsed submodule is shared with any include in a submodule. Therefore, the referenced submodules in the include
79 * record are freed only from main module's records.
80 *
81 * @param[in] ctx libyang context
82 * @param[in] include The include record to be erased, the record itself is not freed.
83 * @param[in] main_module Flag to get know if the include record is placed in main module so also the referenced submodule
84 * is supposed to be freed.
85 */
86static void
87lysp_include_free_(struct ly_ctx *ctx, struct lysp_include *include, ly_bool main_module)
Radek Krejci19a96102018-11-15 13:38:09 +010088{
Radek Krejci771928a2021-01-19 13:42:36 +010089 if (main_module && include->submodule) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +020090 lysp_module_free((struct lysp_module *)include->submodule);
Radek Krejci19a96102018-11-15 13:38:09 +010091 }
Michal Vaskoe180ed02021-02-05 16:31:20 +010092 lydict_remove(ctx, include->name);
93 lydict_remove(ctx, include->dsc);
94 lydict_remove(ctx, include->ref);
Radek Krejci19a96102018-11-15 13:38:09 +010095 FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free);
96}
97
David Sedlákaa854b02019-07-22 14:17:10 +020098void
Radek Krejci771928a2021-01-19 13:42:36 +010099lysp_include_free_submodule(struct ly_ctx *ctx, struct lysp_include *include)
100{
101 return lysp_include_free_(ctx, include, 0);
102}
103
104void
105lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include)
106{
107 return lysp_include_free_(ctx, include, 1);
108}
109
110void
Radek Krejci19a96102018-11-15 13:38:09 +0100111lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev)
112{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100113 lydict_remove(ctx, rev->dsc);
114 lydict_remove(ctx, rev->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100115 FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free);
116}
117
David Sedlák986cb412019-07-04 13:10:11 +0200118void
Radek Krejci19a96102018-11-15 13:38:09 +0100119lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext)
120{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100121 lydict_remove(ctx, ext->name);
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100122 lydict_remove(ctx, ext->argname);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100123 lydict_remove(ctx, ext->dsc);
124 lydict_remove(ctx, ext->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100125 FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free);
Michal Vasko5fe75f12020-03-02 13:52:37 +0100126 if (ext->compiled) {
127 lysc_extension_free(ctx, &ext->compiled);
128 }
Radek Krejci19a96102018-11-15 13:38:09 +0100129}
130
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200131void
Radek Krejci19a96102018-11-15 13:38:09 +0100132lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat)
133{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100134 lydict_remove(ctx, feat->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200135 FREE_ARRAY(ctx, feat->iffeatures, lysp_qname_free);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100136 FREE_ARRAY(ctx, feat->iffeatures_c, lysc_iffeature_free);
137 LY_ARRAY_FREE(feat->depfeatures);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100138 lydict_remove(ctx, feat->dsc);
139 lydict_remove(ctx, feat->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100140 FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
141}
142
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200143void
Radek Krejci19a96102018-11-15 13:38:09 +0100144lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident)
145{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100146 lydict_remove(ctx, ident->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200147 FREE_ARRAY(ctx, ident->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100148 FREE_STRINGS(ctx, ident->bases);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100149 lydict_remove(ctx, ident->dsc);
150 lydict_remove(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100151 FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
152}
153
Michal Vasko7f45cf22020-10-01 12:49:44 +0200154void
Radek Krejci19a96102018-11-15 13:38:09 +0100155lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
156{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100157 lydict_remove(ctx, restr->arg.str);
158 lydict_remove(ctx, restr->emsg);
159 lydict_remove(ctx, restr->eapptag);
160 lydict_remove(ctx, restr->dsc);
161 lydict_remove(ctx, restr->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100162 FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
163}
164
165static void
166lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item)
167{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100168 lydict_remove(ctx, item->name);
169 lydict_remove(ctx, item->dsc);
170 lydict_remove(ctx, item->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200171 FREE_ARRAY(ctx, item->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100172 FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
173}
174
Radek Krejcicdfecd92018-11-26 11:27:32 +0100175void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type);
Michal Vasko004d3152020-06-11 19:59:22 +0200176
David Sedlák32488102019-07-15 17:44:10 +0200177void
Radek Krejci19a96102018-11-15 13:38:09 +0100178lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type)
179{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100180 lydict_remove(ctx, type->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100181 FREE_MEMBER(ctx, type->range, lysp_restr_free);
182 FREE_MEMBER(ctx, type->length, lysp_restr_free);
183 FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
184 FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
185 FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
Michal Vasko004d3152020-06-11 19:59:22 +0200186 lyxp_expr_free(ctx, type->path);
Radek Krejci19a96102018-11-15 13:38:09 +0100187 FREE_STRINGS(ctx, type->bases);
188 FREE_ARRAY(ctx, type->types, lysp_type_free);
189 FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
190 if (type->compiled) {
191 lysc_type_free(ctx, type->compiled);
192 }
193}
194
David Sedlák04e17b22019-07-19 15:29:48 +0200195void
Radek Krejci19a96102018-11-15 13:38:09 +0100196lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
197{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100198 lydict_remove(ctx, tpdf->name);
199 lydict_remove(ctx, tpdf->units);
200 lydict_remove(ctx, tpdf->dflt.str);
201 lydict_remove(ctx, tpdf->dsc);
202 lydict_remove(ctx, tpdf->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100203 FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
204
205 lysp_type_free(ctx, &tpdf->type);
206
207}
208
Radek Krejcif538ce52019-03-05 10:46:14 +0100209void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100210lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp)
Radek Krejci19a96102018-11-15 13:38:09 +0100211{
212 struct lysp_node *node, *next;
213
Radek Krejci19a96102018-11-15 13:38:09 +0100214 FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100215 LY_LIST_FOR_SAFE((struct lysp_node *)grp->groupings, next, node) {
216 lysp_node_free(ctx, node);
217 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100218 LY_LIST_FOR_SAFE(grp->child, next, node) {
Radek Krejci19a96102018-11-15 13:38:09 +0100219 lysp_node_free(ctx, node);
220 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100221 LY_LIST_FOR_SAFE((struct lysp_node *)grp->actions, next, node) {
222 lysp_node_free(ctx, node);
223 }
224 LY_LIST_FOR_SAFE((struct lysp_node *)grp->notifs, next, node) {
225 lysp_node_free(ctx, node);
226 }
Radek Krejci19a96102018-11-15 13:38:09 +0100227}
228
Radek Krejcif09e4e82019-06-14 15:08:11 +0200229void
Radek Krejci19a96102018-11-15 13:38:09 +0100230lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
231{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100232 lydict_remove(ctx, when->cond);
233 lydict_remove(ctx, when->dsc);
234 lydict_remove(ctx, when->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100235 FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
236}
237
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200238void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100239lysp_augment_free(struct ly_ctx *ctx, struct lysp_node_augment *augment)
Radek Krejci19a96102018-11-15 13:38:09 +0100240{
241 struct lysp_node *node, *next;
242
Radek Krejci19a96102018-11-15 13:38:09 +0100243 LY_LIST_FOR_SAFE(augment->child, next, node) {
244 lysp_node_free(ctx, node);
245 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100246 LY_LIST_FOR_SAFE((struct lysp_node *)augment->actions, next, node) {
247 lysp_node_free(ctx, node);
248 }
249 LY_LIST_FOR_SAFE((struct lysp_node *)augment->notifs, next, node) {
250 lysp_node_free(ctx, node);
251 }
Radek Krejci19a96102018-11-15 13:38:09 +0100252}
253
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200254void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200255lysp_qname_free(struct ly_ctx *ctx, struct lysp_qname *qname)
256{
257 if (qname) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100258 lydict_remove(ctx, qname->str);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200259 }
260}
261
262void
Radek Krejci19a96102018-11-15 13:38:09 +0100263lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
264{
Michal Vasko22df3f02020-08-24 13:29:22 +0200265 struct lysp_deviate_add *add = (struct lysp_deviate_add *)d;
266 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl *)d;
Radek Krejci19a96102018-11-15 13:38:09 +0100267
268 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
Michal Vaskod989ba02020-08-24 10:59:24 +0200269 switch (d->mod) {
Radek Krejci19a96102018-11-15 13:38:09 +0100270 case LYS_DEV_NOT_SUPPORTED:
271 /* nothing to do */
272 break;
273 case LYS_DEV_ADD:
274 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100275 lydict_remove(ctx, add->units);
Radek Krejci19a96102018-11-15 13:38:09 +0100276 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200277 FREE_ARRAY(ctx, add->uniques, lysp_qname_free);
278 FREE_ARRAY(ctx, add->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100279 break;
280 case LYS_DEV_REPLACE:
281 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100282 lydict_remove(ctx, rpl->units);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200283 lysp_qname_free(ctx, &rpl->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +0100284 break;
285 default:
286 LOGINT(ctx);
287 break;
288 }
289}
290
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200291void
Radek Krejci19a96102018-11-15 13:38:09 +0100292lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
293{
294 struct lysp_deviate *next, *iter;
295
Michal Vaskoe180ed02021-02-05 16:31:20 +0100296 lydict_remove(ctx, dev->nodeid);
297 lydict_remove(ctx, dev->dsc);
298 lydict_remove(ctx, dev->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100299 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
300 lysp_deviate_free(ctx, iter);
301 free(iter);
302 }
303 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
304}
305
David Sedlákd2d676a2019-07-22 11:28:19 +0200306void
Radek Krejci19a96102018-11-15 13:38:09 +0100307lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
308{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100309 lydict_remove(ctx, ref->nodeid);
310 lydict_remove(ctx, ref->dsc);
311 lydict_remove(ctx, ref->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200312 FREE_ARRAY(ctx, ref->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100313 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100314 lydict_remove(ctx, ref->presence);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200315 FREE_ARRAY(ctx, ref->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100316 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
317}
318
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200319void
Radek Krejci19a96102018-11-15 13:38:09 +0100320lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
321{
322 struct lysp_node *child, *next;
Radek Krejci9a3823e2021-01-27 20:26:46 +0100323 struct lysp_restr *musts = lysp_node_musts(node);
324 struct lysp_when *when = lysp_node_when(node);
Radek Krejci19a96102018-11-15 13:38:09 +0100325
Michal Vaskoe180ed02021-02-05 16:31:20 +0100326 lydict_remove(ctx, node->name);
327 lydict_remove(ctx, node->dsc);
328 lydict_remove(ctx, node->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200329 FREE_ARRAY(ctx, node->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100330 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
331
Radek Krejci9a3823e2021-01-27 20:26:46 +0100332 FREE_MEMBER(ctx, when, lysp_when_free);
333 FREE_ARRAY(ctx, musts, lysp_restr_free);
334
Michal Vaskod989ba02020-08-24 10:59:24 +0200335 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100336 case LYS_CONTAINER:
Michal Vaskoe180ed02021-02-05 16:31:20 +0100337 lydict_remove(ctx, ((struct lysp_node_container *)node)->presence);
Michal Vasko22df3f02020-08-24 13:29:22 +0200338 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100339 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100340 lysp_node_free(ctx, child);
341 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200342 LY_LIST_FOR_SAFE(((struct lysp_node_container *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100343 lysp_node_free(ctx, child);
344 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100345 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->actions->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100346 lysp_node_free(ctx, child);
347 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100348 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->notifs->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100349 lysp_node_free(ctx, child);
350 }
Radek Krejci19a96102018-11-15 13:38:09 +0100351 break;
352 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200353 lysp_type_free(ctx, &((struct lysp_node_leaf *)node)->type);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100354 lydict_remove(ctx, ((struct lysp_node_leaf *)node)->units);
355 lydict_remove(ctx, ((struct lysp_node_leaf *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100356 break;
357 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200358 lysp_type_free(ctx, &((struct lysp_node_leaflist *)node)->type);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100359 lydict_remove(ctx, ((struct lysp_node_leaflist *)node)->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200360 FREE_ARRAY(ctx, ((struct lysp_node_leaflist *)node)->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100361 break;
362 case LYS_LIST:
Michal Vaskoe180ed02021-02-05 16:31:20 +0100363 lydict_remove(ctx, ((struct lysp_node_list *)node)->key);
Michal Vasko22df3f02020-08-24 13:29:22 +0200364 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100365 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100366 lysp_node_free(ctx, child);
367 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200368 LY_LIST_FOR_SAFE(((struct lysp_node_list *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100369 lysp_node_free(ctx, child);
370 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100371 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->actions->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100372 lysp_node_free(ctx, child);
373 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100374 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->notifs->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100375 lysp_node_free(ctx, child);
376 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200377 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->uniques, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100378 break;
379 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200380 LY_LIST_FOR_SAFE(((struct lysp_node_choice *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100381 lysp_node_free(ctx, child);
382 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100383 lydict_remove(ctx, ((struct lysp_node_choice *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100384 break;
385 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200386 LY_LIST_FOR_SAFE(((struct lysp_node_case *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100387 lysp_node_free(ctx, child);
388 }
389 break;
390 case LYS_ANYDATA:
391 case LYS_ANYXML:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100392 /* nothing special to do */
Radek Krejci19a96102018-11-15 13:38:09 +0100393 break;
394 case LYS_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +0200395 FREE_ARRAY(ctx, ((struct lysp_node_uses *)node)->refines, lysp_refine_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100396 LY_LIST_FOR_SAFE(&((struct lysp_node_uses *)node)->augments->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100397 lysp_node_free(ctx, child);
398 }
399 break;
400 case LYS_RPC:
401 case LYS_ACTION:
402 FREE_ARRAY(ctx, ((struct lysp_node_action *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100403 LY_LIST_FOR_SAFE(&((struct lysp_node_action *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100404 lysp_node_free(ctx, child);
405 }
406 if (((struct lysp_node_action *)node)->input.nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100407 lysp_node_free(ctx, &((struct lysp_node_action *)node)->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100408 }
409 if (((struct lysp_node_action *)node)->output.nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100410 lysp_node_free(ctx, &((struct lysp_node_action *)node)->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100411 }
412 break;
413 case LYS_INPUT:
414 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100415 FREE_ARRAY(ctx, ((struct lysp_node_action_inout *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100416 LY_LIST_FOR_SAFE(&((struct lysp_node_action_inout *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100417 lysp_node_free(ctx, child);
418 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100419 LY_LIST_FOR_SAFE(((struct lysp_node_action_inout *)node)->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100420 lysp_node_free(ctx, child);
421 }
422 /* do not free the node, it is never standalone but part of the action node */
423 return;
424 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100425 FREE_ARRAY(ctx, ((struct lysp_node_notif *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100426 LY_LIST_FOR_SAFE(&((struct lysp_node_notif *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100427 lysp_node_free(ctx, child);
428 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100429 LY_LIST_FOR_SAFE(((struct lysp_node_notif *)node)->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100430 lysp_node_free(ctx, child);
431 }
432 break;
433 case LYS_GROUPING:
434 lysp_grp_free(ctx, (struct lysp_node_grp *)node);
435 break;
436 case LYS_AUGMENT:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100437 lysp_augment_free(ctx, ((struct lysp_node_augment *)node));
Radek Krejci19a96102018-11-15 13:38:09 +0100438 break;
439 default:
440 LOGINT(ctx);
441 }
442
443 free(node);
444}
445
Radek Krejci15f10ab2020-11-03 14:14:14 +0100446void
Radek Krejci19a96102018-11-15 13:38:09 +0100447lysp_module_free(struct lysp_module *module)
448{
449 struct ly_ctx *ctx;
450 struct lysp_node *node, *next;
451
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100452 if (!module) {
453 return;
454 }
455 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100456
457 FREE_ARRAY(ctx, module->imports, lysp_import_free);
Radek Krejci771928a2021-01-19 13:42:36 +0100458 FREE_ARRAY(ctx, module->includes, module->is_submod ? lysp_include_free_submodule : lysp_include_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100459
Radek Krejci19a96102018-11-15 13:38:09 +0100460 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
461 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
462 FREE_ARRAY(ctx, module->features, lysp_feature_free);
463 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
464 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100465 LY_LIST_FOR_SAFE((struct lysp_node *)module->groupings, next, node) {
466 lysp_node_free(ctx, node);
467 }
Radek Krejci19a96102018-11-15 13:38:09 +0100468 LY_LIST_FOR_SAFE(module->data, next, node) {
469 lysp_node_free(ctx, node);
470 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100471 LY_LIST_FOR_SAFE((struct lysp_node *)module->augments, next, node) {
472 lysp_node_free(ctx, node);
473 }
474 LY_LIST_FOR_SAFE((struct lysp_node *)module->rpcs, next, node) {
475 lysp_node_free(ctx, node);
476 }
477 LY_LIST_FOR_SAFE((struct lysp_node *)module->notifs, next, node) {
478 lysp_node_free(ctx, node);
479 }
Radek Krejci19a96102018-11-15 13:38:09 +0100480 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
481 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
482
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200483 if (module->is_submod) {
484 struct lysp_submodule *submod = (struct lysp_submodule *)module;
485
Michal Vaskoe180ed02021-02-05 16:31:20 +0100486 lydict_remove(ctx, submod->name);
487 lydict_remove(ctx, submod->filepath);
488 lydict_remove(ctx, submod->prefix);
489 lydict_remove(ctx, submod->org);
490 lydict_remove(ctx, submod->contact);
491 lydict_remove(ctx, submod->dsc);
492 lydict_remove(ctx, submod->ref);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200493 }
494
Radek Krejci19a96102018-11-15 13:38:09 +0100495 free(module);
496}
497
Radek Krejci0af46292019-01-11 16:02:31 +0100498void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100499lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
500{
501 if (--(*ext)->refcount) {
502 return;
503 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100504 lydict_remove(ctx, (*ext)->name);
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100505 lydict_remove(ctx, (*ext)->argname);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100506 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
507 free(*ext);
Radek Krejci720d2612021-03-03 19:44:22 +0100508 *ext = NULL;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100509}
510
511void
Radek Krejci19a96102018-11-15 13:38:09 +0100512lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
513{
fredganebc50572019-10-31 15:39:23 +0800514 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200515 ext->def->plugin->free(ctx, ext);
516 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100517 if (ext->def) {
518 lysc_extension_free(ctx, &ext->def);
519 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100520 lydict_remove(ctx, ext->argument);
Radek Krejci0935f412019-08-20 16:15:18 +0200521 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
522}
523
524void
Radek Krejci19a96102018-11-15 13:38:09 +0100525lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
526{
527 LY_ARRAY_FREE(iff->features);
528 free(iff->expr);
529}
530
531static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100532lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100533{
Radek Krejci00b874b2019-02-12 10:54:50 +0100534 if (--(*w)->refcount) {
535 return;
536 }
537 lyxp_expr_free(ctx, (*w)->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200538 lysc_prefixes_free((*w)->prefixes);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100539 lydict_remove(ctx, (*w)->dsc);
540 lydict_remove(ctx, (*w)->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +0100541 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
542 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100543}
544
Radek Krejciccd20f12019-02-15 14:12:27 +0100545void
Radek Krejci58d171e2018-11-23 13:50:55 +0100546lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
547{
548 lyxp_expr_free(ctx, must->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200549 lysc_prefixes_free(must->prefixes);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100550 lydict_remove(ctx, must->emsg);
551 lydict_remove(ctx, must->eapptag);
552 lydict_remove(ctx, must->dsc);
553 lydict_remove(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100554 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
555}
556
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100557void
Radek Krejci19a96102018-11-15 13:38:09 +0100558lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
559{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100560 lydict_remove(ctx, ident->name);
561 lydict_remove(ctx, ident->dsc);
562 lydict_remove(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100563 LY_ARRAY_FREE(ident->derived);
564 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
565}
566
Radek Krejci19a96102018-11-15 13:38:09 +0100567static void
568lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
569{
570 LY_ARRAY_FREE(range->parts);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100571 lydict_remove(ctx, range->eapptag);
572 lydict_remove(ctx, range->emsg);
573 lydict_remove(ctx, range->dsc);
574 lydict_remove(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100575 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
576}
577
578static void
579lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
580{
581 if (--(*pattern)->refcount) {
582 return;
583 }
Radek Krejci54579462019-04-30 12:47:06 +0200584 pcre2_code_free((*pattern)->code);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100585 lydict_remove(ctx, (*pattern)->expr);
586 lydict_remove(ctx, (*pattern)->eapptag);
587 lydict_remove(ctx, (*pattern)->emsg);
588 lydict_remove(ctx, (*pattern)->dsc);
589 lydict_remove(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100590 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
591 free(*pattern);
592}
593
594static void
Radek Krejci693262f2019-04-29 15:23:20 +0200595lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100596{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100597 lydict_remove(ctx, item->name);
598 lydict_remove(ctx, item->dsc);
599 lydict_remove(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100600 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
601}
602
Radek Krejcia3045382018-11-22 14:30:31 +0100603static void
604lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
605{
606 lysc_type_free(ctx, *type);
607}
Radek Krejci0f969882020-08-21 16:56:47 +0200608
Radek Krejcicdfecd92018-11-26 11:27:32 +0100609void
Radek Krejci19a96102018-11-15 13:38:09 +0100610lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
611{
612 if (--type->refcount) {
613 return;
614 }
Radek Krejcib915ac92020-08-14 23:31:04 +0200615
Michal Vaskod989ba02020-08-24 10:59:24 +0200616 switch (type->basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100617 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +0200618 FREE_MEMBER(ctx, ((struct lysc_type_bin *)type)->length, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100619 break;
620 case LY_TYPE_BITS:
Michal Vasko22df3f02020-08-24 13:29:22 +0200621 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 +0100622 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100623 case LY_TYPE_DEC64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200624 FREE_MEMBER(ctx, ((struct lysc_type_dec *)type)->range, lysc_range_free);
Radek Krejci6cba4292018-11-15 17:33:29 +0100625 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100626 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +0200627 FREE_MEMBER(ctx, ((struct lysc_type_str *)type)->length, lysc_range_free);
628 FREE_ARRAY(ctx, ((struct lysc_type_str *)type)->patterns, lysc_pattern_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100629 break;
630 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +0200631 FREE_ARRAY(ctx, ((struct lysc_type_enum *)type)->enums, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100632 break;
633 case LY_TYPE_INT8:
634 case LY_TYPE_UINT8:
635 case LY_TYPE_INT16:
636 case LY_TYPE_UINT16:
637 case LY_TYPE_INT32:
638 case LY_TYPE_UINT32:
639 case LY_TYPE_INT64:
640 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200641 FREE_MEMBER(ctx, ((struct lysc_type_num *)type)->range, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100642 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100643 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +0200644 LY_ARRAY_FREE(((struct lysc_type_identityref *)type)->bases);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100645 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100646 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +0200647 FREE_ARRAY(ctx, ((struct lysc_type_union *)type)->types, lysc_type2_free);
Radek Krejcia3045382018-11-22 14:30:31 +0100648 break;
649 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200650 lyxp_expr_free(ctx, ((struct lysc_type_leafref *)type)->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200651 lysc_prefixes_free(((struct lysc_type_leafref *)type)->prefixes);
Radek Krejcia3045382018-11-22 14:30:31 +0100652 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100653 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100654 case LY_TYPE_BOOL:
655 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100656 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100657 /* nothing to do */
658 break;
659 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200660
661 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100662 free(type);
663}
664
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100665void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100666lysc_node_action_inout_free(struct ly_ctx *ctx, struct lysc_node_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100667{
668 struct lysc_node *child, *child_next;
669
Radek Krejcif538ce52019-03-05 10:46:14 +0100670 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100671 LY_LIST_FOR_SAFE(inout->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100672 lysc_node_free_(ctx, child);
Radek Krejcif538ce52019-03-05 10:46:14 +0100673 }
674}
675
676void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100677lysc_node_action_free(struct ly_ctx *ctx, struct lysc_node_action *action)
Radek Krejcif538ce52019-03-05 10:46:14 +0100678{
Michal Vasko37a0fe62021-02-03 09:53:04 +0100679 FREE_ARRAY(ctx, action->when, lysc_when_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100680 if (action->input.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100681 lysc_node_free_(ctx, &action->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100682 }
683 if (action->output.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100684 lysc_node_free_(ctx, &action->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100685 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100686}
687
Radek Krejcifc11bd72019-04-11 16:00:05 +0200688void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100689lysc_node_notif_free(struct ly_ctx *ctx, struct lysc_node_notif *notif)
Radek Krejcifc11bd72019-04-11 16:00:05 +0200690{
691 struct lysc_node *child, *child_next;
692
Michal Vasko37a0fe62021-02-03 09:53:04 +0100693 FREE_ARRAY(ctx, notif->when, lysc_when_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200694 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100695 LY_LIST_FOR_SAFE(notif->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100696 lysc_node_free_(ctx, child);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200697 }
698}
699
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200700void
Radek Krejci19a96102018-11-15 13:38:09 +0100701lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
702{
703 struct lysc_node *child, *child_next;
704
705 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100706 lysc_node_free_(ctx, child);
Radek Krejci19a96102018-11-15 13:38:09 +0100707 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100708 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
709 lysc_node_free_(ctx, child);
710 }
711 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
712 lysc_node_free_(ctx, child);
713 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100714 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100715 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100716}
717
718static void
719lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
720{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100721 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100722 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100723 if (node->type) {
724 lysc_type_free(ctx, node->type);
725 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100726 lydict_remove(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200727 if (node->dflt) {
728 node->dflt->realtype->plugin->free(ctx, node->dflt);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200729 lysc_type_free(ctx, (struct lysc_type *)node->dflt->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200730 free(node->dflt);
731 }
Radek Krejci19a96102018-11-15 13:38:09 +0100732}
733
Radek Krejci42452ac2018-11-28 17:09:52 +0100734static void
735lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
736{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200737 LY_ARRAY_COUNT_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100738
Radek Krejci9a3823e2021-01-27 20:26:46 +0100739 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci42452ac2018-11-28 17:09:52 +0100740 FREE_ARRAY(ctx, node->musts, lysc_must_free);
741 if (node->type) {
742 lysc_type_free(ctx, node->type);
743 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100744 lydict_remove(ctx, node->units);
Radek Krejci42452ac2018-11-28 17:09:52 +0100745 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200746 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200747 lysc_type_free(ctx, (struct lysc_type *)node->dflts[u]->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200748 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100749 }
750 LY_ARRAY_FREE(node->dflts);
751}
752
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100753static void
754lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
755{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200756 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100757 struct lysc_node *child, *child_next;
758
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100759 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100760 lysc_node_free_(ctx, child);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100761 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100762 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100763 FREE_ARRAY(ctx, node->musts, lysc_must_free);
764
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100765 LY_ARRAY_FOR(node->uniques, u) {
766 LY_ARRAY_FREE(node->uniques[u]);
767 }
768 LY_ARRAY_FREE(node->uniques);
769
Radek Krejci2a9fc652021-01-22 17:44:34 +0100770 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
771 lysc_node_free_(ctx, child);
772 }
773 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
774 lysc_node_free_(ctx, child);
775 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100776}
777
Radek Krejci056d0a82018-12-06 16:57:25 +0100778static void
779lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
780{
781 struct lysc_node *child, *child_next;
782
Radek Krejci9a3823e2021-01-27 20:26:46 +0100783 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200784 LY_LIST_FOR_SAFE((struct lysc_node *)node->cases, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100785 lysc_node_free_(ctx, child);
Michal Vasko20424b42020-08-31 12:29:38 +0200786 }
787}
788
789static void
790lysc_node_case_free(struct ly_ctx *ctx, struct lysc_node_case *node)
791{
792 struct lysc_node *child, *child_next;
793
Radek Krejci9a3823e2021-01-27 20:26:46 +0100794 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200795 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100796 lysc_node_free_(ctx, child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100797 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100798}
Radek Krejci056d0a82018-12-06 16:57:25 +0100799
Radek Krejci9800fb82018-12-13 14:26:23 +0100800static void
801lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
802{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100803 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9800fb82018-12-13 14:26:23 +0100804 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100805}
806
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100807static void
808lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +0100809{
Radek Krejci2a9fc652021-01-22 17:44:34 +0100810 ly_bool inout = 0;
811
Radek Krejci19a96102018-11-15 13:38:09 +0100812 /* common part */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100813 lydict_remove(ctx, node->name);
814 lydict_remove(ctx, node->dsc);
815 lydict_remove(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100816
817 /* nodetype-specific part */
Michal Vaskod989ba02020-08-24 10:59:24 +0200818 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100819 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200820 lysc_node_container_free(ctx, (struct lysc_node_container *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100821 break;
822 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200823 lysc_node_leaf_free(ctx, (struct lysc_node_leaf *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100824 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100825 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200826 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist *)node);
Radek Krejci42452ac2018-11-28 17:09:52 +0100827 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100828 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200829 lysc_node_list_free(ctx, (struct lysc_node_list *)node);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100830 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100831 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200832 lysc_node_choice_free(ctx, (struct lysc_node_choice *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100833 break;
834 case LYS_CASE:
Michal Vasko20424b42020-08-31 12:29:38 +0200835 lysc_node_case_free(ctx, (struct lysc_node_case *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100836 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100837 case LYS_ANYDATA:
838 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200839 lysc_node_anydata_free(ctx, (struct lysc_node_anydata *)node);
Radek Krejci9800fb82018-12-13 14:26:23 +0100840 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100841 case LYS_RPC:
842 case LYS_ACTION:
843 lysc_node_action_free(ctx, (struct lysc_node_action *)node);
844 break;
845 case LYS_INPUT:
846 case LYS_OUTPUT:
847 lysc_node_action_inout_free(ctx, (struct lysc_node_action_inout *)node);
848 inout = 1;
849 break;
850 case LYS_NOTIF:
851 lysc_node_notif_free(ctx, (struct lysc_node_notif *)node);
852 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100853 default:
854 LOGINT(ctx);
855 }
856
Radek Krejci056d0a82018-12-06 16:57:25 +0100857 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100858
859 if (!inout) {
860 free(node);
861 }
Radek Krejci19a96102018-11-15 13:38:09 +0100862}
863
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100864void
865lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node, ly_bool unlink)
866{
867 struct lysc_node *iter, **child_p;
868
Radek Krejci2a9fc652021-01-22 17:44:34 +0100869 if (node->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
870 /* nothing to do - inouts are part of actions and cannot be unlinked/freed separately */
871 return;
872 }
873
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100874 if (unlink) {
875 /* unlink from siblings */
876 if (node->prev->next) {
877 node->prev->next = node->next;
878 }
879 if (node->next) {
880 node->next->prev = node->prev;
881 } else {
882 /* unlinking the last node */
883 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100884 if (node->nodetype == LYS_ACTION) {
885 iter = (struct lysc_node *)lysc_node_actions(node->parent);
886 } else if (node->nodetype == LYS_NOTIF) {
887 iter = (struct lysc_node *)lysc_node_notifs(node->parent);
888 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +0100889 iter = (struct lysc_node *)lysc_node_child(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100890 }
Michal Vasko539c4a62020-11-03 17:21:34 +0100891 LY_CHECK_ERR_RET(!iter, LOGINT(ctx), );
Radek Krejci2a9fc652021-01-22 17:44:34 +0100892 } else if (node->nodetype == LYS_RPC) {
893 iter = (struct lysc_node *)node->module->compiled->rpcs;
894 } else if (node->nodetype == LYS_NOTIF) {
895 iter = (struct lysc_node *)node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100896 } else {
897 iter = node->module->compiled->data;
898 }
899 /* update the "last" pointer from the first node */
900 iter->prev = node->prev;
901 }
902
903 /* unlink from parent */
904 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100905 if (node->nodetype == LYS_ACTION) {
906 child_p = (struct lysc_node **)lysc_node_actions_p(node->parent);
907 } else if (node->nodetype == LYS_NOTIF) {
908 child_p = (struct lysc_node **)lysc_node_notifs_p(node->parent);
909 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +0100910 child_p = lysc_node_child_p(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100911 }
912 } else if (node->nodetype == LYS_RPC) {
913 child_p = (struct lysc_node **)&node->module->compiled->rpcs;
914 } else if (node->nodetype == LYS_NOTIF) {
915 child_p = (struct lysc_node **)&node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100916 } else {
917 child_p = &node->module->compiled->data;
918 }
919 if (child_p && (*child_p == node)) {
920 /* the node is the first child */
921 *child_p = node->next;
922 }
923 }
924
925 lysc_node_free_(ctx, node);
926}
927
Radek Krejci19a96102018-11-15 13:38:09 +0100928static void
929lysc_module_free_(struct lysc_module *module)
930{
931 struct ly_ctx *ctx;
932 struct lysc_node *node, *node_next;
933
Michal Vaskod989ba02020-08-24 10:59:24 +0200934 LY_CHECK_ARG_RET(NULL, module, );
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100935 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100936
Radek Krejci19a96102018-11-15 13:38:09 +0100937 LY_LIST_FOR_SAFE(module->data, node_next, node) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100938 lysc_node_free_(ctx, node);
Radek Krejci19a96102018-11-15 13:38:09 +0100939 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100940 LY_LIST_FOR_SAFE((struct lysc_node *)module->rpcs, node_next, node) {
941 lysc_node_free_(ctx, node);
942 }
943 LY_LIST_FOR_SAFE((struct lysc_node *)module->notifs, node_next, node) {
944 lysc_node_free_(ctx, node);
945 }
Radek Krejci19a96102018-11-15 13:38:09 +0100946 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
947
948 free(module);
949}
950
951void
952lysc_module_free(struct lysc_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
953{
Radek Krejci9b042892019-02-13 14:28:44 +0100954 /* TODO use the destructor, this just suppress warning about unused parameter */
955 (void) private_destructor;
956
Radek Krejci19a96102018-11-15 13:38:09 +0100957 if (module) {
958 lysc_module_free_(module);
959 }
960}
961
962void
963lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
964{
965 if (!module) {
966 return;
967 }
968
969 lysc_module_free(module->compiled, private_destructor);
Radek Krejci80d281e2020-09-14 17:42:54 +0200970 FREE_ARRAY(module->ctx, module->identities, lysc_ident_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100971 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100972
Michal Vasko7f45cf22020-10-01 12:49:44 +0200973 LY_ARRAY_FREE(module->augmented_by);
974 LY_ARRAY_FREE(module->deviated_by);
975
Michal Vaskoe180ed02021-02-05 16:31:20 +0100976 lydict_remove(module->ctx, module->name);
977 lydict_remove(module->ctx, module->revision);
978 lydict_remove(module->ctx, module->ns);
979 lydict_remove(module->ctx, module->prefix);
980 lydict_remove(module->ctx, module->filepath);
981 lydict_remove(module->ctx, module->org);
982 lydict_remove(module->ctx, module->contact);
983 lydict_remove(module->ctx, module->dsc);
984 lydict_remove(module->ctx, module->ref);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100985
Radek Krejci19a96102018-11-15 13:38:09 +0100986 free(module);
987}
Michal Vasko33ff9422020-07-03 09:50:39 +0200988
Radek Krejci38d85362019-09-05 16:26:38 +0200989API void
Radek Krejci1b2eef82021-02-17 11:17:27 +0100990lysc_extension_instance_substatements_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
Radek Krejci38d85362019-09-05 16:26:38 +0200991{
Radek Krejci1b2eef82021-02-17 11:17:27 +0100992 LY_ARRAY_COUNT_TYPE u;
993
994 LY_ARRAY_FOR(substmts, u) {
Radek Krejci38d85362019-09-05 16:26:38 +0200995 if (!substmts[u].storage) {
996 continue;
997 }
998
Michal Vaskod989ba02020-08-24 10:59:24 +0200999 switch (substmts[u].stmt) {
Radek Krejci6b88a462021-02-17 12:39:34 +01001000 case LY_STMT_ACTION:
1001 case LY_STMT_ANYDATA:
1002 case LY_STMT_ANYXML:
1003 case LY_STMT_CONTAINER:
1004 case LY_STMT_CHOICE:
1005 case LY_STMT_LEAF:
1006 case LY_STMT_LEAF_LIST:
1007 case LY_STMT_LIST:
1008 case LY_STMT_NOTIFICATION:
1009 case LY_STMT_RPC:
1010 case LY_STMT_USES: {
1011 struct lysc_node *child, *child_next;
1012
1013 LY_LIST_FOR_SAFE(*((struct lysc_node **)substmts[u].storage), child_next, child) {
1014 lysc_node_free_(ctx, child);
Radek Krejci38d85362019-09-05 16:26:38 +02001015 }
1016 break;
Radek Krejci6b88a462021-02-17 12:39:34 +01001017 }
1018 case LY_STMT_CONFIG:
1019 case LY_STMT_STATUS:
1020 /* nothing to do */
1021 break;
Radek Krejci1b2eef82021-02-17 11:17:27 +01001022 case LY_STMT_DESCRIPTION:
1023 case LY_STMT_REFERENCE:
Radek Krejci38d85362019-09-05 16:26:38 +02001024 case LY_STMT_UNITS:
1025 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1026 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02001027 const char *str = *((const char **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001028 if (!str) {
1029 break;
1030 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001031 lydict_remove(ctx, str);
Radek Krejci38d85362019-09-05 16:26:38 +02001032 } else {
1033 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +02001034 const char **strs = *((const char ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001035 if (!strs) {
1036 break;
1037 }
1038 FREE_STRINGS(ctx, strs);
1039 }
1040 break;
Radek Krejci38d85362019-09-05 16:26:38 +02001041 case LY_STMT_IF_FEATURE: {
Michal Vasko22df3f02020-08-24 13:29:22 +02001042 struct lysc_iffeature *iff = *((struct lysc_iffeature **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001043 if (!iff) {
1044 break;
1045 }
1046 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1047 /* single item */
1048 lysc_iffeature_free(ctx, iff);
1049 free(iff);
1050 } else {
1051 /* multiple items */
1052 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
1053 }
1054 break;
Radek Krejci6b88a462021-02-17 12:39:34 +01001055 case LY_STMT_TYPE:
1056 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1057 /* single item */
1058 struct lysc_type *type = *((struct lysc_type **)substmts[u].storage);
1059 if (!type) {
1060 break;
1061 }
1062 lysc_type_free(ctx, type);
1063 } else {
1064 /* multiple items */
1065 struct lysc_type **types = *((struct lysc_type ***)substmts[u].storage);
1066 if (!types) {
1067 break;
1068 }
1069 FREE_ARRAY(ctx, types, lysc_type2_free);
1070 }
1071 break;
Radek Krejci38d85362019-09-05 16:26:38 +02001072 }
1073
Radek Krejci0f969882020-08-21 16:56:47 +02001074 /* TODO other statements */
Radek Krejci38d85362019-09-05 16:26:38 +02001075 default:
1076 LOGINT(ctx);
1077 }
1078 }
Radek Krejci1b2eef82021-02-17 11:17:27 +01001079
1080 LY_ARRAY_FREE(substmts);
Radek Krejci38d85362019-09-05 16:26:38 +02001081}
David Sedlákebd3acf2019-07-26 15:04:32 +02001082
1083void
Michal Vaskob36053d2020-03-26 15:49:30 +01001084yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001085{
1086 if (ctx) {
1087 free(ctx);
1088 }
1089}
1090
1091void
Michal Vaskob36053d2020-03-26 15:49:30 +01001092yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001093{
1094 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001095 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +02001096 free(ctx);
1097 }
1098}