blob: 0098040b12dbfcbb19c856fc31f84051b27ce98d [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 Krejci2d7a47b2019-05-16 13:34:10 +020029void lysp_grp_free(struct ly_ctx *ctx, struct lysp_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 Krejci19a96102018-11-15 13:38:09 +0100208lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout)
209{
210 struct lysp_node *node, *next;
211
212 FREE_ARRAY(ctx, inout->musts, lysp_restr_free);
213 FREE_ARRAY(ctx, inout->typedefs, lysp_tpdf_free);
214 FREE_ARRAY(ctx, inout->groupings, lysp_grp_free);
215 LY_LIST_FOR_SAFE(inout->data, next, node) {
216 lysp_node_free(ctx, node);
217 }
218 FREE_ARRAY(ctx, inout->exts, lysp_ext_instance_free);
219
220}
221
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200222void
Radek Krejci19a96102018-11-15 13:38:09 +0100223lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action)
224{
225 FREE_STRING(ctx, action->name);
226 FREE_STRING(ctx, action->dsc);
227 FREE_STRING(ctx, action->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200228 FREE_ARRAY(ctx, action->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100229 FREE_ARRAY(ctx, action->typedefs, lysp_tpdf_free);
230 FREE_ARRAY(ctx, action->groupings, lysp_grp_free);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100231 lysp_action_inout_free(ctx, &action->input);
232 lysp_action_inout_free(ctx, &action->output);
Radek Krejci19a96102018-11-15 13:38:09 +0100233 FREE_ARRAY(ctx, action->exts, lysp_ext_instance_free);
234}
235
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200236void
Radek Krejci19a96102018-11-15 13:38:09 +0100237lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif)
238{
239 struct lysp_node *node, *next;
240
241 FREE_STRING(ctx, notif->name);
242 FREE_STRING(ctx, notif->dsc);
243 FREE_STRING(ctx, notif->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200244 FREE_ARRAY(ctx, notif->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100245 FREE_ARRAY(ctx, notif->musts, lysp_restr_free);
246 FREE_ARRAY(ctx, notif->typedefs, lysp_tpdf_free);
247 FREE_ARRAY(ctx, notif->groupings, lysp_grp_free);
248 LY_LIST_FOR_SAFE(notif->data, next, node) {
249 lysp_node_free(ctx, node);
250 }
251 FREE_ARRAY(ctx, notif->exts, lysp_ext_instance_free);
252}
253
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200254void
Radek Krejci19a96102018-11-15 13:38:09 +0100255lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp)
256{
257 struct lysp_node *node, *next;
258
259 FREE_STRING(ctx, grp->name);
260 FREE_STRING(ctx, grp->dsc);
261 FREE_STRING(ctx, grp->ref);
262 FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
263 FREE_ARRAY(ctx, grp->groupings, lysp_grp_free);
264 LY_LIST_FOR_SAFE(grp->data, next, node) {
265 lysp_node_free(ctx, node);
266 }
267 FREE_ARRAY(ctx, grp->actions, lysp_action_free);
268 FREE_ARRAY(ctx, grp->notifs, lysp_notif_free);
269 FREE_ARRAY(ctx, grp->exts, lysp_ext_instance_free);
270}
271
Radek Krejcif09e4e82019-06-14 15:08:11 +0200272void
Radek Krejci19a96102018-11-15 13:38:09 +0100273lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
274{
275 FREE_STRING(ctx, when->cond);
276 FREE_STRING(ctx, when->dsc);
277 FREE_STRING(ctx, when->ref);
278 FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
279}
280
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200281void
Radek Krejci19a96102018-11-15 13:38:09 +0100282lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment)
283{
284 struct lysp_node *node, *next;
285
286 FREE_STRING(ctx, augment->nodeid);
287 FREE_STRING(ctx, augment->dsc);
288 FREE_STRING(ctx, augment->ref);
289 FREE_MEMBER(ctx, augment->when, lysp_when_free);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200290 FREE_ARRAY(ctx, augment->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100291 LY_LIST_FOR_SAFE(augment->child, next, node) {
292 lysp_node_free(ctx, node);
293 }
294 FREE_ARRAY(ctx, augment->actions, lysp_action_free);
295 FREE_ARRAY(ctx, augment->notifs, lysp_notif_free);
296 FREE_ARRAY(ctx, augment->exts, lysp_ext_instance_free);
297}
298
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200299void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200300lysp_qname_free(struct ly_ctx *ctx, struct lysp_qname *qname)
301{
302 if (qname) {
303 FREE_STRING(ctx, qname->str);
304 }
305}
306
307void
Radek Krejci19a96102018-11-15 13:38:09 +0100308lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
309{
Michal Vasko22df3f02020-08-24 13:29:22 +0200310 struct lysp_deviate_add *add = (struct lysp_deviate_add *)d;
311 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl *)d;
Radek Krejci19a96102018-11-15 13:38:09 +0100312
313 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
Michal Vaskod989ba02020-08-24 10:59:24 +0200314 switch (d->mod) {
Radek Krejci19a96102018-11-15 13:38:09 +0100315 case LYS_DEV_NOT_SUPPORTED:
316 /* nothing to do */
317 break;
318 case LYS_DEV_ADD:
319 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
320 FREE_STRING(ctx, add->units);
321 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200322 FREE_ARRAY(ctx, add->uniques, lysp_qname_free);
323 FREE_ARRAY(ctx, add->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100324 break;
325 case LYS_DEV_REPLACE:
326 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
327 FREE_STRING(ctx, rpl->units);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200328 lysp_qname_free(ctx, &rpl->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +0100329 break;
330 default:
331 LOGINT(ctx);
332 break;
333 }
334}
335
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200336void
Radek Krejci19a96102018-11-15 13:38:09 +0100337lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
338{
339 struct lysp_deviate *next, *iter;
340
341 FREE_STRING(ctx, dev->nodeid);
342 FREE_STRING(ctx, dev->dsc);
343 FREE_STRING(ctx, dev->ref);
344 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
345 lysp_deviate_free(ctx, iter);
346 free(iter);
347 }
348 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
349}
350
David Sedlákd2d676a2019-07-22 11:28:19 +0200351void
Radek Krejci19a96102018-11-15 13:38:09 +0100352lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
353{
354 FREE_STRING(ctx, ref->nodeid);
355 FREE_STRING(ctx, ref->dsc);
356 FREE_STRING(ctx, ref->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200357 FREE_ARRAY(ctx, ref->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100358 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
359 FREE_STRING(ctx, ref->presence);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200360 FREE_ARRAY(ctx, ref->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100361 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
362}
363
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200364void
Radek Krejci19a96102018-11-15 13:38:09 +0100365lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
366{
367 struct lysp_node *child, *next;
368
369 FREE_STRING(ctx, node->name);
370 FREE_STRING(ctx, node->dsc);
371 FREE_STRING(ctx, node->ref);
372 FREE_MEMBER(ctx, node->when, lysp_when_free);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200373 FREE_ARRAY(ctx, node->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100374 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
375
Michal Vaskod989ba02020-08-24 10:59:24 +0200376 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100377 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200378 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->musts, lysp_restr_free);
379 FREE_STRING(ctx, ((struct lysp_node_container *)node)->presence);
380 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->typedefs, lysp_tpdf_free);
381 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->groupings, lysp_grp_free);
382 LY_LIST_FOR_SAFE(((struct lysp_node_container *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100383 lysp_node_free(ctx, child);
384 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200385 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->actions, lysp_action_free);
386 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->notifs, lysp_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100387 break;
388 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200389 FREE_ARRAY(ctx, ((struct lysp_node_leaf *)node)->musts, lysp_restr_free);
390 lysp_type_free(ctx, &((struct lysp_node_leaf *)node)->type);
391 FREE_STRING(ctx, ((struct lysp_node_leaf *)node)->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200392 FREE_STRING(ctx, ((struct lysp_node_leaf *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100393 break;
394 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200395 FREE_ARRAY(ctx, ((struct lysp_node_leaflist *)node)->musts, lysp_restr_free);
396 lysp_type_free(ctx, &((struct lysp_node_leaflist *)node)->type);
397 FREE_STRING(ctx, ((struct lysp_node_leaflist *)node)->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200398 FREE_ARRAY(ctx, ((struct lysp_node_leaflist *)node)->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100399 break;
400 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200401 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->musts, lysp_restr_free);
402 FREE_STRING(ctx, ((struct lysp_node_list *)node)->key);
403 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->typedefs, lysp_tpdf_free);
404 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->groupings, lysp_grp_free);
405 LY_LIST_FOR_SAFE(((struct lysp_node_list *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100406 lysp_node_free(ctx, child);
407 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200408 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->actions, lysp_action_free);
409 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->notifs, lysp_notif_free);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200410 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->uniques, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100411 break;
412 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200413 LY_LIST_FOR_SAFE(((struct lysp_node_choice *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100414 lysp_node_free(ctx, child);
415 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200416 FREE_STRING(ctx, ((struct lysp_node_choice *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100417 break;
418 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200419 LY_LIST_FOR_SAFE(((struct lysp_node_case *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100420 lysp_node_free(ctx, child);
421 }
422 break;
423 case LYS_ANYDATA:
424 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200425 FREE_ARRAY(ctx, ((struct lysp_node_anydata *)node)->musts, lysp_restr_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100426 break;
427 case LYS_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +0200428 FREE_ARRAY(ctx, ((struct lysp_node_uses *)node)->refines, lysp_refine_free);
429 FREE_ARRAY(ctx, ((struct lysp_node_uses *)node)->augments, lysp_augment_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100430 break;
431 default:
432 LOGINT(ctx);
433 }
434
435 free(node);
436}
437
Radek Krejci15f10ab2020-11-03 14:14:14 +0100438void
Radek Krejci19a96102018-11-15 13:38:09 +0100439lysp_module_free(struct lysp_module *module)
440{
441 struct ly_ctx *ctx;
442 struct lysp_node *node, *next;
443
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100444 if (!module) {
445 return;
446 }
447 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100448
449 FREE_ARRAY(ctx, module->imports, lysp_import_free);
Radek Krejci771928a2021-01-19 13:42:36 +0100450 FREE_ARRAY(ctx, module->includes, module->is_submod ? lysp_include_free_submodule : lysp_include_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100451
Radek Krejci19a96102018-11-15 13:38:09 +0100452 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
453 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
454 FREE_ARRAY(ctx, module->features, lysp_feature_free);
455 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
456 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
457 FREE_ARRAY(ctx, module->groupings, lysp_grp_free);
458 LY_LIST_FOR_SAFE(module->data, next, node) {
459 lysp_node_free(ctx, node);
460 }
461 FREE_ARRAY(ctx, module->augments, lysp_augment_free);
462 FREE_ARRAY(ctx, module->rpcs, lysp_action_free);
463 FREE_ARRAY(ctx, module->notifs, lysp_notif_free);
464 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
465 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
466
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200467 if (module->is_submod) {
468 struct lysp_submodule *submod = (struct lysp_submodule *)module;
469
470 FREE_STRING(ctx, submod->name);
471 FREE_STRING(ctx, submod->filepath);
472 FREE_STRING(ctx, submod->prefix);
473 FREE_STRING(ctx, submod->org);
474 FREE_STRING(ctx, submod->contact);
475 FREE_STRING(ctx, submod->dsc);
476 FREE_STRING(ctx, submod->ref);
477 }
478
Radek Krejci19a96102018-11-15 13:38:09 +0100479 free(module);
480}
481
Radek Krejci0af46292019-01-11 16:02:31 +0100482void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100483lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
484{
485 if (--(*ext)->refcount) {
486 return;
487 }
488 FREE_STRING(ctx, (*ext)->name);
489 FREE_STRING(ctx, (*ext)->argument);
490 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
491 free(*ext);
492}
493
494void
Radek Krejci19a96102018-11-15 13:38:09 +0100495lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
496{
fredganebc50572019-10-31 15:39:23 +0800497 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200498 ext->def->plugin->free(ctx, ext);
499 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100500 if (ext->def) {
501 lysc_extension_free(ctx, &ext->def);
502 }
Radek Krejci0935f412019-08-20 16:15:18 +0200503 FREE_STRING(ctx, ext->argument);
504 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
505}
506
507void
Radek Krejci19a96102018-11-15 13:38:09 +0100508lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
509{
510 LY_ARRAY_FREE(iff->features);
511 free(iff->expr);
512}
513
514static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100515lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100516{
Radek Krejci00b874b2019-02-12 10:54:50 +0100517 if (--(*w)->refcount) {
518 return;
519 }
520 lyxp_expr_free(ctx, (*w)->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200521 lysc_prefixes_free((*w)->prefixes);
Radek Krejci00b874b2019-02-12 10:54:50 +0100522 FREE_STRING(ctx, (*w)->dsc);
523 FREE_STRING(ctx, (*w)->ref);
524 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
525 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100526}
527
Radek Krejciccd20f12019-02-15 14:12:27 +0100528void
Radek Krejci58d171e2018-11-23 13:50:55 +0100529lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
530{
531 lyxp_expr_free(ctx, must->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200532 lysc_prefixes_free(must->prefixes);
Radek Krejci58d171e2018-11-23 13:50:55 +0100533 FREE_STRING(ctx, must->emsg);
534 FREE_STRING(ctx, must->eapptag);
Radek Krejcic8b31002019-01-08 10:24:45 +0100535 FREE_STRING(ctx, must->dsc);
536 FREE_STRING(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100537 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
538}
539
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100540void
Radek Krejci19a96102018-11-15 13:38:09 +0100541lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
542{
543 FREE_STRING(ctx, ident->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100544 FREE_STRING(ctx, ident->dsc);
545 FREE_STRING(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100546 LY_ARRAY_FREE(ident->derived);
547 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
548}
549
Radek Krejci19a96102018-11-15 13:38:09 +0100550static void
551lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
552{
553 LY_ARRAY_FREE(range->parts);
554 FREE_STRING(ctx, range->eapptag);
555 FREE_STRING(ctx, range->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100556 FREE_STRING(ctx, range->dsc);
557 FREE_STRING(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100558 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
559}
560
561static void
562lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
563{
564 if (--(*pattern)->refcount) {
565 return;
566 }
Radek Krejci54579462019-04-30 12:47:06 +0200567 pcre2_code_free((*pattern)->code);
568 FREE_STRING(ctx, (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +0100569 FREE_STRING(ctx, (*pattern)->eapptag);
570 FREE_STRING(ctx, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100571 FREE_STRING(ctx, (*pattern)->dsc);
572 FREE_STRING(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100573 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
574 free(*pattern);
575}
576
577static void
Radek Krejci693262f2019-04-29 15:23:20 +0200578lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100579{
580 FREE_STRING(ctx, item->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100581 FREE_STRING(ctx, item->dsc);
582 FREE_STRING(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100583 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
584}
585
Radek Krejcia3045382018-11-22 14:30:31 +0100586static void
587lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
588{
589 lysc_type_free(ctx, *type);
590}
Radek Krejci0f969882020-08-21 16:56:47 +0200591
Radek Krejcicdfecd92018-11-26 11:27:32 +0100592void
Radek Krejci19a96102018-11-15 13:38:09 +0100593lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
594{
595 if (--type->refcount) {
596 return;
597 }
Radek Krejcib915ac92020-08-14 23:31:04 +0200598
Michal Vaskod989ba02020-08-24 10:59:24 +0200599 switch (type->basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100600 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +0200601 FREE_MEMBER(ctx, ((struct lysc_type_bin *)type)->length, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100602 break;
603 case LY_TYPE_BITS:
Michal Vasko22df3f02020-08-24 13:29:22 +0200604 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 +0100605 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100606 case LY_TYPE_DEC64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200607 FREE_MEMBER(ctx, ((struct lysc_type_dec *)type)->range, lysc_range_free);
Radek Krejci6cba4292018-11-15 17:33:29 +0100608 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100609 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +0200610 FREE_MEMBER(ctx, ((struct lysc_type_str *)type)->length, lysc_range_free);
611 FREE_ARRAY(ctx, ((struct lysc_type_str *)type)->patterns, lysc_pattern_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100612 break;
613 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +0200614 FREE_ARRAY(ctx, ((struct lysc_type_enum *)type)->enums, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100615 break;
616 case LY_TYPE_INT8:
617 case LY_TYPE_UINT8:
618 case LY_TYPE_INT16:
619 case LY_TYPE_UINT16:
620 case LY_TYPE_INT32:
621 case LY_TYPE_UINT32:
622 case LY_TYPE_INT64:
623 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200624 FREE_MEMBER(ctx, ((struct lysc_type_num *)type)->range, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100625 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100626 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +0200627 LY_ARRAY_FREE(((struct lysc_type_identityref *)type)->bases);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100628 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100629 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +0200630 FREE_ARRAY(ctx, ((struct lysc_type_union *)type)->types, lysc_type2_free);
Radek Krejcia3045382018-11-22 14:30:31 +0100631 break;
632 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200633 lyxp_expr_free(ctx, ((struct lysc_type_leafref *)type)->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200634 lysc_prefixes_free(((struct lysc_type_leafref *)type)->prefixes);
Radek Krejcia3045382018-11-22 14:30:31 +0100635 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100636 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100637 case LY_TYPE_BOOL:
638 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100639 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100640 /* nothing to do */
641 break;
642 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200643
644 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100645 free(type);
646}
647
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100648void
Radek Krejcif538ce52019-03-05 10:46:14 +0100649lysc_action_inout_free(struct ly_ctx *ctx, struct lysc_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100650{
651 struct lysc_node *child, *child_next;
652
Radek Krejcif538ce52019-03-05 10:46:14 +0100653 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
654 LY_LIST_FOR_SAFE(inout->data, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100655 lysc_node_free_(ctx, child);
Radek Krejcif538ce52019-03-05 10:46:14 +0100656 }
657}
658
659void
660lysc_action_free(struct ly_ctx *ctx, struct lysc_action *action)
661{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100662 FREE_STRING(ctx, action->name);
663 FREE_STRING(ctx, action->dsc);
664 FREE_STRING(ctx, action->ref);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100665 FREE_ARRAY(ctx, action->exts, lysc_ext_instance_free);
Michal Vaskoceab6dd2020-10-09 16:53:36 +0200666 FREE_ARRAY(ctx, action->when, lysc_when_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200667 FREE_ARRAY(ctx, action->input_exts, lysc_ext_instance_free);
Radek Krejcif538ce52019-03-05 10:46:14 +0100668 lysc_action_inout_free(ctx, &action->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200669 FREE_ARRAY(ctx, action->output_exts, lysc_ext_instance_free);
Radek Krejcif538ce52019-03-05 10:46:14 +0100670 lysc_action_inout_free(ctx, &action->output);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100671}
672
Radek Krejcifc11bd72019-04-11 16:00:05 +0200673void
674lysc_notif_free(struct ly_ctx *ctx, struct lysc_notif *notif)
675{
676 struct lysc_node *child, *child_next;
677
678 FREE_STRING(ctx, notif->name);
679 FREE_STRING(ctx, notif->dsc);
680 FREE_STRING(ctx, notif->ref);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200681 FREE_ARRAY(ctx, notif->exts, lysc_ext_instance_free);
Michal Vaskoceab6dd2020-10-09 16:53:36 +0200682 FREE_ARRAY(ctx, notif->when, lysc_when_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200683 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
684 LY_LIST_FOR_SAFE(notif->data, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100685 lysc_node_free_(ctx, child);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200686 }
687}
688
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200689void
Radek Krejci19a96102018-11-15 13:38:09 +0100690lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
691{
692 struct lysc_node *child, *child_next;
693
694 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100695 lysc_node_free_(ctx, child);
Radek Krejci19a96102018-11-15 13:38:09 +0100696 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100697 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100698 FREE_ARRAY(ctx, node->actions, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200699 FREE_ARRAY(ctx, node->notifs, lysc_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100700}
701
702static void
703lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
704{
Radek Krejci58d171e2018-11-23 13:50:55 +0100705 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100706 if (node->type) {
707 lysc_type_free(ctx, node->type);
708 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100709 FREE_STRING(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200710 if (node->dflt) {
711 node->dflt->realtype->plugin->free(ctx, node->dflt);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200712 lysc_type_free(ctx, (struct lysc_type *)node->dflt->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200713 free(node->dflt);
714 }
Radek Krejci19a96102018-11-15 13:38:09 +0100715}
716
Radek Krejci42452ac2018-11-28 17:09:52 +0100717static void
718lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
719{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200720 LY_ARRAY_COUNT_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100721
Radek Krejci42452ac2018-11-28 17:09:52 +0100722 FREE_ARRAY(ctx, node->musts, lysc_must_free);
723 if (node->type) {
724 lysc_type_free(ctx, node->type);
725 }
726 FREE_STRING(ctx, node->units);
727 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200728 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200729 lysc_type_free(ctx, (struct lysc_type *)node->dflts[u]->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200730 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100731 }
732 LY_ARRAY_FREE(node->dflts);
733}
734
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100735static void
736lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
737{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200738 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100739 struct lysc_node *child, *child_next;
740
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100741 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100742 lysc_node_free_(ctx, child);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100743 }
744 FREE_ARRAY(ctx, node->musts, lysc_must_free);
745
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100746 LY_ARRAY_FOR(node->uniques, u) {
747 LY_ARRAY_FREE(node->uniques[u]);
748 }
749 LY_ARRAY_FREE(node->uniques);
750
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100751 FREE_ARRAY(ctx, node->actions, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200752 FREE_ARRAY(ctx, node->notifs, lysc_notif_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100753}
754
Radek Krejci056d0a82018-12-06 16:57:25 +0100755static void
756lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
757{
758 struct lysc_node *child, *child_next;
759
Michal Vasko20424b42020-08-31 12:29:38 +0200760 LY_LIST_FOR_SAFE((struct lysc_node *)node->cases, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100761 lysc_node_free_(ctx, child);
Michal Vasko20424b42020-08-31 12:29:38 +0200762 }
763}
764
765static void
766lysc_node_case_free(struct ly_ctx *ctx, struct lysc_node_case *node)
767{
768 struct lysc_node *child, *child_next;
769
770 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100771 lysc_node_free_(ctx, child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100772 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100773}
Radek Krejci056d0a82018-12-06 16:57:25 +0100774
Radek Krejci9800fb82018-12-13 14:26:23 +0100775static void
776lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
777{
778 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100779}
780
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100781static void
782lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +0100783{
784 /* common part */
785 FREE_STRING(ctx, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +0100786 FREE_STRING(ctx, node->dsc);
787 FREE_STRING(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100788
789 /* nodetype-specific part */
Michal Vaskod989ba02020-08-24 10:59:24 +0200790 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100791 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200792 lysc_node_container_free(ctx, (struct lysc_node_container *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100793 break;
794 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200795 lysc_node_leaf_free(ctx, (struct lysc_node_leaf *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100796 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100797 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200798 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist *)node);
Radek Krejci42452ac2018-11-28 17:09:52 +0100799 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100800 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200801 lysc_node_list_free(ctx, (struct lysc_node_list *)node);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100802 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100803 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200804 lysc_node_choice_free(ctx, (struct lysc_node_choice *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100805 break;
806 case LYS_CASE:
Michal Vasko20424b42020-08-31 12:29:38 +0200807 lysc_node_case_free(ctx, (struct lysc_node_case *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100808 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100809 case LYS_ANYDATA:
810 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200811 lysc_node_anydata_free(ctx, (struct lysc_node_anydata *)node);
Radek Krejci9800fb82018-12-13 14:26:23 +0100812 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100813 default:
814 LOGINT(ctx);
815 }
816
Radek Krejci00b874b2019-02-12 10:54:50 +0100817 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100818 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100819 free(node);
820}
821
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100822void
823lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node, ly_bool unlink)
824{
825 struct lysc_node *iter, **child_p;
826
827 if (unlink) {
828 /* unlink from siblings */
829 if (node->prev->next) {
830 node->prev->next = node->next;
831 }
832 if (node->next) {
833 node->next->prev = node->prev;
834 } else {
835 /* unlinking the last node */
836 if (node->parent) {
837 iter = (struct lysc_node *)lysc_node_children(node->parent, node->flags & LYS_CONFIG_MASK);
Michal Vasko539c4a62020-11-03 17:21:34 +0100838 LY_CHECK_ERR_RET(!iter, LOGINT(ctx), );
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100839 } else {
840 iter = node->module->compiled->data;
841 }
842 /* update the "last" pointer from the first node */
843 iter->prev = node->prev;
844 }
845
846 /* unlink from parent */
847 if (node->parent) {
848 child_p = lysc_node_children_p(node->parent, node->flags & LYS_CONFIG_MASK);
849 } else {
850 child_p = &node->module->compiled->data;
851 }
852 if (child_p && (*child_p == node)) {
853 /* the node is the first child */
854 *child_p = node->next;
855 }
856 }
857
858 lysc_node_free_(ctx, node);
859}
860
Radek Krejci19a96102018-11-15 13:38:09 +0100861static void
862lysc_module_free_(struct lysc_module *module)
863{
864 struct ly_ctx *ctx;
865 struct lysc_node *node, *node_next;
866
Michal Vaskod989ba02020-08-24 10:59:24 +0200867 LY_CHECK_ARG_RET(NULL, module, );
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100868 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100869
Radek Krejci19a96102018-11-15 13:38:09 +0100870 LY_LIST_FOR_SAFE(module->data, node_next, node) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100871 lysc_node_free_(ctx, node);
Radek Krejci19a96102018-11-15 13:38:09 +0100872 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100873 FREE_ARRAY(ctx, module->rpcs, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200874 FREE_ARRAY(ctx, module->notifs, lysc_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100875 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
876
877 free(module);
878}
879
880void
881lysc_module_free(struct lysc_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
882{
Radek Krejci9b042892019-02-13 14:28:44 +0100883 /* TODO use the destructor, this just suppress warning about unused parameter */
884 (void) private_destructor;
885
Radek Krejci19a96102018-11-15 13:38:09 +0100886 if (module) {
887 lysc_module_free_(module);
888 }
889}
890
891void
892lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
893{
894 if (!module) {
895 return;
896 }
897
898 lysc_module_free(module->compiled, private_destructor);
Radek Krejci80d281e2020-09-14 17:42:54 +0200899 FREE_ARRAY(module->ctx, module->identities, lysc_ident_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100900 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100901
Michal Vasko7f45cf22020-10-01 12:49:44 +0200902 LY_ARRAY_FREE(module->augmented_by);
903 LY_ARRAY_FREE(module->deviated_by);
904
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100905 FREE_STRING(module->ctx, module->name);
Radek Krejci0af46292019-01-11 16:02:31 +0100906 FREE_STRING(module->ctx, module->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100907 FREE_STRING(module->ctx, module->ns);
908 FREE_STRING(module->ctx, module->prefix);
909 FREE_STRING(module->ctx, module->filepath);
910 FREE_STRING(module->ctx, module->org);
911 FREE_STRING(module->ctx, module->contact);
912 FREE_STRING(module->ctx, module->dsc);
913 FREE_STRING(module->ctx, module->ref);
914
Radek Krejci19a96102018-11-15 13:38:09 +0100915 free(module);
916}
Michal Vasko33ff9422020-07-03 09:50:39 +0200917
Radek Krejci38d85362019-09-05 16:26:38 +0200918API void
919lysc_extension_instance_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
920{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200921 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
Radek Krejci38d85362019-09-05 16:26:38 +0200922 if (!substmts[u].storage) {
923 continue;
924 }
925
Michal Vaskod989ba02020-08-24 10:59:24 +0200926 switch (substmts[u].stmt) {
Radek Krejci38d85362019-09-05 16:26:38 +0200927 case LY_STMT_TYPE:
928 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
929 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +0200930 struct lysc_type *type = *((struct lysc_type **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200931 if (!type) {
932 break;
933 }
934 lysc_type_free(ctx, type);
935 } else {
936 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +0200937 struct lysc_type **types = *((struct lysc_type ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200938 if (!types) {
939 break;
940 }
941 FREE_ARRAY(ctx, types, lysc_type2_free);
942 }
943 break;
944 case LY_STMT_UNITS:
945 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
946 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +0200947 const char *str = *((const char **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200948 if (!str) {
949 break;
950 }
951 FREE_STRING(ctx, str);
952 } else {
953 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +0200954 const char **strs = *((const char ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200955 if (!strs) {
956 break;
957 }
958 FREE_STRINGS(ctx, strs);
959 }
960 break;
961 case LY_STMT_STATUS:
Radek Krejciad5963b2019-09-06 16:03:05 +0200962 case LY_STMT_CONFIG:
Radek Krejci38d85362019-09-05 16:26:38 +0200963 /* nothing to do */
964 break;
965 case LY_STMT_IF_FEATURE: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200966 struct lysc_iffeature *iff = *((struct lysc_iffeature **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200967 if (!iff) {
968 break;
969 }
970 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
971 /* single item */
972 lysc_iffeature_free(ctx, iff);
973 free(iff);
974 } else {
975 /* multiple items */
976 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
977 }
978 break;
979 }
980
Radek Krejci0f969882020-08-21 16:56:47 +0200981 /* TODO other statements */
Radek Krejci38d85362019-09-05 16:26:38 +0200982 default:
983 LOGINT(ctx);
984 }
985 }
986}
David Sedlákebd3acf2019-07-26 15:04:32 +0200987
988void
Michal Vaskob36053d2020-03-26 15:49:30 +0100989yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +0200990{
991 if (ctx) {
992 free(ctx);
993 }
994}
995
996void
Michal Vaskob36053d2020-03-26 15:49:30 +0100997yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +0200998{
999 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001000 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +02001001 free(ctx);
1002 }
1003}