blob: 72357c9aed2dfb4139e882748409adea0550fcc9 [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
Radek Krejcie7b95092019-05-15 11:03:07 +02002 * @file tree_schema_free.c
Radek Krejci19a96102018-11-15 13:38:09 +01003 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejcie7b95092019-05-15 11:03:07 +02004 * @brief Freeing functions for schema tree structures.
Radek Krejci19a96102018-11-15 13:38:09 +01005 *
Radek Krejcie7b95092019-05-15 11:03:07 +02006 * Copyright (c) 2019 CESNET, z.s.p.o.
Radek Krejci19a96102018-11-15 13:38:09 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejcie7b95092019-05-15 11:03:07 +020015#include <stdlib.h>
16
Radek Krejci535ea9f2020-05-29 16:01:05 +020017#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020018#include "compat.h"
Radek Krejci47fab892020-11-05 17:02:41 +010019#include "log.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include "plugins_exts.h"
21#include "plugins_types.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010025#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "xml.h"
Radek Krejci19a96102018-11-15 13:38:09 +010027#include "xpath.h"
28
Radek Krejci2a9fc652021-01-22 17:44:34 +010029void lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp);
Michal Vasko5fe75f12020-03-02 13:52:37 +010030void lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010031static void lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node);
Radek Krejci19a96102018-11-15 13:38:09 +010032
33static void
34lysp_stmt_free(struct ly_ctx *ctx, struct lysp_stmt *stmt)
35{
36 struct lysp_stmt *child, *next;
37
38 FREE_STRING(ctx, stmt->stmt);
39 FREE_STRING(ctx, stmt->arg);
40
41 LY_LIST_FOR_SAFE(stmt->child, next, child) {
42 lysp_stmt_free(ctx, child);
43 }
44
45 free(stmt);
46}
47
Radek Krejci2d7a47b2019-05-16 13:34:10 +020048void
Radek Krejci19a96102018-11-15 13:38:09 +010049lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext)
50{
51 struct lysp_stmt *stmt, *next;
52
53 FREE_STRING(ctx, ext->name);
54 FREE_STRING(ctx, ext->argument);
55
56 LY_LIST_FOR_SAFE(ext->child, next, stmt) {
57 lysp_stmt_free(ctx, stmt);
58 }
59}
60
David Sedlák298ff6d2019-07-26 14:29:03 +020061void
Radek Krejci19a96102018-11-15 13:38:09 +010062lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import)
63{
64 /* imported module is freed directly from the context's list */
65 FREE_STRING(ctx, import->name);
66 FREE_STRING(ctx, import->prefix);
67 FREE_STRING(ctx, import->dsc);
68 FREE_STRING(ctx, import->ref);
69 FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free);
70}
71
Radek Krejci771928a2021-01-19 13:42:36 +010072/**
73 * @brief Common function to erase include record in main module and submodule.
74 *
75 * There is a difference since the main module is expected to have the complete list if the included submodules and
76 * the parsed submodule is shared with any include in a submodule. Therefore, the referenced submodules in the include
77 * record are freed only from main module's records.
78 *
79 * @param[in] ctx libyang context
80 * @param[in] include The include record to be erased, the record itself is not freed.
81 * @param[in] main_module Flag to get know if the include record is placed in main module so also the referenced submodule
82 * is supposed to be freed.
83 */
84static void
85lysp_include_free_(struct ly_ctx *ctx, struct lysp_include *include, ly_bool main_module)
Radek Krejci19a96102018-11-15 13:38:09 +010086{
Radek Krejci771928a2021-01-19 13:42:36 +010087 if (main_module && include->submodule) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +020088 lysp_module_free((struct lysp_module *)include->submodule);
Radek Krejci19a96102018-11-15 13:38:09 +010089 }
90 FREE_STRING(ctx, include->name);
91 FREE_STRING(ctx, include->dsc);
92 FREE_STRING(ctx, include->ref);
93 FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free);
94}
95
David Sedlákaa854b02019-07-22 14:17:10 +020096void
Radek Krejci771928a2021-01-19 13:42:36 +010097lysp_include_free_submodule(struct ly_ctx *ctx, struct lysp_include *include)
98{
99 return lysp_include_free_(ctx, include, 0);
100}
101
102void
103lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include)
104{
105 return lysp_include_free_(ctx, include, 1);
106}
107
108void
Radek Krejci19a96102018-11-15 13:38:09 +0100109lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev)
110{
111 FREE_STRING(ctx, rev->dsc);
112 FREE_STRING(ctx, rev->ref);
113 FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free);
114}
115
David Sedlák986cb412019-07-04 13:10:11 +0200116void
Radek Krejci19a96102018-11-15 13:38:09 +0100117lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext)
118{
119 FREE_STRING(ctx, ext->name);
120 FREE_STRING(ctx, ext->argument);
121 FREE_STRING(ctx, ext->dsc);
122 FREE_STRING(ctx, ext->ref);
123 FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free);
Michal Vasko5fe75f12020-03-02 13:52:37 +0100124 if (ext->compiled) {
125 lysc_extension_free(ctx, &ext->compiled);
126 }
Radek Krejci19a96102018-11-15 13:38:09 +0100127}
128
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200129void
Radek Krejci19a96102018-11-15 13:38:09 +0100130lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat)
131{
132 FREE_STRING(ctx, feat->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200133 FREE_ARRAY(ctx, feat->iffeatures, lysp_qname_free);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100134 FREE_ARRAY(ctx, feat->iffeatures_c, lysc_iffeature_free);
135 LY_ARRAY_FREE(feat->depfeatures);
Radek Krejci19a96102018-11-15 13:38:09 +0100136 FREE_STRING(ctx, feat->dsc);
137 FREE_STRING(ctx, feat->ref);
138 FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
139}
140
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200141void
Radek Krejci19a96102018-11-15 13:38:09 +0100142lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident)
143{
144 FREE_STRING(ctx, ident->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200145 FREE_ARRAY(ctx, ident->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100146 FREE_STRINGS(ctx, ident->bases);
147 FREE_STRING(ctx, ident->dsc);
148 FREE_STRING(ctx, ident->ref);
149 FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
150}
151
Michal Vasko7f45cf22020-10-01 12:49:44 +0200152void
Radek Krejci19a96102018-11-15 13:38:09 +0100153lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
154{
Michal Vasko7f45cf22020-10-01 12:49:44 +0200155 FREE_STRING(ctx, restr->arg.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100156 FREE_STRING(ctx, restr->emsg);
157 FREE_STRING(ctx, restr->eapptag);
158 FREE_STRING(ctx, restr->dsc);
159 FREE_STRING(ctx, restr->ref);
160 FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
161}
162
163static void
164lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item)
165{
166 FREE_STRING(ctx, item->name);
167 FREE_STRING(ctx, item->dsc);
168 FREE_STRING(ctx, item->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200169 FREE_ARRAY(ctx, item->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100170 FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
171}
172
Radek Krejcicdfecd92018-11-26 11:27:32 +0100173void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type);
Michal Vasko004d3152020-06-11 19:59:22 +0200174
David Sedlák32488102019-07-15 17:44:10 +0200175void
Radek Krejci19a96102018-11-15 13:38:09 +0100176lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type)
177{
178 FREE_STRING(ctx, type->name);
179 FREE_MEMBER(ctx, type->range, lysp_restr_free);
180 FREE_MEMBER(ctx, type->length, lysp_restr_free);
181 FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
182 FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
183 FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
Michal Vasko004d3152020-06-11 19:59:22 +0200184 lyxp_expr_free(ctx, type->path);
Radek Krejci19a96102018-11-15 13:38:09 +0100185 FREE_STRINGS(ctx, type->bases);
186 FREE_ARRAY(ctx, type->types, lysp_type_free);
187 FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
188 if (type->compiled) {
189 lysc_type_free(ctx, type->compiled);
190 }
191}
192
David Sedlák04e17b22019-07-19 15:29:48 +0200193void
Radek Krejci19a96102018-11-15 13:38:09 +0100194lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
195{
196 FREE_STRING(ctx, tpdf->name);
197 FREE_STRING(ctx, tpdf->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200198 FREE_STRING(ctx, tpdf->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100199 FREE_STRING(ctx, tpdf->dsc);
200 FREE_STRING(ctx, tpdf->ref);
201 FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
202
203 lysp_type_free(ctx, &tpdf->type);
204
205}
206
Radek Krejcif538ce52019-03-05 10:46:14 +0100207void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100208lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp)
Radek Krejci19a96102018-11-15 13:38:09 +0100209{
210 struct lysp_node *node, *next;
211
Radek Krejci19a96102018-11-15 13:38:09 +0100212 FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100213 LY_LIST_FOR_SAFE((struct lysp_node *)grp->groupings, next, node) {
214 lysp_node_free(ctx, node);
215 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100216 LY_LIST_FOR_SAFE(grp->child, next, node) {
Radek Krejci19a96102018-11-15 13:38:09 +0100217 lysp_node_free(ctx, node);
218 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100219 LY_LIST_FOR_SAFE((struct lysp_node *)grp->actions, next, node) {
220 lysp_node_free(ctx, node);
221 }
222 LY_LIST_FOR_SAFE((struct lysp_node *)grp->notifs, next, node) {
223 lysp_node_free(ctx, node);
224 }
Radek Krejci19a96102018-11-15 13:38:09 +0100225}
226
Radek Krejcif09e4e82019-06-14 15:08:11 +0200227void
Radek Krejci19a96102018-11-15 13:38:09 +0100228lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
229{
230 FREE_STRING(ctx, when->cond);
231 FREE_STRING(ctx, when->dsc);
232 FREE_STRING(ctx, when->ref);
233 FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
234}
235
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200236void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100237lysp_augment_free(struct ly_ctx *ctx, struct lysp_node_augment *augment)
Radek Krejci19a96102018-11-15 13:38:09 +0100238{
239 struct lysp_node *node, *next;
240
Radek Krejci19a96102018-11-15 13:38:09 +0100241 LY_LIST_FOR_SAFE(augment->child, next, node) {
242 lysp_node_free(ctx, node);
243 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100244 LY_LIST_FOR_SAFE((struct lysp_node *)augment->actions, next, node) {
245 lysp_node_free(ctx, node);
246 }
247 LY_LIST_FOR_SAFE((struct lysp_node *)augment->notifs, next, node) {
248 lysp_node_free(ctx, node);
249 }
Radek Krejci19a96102018-11-15 13:38:09 +0100250}
251
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200252void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200253lysp_qname_free(struct ly_ctx *ctx, struct lysp_qname *qname)
254{
255 if (qname) {
256 FREE_STRING(ctx, qname->str);
257 }
258}
259
260void
Radek Krejci19a96102018-11-15 13:38:09 +0100261lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
262{
Michal Vasko22df3f02020-08-24 13:29:22 +0200263 struct lysp_deviate_add *add = (struct lysp_deviate_add *)d;
264 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl *)d;
Radek Krejci19a96102018-11-15 13:38:09 +0100265
266 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
Michal Vaskod989ba02020-08-24 10:59:24 +0200267 switch (d->mod) {
Radek Krejci19a96102018-11-15 13:38:09 +0100268 case LYS_DEV_NOT_SUPPORTED:
269 /* nothing to do */
270 break;
271 case LYS_DEV_ADD:
272 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
273 FREE_STRING(ctx, add->units);
274 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200275 FREE_ARRAY(ctx, add->uniques, lysp_qname_free);
276 FREE_ARRAY(ctx, add->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100277 break;
278 case LYS_DEV_REPLACE:
279 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
280 FREE_STRING(ctx, rpl->units);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200281 lysp_qname_free(ctx, &rpl->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +0100282 break;
283 default:
284 LOGINT(ctx);
285 break;
286 }
287}
288
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200289void
Radek Krejci19a96102018-11-15 13:38:09 +0100290lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
291{
292 struct lysp_deviate *next, *iter;
293
294 FREE_STRING(ctx, dev->nodeid);
295 FREE_STRING(ctx, dev->dsc);
296 FREE_STRING(ctx, dev->ref);
297 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
298 lysp_deviate_free(ctx, iter);
299 free(iter);
300 }
301 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
302}
303
David Sedlákd2d676a2019-07-22 11:28:19 +0200304void
Radek Krejci19a96102018-11-15 13:38:09 +0100305lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
306{
307 FREE_STRING(ctx, ref->nodeid);
308 FREE_STRING(ctx, ref->dsc);
309 FREE_STRING(ctx, ref->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200310 FREE_ARRAY(ctx, ref->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100311 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
312 FREE_STRING(ctx, ref->presence);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200313 FREE_ARRAY(ctx, ref->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100314 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
315}
316
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200317void
Radek Krejci19a96102018-11-15 13:38:09 +0100318lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
319{
320 struct lysp_node *child, *next;
Radek Krejci9a3823e2021-01-27 20:26:46 +0100321 struct lysp_restr *musts = lysp_node_musts(node);
322 struct lysp_when *when = lysp_node_when(node);
Radek Krejci19a96102018-11-15 13:38:09 +0100323
324 FREE_STRING(ctx, node->name);
325 FREE_STRING(ctx, node->dsc);
326 FREE_STRING(ctx, node->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200327 FREE_ARRAY(ctx, node->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100328 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
329
Radek Krejci9a3823e2021-01-27 20:26:46 +0100330 FREE_MEMBER(ctx, when, lysp_when_free);
331 FREE_ARRAY(ctx, musts, lysp_restr_free);
332
Michal Vaskod989ba02020-08-24 10:59:24 +0200333 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100334 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200335 FREE_STRING(ctx, ((struct lysp_node_container *)node)->presence);
336 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100337 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100338 lysp_node_free(ctx, child);
339 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200340 LY_LIST_FOR_SAFE(((struct lysp_node_container *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100341 lysp_node_free(ctx, child);
342 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100343 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->actions->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100344 lysp_node_free(ctx, child);
345 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100346 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->notifs->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100347 lysp_node_free(ctx, child);
348 }
Radek Krejci19a96102018-11-15 13:38:09 +0100349 break;
350 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200351 lysp_type_free(ctx, &((struct lysp_node_leaf *)node)->type);
352 FREE_STRING(ctx, ((struct lysp_node_leaf *)node)->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200353 FREE_STRING(ctx, ((struct lysp_node_leaf *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100354 break;
355 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200356 lysp_type_free(ctx, &((struct lysp_node_leaflist *)node)->type);
357 FREE_STRING(ctx, ((struct lysp_node_leaflist *)node)->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200358 FREE_ARRAY(ctx, ((struct lysp_node_leaflist *)node)->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100359 break;
360 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200361 FREE_STRING(ctx, ((struct lysp_node_list *)node)->key);
362 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100363 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100364 lysp_node_free(ctx, child);
365 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200366 LY_LIST_FOR_SAFE(((struct lysp_node_list *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100367 lysp_node_free(ctx, child);
368 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100369 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->actions->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100370 lysp_node_free(ctx, child);
371 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100372 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->notifs->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100373 lysp_node_free(ctx, child);
374 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200375 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->uniques, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100376 break;
377 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200378 LY_LIST_FOR_SAFE(((struct lysp_node_choice *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100379 lysp_node_free(ctx, child);
380 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200381 FREE_STRING(ctx, ((struct lysp_node_choice *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100382 break;
383 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200384 LY_LIST_FOR_SAFE(((struct lysp_node_case *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100385 lysp_node_free(ctx, child);
386 }
387 break;
388 case LYS_ANYDATA:
389 case LYS_ANYXML:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100390 /* nothing special to do */
Radek Krejci19a96102018-11-15 13:38:09 +0100391 break;
392 case LYS_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +0200393 FREE_ARRAY(ctx, ((struct lysp_node_uses *)node)->refines, lysp_refine_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100394 LY_LIST_FOR_SAFE(&((struct lysp_node_uses *)node)->augments->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100395 lysp_node_free(ctx, child);
396 }
397 break;
398 case LYS_RPC:
399 case LYS_ACTION:
400 FREE_ARRAY(ctx, ((struct lysp_node_action *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100401 LY_LIST_FOR_SAFE(&((struct lysp_node_action *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100402 lysp_node_free(ctx, child);
403 }
404 if (((struct lysp_node_action *)node)->input.nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100405 lysp_node_free(ctx, &((struct lysp_node_action *)node)->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100406 }
407 if (((struct lysp_node_action *)node)->output.nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100408 lysp_node_free(ctx, &((struct lysp_node_action *)node)->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100409 }
410 break;
411 case LYS_INPUT:
412 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100413 FREE_ARRAY(ctx, ((struct lysp_node_action_inout *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100414 LY_LIST_FOR_SAFE(&((struct lysp_node_action_inout *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100415 lysp_node_free(ctx, child);
416 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100417 LY_LIST_FOR_SAFE(((struct lysp_node_action_inout *)node)->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100418 lysp_node_free(ctx, child);
419 }
420 /* do not free the node, it is never standalone but part of the action node */
421 return;
422 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100423 FREE_ARRAY(ctx, ((struct lysp_node_notif *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100424 LY_LIST_FOR_SAFE(&((struct lysp_node_notif *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100425 lysp_node_free(ctx, child);
426 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100427 LY_LIST_FOR_SAFE(((struct lysp_node_notif *)node)->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100428 lysp_node_free(ctx, child);
429 }
430 break;
431 case LYS_GROUPING:
432 lysp_grp_free(ctx, (struct lysp_node_grp *)node);
433 break;
434 case LYS_AUGMENT:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100435 lysp_augment_free(ctx, ((struct lysp_node_augment *)node));
Radek Krejci19a96102018-11-15 13:38:09 +0100436 break;
437 default:
438 LOGINT(ctx);
439 }
440
441 free(node);
442}
443
Radek Krejci15f10ab2020-11-03 14:14:14 +0100444void
Radek Krejci19a96102018-11-15 13:38:09 +0100445lysp_module_free(struct lysp_module *module)
446{
447 struct ly_ctx *ctx;
448 struct lysp_node *node, *next;
449
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100450 if (!module) {
451 return;
452 }
453 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100454
455 FREE_ARRAY(ctx, module->imports, lysp_import_free);
Radek Krejci771928a2021-01-19 13:42:36 +0100456 FREE_ARRAY(ctx, module->includes, module->is_submod ? lysp_include_free_submodule : lysp_include_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100457
Radek Krejci19a96102018-11-15 13:38:09 +0100458 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
459 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
460 FREE_ARRAY(ctx, module->features, lysp_feature_free);
461 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
462 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100463 LY_LIST_FOR_SAFE((struct lysp_node *)module->groupings, next, node) {
464 lysp_node_free(ctx, node);
465 }
Radek Krejci19a96102018-11-15 13:38:09 +0100466 LY_LIST_FOR_SAFE(module->data, next, node) {
467 lysp_node_free(ctx, node);
468 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100469 LY_LIST_FOR_SAFE((struct lysp_node *)module->augments, next, node) {
470 lysp_node_free(ctx, node);
471 }
472 LY_LIST_FOR_SAFE((struct lysp_node *)module->rpcs, next, node) {
473 lysp_node_free(ctx, node);
474 }
475 LY_LIST_FOR_SAFE((struct lysp_node *)module->notifs, next, node) {
476 lysp_node_free(ctx, node);
477 }
Radek Krejci19a96102018-11-15 13:38:09 +0100478 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
479 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
480
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200481 if (module->is_submod) {
482 struct lysp_submodule *submod = (struct lysp_submodule *)module;
483
484 FREE_STRING(ctx, submod->name);
485 FREE_STRING(ctx, submod->filepath);
486 FREE_STRING(ctx, submod->prefix);
487 FREE_STRING(ctx, submod->org);
488 FREE_STRING(ctx, submod->contact);
489 FREE_STRING(ctx, submod->dsc);
490 FREE_STRING(ctx, submod->ref);
491 }
492
Radek Krejci19a96102018-11-15 13:38:09 +0100493 free(module);
494}
495
Radek Krejci0af46292019-01-11 16:02:31 +0100496void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100497lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
498{
499 if (--(*ext)->refcount) {
500 return;
501 }
502 FREE_STRING(ctx, (*ext)->name);
503 FREE_STRING(ctx, (*ext)->argument);
504 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
505 free(*ext);
506}
507
508void
Radek Krejci19a96102018-11-15 13:38:09 +0100509lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
510{
fredganebc50572019-10-31 15:39:23 +0800511 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200512 ext->def->plugin->free(ctx, ext);
513 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100514 if (ext->def) {
515 lysc_extension_free(ctx, &ext->def);
516 }
Radek Krejci0935f412019-08-20 16:15:18 +0200517 FREE_STRING(ctx, ext->argument);
518 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
519}
520
521void
Radek Krejci19a96102018-11-15 13:38:09 +0100522lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
523{
524 LY_ARRAY_FREE(iff->features);
525 free(iff->expr);
526}
527
528static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100529lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100530{
Radek Krejci00b874b2019-02-12 10:54:50 +0100531 if (--(*w)->refcount) {
532 return;
533 }
534 lyxp_expr_free(ctx, (*w)->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200535 lysc_prefixes_free((*w)->prefixes);
Radek Krejci00b874b2019-02-12 10:54:50 +0100536 FREE_STRING(ctx, (*w)->dsc);
537 FREE_STRING(ctx, (*w)->ref);
538 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
539 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100540}
541
Radek Krejciccd20f12019-02-15 14:12:27 +0100542void
Radek Krejci58d171e2018-11-23 13:50:55 +0100543lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
544{
545 lyxp_expr_free(ctx, must->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200546 lysc_prefixes_free(must->prefixes);
Radek Krejci58d171e2018-11-23 13:50:55 +0100547 FREE_STRING(ctx, must->emsg);
548 FREE_STRING(ctx, must->eapptag);
Radek Krejcic8b31002019-01-08 10:24:45 +0100549 FREE_STRING(ctx, must->dsc);
550 FREE_STRING(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100551 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
552}
553
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100554void
Radek Krejci19a96102018-11-15 13:38:09 +0100555lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
556{
557 FREE_STRING(ctx, ident->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100558 FREE_STRING(ctx, ident->dsc);
559 FREE_STRING(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100560 LY_ARRAY_FREE(ident->derived);
561 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
562}
563
Radek Krejci19a96102018-11-15 13:38:09 +0100564static void
565lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
566{
567 LY_ARRAY_FREE(range->parts);
568 FREE_STRING(ctx, range->eapptag);
569 FREE_STRING(ctx, range->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100570 FREE_STRING(ctx, range->dsc);
571 FREE_STRING(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100572 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
573}
574
575static void
576lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
577{
578 if (--(*pattern)->refcount) {
579 return;
580 }
Radek Krejci54579462019-04-30 12:47:06 +0200581 pcre2_code_free((*pattern)->code);
582 FREE_STRING(ctx, (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +0100583 FREE_STRING(ctx, (*pattern)->eapptag);
584 FREE_STRING(ctx, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100585 FREE_STRING(ctx, (*pattern)->dsc);
586 FREE_STRING(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100587 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
588 free(*pattern);
589}
590
591static void
Radek Krejci693262f2019-04-29 15:23:20 +0200592lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100593{
594 FREE_STRING(ctx, item->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100595 FREE_STRING(ctx, item->dsc);
596 FREE_STRING(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100597 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
598}
599
Radek Krejcia3045382018-11-22 14:30:31 +0100600static void
601lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
602{
603 lysc_type_free(ctx, *type);
604}
Radek Krejci0f969882020-08-21 16:56:47 +0200605
Radek Krejcicdfecd92018-11-26 11:27:32 +0100606void
Radek Krejci19a96102018-11-15 13:38:09 +0100607lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
608{
609 if (--type->refcount) {
610 return;
611 }
Radek Krejcib915ac92020-08-14 23:31:04 +0200612
Michal Vaskod989ba02020-08-24 10:59:24 +0200613 switch (type->basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100614 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +0200615 FREE_MEMBER(ctx, ((struct lysc_type_bin *)type)->length, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100616 break;
617 case LY_TYPE_BITS:
Michal Vasko22df3f02020-08-24 13:29:22 +0200618 FREE_ARRAY(ctx, (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)type)->bits, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100619 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100620 case LY_TYPE_DEC64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200621 FREE_MEMBER(ctx, ((struct lysc_type_dec *)type)->range, lysc_range_free);
Radek Krejci6cba4292018-11-15 17:33:29 +0100622 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100623 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +0200624 FREE_MEMBER(ctx, ((struct lysc_type_str *)type)->length, lysc_range_free);
625 FREE_ARRAY(ctx, ((struct lysc_type_str *)type)->patterns, lysc_pattern_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100626 break;
627 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +0200628 FREE_ARRAY(ctx, ((struct lysc_type_enum *)type)->enums, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100629 break;
630 case LY_TYPE_INT8:
631 case LY_TYPE_UINT8:
632 case LY_TYPE_INT16:
633 case LY_TYPE_UINT16:
634 case LY_TYPE_INT32:
635 case LY_TYPE_UINT32:
636 case LY_TYPE_INT64:
637 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200638 FREE_MEMBER(ctx, ((struct lysc_type_num *)type)->range, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100639 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100640 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +0200641 LY_ARRAY_FREE(((struct lysc_type_identityref *)type)->bases);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100642 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100643 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +0200644 FREE_ARRAY(ctx, ((struct lysc_type_union *)type)->types, lysc_type2_free);
Radek Krejcia3045382018-11-22 14:30:31 +0100645 break;
646 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200647 lyxp_expr_free(ctx, ((struct lysc_type_leafref *)type)->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200648 lysc_prefixes_free(((struct lysc_type_leafref *)type)->prefixes);
Radek Krejcia3045382018-11-22 14:30:31 +0100649 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100650 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100651 case LY_TYPE_BOOL:
652 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100653 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100654 /* nothing to do */
655 break;
656 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200657
658 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100659 free(type);
660}
661
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100662void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100663lysc_node_action_inout_free(struct ly_ctx *ctx, struct lysc_node_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100664{
665 struct lysc_node *child, *child_next;
666
Radek Krejcif538ce52019-03-05 10:46:14 +0100667 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100668 LY_LIST_FOR_SAFE(inout->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100669 lysc_node_free_(ctx, child);
Radek Krejcif538ce52019-03-05 10:46:14 +0100670 }
671}
672
673void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100674lysc_node_action_free(struct ly_ctx *ctx, struct lysc_node_action *action)
Radek Krejcif538ce52019-03-05 10:46:14 +0100675{
Radek Krejci2a9fc652021-01-22 17:44:34 +0100676 if (action->input.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100677 lysc_node_free_(ctx, &action->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100678 }
679 if (action->output.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100680 lysc_node_free_(ctx, &action->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100681 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100682}
683
Radek Krejcifc11bd72019-04-11 16:00:05 +0200684void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100685lysc_node_notif_free(struct ly_ctx *ctx, struct lysc_node_notif *notif)
Radek Krejcifc11bd72019-04-11 16:00:05 +0200686{
687 struct lysc_node *child, *child_next;
688
Radek Krejcifc11bd72019-04-11 16:00:05 +0200689 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100690 LY_LIST_FOR_SAFE(notif->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100691 lysc_node_free_(ctx, child);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200692 }
693}
694
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200695void
Radek Krejci19a96102018-11-15 13:38:09 +0100696lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
697{
698 struct lysc_node *child, *child_next;
699
700 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100701 lysc_node_free_(ctx, child);
Radek Krejci19a96102018-11-15 13:38:09 +0100702 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100703 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
704 lysc_node_free_(ctx, child);
705 }
706 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
707 lysc_node_free_(ctx, child);
708 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100709 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100710 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100711}
712
713static void
714lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
715{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100716 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100717 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100718 if (node->type) {
719 lysc_type_free(ctx, node->type);
720 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100721 FREE_STRING(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200722 if (node->dflt) {
723 node->dflt->realtype->plugin->free(ctx, node->dflt);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200724 lysc_type_free(ctx, (struct lysc_type *)node->dflt->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200725 free(node->dflt);
726 }
Radek Krejci19a96102018-11-15 13:38:09 +0100727}
728
Radek Krejci42452ac2018-11-28 17:09:52 +0100729static void
730lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
731{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200732 LY_ARRAY_COUNT_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100733
Radek Krejci9a3823e2021-01-27 20:26:46 +0100734 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci42452ac2018-11-28 17:09:52 +0100735 FREE_ARRAY(ctx, node->musts, lysc_must_free);
736 if (node->type) {
737 lysc_type_free(ctx, node->type);
738 }
739 FREE_STRING(ctx, node->units);
740 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200741 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200742 lysc_type_free(ctx, (struct lysc_type *)node->dflts[u]->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200743 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100744 }
745 LY_ARRAY_FREE(node->dflts);
746}
747
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100748static void
749lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
750{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200751 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100752 struct lysc_node *child, *child_next;
753
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100754 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100755 lysc_node_free_(ctx, child);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100756 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100757 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100758 FREE_ARRAY(ctx, node->musts, lysc_must_free);
759
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100760 LY_ARRAY_FOR(node->uniques, u) {
761 LY_ARRAY_FREE(node->uniques[u]);
762 }
763 LY_ARRAY_FREE(node->uniques);
764
Radek Krejci2a9fc652021-01-22 17:44:34 +0100765 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
766 lysc_node_free_(ctx, child);
767 }
768 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
769 lysc_node_free_(ctx, child);
770 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100771}
772
Radek Krejci056d0a82018-12-06 16:57:25 +0100773static void
774lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
775{
776 struct lysc_node *child, *child_next;
777
Radek Krejci9a3823e2021-01-27 20:26:46 +0100778 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200779 LY_LIST_FOR_SAFE((struct lysc_node *)node->cases, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100780 lysc_node_free_(ctx, child);
Michal Vasko20424b42020-08-31 12:29:38 +0200781 }
782}
783
784static void
785lysc_node_case_free(struct ly_ctx *ctx, struct lysc_node_case *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(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100791 lysc_node_free_(ctx, child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100792 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100793}
Radek Krejci056d0a82018-12-06 16:57:25 +0100794
Radek Krejci9800fb82018-12-13 14:26:23 +0100795static void
796lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
797{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100798 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9800fb82018-12-13 14:26:23 +0100799 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100800}
801
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100802static void
803lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +0100804{
Radek Krejci2a9fc652021-01-22 17:44:34 +0100805 ly_bool inout = 0;
806
Radek Krejci19a96102018-11-15 13:38:09 +0100807 /* common part */
808 FREE_STRING(ctx, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +0100809 FREE_STRING(ctx, node->dsc);
810 FREE_STRING(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100811
812 /* nodetype-specific part */
Michal Vaskod989ba02020-08-24 10:59:24 +0200813 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100814 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200815 lysc_node_container_free(ctx, (struct lysc_node_container *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100816 break;
817 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200818 lysc_node_leaf_free(ctx, (struct lysc_node_leaf *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100819 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100820 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200821 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist *)node);
Radek Krejci42452ac2018-11-28 17:09:52 +0100822 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100823 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200824 lysc_node_list_free(ctx, (struct lysc_node_list *)node);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100825 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100826 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200827 lysc_node_choice_free(ctx, (struct lysc_node_choice *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100828 break;
829 case LYS_CASE:
Michal Vasko20424b42020-08-31 12:29:38 +0200830 lysc_node_case_free(ctx, (struct lysc_node_case *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100831 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100832 case LYS_ANYDATA:
833 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200834 lysc_node_anydata_free(ctx, (struct lysc_node_anydata *)node);
Radek Krejci9800fb82018-12-13 14:26:23 +0100835 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100836 case LYS_RPC:
837 case LYS_ACTION:
838 lysc_node_action_free(ctx, (struct lysc_node_action *)node);
839 break;
840 case LYS_INPUT:
841 case LYS_OUTPUT:
842 lysc_node_action_inout_free(ctx, (struct lysc_node_action_inout *)node);
843 inout = 1;
844 break;
845 case LYS_NOTIF:
846 lysc_node_notif_free(ctx, (struct lysc_node_notif *)node);
847 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100848 default:
849 LOGINT(ctx);
850 }
851
Radek Krejci056d0a82018-12-06 16:57:25 +0100852 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100853
854 if (!inout) {
855 free(node);
856 }
Radek Krejci19a96102018-11-15 13:38:09 +0100857}
858
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100859void
860lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node, ly_bool unlink)
861{
862 struct lysc_node *iter, **child_p;
863
Radek Krejci2a9fc652021-01-22 17:44:34 +0100864 if (node->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
865 /* nothing to do - inouts are part of actions and cannot be unlinked/freed separately */
866 return;
867 }
868
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100869 if (unlink) {
870 /* unlink from siblings */
871 if (node->prev->next) {
872 node->prev->next = node->next;
873 }
874 if (node->next) {
875 node->next->prev = node->prev;
876 } else {
877 /* unlinking the last node */
878 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100879 if (node->nodetype == LYS_ACTION) {
880 iter = (struct lysc_node *)lysc_node_actions(node->parent);
881 } else if (node->nodetype == LYS_NOTIF) {
882 iter = (struct lysc_node *)lysc_node_notifs(node->parent);
883 } else {
Michal Vaskod1e53b92021-01-28 13:11:06 +0100884 iter = (struct lysc_node *)lysc_node_children(node->parent, node->flags & LYS_IS_OUTPUT);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100885 }
Michal Vasko539c4a62020-11-03 17:21:34 +0100886 LY_CHECK_ERR_RET(!iter, LOGINT(ctx), );
Radek Krejci2a9fc652021-01-22 17:44:34 +0100887 } else if (node->nodetype == LYS_RPC) {
888 iter = (struct lysc_node *)node->module->compiled->rpcs;
889 } else if (node->nodetype == LYS_NOTIF) {
890 iter = (struct lysc_node *)node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100891 } else {
892 iter = node->module->compiled->data;
893 }
894 /* update the "last" pointer from the first node */
895 iter->prev = node->prev;
896 }
897
898 /* unlink from parent */
899 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100900 if (node->nodetype == LYS_ACTION) {
901 child_p = (struct lysc_node **)lysc_node_actions_p(node->parent);
902 } else if (node->nodetype == LYS_NOTIF) {
903 child_p = (struct lysc_node **)lysc_node_notifs_p(node->parent);
904 } else {
Michal Vaskod1e53b92021-01-28 13:11:06 +0100905 child_p = lysc_node_children_p(node->parent, node->flags & LYS_IS_OUTPUT);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100906 }
907 } else if (node->nodetype == LYS_RPC) {
908 child_p = (struct lysc_node **)&node->module->compiled->rpcs;
909 } else if (node->nodetype == LYS_NOTIF) {
910 child_p = (struct lysc_node **)&node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100911 } else {
912 child_p = &node->module->compiled->data;
913 }
914 if (child_p && (*child_p == node)) {
915 /* the node is the first child */
916 *child_p = node->next;
917 }
918 }
919
920 lysc_node_free_(ctx, node);
921}
922
Radek Krejci19a96102018-11-15 13:38:09 +0100923static void
924lysc_module_free_(struct lysc_module *module)
925{
926 struct ly_ctx *ctx;
927 struct lysc_node *node, *node_next;
928
Michal Vaskod989ba02020-08-24 10:59:24 +0200929 LY_CHECK_ARG_RET(NULL, module, );
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100930 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100931
Radek Krejci19a96102018-11-15 13:38:09 +0100932 LY_LIST_FOR_SAFE(module->data, node_next, node) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100933 lysc_node_free_(ctx, node);
Radek Krejci19a96102018-11-15 13:38:09 +0100934 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100935 LY_LIST_FOR_SAFE((struct lysc_node *)module->rpcs, node_next, node) {
936 lysc_node_free_(ctx, node);
937 }
938 LY_LIST_FOR_SAFE((struct lysc_node *)module->notifs, node_next, node) {
939 lysc_node_free_(ctx, node);
940 }
Radek Krejci19a96102018-11-15 13:38:09 +0100941 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
942
943 free(module);
944}
945
946void
947lysc_module_free(struct lysc_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
948{
Radek Krejci9b042892019-02-13 14:28:44 +0100949 /* TODO use the destructor, this just suppress warning about unused parameter */
950 (void) private_destructor;
951
Radek Krejci19a96102018-11-15 13:38:09 +0100952 if (module) {
953 lysc_module_free_(module);
954 }
955}
956
957void
958lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
959{
960 if (!module) {
961 return;
962 }
963
964 lysc_module_free(module->compiled, private_destructor);
Radek Krejci80d281e2020-09-14 17:42:54 +0200965 FREE_ARRAY(module->ctx, module->identities, lysc_ident_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100966 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100967
Michal Vasko7f45cf22020-10-01 12:49:44 +0200968 LY_ARRAY_FREE(module->augmented_by);
969 LY_ARRAY_FREE(module->deviated_by);
970
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100971 FREE_STRING(module->ctx, module->name);
Radek Krejci0af46292019-01-11 16:02:31 +0100972 FREE_STRING(module->ctx, module->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100973 FREE_STRING(module->ctx, module->ns);
974 FREE_STRING(module->ctx, module->prefix);
975 FREE_STRING(module->ctx, module->filepath);
976 FREE_STRING(module->ctx, module->org);
977 FREE_STRING(module->ctx, module->contact);
978 FREE_STRING(module->ctx, module->dsc);
979 FREE_STRING(module->ctx, module->ref);
980
Radek Krejci19a96102018-11-15 13:38:09 +0100981 free(module);
982}
Michal Vasko33ff9422020-07-03 09:50:39 +0200983
Radek Krejci38d85362019-09-05 16:26:38 +0200984API void
985lysc_extension_instance_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
986{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200987 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
Radek Krejci38d85362019-09-05 16:26:38 +0200988 if (!substmts[u].storage) {
989 continue;
990 }
991
Michal Vaskod989ba02020-08-24 10:59:24 +0200992 switch (substmts[u].stmt) {
Radek Krejci38d85362019-09-05 16:26:38 +0200993 case LY_STMT_TYPE:
994 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
995 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +0200996 struct lysc_type *type = *((struct lysc_type **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200997 if (!type) {
998 break;
999 }
1000 lysc_type_free(ctx, type);
1001 } else {
1002 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +02001003 struct lysc_type **types = *((struct lysc_type ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001004 if (!types) {
1005 break;
1006 }
1007 FREE_ARRAY(ctx, types, lysc_type2_free);
1008 }
1009 break;
1010 case LY_STMT_UNITS:
1011 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1012 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02001013 const char *str = *((const char **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001014 if (!str) {
1015 break;
1016 }
1017 FREE_STRING(ctx, str);
1018 } else {
1019 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +02001020 const char **strs = *((const char ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001021 if (!strs) {
1022 break;
1023 }
1024 FREE_STRINGS(ctx, strs);
1025 }
1026 break;
1027 case LY_STMT_STATUS:
Radek Krejciad5963b2019-09-06 16:03:05 +02001028 case LY_STMT_CONFIG:
Radek Krejci38d85362019-09-05 16:26:38 +02001029 /* nothing to do */
1030 break;
1031 case LY_STMT_IF_FEATURE: {
Michal Vasko22df3f02020-08-24 13:29:22 +02001032 struct lysc_iffeature *iff = *((struct lysc_iffeature **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001033 if (!iff) {
1034 break;
1035 }
1036 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1037 /* single item */
1038 lysc_iffeature_free(ctx, iff);
1039 free(iff);
1040 } else {
1041 /* multiple items */
1042 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
1043 }
1044 break;
1045 }
1046
Radek Krejci0f969882020-08-21 16:56:47 +02001047 /* TODO other statements */
Radek Krejci38d85362019-09-05 16:26:38 +02001048 default:
1049 LOGINT(ctx);
1050 }
1051 }
1052}
David Sedlákebd3acf2019-07-26 15:04:32 +02001053
1054void
Michal Vaskob36053d2020-03-26 15:49:30 +01001055yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001056{
1057 if (ctx) {
1058 free(ctx);
1059 }
1060}
1061
1062void
Michal Vaskob36053d2020-03-26 15:49:30 +01001063yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001064{
1065 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001066 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +02001067 free(ctx);
1068 }
1069}