blob: 4026c7fe992d4e55e7eb210313df14b6bb2dd716 [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 Krejci535ea9f2020-05-29 16:01:05 +020019#include "plugins_exts.h"
20#include "plugins_types.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010024#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "xml.h"
Radek Krejci19a96102018-11-15 13:38:09 +010026#include "xpath.h"
27
Radek Krejci2d7a47b2019-05-16 13:34:10 +020028void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
Michal Vasko5fe75f12020-03-02 13:52:37 +010029void lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010030static void lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node);
Radek Krejci19a96102018-11-15 13:38:09 +010031
32static void
33lysp_stmt_free(struct ly_ctx *ctx, struct lysp_stmt *stmt)
34{
35 struct lysp_stmt *child, *next;
36
37 FREE_STRING(ctx, stmt->stmt);
38 FREE_STRING(ctx, stmt->arg);
39
40 LY_LIST_FOR_SAFE(stmt->child, next, child) {
41 lysp_stmt_free(ctx, child);
42 }
43
44 free(stmt);
45}
46
Radek Krejci2d7a47b2019-05-16 13:34:10 +020047void
Radek Krejci19a96102018-11-15 13:38:09 +010048lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext)
49{
50 struct lysp_stmt *stmt, *next;
51
52 FREE_STRING(ctx, ext->name);
53 FREE_STRING(ctx, ext->argument);
54
55 LY_LIST_FOR_SAFE(ext->child, next, stmt) {
56 lysp_stmt_free(ctx, stmt);
57 }
58}
59
David Sedlák298ff6d2019-07-26 14:29:03 +020060void
Radek Krejci19a96102018-11-15 13:38:09 +010061lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import)
62{
63 /* imported module is freed directly from the context's list */
64 FREE_STRING(ctx, import->name);
65 FREE_STRING(ctx, import->prefix);
66 FREE_STRING(ctx, import->dsc);
67 FREE_STRING(ctx, import->ref);
68 FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free);
69}
70
David Sedlák0c2bab92019-07-22 15:33:19 +020071void
Radek Krejci19a96102018-11-15 13:38:09 +010072lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include)
73{
74 if (include->submodule) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +020075 lysp_module_free((struct lysp_module *)include->submodule);
Radek Krejci19a96102018-11-15 13:38:09 +010076 }
77 FREE_STRING(ctx, include->name);
78 FREE_STRING(ctx, include->dsc);
79 FREE_STRING(ctx, include->ref);
80 FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free);
81}
82
David Sedlákaa854b02019-07-22 14:17:10 +020083void
Radek Krejci19a96102018-11-15 13:38:09 +010084lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev)
85{
86 FREE_STRING(ctx, rev->dsc);
87 FREE_STRING(ctx, rev->ref);
88 FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free);
89}
90
David Sedlák986cb412019-07-04 13:10:11 +020091void
Radek Krejci19a96102018-11-15 13:38:09 +010092lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext)
93{
94 FREE_STRING(ctx, ext->name);
95 FREE_STRING(ctx, ext->argument);
96 FREE_STRING(ctx, ext->dsc);
97 FREE_STRING(ctx, ext->ref);
98 FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free);
Michal Vasko5fe75f12020-03-02 13:52:37 +010099 if (ext->compiled) {
100 lysc_extension_free(ctx, &ext->compiled);
101 }
Radek Krejci19a96102018-11-15 13:38:09 +0100102}
103
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200104void
Radek Krejci19a96102018-11-15 13:38:09 +0100105lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat)
106{
107 FREE_STRING(ctx, feat->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200108 FREE_ARRAY(ctx, feat->iffeatures, lysp_qname_free);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100109 FREE_ARRAY(ctx, feat->iffeatures_c, lysc_iffeature_free);
110 LY_ARRAY_FREE(feat->depfeatures);
Radek Krejci19a96102018-11-15 13:38:09 +0100111 FREE_STRING(ctx, feat->dsc);
112 FREE_STRING(ctx, feat->ref);
113 FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
114}
115
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200116void
Radek Krejci19a96102018-11-15 13:38:09 +0100117lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident)
118{
119 FREE_STRING(ctx, ident->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200120 FREE_ARRAY(ctx, ident->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100121 FREE_STRINGS(ctx, ident->bases);
122 FREE_STRING(ctx, ident->dsc);
123 FREE_STRING(ctx, ident->ref);
124 FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
125}
126
Michal Vasko7f45cf22020-10-01 12:49:44 +0200127void
Radek Krejci19a96102018-11-15 13:38:09 +0100128lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
129{
Michal Vasko7f45cf22020-10-01 12:49:44 +0200130 FREE_STRING(ctx, restr->arg.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100131 FREE_STRING(ctx, restr->emsg);
132 FREE_STRING(ctx, restr->eapptag);
133 FREE_STRING(ctx, restr->dsc);
134 FREE_STRING(ctx, restr->ref);
135 FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
136}
137
138static void
139lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item)
140{
141 FREE_STRING(ctx, item->name);
142 FREE_STRING(ctx, item->dsc);
143 FREE_STRING(ctx, item->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200144 FREE_ARRAY(ctx, item->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100145 FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
146}
147
Radek Krejcicdfecd92018-11-26 11:27:32 +0100148void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type);
Michal Vasko004d3152020-06-11 19:59:22 +0200149
David Sedlák32488102019-07-15 17:44:10 +0200150void
Radek Krejci19a96102018-11-15 13:38:09 +0100151lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type)
152{
153 FREE_STRING(ctx, type->name);
154 FREE_MEMBER(ctx, type->range, lysp_restr_free);
155 FREE_MEMBER(ctx, type->length, lysp_restr_free);
156 FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
157 FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
158 FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
Michal Vasko004d3152020-06-11 19:59:22 +0200159 lyxp_expr_free(ctx, type->path);
Radek Krejci19a96102018-11-15 13:38:09 +0100160 FREE_STRINGS(ctx, type->bases);
161 FREE_ARRAY(ctx, type->types, lysp_type_free);
162 FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
163 if (type->compiled) {
164 lysc_type_free(ctx, type->compiled);
165 }
166}
167
David Sedlák04e17b22019-07-19 15:29:48 +0200168void
Radek Krejci19a96102018-11-15 13:38:09 +0100169lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
170{
171 FREE_STRING(ctx, tpdf->name);
172 FREE_STRING(ctx, tpdf->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200173 FREE_STRING(ctx, tpdf->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100174 FREE_STRING(ctx, tpdf->dsc);
175 FREE_STRING(ctx, tpdf->ref);
176 FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
177
178 lysp_type_free(ctx, &tpdf->type);
179
180}
181
Radek Krejcif538ce52019-03-05 10:46:14 +0100182void
Radek Krejci19a96102018-11-15 13:38:09 +0100183lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout)
184{
185 struct lysp_node *node, *next;
186
187 FREE_ARRAY(ctx, inout->musts, lysp_restr_free);
188 FREE_ARRAY(ctx, inout->typedefs, lysp_tpdf_free);
189 FREE_ARRAY(ctx, inout->groupings, lysp_grp_free);
190 LY_LIST_FOR_SAFE(inout->data, next, node) {
191 lysp_node_free(ctx, node);
192 }
193 FREE_ARRAY(ctx, inout->exts, lysp_ext_instance_free);
194
195}
196
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200197void
Radek Krejci19a96102018-11-15 13:38:09 +0100198lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action)
199{
200 FREE_STRING(ctx, action->name);
201 FREE_STRING(ctx, action->dsc);
202 FREE_STRING(ctx, action->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200203 FREE_ARRAY(ctx, action->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100204 FREE_ARRAY(ctx, action->typedefs, lysp_tpdf_free);
205 FREE_ARRAY(ctx, action->groupings, lysp_grp_free);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100206 lysp_action_inout_free(ctx, &action->input);
207 lysp_action_inout_free(ctx, &action->output);
Radek Krejci19a96102018-11-15 13:38:09 +0100208 FREE_ARRAY(ctx, action->exts, lysp_ext_instance_free);
209}
210
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200211void
Radek Krejci19a96102018-11-15 13:38:09 +0100212lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif)
213{
214 struct lysp_node *node, *next;
215
216 FREE_STRING(ctx, notif->name);
217 FREE_STRING(ctx, notif->dsc);
218 FREE_STRING(ctx, notif->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200219 FREE_ARRAY(ctx, notif->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100220 FREE_ARRAY(ctx, notif->musts, lysp_restr_free);
221 FREE_ARRAY(ctx, notif->typedefs, lysp_tpdf_free);
222 FREE_ARRAY(ctx, notif->groupings, lysp_grp_free);
223 LY_LIST_FOR_SAFE(notif->data, next, node) {
224 lysp_node_free(ctx, node);
225 }
226 FREE_ARRAY(ctx, notif->exts, lysp_ext_instance_free);
227}
228
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200229void
Radek Krejci19a96102018-11-15 13:38:09 +0100230lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp)
231{
232 struct lysp_node *node, *next;
233
234 FREE_STRING(ctx, grp->name);
235 FREE_STRING(ctx, grp->dsc);
236 FREE_STRING(ctx, grp->ref);
237 FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
238 FREE_ARRAY(ctx, grp->groupings, lysp_grp_free);
239 LY_LIST_FOR_SAFE(grp->data, next, node) {
240 lysp_node_free(ctx, node);
241 }
242 FREE_ARRAY(ctx, grp->actions, lysp_action_free);
243 FREE_ARRAY(ctx, grp->notifs, lysp_notif_free);
244 FREE_ARRAY(ctx, grp->exts, lysp_ext_instance_free);
245}
246
Radek Krejcif09e4e82019-06-14 15:08:11 +0200247void
Radek Krejci19a96102018-11-15 13:38:09 +0100248lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
249{
250 FREE_STRING(ctx, when->cond);
251 FREE_STRING(ctx, when->dsc);
252 FREE_STRING(ctx, when->ref);
253 FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
254}
255
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200256void
Radek Krejci19a96102018-11-15 13:38:09 +0100257lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment)
258{
259 struct lysp_node *node, *next;
260
261 FREE_STRING(ctx, augment->nodeid);
262 FREE_STRING(ctx, augment->dsc);
263 FREE_STRING(ctx, augment->ref);
264 FREE_MEMBER(ctx, augment->when, lysp_when_free);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200265 FREE_ARRAY(ctx, augment->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100266 LY_LIST_FOR_SAFE(augment->child, next, node) {
267 lysp_node_free(ctx, node);
268 }
269 FREE_ARRAY(ctx, augment->actions, lysp_action_free);
270 FREE_ARRAY(ctx, augment->notifs, lysp_notif_free);
271 FREE_ARRAY(ctx, augment->exts, lysp_ext_instance_free);
272}
273
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200274void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200275lysp_qname_free(struct ly_ctx *ctx, struct lysp_qname *qname)
276{
277 if (qname) {
278 FREE_STRING(ctx, qname->str);
279 }
280}
281
282void
Radek Krejci19a96102018-11-15 13:38:09 +0100283lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
284{
Michal Vasko22df3f02020-08-24 13:29:22 +0200285 struct lysp_deviate_add *add = (struct lysp_deviate_add *)d;
286 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl *)d;
Radek Krejci19a96102018-11-15 13:38:09 +0100287
288 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
Michal Vaskod989ba02020-08-24 10:59:24 +0200289 switch (d->mod) {
Radek Krejci19a96102018-11-15 13:38:09 +0100290 case LYS_DEV_NOT_SUPPORTED:
291 /* nothing to do */
292 break;
293 case LYS_DEV_ADD:
294 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
295 FREE_STRING(ctx, add->units);
296 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200297 FREE_ARRAY(ctx, add->uniques, lysp_qname_free);
298 FREE_ARRAY(ctx, add->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100299 break;
300 case LYS_DEV_REPLACE:
301 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
302 FREE_STRING(ctx, rpl->units);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200303 lysp_qname_free(ctx, &rpl->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +0100304 break;
305 default:
306 LOGINT(ctx);
307 break;
308 }
309}
310
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200311void
Radek Krejci19a96102018-11-15 13:38:09 +0100312lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
313{
314 struct lysp_deviate *next, *iter;
315
316 FREE_STRING(ctx, dev->nodeid);
317 FREE_STRING(ctx, dev->dsc);
318 FREE_STRING(ctx, dev->ref);
319 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
320 lysp_deviate_free(ctx, iter);
321 free(iter);
322 }
323 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
324}
325
David Sedlákd2d676a2019-07-22 11:28:19 +0200326void
Radek Krejci19a96102018-11-15 13:38:09 +0100327lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
328{
329 FREE_STRING(ctx, ref->nodeid);
330 FREE_STRING(ctx, ref->dsc);
331 FREE_STRING(ctx, ref->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200332 FREE_ARRAY(ctx, ref->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100333 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
334 FREE_STRING(ctx, ref->presence);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200335 FREE_ARRAY(ctx, ref->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100336 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
337}
338
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200339void
Radek Krejci19a96102018-11-15 13:38:09 +0100340lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
341{
342 struct lysp_node *child, *next;
343
344 FREE_STRING(ctx, node->name);
345 FREE_STRING(ctx, node->dsc);
346 FREE_STRING(ctx, node->ref);
347 FREE_MEMBER(ctx, node->when, lysp_when_free);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200348 FREE_ARRAY(ctx, node->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100349 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
350
Michal Vaskod989ba02020-08-24 10:59:24 +0200351 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100352 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200353 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->musts, lysp_restr_free);
354 FREE_STRING(ctx, ((struct lysp_node_container *)node)->presence);
355 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->typedefs, lysp_tpdf_free);
356 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->groupings, lysp_grp_free);
357 LY_LIST_FOR_SAFE(((struct lysp_node_container *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100358 lysp_node_free(ctx, child);
359 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200360 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->actions, lysp_action_free);
361 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->notifs, lysp_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100362 break;
363 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200364 FREE_ARRAY(ctx, ((struct lysp_node_leaf *)node)->musts, lysp_restr_free);
365 lysp_type_free(ctx, &((struct lysp_node_leaf *)node)->type);
366 FREE_STRING(ctx, ((struct lysp_node_leaf *)node)->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200367 FREE_STRING(ctx, ((struct lysp_node_leaf *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100368 break;
369 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200370 FREE_ARRAY(ctx, ((struct lysp_node_leaflist *)node)->musts, lysp_restr_free);
371 lysp_type_free(ctx, &((struct lysp_node_leaflist *)node)->type);
372 FREE_STRING(ctx, ((struct lysp_node_leaflist *)node)->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200373 FREE_ARRAY(ctx, ((struct lysp_node_leaflist *)node)->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100374 break;
375 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200376 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->musts, lysp_restr_free);
377 FREE_STRING(ctx, ((struct lysp_node_list *)node)->key);
378 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->typedefs, lysp_tpdf_free);
379 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->groupings, lysp_grp_free);
380 LY_LIST_FOR_SAFE(((struct lysp_node_list *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100381 lysp_node_free(ctx, child);
382 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200383 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->actions, lysp_action_free);
384 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->notifs, lysp_notif_free);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200385 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->uniques, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100386 break;
387 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200388 LY_LIST_FOR_SAFE(((struct lysp_node_choice *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100389 lysp_node_free(ctx, child);
390 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200391 FREE_STRING(ctx, ((struct lysp_node_choice *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100392 break;
393 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200394 LY_LIST_FOR_SAFE(((struct lysp_node_case *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100395 lysp_node_free(ctx, child);
396 }
397 break;
398 case LYS_ANYDATA:
399 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200400 FREE_ARRAY(ctx, ((struct lysp_node_anydata *)node)->musts, lysp_restr_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100401 break;
402 case LYS_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +0200403 FREE_ARRAY(ctx, ((struct lysp_node_uses *)node)->refines, lysp_refine_free);
404 FREE_ARRAY(ctx, ((struct lysp_node_uses *)node)->augments, lysp_augment_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100405 break;
406 default:
407 LOGINT(ctx);
408 }
409
410 free(node);
411}
412
413API void
414lysp_module_free(struct lysp_module *module)
415{
416 struct ly_ctx *ctx;
417 struct lysp_node *node, *next;
418
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100419 if (!module) {
420 return;
421 }
422 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100423
424 FREE_ARRAY(ctx, module->imports, lysp_import_free);
425 FREE_ARRAY(ctx, module->includes, lysp_include_free);
426
Radek Krejci19a96102018-11-15 13:38:09 +0100427 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
428 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
429 FREE_ARRAY(ctx, module->features, lysp_feature_free);
430 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
431 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
432 FREE_ARRAY(ctx, module->groupings, lysp_grp_free);
433 LY_LIST_FOR_SAFE(module->data, next, node) {
434 lysp_node_free(ctx, node);
435 }
436 FREE_ARRAY(ctx, module->augments, lysp_augment_free);
437 FREE_ARRAY(ctx, module->rpcs, lysp_action_free);
438 FREE_ARRAY(ctx, module->notifs, lysp_notif_free);
439 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
440 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
441
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200442 if (module->is_submod) {
443 struct lysp_submodule *submod = (struct lysp_submodule *)module;
444
445 FREE_STRING(ctx, submod->name);
446 FREE_STRING(ctx, submod->filepath);
447 FREE_STRING(ctx, submod->prefix);
448 FREE_STRING(ctx, submod->org);
449 FREE_STRING(ctx, submod->contact);
450 FREE_STRING(ctx, submod->dsc);
451 FREE_STRING(ctx, submod->ref);
452 }
453
Radek Krejci19a96102018-11-15 13:38:09 +0100454 free(module);
455}
456
Radek Krejci0af46292019-01-11 16:02:31 +0100457void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100458lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
459{
460 if (--(*ext)->refcount) {
461 return;
462 }
463 FREE_STRING(ctx, (*ext)->name);
464 FREE_STRING(ctx, (*ext)->argument);
465 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
466 free(*ext);
467}
468
469void
Radek Krejci19a96102018-11-15 13:38:09 +0100470lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
471{
fredganebc50572019-10-31 15:39:23 +0800472 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200473 ext->def->plugin->free(ctx, ext);
474 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100475 if (ext->def) {
476 lysc_extension_free(ctx, &ext->def);
477 }
Radek Krejci0935f412019-08-20 16:15:18 +0200478 FREE_STRING(ctx, ext->argument);
479 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
480}
481
482void
Radek Krejci19a96102018-11-15 13:38:09 +0100483lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
484{
485 LY_ARRAY_FREE(iff->features);
486 free(iff->expr);
487}
488
489static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100490lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100491{
Radek Krejci00b874b2019-02-12 10:54:50 +0100492 if (--(*w)->refcount) {
493 return;
494 }
495 lyxp_expr_free(ctx, (*w)->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200496 lysc_prefixes_free((*w)->prefixes);
Radek Krejci00b874b2019-02-12 10:54:50 +0100497 FREE_STRING(ctx, (*w)->dsc);
498 FREE_STRING(ctx, (*w)->ref);
499 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
500 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100501}
502
Radek Krejciccd20f12019-02-15 14:12:27 +0100503void
Radek Krejci58d171e2018-11-23 13:50:55 +0100504lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
505{
506 lyxp_expr_free(ctx, must->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200507 lysc_prefixes_free(must->prefixes);
Radek Krejci58d171e2018-11-23 13:50:55 +0100508 FREE_STRING(ctx, must->emsg);
509 FREE_STRING(ctx, must->eapptag);
Radek Krejcic8b31002019-01-08 10:24:45 +0100510 FREE_STRING(ctx, must->dsc);
511 FREE_STRING(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100512 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
513}
514
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100515void
Radek Krejci19a96102018-11-15 13:38:09 +0100516lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
517{
518 FREE_STRING(ctx, ident->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100519 FREE_STRING(ctx, ident->dsc);
520 FREE_STRING(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100521 LY_ARRAY_FREE(ident->derived);
522 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
523}
524
Radek Krejci19a96102018-11-15 13:38:09 +0100525static void
526lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
527{
528 LY_ARRAY_FREE(range->parts);
529 FREE_STRING(ctx, range->eapptag);
530 FREE_STRING(ctx, range->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100531 FREE_STRING(ctx, range->dsc);
532 FREE_STRING(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100533 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
534}
535
536static void
537lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
538{
539 if (--(*pattern)->refcount) {
540 return;
541 }
Radek Krejci54579462019-04-30 12:47:06 +0200542 pcre2_code_free((*pattern)->code);
543 FREE_STRING(ctx, (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +0100544 FREE_STRING(ctx, (*pattern)->eapptag);
545 FREE_STRING(ctx, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100546 FREE_STRING(ctx, (*pattern)->dsc);
547 FREE_STRING(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100548 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
549 free(*pattern);
550}
551
552static void
Radek Krejci693262f2019-04-29 15:23:20 +0200553lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100554{
555 FREE_STRING(ctx, item->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100556 FREE_STRING(ctx, item->dsc);
557 FREE_STRING(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100558 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
559}
560
Radek Krejcia3045382018-11-22 14:30:31 +0100561static void
562lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
563{
564 lysc_type_free(ctx, *type);
565}
Radek Krejci0f969882020-08-21 16:56:47 +0200566
Radek Krejcicdfecd92018-11-26 11:27:32 +0100567void
Radek Krejci19a96102018-11-15 13:38:09 +0100568lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
569{
570 if (--type->refcount) {
571 return;
572 }
Radek Krejcib915ac92020-08-14 23:31:04 +0200573
Michal Vaskod989ba02020-08-24 10:59:24 +0200574 switch (type->basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100575 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +0200576 FREE_MEMBER(ctx, ((struct lysc_type_bin *)type)->length, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100577 break;
578 case LY_TYPE_BITS:
Michal Vasko22df3f02020-08-24 13:29:22 +0200579 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 +0100580 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100581 case LY_TYPE_DEC64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200582 FREE_MEMBER(ctx, ((struct lysc_type_dec *)type)->range, lysc_range_free);
Radek Krejci6cba4292018-11-15 17:33:29 +0100583 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100584 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +0200585 FREE_MEMBER(ctx, ((struct lysc_type_str *)type)->length, lysc_range_free);
586 FREE_ARRAY(ctx, ((struct lysc_type_str *)type)->patterns, lysc_pattern_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100587 break;
588 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +0200589 FREE_ARRAY(ctx, ((struct lysc_type_enum *)type)->enums, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100590 break;
591 case LY_TYPE_INT8:
592 case LY_TYPE_UINT8:
593 case LY_TYPE_INT16:
594 case LY_TYPE_UINT16:
595 case LY_TYPE_INT32:
596 case LY_TYPE_UINT32:
597 case LY_TYPE_INT64:
598 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200599 FREE_MEMBER(ctx, ((struct lysc_type_num *)type)->range, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100600 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100601 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +0200602 LY_ARRAY_FREE(((struct lysc_type_identityref *)type)->bases);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100603 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100604 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +0200605 FREE_ARRAY(ctx, ((struct lysc_type_union *)type)->types, lysc_type2_free);
Radek Krejcia3045382018-11-22 14:30:31 +0100606 break;
607 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200608 lyxp_expr_free(ctx, ((struct lysc_type_leafref *)type)->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200609 lysc_prefixes_free(((struct lysc_type_leafref *)type)->prefixes);
Radek Krejcia3045382018-11-22 14:30:31 +0100610 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100611 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100612 case LY_TYPE_BOOL:
613 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100614 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100615 /* nothing to do */
616 break;
617 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200618
619 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100620 free(type);
621}
622
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100623void
Radek Krejcif538ce52019-03-05 10:46:14 +0100624lysc_action_inout_free(struct ly_ctx *ctx, struct lysc_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100625{
626 struct lysc_node *child, *child_next;
627
Radek Krejcif538ce52019-03-05 10:46:14 +0100628 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
629 LY_LIST_FOR_SAFE(inout->data, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100630 lysc_node_free_(ctx, child);
Radek Krejcif538ce52019-03-05 10:46:14 +0100631 }
632}
633
634void
635lysc_action_free(struct ly_ctx *ctx, struct lysc_action *action)
636{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100637 FREE_STRING(ctx, action->name);
638 FREE_STRING(ctx, action->dsc);
639 FREE_STRING(ctx, action->ref);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100640 FREE_ARRAY(ctx, action->exts, lysc_ext_instance_free);
Michal Vaskoceab6dd2020-10-09 16:53:36 +0200641 FREE_ARRAY(ctx, action->when, lysc_when_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200642 FREE_ARRAY(ctx, action->input_exts, lysc_ext_instance_free);
Radek Krejcif538ce52019-03-05 10:46:14 +0100643 lysc_action_inout_free(ctx, &action->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200644 FREE_ARRAY(ctx, action->output_exts, lysc_ext_instance_free);
Radek Krejcif538ce52019-03-05 10:46:14 +0100645 lysc_action_inout_free(ctx, &action->output);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100646}
647
Radek Krejcifc11bd72019-04-11 16:00:05 +0200648void
649lysc_notif_free(struct ly_ctx *ctx, struct lysc_notif *notif)
650{
651 struct lysc_node *child, *child_next;
652
653 FREE_STRING(ctx, notif->name);
654 FREE_STRING(ctx, notif->dsc);
655 FREE_STRING(ctx, notif->ref);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200656 FREE_ARRAY(ctx, notif->exts, lysc_ext_instance_free);
Michal Vaskoceab6dd2020-10-09 16:53:36 +0200657 FREE_ARRAY(ctx, notif->when, lysc_when_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200658 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
659 LY_LIST_FOR_SAFE(notif->data, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100660 lysc_node_free_(ctx, child);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200661 }
662}
663
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200664void
Radek Krejci19a96102018-11-15 13:38:09 +0100665lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
666{
667 struct lysc_node *child, *child_next;
668
669 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100670 lysc_node_free_(ctx, child);
Radek Krejci19a96102018-11-15 13:38:09 +0100671 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100672 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100673 FREE_ARRAY(ctx, node->actions, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200674 FREE_ARRAY(ctx, node->notifs, lysc_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100675}
676
677static void
678lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
679{
Radek Krejci58d171e2018-11-23 13:50:55 +0100680 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100681 if (node->type) {
682 lysc_type_free(ctx, node->type);
683 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100684 FREE_STRING(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200685 if (node->dflt) {
686 node->dflt->realtype->plugin->free(ctx, node->dflt);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200687 lysc_type_free(ctx, (struct lysc_type *)node->dflt->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200688 free(node->dflt);
689 }
Radek Krejci19a96102018-11-15 13:38:09 +0100690}
691
Radek Krejci42452ac2018-11-28 17:09:52 +0100692static void
693lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
694{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200695 LY_ARRAY_COUNT_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100696
Radek Krejci42452ac2018-11-28 17:09:52 +0100697 FREE_ARRAY(ctx, node->musts, lysc_must_free);
698 if (node->type) {
699 lysc_type_free(ctx, node->type);
700 }
701 FREE_STRING(ctx, node->units);
702 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200703 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200704 lysc_type_free(ctx, (struct lysc_type *)node->dflts[u]->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200705 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100706 }
707 LY_ARRAY_FREE(node->dflts);
708}
709
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100710static void
711lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
712{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200713 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100714 struct lysc_node *child, *child_next;
715
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100716 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100717 lysc_node_free_(ctx, child);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100718 }
719 FREE_ARRAY(ctx, node->musts, lysc_must_free);
720
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100721 LY_ARRAY_FOR(node->uniques, u) {
722 LY_ARRAY_FREE(node->uniques[u]);
723 }
724 LY_ARRAY_FREE(node->uniques);
725
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100726 FREE_ARRAY(ctx, node->actions, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200727 FREE_ARRAY(ctx, node->notifs, lysc_notif_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100728}
729
Radek Krejci056d0a82018-12-06 16:57:25 +0100730static void
731lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
732{
733 struct lysc_node *child, *child_next;
734
Michal Vasko20424b42020-08-31 12:29:38 +0200735 LY_LIST_FOR_SAFE((struct lysc_node *)node->cases, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100736 lysc_node_free_(ctx, child);
Michal Vasko20424b42020-08-31 12:29:38 +0200737 }
738}
739
740static void
741lysc_node_case_free(struct ly_ctx *ctx, struct lysc_node_case *node)
742{
743 struct lysc_node *child, *child_next;
744
745 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100746 lysc_node_free_(ctx, child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100747 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100748}
Radek Krejci056d0a82018-12-06 16:57:25 +0100749
Radek Krejci9800fb82018-12-13 14:26:23 +0100750static void
751lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
752{
753 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100754}
755
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100756static void
757lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +0100758{
759 /* common part */
760 FREE_STRING(ctx, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +0100761 FREE_STRING(ctx, node->dsc);
762 FREE_STRING(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100763
764 /* nodetype-specific part */
Michal Vaskod989ba02020-08-24 10:59:24 +0200765 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100766 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200767 lysc_node_container_free(ctx, (struct lysc_node_container *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100768 break;
769 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200770 lysc_node_leaf_free(ctx, (struct lysc_node_leaf *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100771 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100772 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200773 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist *)node);
Radek Krejci42452ac2018-11-28 17:09:52 +0100774 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100775 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200776 lysc_node_list_free(ctx, (struct lysc_node_list *)node);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100777 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100778 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200779 lysc_node_choice_free(ctx, (struct lysc_node_choice *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100780 break;
781 case LYS_CASE:
Michal Vasko20424b42020-08-31 12:29:38 +0200782 lysc_node_case_free(ctx, (struct lysc_node_case *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100783 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100784 case LYS_ANYDATA:
785 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200786 lysc_node_anydata_free(ctx, (struct lysc_node_anydata *)node);
Radek Krejci9800fb82018-12-13 14:26:23 +0100787 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100788 default:
789 LOGINT(ctx);
790 }
791
Radek Krejci00b874b2019-02-12 10:54:50 +0100792 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100793 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100794 free(node);
795}
796
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100797void
798lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node, ly_bool unlink)
799{
800 struct lysc_node *iter, **child_p;
801
802 if (unlink) {
803 /* unlink from siblings */
804 if (node->prev->next) {
805 node->prev->next = node->next;
806 }
807 if (node->next) {
808 node->next->prev = node->prev;
809 } else {
810 /* unlinking the last node */
811 if (node->parent) {
812 iter = (struct lysc_node *)lysc_node_children(node->parent, node->flags & LYS_CONFIG_MASK);
Michal Vasko539c4a62020-11-03 17:21:34 +0100813 LY_CHECK_ERR_RET(!iter, LOGINT(ctx), );
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100814 } else {
815 iter = node->module->compiled->data;
816 }
817 /* update the "last" pointer from the first node */
818 iter->prev = node->prev;
819 }
820
821 /* unlink from parent */
822 if (node->parent) {
823 child_p = lysc_node_children_p(node->parent, node->flags & LYS_CONFIG_MASK);
824 } else {
825 child_p = &node->module->compiled->data;
826 }
827 if (child_p && (*child_p == node)) {
828 /* the node is the first child */
829 *child_p = node->next;
830 }
831 }
832
833 lysc_node_free_(ctx, node);
834}
835
Radek Krejci19a96102018-11-15 13:38:09 +0100836static void
837lysc_module_free_(struct lysc_module *module)
838{
839 struct ly_ctx *ctx;
840 struct lysc_node *node, *node_next;
841
Michal Vaskod989ba02020-08-24 10:59:24 +0200842 LY_CHECK_ARG_RET(NULL, module, );
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100843 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100844
Radek Krejci19a96102018-11-15 13:38:09 +0100845 LY_LIST_FOR_SAFE(module->data, node_next, node) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100846 lysc_node_free_(ctx, node);
Radek Krejci19a96102018-11-15 13:38:09 +0100847 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100848 FREE_ARRAY(ctx, module->rpcs, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200849 FREE_ARRAY(ctx, module->notifs, lysc_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100850 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
851
852 free(module);
853}
854
855void
856lysc_module_free(struct lysc_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
857{
Radek Krejci9b042892019-02-13 14:28:44 +0100858 /* TODO use the destructor, this just suppress warning about unused parameter */
859 (void) private_destructor;
860
Radek Krejci19a96102018-11-15 13:38:09 +0100861 if (module) {
862 lysc_module_free_(module);
863 }
864}
865
866void
867lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
868{
869 if (!module) {
870 return;
871 }
872
873 lysc_module_free(module->compiled, private_destructor);
Radek Krejci80d281e2020-09-14 17:42:54 +0200874 FREE_ARRAY(module->ctx, module->identities, lysc_ident_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100875 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100876
Michal Vasko7f45cf22020-10-01 12:49:44 +0200877 LY_ARRAY_FREE(module->augmented_by);
878 LY_ARRAY_FREE(module->deviated_by);
879
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100880 FREE_STRING(module->ctx, module->name);
Radek Krejci0af46292019-01-11 16:02:31 +0100881 FREE_STRING(module->ctx, module->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100882 FREE_STRING(module->ctx, module->ns);
883 FREE_STRING(module->ctx, module->prefix);
884 FREE_STRING(module->ctx, module->filepath);
885 FREE_STRING(module->ctx, module->org);
886 FREE_STRING(module->ctx, module->contact);
887 FREE_STRING(module->ctx, module->dsc);
888 FREE_STRING(module->ctx, module->ref);
889
Radek Krejci19a96102018-11-15 13:38:09 +0100890 free(module);
891}
Michal Vasko33ff9422020-07-03 09:50:39 +0200892
Radek Krejci38d85362019-09-05 16:26:38 +0200893API void
894lysc_extension_instance_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
895{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200896 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
Radek Krejci38d85362019-09-05 16:26:38 +0200897 if (!substmts[u].storage) {
898 continue;
899 }
900
Michal Vaskod989ba02020-08-24 10:59:24 +0200901 switch (substmts[u].stmt) {
Radek Krejci38d85362019-09-05 16:26:38 +0200902 case LY_STMT_TYPE:
903 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
904 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +0200905 struct lysc_type *type = *((struct lysc_type **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200906 if (!type) {
907 break;
908 }
909 lysc_type_free(ctx, type);
910 } else {
911 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +0200912 struct lysc_type **types = *((struct lysc_type ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200913 if (!types) {
914 break;
915 }
916 FREE_ARRAY(ctx, types, lysc_type2_free);
917 }
918 break;
919 case LY_STMT_UNITS:
920 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
921 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +0200922 const char *str = *((const char **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200923 if (!str) {
924 break;
925 }
926 FREE_STRING(ctx, str);
927 } else {
928 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +0200929 const char **strs = *((const char ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200930 if (!strs) {
931 break;
932 }
933 FREE_STRINGS(ctx, strs);
934 }
935 break;
936 case LY_STMT_STATUS:
Radek Krejciad5963b2019-09-06 16:03:05 +0200937 case LY_STMT_CONFIG:
Radek Krejci38d85362019-09-05 16:26:38 +0200938 /* nothing to do */
939 break;
940 case LY_STMT_IF_FEATURE: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200941 struct lysc_iffeature *iff = *((struct lysc_iffeature **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200942 if (!iff) {
943 break;
944 }
945 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
946 /* single item */
947 lysc_iffeature_free(ctx, iff);
948 free(iff);
949 } else {
950 /* multiple items */
951 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
952 }
953 break;
954 }
955
Radek Krejci0f969882020-08-21 16:56:47 +0200956 /* TODO other statements */
Radek Krejci38d85362019-09-05 16:26:38 +0200957 default:
958 LOGINT(ctx);
959 }
960 }
961}
David Sedlákebd3acf2019-07-26 15:04:32 +0200962
963void
Michal Vaskob36053d2020-03-26 15:49:30 +0100964yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +0200965{
966 if (ctx) {
967 free(ctx);
968 }
969}
970
971void
Michal Vaskob36053d2020-03-26 15:49:30 +0100972yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +0200973{
974 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100975 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +0200976 free(ctx);
977 }
978}