blob: 7873b57a413df67fe8a91421e529fed5f64bc3ee [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 Krejci77114102021-03-10 15:21:57 +010019#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010020#include "log.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include "plugins_exts.h"
22#include "plugins_types.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "tree_data.h"
Michal Vaskofc2cd072021-02-24 13:17:17 +010025#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010026#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010028#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "xml.h"
Radek Krejci19a96102018-11-15 13:38:09 +010030#include "xpath.h"
31
Radek Krejci2a9fc652021-01-22 17:44:34 +010032void lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010033static void lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node);
Radek Krejci19a96102018-11-15 13:38:09 +010034
35static void
36lysp_stmt_free(struct ly_ctx *ctx, struct lysp_stmt *stmt)
37{
38 struct lysp_stmt *child, *next;
39
Michal Vaskoe180ed02021-02-05 16:31:20 +010040 lydict_remove(ctx, stmt->stmt);
41 lydict_remove(ctx, stmt->arg);
Michal Vaskofc2cd072021-02-24 13:17:17 +010042 ly_free_prefix_data(stmt->format, stmt->prefix_data);
Radek Krejci19a96102018-11-15 13:38:09 +010043
44 LY_LIST_FOR_SAFE(stmt->child, next, child) {
45 lysp_stmt_free(ctx, child);
46 }
47
48 free(stmt);
49}
50
Radek Krejci2d7a47b2019-05-16 13:34:10 +020051void
Radek Krejci19a96102018-11-15 13:38:09 +010052lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext)
53{
54 struct lysp_stmt *stmt, *next;
aPiecek60d9d672021-04-27 15:49:57 +020055 struct lysp_node *node, *next_node;
Radek Krejci19a96102018-11-15 13:38:09 +010056
Michal Vaskoe180ed02021-02-05 16:31:20 +010057 lydict_remove(ctx, ext->name);
58 lydict_remove(ctx, ext->argument);
Michal Vaskofc2cd072021-02-24 13:17:17 +010059 ly_free_prefix_data(ext->format, ext->prefix_data);
aPiecek60d9d672021-04-27 15:49:57 +020060 LY_LIST_FOR_SAFE(ext->parsed, next_node, node) {
61 lysp_node_free(ctx, node);
62 }
Radek Krejci19a96102018-11-15 13:38:09 +010063
64 LY_LIST_FOR_SAFE(ext->child, next, stmt) {
65 lysp_stmt_free(ctx, stmt);
66 }
67}
68
David Sedlák298ff6d2019-07-26 14:29:03 +020069void
Radek Krejci19a96102018-11-15 13:38:09 +010070lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import)
71{
72 /* imported module is freed directly from the context's list */
Michal Vaskoe180ed02021-02-05 16:31:20 +010073 lydict_remove(ctx, import->name);
74 lydict_remove(ctx, import->prefix);
75 lydict_remove(ctx, import->dsc);
76 lydict_remove(ctx, import->ref);
Radek Krejci19a96102018-11-15 13:38:09 +010077 FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free);
78}
79
Radek Krejci771928a2021-01-19 13:42:36 +010080/**
81 * @brief Common function to erase include record in main module and submodule.
82 *
83 * There is a difference since the main module is expected to have the complete list if the included submodules and
84 * the parsed submodule is shared with any include in a submodule. Therefore, the referenced submodules in the include
85 * record are freed only from main module's records.
86 *
87 * @param[in] ctx libyang context
88 * @param[in] include The include record to be erased, the record itself is not freed.
89 * @param[in] main_module Flag to get know if the include record is placed in main module so also the referenced submodule
90 * is supposed to be freed.
91 */
92static void
93lysp_include_free_(struct ly_ctx *ctx, struct lysp_include *include, ly_bool main_module)
Radek Krejci19a96102018-11-15 13:38:09 +010094{
Radek Krejci771928a2021-01-19 13:42:36 +010095 if (main_module && include->submodule) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +020096 lysp_module_free((struct lysp_module *)include->submodule);
Radek Krejci19a96102018-11-15 13:38:09 +010097 }
Michal Vaskoe180ed02021-02-05 16:31:20 +010098 lydict_remove(ctx, include->name);
99 lydict_remove(ctx, include->dsc);
100 lydict_remove(ctx, include->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100101 FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free);
102}
103
David Sedlákaa854b02019-07-22 14:17:10 +0200104void
Radek Krejci771928a2021-01-19 13:42:36 +0100105lysp_include_free_submodule(struct ly_ctx *ctx, struct lysp_include *include)
106{
107 return lysp_include_free_(ctx, include, 0);
108}
109
110void
111lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include)
112{
113 return lysp_include_free_(ctx, include, 1);
114}
115
116void
Radek Krejci19a96102018-11-15 13:38:09 +0100117lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev)
118{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100119 lydict_remove(ctx, rev->dsc);
120 lydict_remove(ctx, rev->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100121 FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free);
122}
123
David Sedlák986cb412019-07-04 13:10:11 +0200124void
Radek Krejci19a96102018-11-15 13:38:09 +0100125lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext)
126{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100127 lydict_remove(ctx, ext->name);
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100128 lydict_remove(ctx, ext->argname);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100129 lydict_remove(ctx, ext->dsc);
130 lydict_remove(ctx, ext->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100131 FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free);
Michal Vasko5fe75f12020-03-02 13:52:37 +0100132 if (ext->compiled) {
133 lysc_extension_free(ctx, &ext->compiled);
134 }
Radek Krejci19a96102018-11-15 13:38:09 +0100135}
136
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200137void
Radek Krejci19a96102018-11-15 13:38:09 +0100138lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat)
139{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100140 lydict_remove(ctx, feat->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200141 FREE_ARRAY(ctx, feat->iffeatures, lysp_qname_free);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100142 FREE_ARRAY(ctx, feat->iffeatures_c, lysc_iffeature_free);
143 LY_ARRAY_FREE(feat->depfeatures);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100144 lydict_remove(ctx, feat->dsc);
145 lydict_remove(ctx, feat->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100146 FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
147}
148
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200149void
Radek Krejci19a96102018-11-15 13:38:09 +0100150lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident)
151{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100152 lydict_remove(ctx, ident->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200153 FREE_ARRAY(ctx, ident->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100154 FREE_STRINGS(ctx, ident->bases);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100155 lydict_remove(ctx, ident->dsc);
156 lydict_remove(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100157 FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
158}
159
Michal Vasko7f45cf22020-10-01 12:49:44 +0200160void
Radek Krejci19a96102018-11-15 13:38:09 +0100161lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
162{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100163 lydict_remove(ctx, restr->arg.str);
164 lydict_remove(ctx, restr->emsg);
165 lydict_remove(ctx, restr->eapptag);
166 lydict_remove(ctx, restr->dsc);
167 lydict_remove(ctx, restr->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100168 FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
169}
170
171static void
172lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item)
173{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100174 lydict_remove(ctx, item->name);
175 lydict_remove(ctx, item->dsc);
176 lydict_remove(ctx, item->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200177 FREE_ARRAY(ctx, item->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100178 FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
179}
180
Radek Krejcicdfecd92018-11-26 11:27:32 +0100181void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type);
Michal Vasko004d3152020-06-11 19:59:22 +0200182
David Sedlák32488102019-07-15 17:44:10 +0200183void
Radek Krejci19a96102018-11-15 13:38:09 +0100184lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type)
185{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100186 lydict_remove(ctx, type->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100187 FREE_MEMBER(ctx, type->range, lysp_restr_free);
188 FREE_MEMBER(ctx, type->length, lysp_restr_free);
189 FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
190 FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
191 FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
Michal Vasko004d3152020-06-11 19:59:22 +0200192 lyxp_expr_free(ctx, type->path);
Radek Krejci19a96102018-11-15 13:38:09 +0100193 FREE_STRINGS(ctx, type->bases);
194 FREE_ARRAY(ctx, type->types, lysp_type_free);
195 FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
196 if (type->compiled) {
197 lysc_type_free(ctx, type->compiled);
198 }
199}
200
David Sedlák04e17b22019-07-19 15:29:48 +0200201void
Radek Krejci19a96102018-11-15 13:38:09 +0100202lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
203{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100204 lydict_remove(ctx, tpdf->name);
205 lydict_remove(ctx, tpdf->units);
206 lydict_remove(ctx, tpdf->dflt.str);
207 lydict_remove(ctx, tpdf->dsc);
208 lydict_remove(ctx, tpdf->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100209 FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
210
211 lysp_type_free(ctx, &tpdf->type);
212
213}
214
Radek Krejcif538ce52019-03-05 10:46:14 +0100215void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100216lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp)
Radek Krejci19a96102018-11-15 13:38:09 +0100217{
218 struct lysp_node *node, *next;
219
Radek Krejci19a96102018-11-15 13:38:09 +0100220 FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100221 LY_LIST_FOR_SAFE((struct lysp_node *)grp->groupings, next, node) {
222 lysp_node_free(ctx, node);
223 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100224 LY_LIST_FOR_SAFE(grp->child, next, node) {
Radek Krejci19a96102018-11-15 13:38:09 +0100225 lysp_node_free(ctx, node);
226 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100227 LY_LIST_FOR_SAFE((struct lysp_node *)grp->actions, next, node) {
228 lysp_node_free(ctx, node);
229 }
230 LY_LIST_FOR_SAFE((struct lysp_node *)grp->notifs, next, node) {
231 lysp_node_free(ctx, node);
232 }
Radek Krejci19a96102018-11-15 13:38:09 +0100233}
234
Radek Krejcif09e4e82019-06-14 15:08:11 +0200235void
Radek Krejci19a96102018-11-15 13:38:09 +0100236lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
237{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100238 lydict_remove(ctx, when->cond);
239 lydict_remove(ctx, when->dsc);
240 lydict_remove(ctx, when->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100241 FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
242}
243
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200244void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100245lysp_augment_free(struct ly_ctx *ctx, struct lysp_node_augment *augment)
Radek Krejci19a96102018-11-15 13:38:09 +0100246{
247 struct lysp_node *node, *next;
248
Radek Krejci19a96102018-11-15 13:38:09 +0100249 LY_LIST_FOR_SAFE(augment->child, next, node) {
250 lysp_node_free(ctx, node);
251 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100252 LY_LIST_FOR_SAFE((struct lysp_node *)augment->actions, next, node) {
253 lysp_node_free(ctx, node);
254 }
255 LY_LIST_FOR_SAFE((struct lysp_node *)augment->notifs, next, node) {
256 lysp_node_free(ctx, node);
257 }
Radek Krejci19a96102018-11-15 13:38:09 +0100258}
259
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200260void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200261lysp_qname_free(struct ly_ctx *ctx, struct lysp_qname *qname)
262{
263 if (qname) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100264 lydict_remove(ctx, qname->str);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200265 }
266}
267
268void
Radek Krejci19a96102018-11-15 13:38:09 +0100269lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
270{
Michal Vasko22df3f02020-08-24 13:29:22 +0200271 struct lysp_deviate_add *add = (struct lysp_deviate_add *)d;
272 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl *)d;
Radek Krejci19a96102018-11-15 13:38:09 +0100273
274 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
Michal Vaskod989ba02020-08-24 10:59:24 +0200275 switch (d->mod) {
Radek Krejci19a96102018-11-15 13:38:09 +0100276 case LYS_DEV_NOT_SUPPORTED:
277 /* nothing to do */
278 break;
279 case LYS_DEV_ADD:
280 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100281 lydict_remove(ctx, add->units);
Radek Krejci19a96102018-11-15 13:38:09 +0100282 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200283 FREE_ARRAY(ctx, add->uniques, lysp_qname_free);
284 FREE_ARRAY(ctx, add->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100285 break;
286 case LYS_DEV_REPLACE:
287 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100288 lydict_remove(ctx, rpl->units);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200289 lysp_qname_free(ctx, &rpl->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +0100290 break;
291 default:
292 LOGINT(ctx);
293 break;
294 }
295}
296
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200297void
Radek Krejci19a96102018-11-15 13:38:09 +0100298lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
299{
300 struct lysp_deviate *next, *iter;
301
Michal Vaskoe180ed02021-02-05 16:31:20 +0100302 lydict_remove(ctx, dev->nodeid);
303 lydict_remove(ctx, dev->dsc);
304 lydict_remove(ctx, dev->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100305 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
306 lysp_deviate_free(ctx, iter);
307 free(iter);
308 }
309 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
310}
311
David Sedlákd2d676a2019-07-22 11:28:19 +0200312void
Radek Krejci19a96102018-11-15 13:38:09 +0100313lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
314{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100315 lydict_remove(ctx, ref->nodeid);
316 lydict_remove(ctx, ref->dsc);
317 lydict_remove(ctx, ref->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200318 FREE_ARRAY(ctx, ref->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100319 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100320 lydict_remove(ctx, ref->presence);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200321 FREE_ARRAY(ctx, ref->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100322 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
323}
324
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200325void
Radek Krejci19a96102018-11-15 13:38:09 +0100326lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
327{
328 struct lysp_node *child, *next;
Radek Krejci9a3823e2021-01-27 20:26:46 +0100329 struct lysp_restr *musts = lysp_node_musts(node);
330 struct lysp_when *when = lysp_node_when(node);
Radek Krejci19a96102018-11-15 13:38:09 +0100331
Michal Vaskoe180ed02021-02-05 16:31:20 +0100332 lydict_remove(ctx, node->name);
333 lydict_remove(ctx, node->dsc);
334 lydict_remove(ctx, node->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200335 FREE_ARRAY(ctx, node->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100336 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
337
Radek Krejci9a3823e2021-01-27 20:26:46 +0100338 FREE_MEMBER(ctx, when, lysp_when_free);
339 FREE_ARRAY(ctx, musts, lysp_restr_free);
340
Michal Vaskod989ba02020-08-24 10:59:24 +0200341 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100342 case LYS_CONTAINER:
Michal Vaskoe180ed02021-02-05 16:31:20 +0100343 lydict_remove(ctx, ((struct lysp_node_container *)node)->presence);
Michal Vasko22df3f02020-08-24 13:29:22 +0200344 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100345 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100346 lysp_node_free(ctx, child);
347 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200348 LY_LIST_FOR_SAFE(((struct lysp_node_container *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100349 lysp_node_free(ctx, child);
350 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100351 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->actions->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100352 lysp_node_free(ctx, child);
353 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100354 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->notifs->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100355 lysp_node_free(ctx, child);
356 }
Radek Krejci19a96102018-11-15 13:38:09 +0100357 break;
358 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200359 lysp_type_free(ctx, &((struct lysp_node_leaf *)node)->type);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100360 lydict_remove(ctx, ((struct lysp_node_leaf *)node)->units);
361 lydict_remove(ctx, ((struct lysp_node_leaf *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100362 break;
363 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200364 lysp_type_free(ctx, &((struct lysp_node_leaflist *)node)->type);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100365 lydict_remove(ctx, ((struct lysp_node_leaflist *)node)->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200366 FREE_ARRAY(ctx, ((struct lysp_node_leaflist *)node)->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100367 break;
368 case LYS_LIST:
Michal Vaskoe180ed02021-02-05 16:31:20 +0100369 lydict_remove(ctx, ((struct lysp_node_list *)node)->key);
Michal Vasko22df3f02020-08-24 13:29:22 +0200370 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100371 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100372 lysp_node_free(ctx, child);
373 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200374 LY_LIST_FOR_SAFE(((struct lysp_node_list *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100375 lysp_node_free(ctx, child);
376 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100377 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->actions->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100378 lysp_node_free(ctx, child);
379 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100380 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->notifs->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100381 lysp_node_free(ctx, child);
382 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200383 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->uniques, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100384 break;
385 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200386 LY_LIST_FOR_SAFE(((struct lysp_node_choice *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100387 lysp_node_free(ctx, child);
388 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100389 lydict_remove(ctx, ((struct lysp_node_choice *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100390 break;
391 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200392 LY_LIST_FOR_SAFE(((struct lysp_node_case *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100393 lysp_node_free(ctx, child);
394 }
395 break;
396 case LYS_ANYDATA:
397 case LYS_ANYXML:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100398 /* nothing special to do */
Radek Krejci19a96102018-11-15 13:38:09 +0100399 break;
400 case LYS_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +0200401 FREE_ARRAY(ctx, ((struct lysp_node_uses *)node)->refines, lysp_refine_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100402 LY_LIST_FOR_SAFE(&((struct lysp_node_uses *)node)->augments->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100403 lysp_node_free(ctx, child);
404 }
405 break;
406 case LYS_RPC:
407 case LYS_ACTION:
408 FREE_ARRAY(ctx, ((struct lysp_node_action *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100409 LY_LIST_FOR_SAFE(&((struct lysp_node_action *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100410 lysp_node_free(ctx, child);
411 }
412 if (((struct lysp_node_action *)node)->input.nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100413 lysp_node_free(ctx, &((struct lysp_node_action *)node)->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100414 }
415 if (((struct lysp_node_action *)node)->output.nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100416 lysp_node_free(ctx, &((struct lysp_node_action *)node)->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100417 }
418 break;
419 case LYS_INPUT:
420 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100421 FREE_ARRAY(ctx, ((struct lysp_node_action_inout *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100422 LY_LIST_FOR_SAFE(&((struct lysp_node_action_inout *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100423 lysp_node_free(ctx, child);
424 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100425 LY_LIST_FOR_SAFE(((struct lysp_node_action_inout *)node)->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100426 lysp_node_free(ctx, child);
427 }
428 /* do not free the node, it is never standalone but part of the action node */
429 return;
430 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100431 FREE_ARRAY(ctx, ((struct lysp_node_notif *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100432 LY_LIST_FOR_SAFE(&((struct lysp_node_notif *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100433 lysp_node_free(ctx, child);
434 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100435 LY_LIST_FOR_SAFE(((struct lysp_node_notif *)node)->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100436 lysp_node_free(ctx, child);
437 }
438 break;
439 case LYS_GROUPING:
440 lysp_grp_free(ctx, (struct lysp_node_grp *)node);
441 break;
442 case LYS_AUGMENT:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100443 lysp_augment_free(ctx, ((struct lysp_node_augment *)node));
Radek Krejci19a96102018-11-15 13:38:09 +0100444 break;
445 default:
446 LOGINT(ctx);
447 }
448
449 free(node);
450}
451
Radek Krejci15f10ab2020-11-03 14:14:14 +0100452void
Radek Krejci19a96102018-11-15 13:38:09 +0100453lysp_module_free(struct lysp_module *module)
454{
455 struct ly_ctx *ctx;
456 struct lysp_node *node, *next;
457
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100458 if (!module) {
459 return;
460 }
461 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100462
463 FREE_ARRAY(ctx, module->imports, lysp_import_free);
Radek Krejci771928a2021-01-19 13:42:36 +0100464 FREE_ARRAY(ctx, module->includes, module->is_submod ? lysp_include_free_submodule : lysp_include_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100465
Radek Krejci19a96102018-11-15 13:38:09 +0100466 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
467 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
468 FREE_ARRAY(ctx, module->features, lysp_feature_free);
469 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
470 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100471 LY_LIST_FOR_SAFE((struct lysp_node *)module->groupings, next, node) {
472 lysp_node_free(ctx, node);
473 }
Radek Krejci19a96102018-11-15 13:38:09 +0100474 LY_LIST_FOR_SAFE(module->data, next, node) {
475 lysp_node_free(ctx, node);
476 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100477 LY_LIST_FOR_SAFE((struct lysp_node *)module->augments, next, node) {
478 lysp_node_free(ctx, node);
479 }
480 LY_LIST_FOR_SAFE((struct lysp_node *)module->rpcs, next, node) {
481 lysp_node_free(ctx, node);
482 }
483 LY_LIST_FOR_SAFE((struct lysp_node *)module->notifs, next, node) {
484 lysp_node_free(ctx, node);
485 }
Radek Krejci19a96102018-11-15 13:38:09 +0100486 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
487 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
488
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200489 if (module->is_submod) {
490 struct lysp_submodule *submod = (struct lysp_submodule *)module;
491
Michal Vaskoe180ed02021-02-05 16:31:20 +0100492 lydict_remove(ctx, submod->name);
493 lydict_remove(ctx, submod->filepath);
494 lydict_remove(ctx, submod->prefix);
495 lydict_remove(ctx, submod->org);
496 lydict_remove(ctx, submod->contact);
497 lydict_remove(ctx, submod->dsc);
498 lydict_remove(ctx, submod->ref);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200499 }
500
Radek Krejci19a96102018-11-15 13:38:09 +0100501 free(module);
502}
503
Radek Krejci0af46292019-01-11 16:02:31 +0100504void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100505lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
506{
507 if (--(*ext)->refcount) {
508 return;
509 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100510 lydict_remove(ctx, (*ext)->name);
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100511 lydict_remove(ctx, (*ext)->argname);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100512 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
513 free(*ext);
Radek Krejci720d2612021-03-03 19:44:22 +0100514 *ext = NULL;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100515}
516
517void
Radek Krejci19a96102018-11-15 13:38:09 +0100518lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
519{
fredganebc50572019-10-31 15:39:23 +0800520 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200521 ext->def->plugin->free(ctx, ext);
522 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100523 if (ext->def) {
524 lysc_extension_free(ctx, &ext->def);
525 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100526 lydict_remove(ctx, ext->argument);
Radek Krejci0935f412019-08-20 16:15:18 +0200527 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
528}
529
530void
Radek Krejci19a96102018-11-15 13:38:09 +0100531lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
532{
533 LY_ARRAY_FREE(iff->features);
534 free(iff->expr);
535}
536
537static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100538lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100539{
Radek Krejci00b874b2019-02-12 10:54:50 +0100540 if (--(*w)->refcount) {
541 return;
542 }
543 lyxp_expr_free(ctx, (*w)->cond);
Radek Krejci8df109d2021-04-23 12:19:08 +0200544 ly_free_prefix_data(LY_VALUE_SCHEMA_RESOLVED, (*w)->prefixes);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100545 lydict_remove(ctx, (*w)->dsc);
546 lydict_remove(ctx, (*w)->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +0100547 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
548 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100549}
550
Radek Krejciccd20f12019-02-15 14:12:27 +0100551void
Radek Krejci58d171e2018-11-23 13:50:55 +0100552lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
553{
554 lyxp_expr_free(ctx, must->cond);
Radek Krejci8df109d2021-04-23 12:19:08 +0200555 ly_free_prefix_data(LY_VALUE_SCHEMA_RESOLVED, must->prefixes);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100556 lydict_remove(ctx, must->emsg);
557 lydict_remove(ctx, must->eapptag);
558 lydict_remove(ctx, must->dsc);
559 lydict_remove(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100560 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
561}
562
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100563void
Radek Krejci19a96102018-11-15 13:38:09 +0100564lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
565{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100566 lydict_remove(ctx, ident->name);
567 lydict_remove(ctx, ident->dsc);
568 lydict_remove(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100569 LY_ARRAY_FREE(ident->derived);
570 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
571}
572
Radek Krejci19a96102018-11-15 13:38:09 +0100573static void
574lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
575{
576 LY_ARRAY_FREE(range->parts);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100577 lydict_remove(ctx, range->eapptag);
578 lydict_remove(ctx, range->emsg);
579 lydict_remove(ctx, range->dsc);
580 lydict_remove(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100581 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
582}
583
584static void
585lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
586{
587 if (--(*pattern)->refcount) {
588 return;
589 }
Radek Krejci54579462019-04-30 12:47:06 +0200590 pcre2_code_free((*pattern)->code);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100591 lydict_remove(ctx, (*pattern)->expr);
592 lydict_remove(ctx, (*pattern)->eapptag);
593 lydict_remove(ctx, (*pattern)->emsg);
594 lydict_remove(ctx, (*pattern)->dsc);
595 lydict_remove(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100596 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
597 free(*pattern);
598}
599
600static void
Radek Krejci693262f2019-04-29 15:23:20 +0200601lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100602{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100603 lydict_remove(ctx, item->name);
604 lydict_remove(ctx, item->dsc);
605 lydict_remove(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100606 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
607}
608
Radek Krejcia3045382018-11-22 14:30:31 +0100609static void
610lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
611{
612 lysc_type_free(ctx, *type);
613}
Radek Krejci0f969882020-08-21 16:56:47 +0200614
Radek Krejcicdfecd92018-11-26 11:27:32 +0100615void
Radek Krejci19a96102018-11-15 13:38:09 +0100616lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
617{
618 if (--type->refcount) {
619 return;
620 }
Radek Krejcib915ac92020-08-14 23:31:04 +0200621
Michal Vaskod989ba02020-08-24 10:59:24 +0200622 switch (type->basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100623 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +0200624 FREE_MEMBER(ctx, ((struct lysc_type_bin *)type)->length, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100625 break;
626 case LY_TYPE_BITS:
Michal Vasko22df3f02020-08-24 13:29:22 +0200627 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 +0100628 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100629 case LY_TYPE_DEC64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200630 FREE_MEMBER(ctx, ((struct lysc_type_dec *)type)->range, lysc_range_free);
Radek Krejci6cba4292018-11-15 17:33:29 +0100631 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100632 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +0200633 FREE_MEMBER(ctx, ((struct lysc_type_str *)type)->length, lysc_range_free);
634 FREE_ARRAY(ctx, ((struct lysc_type_str *)type)->patterns, lysc_pattern_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100635 break;
636 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +0200637 FREE_ARRAY(ctx, ((struct lysc_type_enum *)type)->enums, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100638 break;
639 case LY_TYPE_INT8:
640 case LY_TYPE_UINT8:
641 case LY_TYPE_INT16:
642 case LY_TYPE_UINT16:
643 case LY_TYPE_INT32:
644 case LY_TYPE_UINT32:
645 case LY_TYPE_INT64:
646 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200647 FREE_MEMBER(ctx, ((struct lysc_type_num *)type)->range, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100648 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100649 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +0200650 LY_ARRAY_FREE(((struct lysc_type_identityref *)type)->bases);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100651 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100652 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +0200653 FREE_ARRAY(ctx, ((struct lysc_type_union *)type)->types, lysc_type2_free);
Radek Krejcia3045382018-11-22 14:30:31 +0100654 break;
655 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200656 lyxp_expr_free(ctx, ((struct lysc_type_leafref *)type)->path);
Radek Krejci8df109d2021-04-23 12:19:08 +0200657 ly_free_prefix_data(LY_VALUE_SCHEMA_RESOLVED, ((struct lysc_type_leafref *)type)->prefixes);
Radek Krejcia3045382018-11-22 14:30:31 +0100658 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100659 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100660 case LY_TYPE_BOOL:
661 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100662 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100663 /* nothing to do */
664 break;
665 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200666
667 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100668 free(type);
669}
670
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100671void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100672lysc_node_action_inout_free(struct ly_ctx *ctx, struct lysc_node_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100673{
674 struct lysc_node *child, *child_next;
675
Radek Krejcif538ce52019-03-05 10:46:14 +0100676 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100677 LY_LIST_FOR_SAFE(inout->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100678 lysc_node_free_(ctx, child);
Radek Krejcif538ce52019-03-05 10:46:14 +0100679 }
680}
681
682void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100683lysc_node_action_free(struct ly_ctx *ctx, struct lysc_node_action *action)
Radek Krejcif538ce52019-03-05 10:46:14 +0100684{
Michal Vasko37a0fe62021-02-03 09:53:04 +0100685 FREE_ARRAY(ctx, action->when, lysc_when_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100686 if (action->input.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100687 lysc_node_free_(ctx, &action->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100688 }
689 if (action->output.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100690 lysc_node_free_(ctx, &action->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100691 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100692}
693
Radek Krejcifc11bd72019-04-11 16:00:05 +0200694void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100695lysc_node_notif_free(struct ly_ctx *ctx, struct lysc_node_notif *notif)
Radek Krejcifc11bd72019-04-11 16:00:05 +0200696{
697 struct lysc_node *child, *child_next;
698
Michal Vasko37a0fe62021-02-03 09:53:04 +0100699 FREE_ARRAY(ctx, notif->when, lysc_when_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200700 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100701 LY_LIST_FOR_SAFE(notif->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100702 lysc_node_free_(ctx, child);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200703 }
704}
705
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200706void
Radek Krejci19a96102018-11-15 13:38:09 +0100707lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
708{
709 struct lysc_node *child, *child_next;
710
711 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100712 lysc_node_free_(ctx, child);
Radek Krejci19a96102018-11-15 13:38:09 +0100713 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100714 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
715 lysc_node_free_(ctx, child);
716 }
717 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
718 lysc_node_free_(ctx, child);
719 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100720 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100721 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100722}
723
724static void
725lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
726{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100727 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100728 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100729 if (node->type) {
730 lysc_type_free(ctx, node->type);
731 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100732 lydict_remove(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200733 if (node->dflt) {
734 node->dflt->realtype->plugin->free(ctx, node->dflt);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200735 lysc_type_free(ctx, (struct lysc_type *)node->dflt->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200736 free(node->dflt);
737 }
Radek Krejci19a96102018-11-15 13:38:09 +0100738}
739
Radek Krejci42452ac2018-11-28 17:09:52 +0100740static void
741lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
742{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200743 LY_ARRAY_COUNT_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100744
Radek Krejci9a3823e2021-01-27 20:26:46 +0100745 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci42452ac2018-11-28 17:09:52 +0100746 FREE_ARRAY(ctx, node->musts, lysc_must_free);
747 if (node->type) {
748 lysc_type_free(ctx, node->type);
749 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100750 lydict_remove(ctx, node->units);
Radek Krejci42452ac2018-11-28 17:09:52 +0100751 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200752 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200753 lysc_type_free(ctx, (struct lysc_type *)node->dflts[u]->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200754 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100755 }
756 LY_ARRAY_FREE(node->dflts);
757}
758
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100759static void
760lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
761{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200762 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100763 struct lysc_node *child, *child_next;
764
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100765 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100766 lysc_node_free_(ctx, child);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100767 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100768 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100769 FREE_ARRAY(ctx, node->musts, lysc_must_free);
770
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100771 LY_ARRAY_FOR(node->uniques, u) {
772 LY_ARRAY_FREE(node->uniques[u]);
773 }
774 LY_ARRAY_FREE(node->uniques);
775
Radek Krejci2a9fc652021-01-22 17:44:34 +0100776 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
777 lysc_node_free_(ctx, child);
778 }
779 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
780 lysc_node_free_(ctx, child);
781 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100782}
783
Radek Krejci056d0a82018-12-06 16:57:25 +0100784static void
785lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
786{
787 struct lysc_node *child, *child_next;
788
Radek Krejci9a3823e2021-01-27 20:26:46 +0100789 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200790 LY_LIST_FOR_SAFE((struct lysc_node *)node->cases, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100791 lysc_node_free_(ctx, child);
Michal Vasko20424b42020-08-31 12:29:38 +0200792 }
793}
794
795static void
796lysc_node_case_free(struct ly_ctx *ctx, struct lysc_node_case *node)
797{
798 struct lysc_node *child, *child_next;
799
Radek Krejci9a3823e2021-01-27 20:26:46 +0100800 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200801 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100802 lysc_node_free_(ctx, child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100803 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100804}
Radek Krejci056d0a82018-12-06 16:57:25 +0100805
Radek Krejci9800fb82018-12-13 14:26:23 +0100806static void
807lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
808{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100809 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9800fb82018-12-13 14:26:23 +0100810 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100811}
812
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100813static void
814lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +0100815{
Radek Krejci2a9fc652021-01-22 17:44:34 +0100816 ly_bool inout = 0;
817
Radek Krejci19a96102018-11-15 13:38:09 +0100818 /* common part */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100819 lydict_remove(ctx, node->name);
820 lydict_remove(ctx, node->dsc);
821 lydict_remove(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100822
823 /* nodetype-specific part */
Michal Vaskod989ba02020-08-24 10:59:24 +0200824 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100825 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200826 lysc_node_container_free(ctx, (struct lysc_node_container *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100827 break;
828 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200829 lysc_node_leaf_free(ctx, (struct lysc_node_leaf *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100830 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100831 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200832 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist *)node);
Radek Krejci42452ac2018-11-28 17:09:52 +0100833 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100834 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200835 lysc_node_list_free(ctx, (struct lysc_node_list *)node);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100836 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100837 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200838 lysc_node_choice_free(ctx, (struct lysc_node_choice *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100839 break;
840 case LYS_CASE:
Michal Vasko20424b42020-08-31 12:29:38 +0200841 lysc_node_case_free(ctx, (struct lysc_node_case *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100842 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100843 case LYS_ANYDATA:
844 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200845 lysc_node_anydata_free(ctx, (struct lysc_node_anydata *)node);
Radek Krejci9800fb82018-12-13 14:26:23 +0100846 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100847 case LYS_RPC:
848 case LYS_ACTION:
849 lysc_node_action_free(ctx, (struct lysc_node_action *)node);
850 break;
851 case LYS_INPUT:
852 case LYS_OUTPUT:
853 lysc_node_action_inout_free(ctx, (struct lysc_node_action_inout *)node);
854 inout = 1;
855 break;
856 case LYS_NOTIF:
857 lysc_node_notif_free(ctx, (struct lysc_node_notif *)node);
858 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100859 default:
860 LOGINT(ctx);
861 }
862
Radek Krejci056d0a82018-12-06 16:57:25 +0100863 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100864
865 if (!inout) {
866 free(node);
867 }
Radek Krejci19a96102018-11-15 13:38:09 +0100868}
869
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100870void
871lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node, ly_bool unlink)
872{
873 struct lysc_node *iter, **child_p;
874
Radek Krejci2a9fc652021-01-22 17:44:34 +0100875 if (node->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
876 /* nothing to do - inouts are part of actions and cannot be unlinked/freed separately */
877 return;
878 }
879
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100880 if (unlink) {
881 /* unlink from siblings */
882 if (node->prev->next) {
883 node->prev->next = node->next;
884 }
885 if (node->next) {
886 node->next->prev = node->prev;
887 } else {
888 /* unlinking the last node */
889 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100890 if (node->nodetype == LYS_ACTION) {
891 iter = (struct lysc_node *)lysc_node_actions(node->parent);
892 } else if (node->nodetype == LYS_NOTIF) {
893 iter = (struct lysc_node *)lysc_node_notifs(node->parent);
894 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +0100895 iter = (struct lysc_node *)lysc_node_child(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100896 }
Michal Vasko539c4a62020-11-03 17:21:34 +0100897 LY_CHECK_ERR_RET(!iter, LOGINT(ctx), );
Radek Krejci2a9fc652021-01-22 17:44:34 +0100898 } else if (node->nodetype == LYS_RPC) {
899 iter = (struct lysc_node *)node->module->compiled->rpcs;
900 } else if (node->nodetype == LYS_NOTIF) {
901 iter = (struct lysc_node *)node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100902 } else {
903 iter = node->module->compiled->data;
904 }
905 /* update the "last" pointer from the first node */
906 iter->prev = node->prev;
907 }
908
909 /* unlink from parent */
910 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100911 if (node->nodetype == LYS_ACTION) {
912 child_p = (struct lysc_node **)lysc_node_actions_p(node->parent);
913 } else if (node->nodetype == LYS_NOTIF) {
914 child_p = (struct lysc_node **)lysc_node_notifs_p(node->parent);
915 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +0100916 child_p = lysc_node_child_p(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100917 }
918 } else if (node->nodetype == LYS_RPC) {
919 child_p = (struct lysc_node **)&node->module->compiled->rpcs;
920 } else if (node->nodetype == LYS_NOTIF) {
921 child_p = (struct lysc_node **)&node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100922 } else {
923 child_p = &node->module->compiled->data;
924 }
925 if (child_p && (*child_p == node)) {
926 /* the node is the first child */
927 *child_p = node->next;
928 }
929 }
930
931 lysc_node_free_(ctx, node);
932}
933
Radek Krejci90ed21e2021-04-12 14:47:46 +0200934void
935lysc_module_free(struct lysc_module *module)
Radek Krejci19a96102018-11-15 13:38:09 +0100936{
937 struct ly_ctx *ctx;
938 struct lysc_node *node, *node_next;
939
Radek Krejci90ed21e2021-04-12 14:47:46 +0200940 if (!module) {
941 return;
942 }
943
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100944 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100945
Radek Krejci19a96102018-11-15 13:38:09 +0100946 LY_LIST_FOR_SAFE(module->data, node_next, node) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100947 lysc_node_free_(ctx, node);
Radek Krejci19a96102018-11-15 13:38:09 +0100948 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100949 LY_LIST_FOR_SAFE((struct lysc_node *)module->rpcs, node_next, node) {
950 lysc_node_free_(ctx, node);
951 }
952 LY_LIST_FOR_SAFE((struct lysc_node *)module->notifs, node_next, node) {
953 lysc_node_free_(ctx, node);
954 }
Radek Krejci19a96102018-11-15 13:38:09 +0100955 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
956
957 free(module);
958}
959
960void
Radek Krejci90ed21e2021-04-12 14:47:46 +0200961lys_module_free(struct lys_module *module)
Radek Krejci19a96102018-11-15 13:38:09 +0100962{
963 if (!module) {
964 return;
965 }
966
Radek Krejci90ed21e2021-04-12 14:47:46 +0200967 lysc_module_free(module->compiled);
Radek Krejci80d281e2020-09-14 17:42:54 +0200968 FREE_ARRAY(module->ctx, module->identities, lysc_ident_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100969 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100970
Michal Vasko7f45cf22020-10-01 12:49:44 +0200971 LY_ARRAY_FREE(module->augmented_by);
972 LY_ARRAY_FREE(module->deviated_by);
973
Michal Vaskoe180ed02021-02-05 16:31:20 +0100974 lydict_remove(module->ctx, module->name);
975 lydict_remove(module->ctx, module->revision);
976 lydict_remove(module->ctx, module->ns);
977 lydict_remove(module->ctx, module->prefix);
978 lydict_remove(module->ctx, module->filepath);
979 lydict_remove(module->ctx, module->org);
980 lydict_remove(module->ctx, module->contact);
981 lydict_remove(module->ctx, module->dsc);
982 lydict_remove(module->ctx, module->ref);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100983
Radek Krejci19a96102018-11-15 13:38:09 +0100984 free(module);
985}
Michal Vasko33ff9422020-07-03 09:50:39 +0200986
Radek Krejci38d85362019-09-05 16:26:38 +0200987API void
Radek Krejci0b013302021-03-29 15:22:32 +0200988lyplg_ext_instance_substatements_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
Radek Krejci38d85362019-09-05 16:26:38 +0200989{
Radek Krejci1b2eef82021-02-17 11:17:27 +0100990 LY_ARRAY_COUNT_TYPE u;
991
992 LY_ARRAY_FOR(substmts, u) {
Radek Krejci38d85362019-09-05 16:26:38 +0200993 if (!substmts[u].storage) {
994 continue;
995 }
996
Michal Vaskod989ba02020-08-24 10:59:24 +0200997 switch (substmts[u].stmt) {
Radek Krejci6b88a462021-02-17 12:39:34 +0100998 case LY_STMT_ACTION:
999 case LY_STMT_ANYDATA:
1000 case LY_STMT_ANYXML:
1001 case LY_STMT_CONTAINER:
1002 case LY_STMT_CHOICE:
1003 case LY_STMT_LEAF:
1004 case LY_STMT_LEAF_LIST:
1005 case LY_STMT_LIST:
1006 case LY_STMT_NOTIFICATION:
1007 case LY_STMT_RPC:
1008 case LY_STMT_USES: {
1009 struct lysc_node *child, *child_next;
1010
1011 LY_LIST_FOR_SAFE(*((struct lysc_node **)substmts[u].storage), child_next, child) {
1012 lysc_node_free_(ctx, child);
Radek Krejci38d85362019-09-05 16:26:38 +02001013 }
1014 break;
Radek Krejci6b88a462021-02-17 12:39:34 +01001015 }
1016 case LY_STMT_CONFIG:
1017 case LY_STMT_STATUS:
1018 /* nothing to do */
1019 break;
Radek Krejci1b2eef82021-02-17 11:17:27 +01001020 case LY_STMT_DESCRIPTION:
1021 case LY_STMT_REFERENCE:
Radek Krejci38d85362019-09-05 16:26:38 +02001022 case LY_STMT_UNITS:
1023 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1024 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02001025 const char *str = *((const char **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001026 if (!str) {
1027 break;
1028 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001029 lydict_remove(ctx, str);
Radek Krejci38d85362019-09-05 16:26:38 +02001030 } else {
1031 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +02001032 const char **strs = *((const char ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001033 if (!strs) {
1034 break;
1035 }
1036 FREE_STRINGS(ctx, strs);
1037 }
1038 break;
Radek Krejci38d85362019-09-05 16:26:38 +02001039 case LY_STMT_IF_FEATURE: {
Michal Vasko22df3f02020-08-24 13:29:22 +02001040 struct lysc_iffeature *iff = *((struct lysc_iffeature **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001041 if (!iff) {
1042 break;
1043 }
1044 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1045 /* single item */
1046 lysc_iffeature_free(ctx, iff);
1047 free(iff);
1048 } else {
1049 /* multiple items */
1050 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
1051 }
1052 break;
Radek Krejci6b88a462021-02-17 12:39:34 +01001053 case LY_STMT_TYPE:
1054 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1055 /* single item */
1056 struct lysc_type *type = *((struct lysc_type **)substmts[u].storage);
1057 if (!type) {
1058 break;
1059 }
1060 lysc_type_free(ctx, type);
1061 } else {
1062 /* multiple items */
1063 struct lysc_type **types = *((struct lysc_type ***)substmts[u].storage);
1064 if (!types) {
1065 break;
1066 }
1067 FREE_ARRAY(ctx, types, lysc_type2_free);
1068 }
1069 break;
Radek Krejci38d85362019-09-05 16:26:38 +02001070 }
1071
Radek Krejci0f969882020-08-21 16:56:47 +02001072 /* TODO other statements */
Radek Krejci38d85362019-09-05 16:26:38 +02001073 default:
1074 LOGINT(ctx);
1075 }
1076 }
Radek Krejci1b2eef82021-02-17 11:17:27 +01001077
1078 LY_ARRAY_FREE(substmts);
Radek Krejci38d85362019-09-05 16:26:38 +02001079}
David Sedlákebd3acf2019-07-26 15:04:32 +02001080
1081void
Michal Vaskob36053d2020-03-26 15:49:30 +01001082yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001083{
1084 if (ctx) {
1085 free(ctx);
1086 }
1087}
1088
1089void
Michal Vaskob36053d2020-03-26 15:49:30 +01001090yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001091{
1092 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001093 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +02001094 free(ctx);
1095 }
1096}