blob: d19db152a09861f1a29784c8fdfc0e3a40e22c8f [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 Krejci19a96102018-11-15 13:38:09 +0100216 LY_LIST_FOR_SAFE(grp->data, next, node) {
217 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;
321
322 FREE_STRING(ctx, node->name);
323 FREE_STRING(ctx, node->dsc);
324 FREE_STRING(ctx, node->ref);
325 FREE_MEMBER(ctx, node->when, lysp_when_free);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200326 FREE_ARRAY(ctx, node->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100327 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
328
Michal Vaskod989ba02020-08-24 10:59:24 +0200329 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100330 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200331 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->musts, lysp_restr_free);
332 FREE_STRING(ctx, ((struct lysp_node_container *)node)->presence);
333 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100334 LY_LIST_FOR_SAFE((struct lysp_node *)((struct lysp_node_container *)node)->groupings, next, child) {
335 lysp_node_free(ctx, child);
336 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200337 LY_LIST_FOR_SAFE(((struct lysp_node_container *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100338 lysp_node_free(ctx, child);
339 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100340 LY_LIST_FOR_SAFE((struct lysp_node *)((struct lysp_node_container *)node)->actions, next, child) {
341 lysp_node_free(ctx, child);
342 }
343 LY_LIST_FOR_SAFE((struct lysp_node *)((struct lysp_node_container *)node)->notifs, next, child) {
344 lysp_node_free(ctx, child);
345 }
Radek Krejci19a96102018-11-15 13:38:09 +0100346 break;
347 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200348 FREE_ARRAY(ctx, ((struct lysp_node_leaf *)node)->musts, lysp_restr_free);
349 lysp_type_free(ctx, &((struct lysp_node_leaf *)node)->type);
350 FREE_STRING(ctx, ((struct lysp_node_leaf *)node)->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200351 FREE_STRING(ctx, ((struct lysp_node_leaf *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100352 break;
353 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200354 FREE_ARRAY(ctx, ((struct lysp_node_leaflist *)node)->musts, lysp_restr_free);
355 lysp_type_free(ctx, &((struct lysp_node_leaflist *)node)->type);
356 FREE_STRING(ctx, ((struct lysp_node_leaflist *)node)->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200357 FREE_ARRAY(ctx, ((struct lysp_node_leaflist *)node)->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100358 break;
359 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200360 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->musts, lysp_restr_free);
361 FREE_STRING(ctx, ((struct lysp_node_list *)node)->key);
362 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100363 LY_LIST_FOR_SAFE((struct lysp_node *)((struct lysp_node_list *)node)->groupings, next, child) {
364 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 Krejci2a9fc652021-01-22 17:44:34 +0100369 LY_LIST_FOR_SAFE((struct lysp_node *)((struct lysp_node_list *)node)->actions, next, child) {
370 lysp_node_free(ctx, child);
371 }
372 LY_LIST_FOR_SAFE((struct lysp_node *)((struct lysp_node_list *)node)->notifs, next, child) {
373 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:
Michal Vasko22df3f02020-08-24 13:29:22 +0200390 FREE_ARRAY(ctx, ((struct lysp_node_anydata *)node)->musts, lysp_restr_free);
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 Krejci2a9fc652021-01-22 17:44:34 +0100394 LY_LIST_FOR_SAFE((struct lysp_node *)((struct lysp_node_uses *)node)->augments, next, child) {
395 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);
401 LY_LIST_FOR_SAFE((struct lysp_node *)((struct lysp_node_action *)node)->groupings, next, child) {
402 lysp_node_free(ctx, child);
403 }
404 if (((struct lysp_node_action *)node)->input.nodetype) {
405 lysp_node_free(ctx, (struct lysp_node *)&((struct lysp_node_action *)node)->input);
406 }
407 if (((struct lysp_node_action *)node)->output.nodetype) {
408 lysp_node_free(ctx, (struct lysp_node *)&((struct lysp_node_action *)node)->output);
409 }
410 break;
411 case LYS_INPUT:
412 case LYS_OUTPUT:
413 FREE_ARRAY(ctx, ((struct lysp_node_action_inout *)node)->musts, lysp_restr_free);
414 FREE_ARRAY(ctx, ((struct lysp_node_action_inout *)node)->typedefs, lysp_tpdf_free);
415 LY_LIST_FOR_SAFE((struct lysp_node *)((struct lysp_node_action_inout *)node)->groupings, next, child) {
416 lysp_node_free(ctx, child);
417 }
418 LY_LIST_FOR_SAFE(((struct lysp_node_action_inout *)node)->data, next, child) {
419 lysp_node_free(ctx, child);
420 }
421 /* do not free the node, it is never standalone but part of the action node */
422 return;
423 case LYS_NOTIF:
424 FREE_ARRAY(ctx, ((struct lysp_node_notif *)node)->musts, lysp_restr_free);
425 FREE_ARRAY(ctx, ((struct lysp_node_notif *)node)->typedefs, lysp_tpdf_free);
426 LY_LIST_FOR_SAFE((struct lysp_node *)((struct lysp_node_notif *)node)->groupings, next, child) {
427 lysp_node_free(ctx, child);
428 }
429 LY_LIST_FOR_SAFE(((struct lysp_node_notif *)node)->data, next, child) {
430 lysp_node_free(ctx, child);
431 }
432 break;
433 case LYS_GROUPING:
434 lysp_grp_free(ctx, (struct lysp_node_grp *)node);
435 break;
436 case LYS_AUGMENT:
437 lysp_augment_free(ctx, (struct lysp_node_augment *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100438 break;
439 default:
440 LOGINT(ctx);
441 }
442
443 free(node);
444}
445
Radek Krejci15f10ab2020-11-03 14:14:14 +0100446void
Radek Krejci19a96102018-11-15 13:38:09 +0100447lysp_module_free(struct lysp_module *module)
448{
449 struct ly_ctx *ctx;
450 struct lysp_node *node, *next;
451
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100452 if (!module) {
453 return;
454 }
455 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100456
457 FREE_ARRAY(ctx, module->imports, lysp_import_free);
Radek Krejci771928a2021-01-19 13:42:36 +0100458 FREE_ARRAY(ctx, module->includes, module->is_submod ? lysp_include_free_submodule : lysp_include_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100459
Radek Krejci19a96102018-11-15 13:38:09 +0100460 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
461 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
462 FREE_ARRAY(ctx, module->features, lysp_feature_free);
463 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
464 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100465 LY_LIST_FOR_SAFE((struct lysp_node *)module->groupings, next, node) {
466 lysp_node_free(ctx, node);
467 }
Radek Krejci19a96102018-11-15 13:38:09 +0100468 LY_LIST_FOR_SAFE(module->data, next, node) {
469 lysp_node_free(ctx, node);
470 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100471 LY_LIST_FOR_SAFE((struct lysp_node *)module->augments, next, node) {
472 lysp_node_free(ctx, node);
473 }
474 LY_LIST_FOR_SAFE((struct lysp_node *)module->rpcs, next, node) {
475 lysp_node_free(ctx, node);
476 }
477 LY_LIST_FOR_SAFE((struct lysp_node *)module->notifs, next, node) {
478 lysp_node_free(ctx, node);
479 }
Radek Krejci19a96102018-11-15 13:38:09 +0100480 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
481 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
482
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200483 if (module->is_submod) {
484 struct lysp_submodule *submod = (struct lysp_submodule *)module;
485
486 FREE_STRING(ctx, submod->name);
487 FREE_STRING(ctx, submod->filepath);
488 FREE_STRING(ctx, submod->prefix);
489 FREE_STRING(ctx, submod->org);
490 FREE_STRING(ctx, submod->contact);
491 FREE_STRING(ctx, submod->dsc);
492 FREE_STRING(ctx, submod->ref);
493 }
494
Radek Krejci19a96102018-11-15 13:38:09 +0100495 free(module);
496}
497
Radek Krejci0af46292019-01-11 16:02:31 +0100498void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100499lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
500{
501 if (--(*ext)->refcount) {
502 return;
503 }
504 FREE_STRING(ctx, (*ext)->name);
505 FREE_STRING(ctx, (*ext)->argument);
506 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
507 free(*ext);
508}
509
510void
Radek Krejci19a96102018-11-15 13:38:09 +0100511lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
512{
fredganebc50572019-10-31 15:39:23 +0800513 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200514 ext->def->plugin->free(ctx, ext);
515 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100516 if (ext->def) {
517 lysc_extension_free(ctx, &ext->def);
518 }
Radek Krejci0935f412019-08-20 16:15:18 +0200519 FREE_STRING(ctx, ext->argument);
520 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
521}
522
523void
Radek Krejci19a96102018-11-15 13:38:09 +0100524lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
525{
526 LY_ARRAY_FREE(iff->features);
527 free(iff->expr);
528}
529
530static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100531lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100532{
Radek Krejci00b874b2019-02-12 10:54:50 +0100533 if (--(*w)->refcount) {
534 return;
535 }
536 lyxp_expr_free(ctx, (*w)->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200537 lysc_prefixes_free((*w)->prefixes);
Radek Krejci00b874b2019-02-12 10:54:50 +0100538 FREE_STRING(ctx, (*w)->dsc);
539 FREE_STRING(ctx, (*w)->ref);
540 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
541 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100542}
543
Radek Krejciccd20f12019-02-15 14:12:27 +0100544void
Radek Krejci58d171e2018-11-23 13:50:55 +0100545lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
546{
547 lyxp_expr_free(ctx, must->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200548 lysc_prefixes_free(must->prefixes);
Radek Krejci58d171e2018-11-23 13:50:55 +0100549 FREE_STRING(ctx, must->emsg);
550 FREE_STRING(ctx, must->eapptag);
Radek Krejcic8b31002019-01-08 10:24:45 +0100551 FREE_STRING(ctx, must->dsc);
552 FREE_STRING(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100553 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
554}
555
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100556void
Radek Krejci19a96102018-11-15 13:38:09 +0100557lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
558{
559 FREE_STRING(ctx, ident->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100560 FREE_STRING(ctx, ident->dsc);
561 FREE_STRING(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100562 LY_ARRAY_FREE(ident->derived);
563 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
564}
565
Radek Krejci19a96102018-11-15 13:38:09 +0100566static void
567lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
568{
569 LY_ARRAY_FREE(range->parts);
570 FREE_STRING(ctx, range->eapptag);
571 FREE_STRING(ctx, range->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100572 FREE_STRING(ctx, range->dsc);
573 FREE_STRING(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100574 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
575}
576
577static void
578lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
579{
580 if (--(*pattern)->refcount) {
581 return;
582 }
Radek Krejci54579462019-04-30 12:47:06 +0200583 pcre2_code_free((*pattern)->code);
584 FREE_STRING(ctx, (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +0100585 FREE_STRING(ctx, (*pattern)->eapptag);
586 FREE_STRING(ctx, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100587 FREE_STRING(ctx, (*pattern)->dsc);
588 FREE_STRING(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100589 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
590 free(*pattern);
591}
592
593static void
Radek Krejci693262f2019-04-29 15:23:20 +0200594lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100595{
596 FREE_STRING(ctx, item->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100597 FREE_STRING(ctx, item->dsc);
598 FREE_STRING(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100599 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
600}
601
Radek Krejcia3045382018-11-22 14:30:31 +0100602static void
603lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
604{
605 lysc_type_free(ctx, *type);
606}
Radek Krejci0f969882020-08-21 16:56:47 +0200607
Radek Krejcicdfecd92018-11-26 11:27:32 +0100608void
Radek Krejci19a96102018-11-15 13:38:09 +0100609lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
610{
611 if (--type->refcount) {
612 return;
613 }
Radek Krejcib915ac92020-08-14 23:31:04 +0200614
Michal Vaskod989ba02020-08-24 10:59:24 +0200615 switch (type->basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100616 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +0200617 FREE_MEMBER(ctx, ((struct lysc_type_bin *)type)->length, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100618 break;
619 case LY_TYPE_BITS:
Michal Vasko22df3f02020-08-24 13:29:22 +0200620 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 +0100621 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100622 case LY_TYPE_DEC64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200623 FREE_MEMBER(ctx, ((struct lysc_type_dec *)type)->range, lysc_range_free);
Radek Krejci6cba4292018-11-15 17:33:29 +0100624 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100625 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +0200626 FREE_MEMBER(ctx, ((struct lysc_type_str *)type)->length, lysc_range_free);
627 FREE_ARRAY(ctx, ((struct lysc_type_str *)type)->patterns, lysc_pattern_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100628 break;
629 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +0200630 FREE_ARRAY(ctx, ((struct lysc_type_enum *)type)->enums, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100631 break;
632 case LY_TYPE_INT8:
633 case LY_TYPE_UINT8:
634 case LY_TYPE_INT16:
635 case LY_TYPE_UINT16:
636 case LY_TYPE_INT32:
637 case LY_TYPE_UINT32:
638 case LY_TYPE_INT64:
639 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200640 FREE_MEMBER(ctx, ((struct lysc_type_num *)type)->range, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100641 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100642 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +0200643 LY_ARRAY_FREE(((struct lysc_type_identityref *)type)->bases);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100644 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100645 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +0200646 FREE_ARRAY(ctx, ((struct lysc_type_union *)type)->types, lysc_type2_free);
Radek Krejcia3045382018-11-22 14:30:31 +0100647 break;
648 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200649 lyxp_expr_free(ctx, ((struct lysc_type_leafref *)type)->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200650 lysc_prefixes_free(((struct lysc_type_leafref *)type)->prefixes);
Radek Krejcia3045382018-11-22 14:30:31 +0100651 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100652 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100653 case LY_TYPE_BOOL:
654 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100655 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100656 /* nothing to do */
657 break;
658 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200659
660 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100661 free(type);
662}
663
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100664void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100665lysc_node_action_inout_free(struct ly_ctx *ctx, struct lysc_node_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100666{
667 struct lysc_node *child, *child_next;
668
Radek Krejcif538ce52019-03-05 10:46:14 +0100669 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
670 LY_LIST_FOR_SAFE(inout->data, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100671 lysc_node_free_(ctx, child);
Radek Krejcif538ce52019-03-05 10:46:14 +0100672 }
673}
674
675void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100676lysc_node_action_free(struct ly_ctx *ctx, struct lysc_node_action *action)
Radek Krejcif538ce52019-03-05 10:46:14 +0100677{
Radek Krejci2a9fc652021-01-22 17:44:34 +0100678 if (action->input.nodetype) {
679 lysc_node_free_(ctx, (struct lysc_node *)&action->input);
680 }
681 if (action->output.nodetype) {
682 lysc_node_free_(ctx, (struct lysc_node *)&action->output);
683 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100684}
685
Radek Krejcifc11bd72019-04-11 16:00:05 +0200686void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100687lysc_node_notif_free(struct ly_ctx *ctx, struct lysc_node_notif *notif)
Radek Krejcifc11bd72019-04-11 16:00:05 +0200688{
689 struct lysc_node *child, *child_next;
690
Radek Krejcifc11bd72019-04-11 16:00:05 +0200691 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
692 LY_LIST_FOR_SAFE(notif->data, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100693 lysc_node_free_(ctx, child);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200694 }
695}
696
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200697void
Radek Krejci19a96102018-11-15 13:38:09 +0100698lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
699{
700 struct lysc_node *child, *child_next;
701
702 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100703 lysc_node_free_(ctx, child);
Radek Krejci19a96102018-11-15 13:38:09 +0100704 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100705 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
706 lysc_node_free_(ctx, child);
707 }
708 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
709 lysc_node_free_(ctx, child);
710 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100711 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100712}
713
714static void
715lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
716{
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 Krejci42452ac2018-11-28 17:09:52 +0100734 FREE_ARRAY(ctx, node->musts, lysc_must_free);
735 if (node->type) {
736 lysc_type_free(ctx, node->type);
737 }
738 FREE_STRING(ctx, node->units);
739 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200740 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200741 lysc_type_free(ctx, (struct lysc_type *)node->dflts[u]->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200742 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100743 }
744 LY_ARRAY_FREE(node->dflts);
745}
746
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100747static void
748lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
749{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200750 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100751 struct lysc_node *child, *child_next;
752
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100753 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100754 lysc_node_free_(ctx, child);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100755 }
756 FREE_ARRAY(ctx, node->musts, lysc_must_free);
757
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100758 LY_ARRAY_FOR(node->uniques, u) {
759 LY_ARRAY_FREE(node->uniques[u]);
760 }
761 LY_ARRAY_FREE(node->uniques);
762
Radek Krejci2a9fc652021-01-22 17:44:34 +0100763 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
764 lysc_node_free_(ctx, child);
765 }
766 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
767 lysc_node_free_(ctx, child);
768 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100769}
770
Radek Krejci056d0a82018-12-06 16:57:25 +0100771static void
772lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
773{
774 struct lysc_node *child, *child_next;
775
Michal Vasko20424b42020-08-31 12:29:38 +0200776 LY_LIST_FOR_SAFE((struct lysc_node *)node->cases, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100777 lysc_node_free_(ctx, child);
Michal Vasko20424b42020-08-31 12:29:38 +0200778 }
779}
780
781static void
782lysc_node_case_free(struct ly_ctx *ctx, struct lysc_node_case *node)
783{
784 struct lysc_node *child, *child_next;
785
786 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100787 lysc_node_free_(ctx, child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100788 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100789}
Radek Krejci056d0a82018-12-06 16:57:25 +0100790
Radek Krejci9800fb82018-12-13 14:26:23 +0100791static void
792lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
793{
794 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100795}
796
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100797static void
798lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +0100799{
Radek Krejci2a9fc652021-01-22 17:44:34 +0100800 ly_bool inout = 0;
801
Radek Krejci19a96102018-11-15 13:38:09 +0100802 /* common part */
803 FREE_STRING(ctx, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +0100804 FREE_STRING(ctx, node->dsc);
805 FREE_STRING(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100806
807 /* nodetype-specific part */
Michal Vaskod989ba02020-08-24 10:59:24 +0200808 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100809 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200810 lysc_node_container_free(ctx, (struct lysc_node_container *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100811 break;
812 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200813 lysc_node_leaf_free(ctx, (struct lysc_node_leaf *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100814 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100815 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200816 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist *)node);
Radek Krejci42452ac2018-11-28 17:09:52 +0100817 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100818 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200819 lysc_node_list_free(ctx, (struct lysc_node_list *)node);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100820 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100821 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200822 lysc_node_choice_free(ctx, (struct lysc_node_choice *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100823 break;
824 case LYS_CASE:
Michal Vasko20424b42020-08-31 12:29:38 +0200825 lysc_node_case_free(ctx, (struct lysc_node_case *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100826 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100827 case LYS_ANYDATA:
828 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200829 lysc_node_anydata_free(ctx, (struct lysc_node_anydata *)node);
Radek Krejci9800fb82018-12-13 14:26:23 +0100830 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100831 case LYS_RPC:
832 case LYS_ACTION:
833 lysc_node_action_free(ctx, (struct lysc_node_action *)node);
834 break;
835 case LYS_INPUT:
836 case LYS_OUTPUT:
837 lysc_node_action_inout_free(ctx, (struct lysc_node_action_inout *)node);
838 inout = 1;
839 break;
840 case LYS_NOTIF:
841 lysc_node_notif_free(ctx, (struct lysc_node_notif *)node);
842 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100843 default:
844 LOGINT(ctx);
845 }
846
Radek Krejci00b874b2019-02-12 10:54:50 +0100847 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100848 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100849
850 if (!inout) {
851 free(node);
852 }
Radek Krejci19a96102018-11-15 13:38:09 +0100853}
854
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100855void
856lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node, ly_bool unlink)
857{
858 struct lysc_node *iter, **child_p;
859
Radek Krejci2a9fc652021-01-22 17:44:34 +0100860 if (node->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
861 /* nothing to do - inouts are part of actions and cannot be unlinked/freed separately */
862 return;
863 }
864
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100865 if (unlink) {
866 /* unlink from siblings */
867 if (node->prev->next) {
868 node->prev->next = node->next;
869 }
870 if (node->next) {
871 node->next->prev = node->prev;
872 } else {
873 /* unlinking the last node */
874 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100875 if (node->nodetype == LYS_ACTION) {
876 iter = (struct lysc_node *)lysc_node_actions(node->parent);
877 } else if (node->nodetype == LYS_NOTIF) {
878 iter = (struct lysc_node *)lysc_node_notifs(node->parent);
879 } else {
880 iter = (struct lysc_node *)lysc_node_children(node->parent, node->flags & LYS_CONFIG_MASK);
881 }
Michal Vasko539c4a62020-11-03 17:21:34 +0100882 LY_CHECK_ERR_RET(!iter, LOGINT(ctx), );
Radek Krejci2a9fc652021-01-22 17:44:34 +0100883 } else if (node->nodetype == LYS_RPC) {
884 iter = (struct lysc_node *)node->module->compiled->rpcs;
885 } else if (node->nodetype == LYS_NOTIF) {
886 iter = (struct lysc_node *)node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100887 } else {
888 iter = node->module->compiled->data;
889 }
890 /* update the "last" pointer from the first node */
891 iter->prev = node->prev;
892 }
893
894 /* unlink from parent */
895 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100896 if (node->nodetype == LYS_ACTION) {
897 child_p = (struct lysc_node **)lysc_node_actions_p(node->parent);
898 } else if (node->nodetype == LYS_NOTIF) {
899 child_p = (struct lysc_node **)lysc_node_notifs_p(node->parent);
900 } else {
901 child_p = lysc_node_children_p(node->parent, node->flags & LYS_CONFIG_MASK);
902 }
903 } else if (node->nodetype == LYS_RPC) {
904 child_p = (struct lysc_node **)&node->module->compiled->rpcs;
905 } else if (node->nodetype == LYS_NOTIF) {
906 child_p = (struct lysc_node **)&node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100907 } else {
908 child_p = &node->module->compiled->data;
909 }
910 if (child_p && (*child_p == node)) {
911 /* the node is the first child */
912 *child_p = node->next;
913 }
914 }
915
916 lysc_node_free_(ctx, node);
917}
918
Radek Krejci19a96102018-11-15 13:38:09 +0100919static void
920lysc_module_free_(struct lysc_module *module)
921{
922 struct ly_ctx *ctx;
923 struct lysc_node *node, *node_next;
924
Michal Vaskod989ba02020-08-24 10:59:24 +0200925 LY_CHECK_ARG_RET(NULL, module, );
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100926 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100927
Radek Krejci19a96102018-11-15 13:38:09 +0100928 LY_LIST_FOR_SAFE(module->data, node_next, node) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100929 lysc_node_free_(ctx, node);
Radek Krejci19a96102018-11-15 13:38:09 +0100930 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100931 LY_LIST_FOR_SAFE((struct lysc_node *)module->rpcs, node_next, node) {
932 lysc_node_free_(ctx, node);
933 }
934 LY_LIST_FOR_SAFE((struct lysc_node *)module->notifs, node_next, node) {
935 lysc_node_free_(ctx, node);
936 }
Radek Krejci19a96102018-11-15 13:38:09 +0100937 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
938
939 free(module);
940}
941
942void
943lysc_module_free(struct lysc_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
944{
Radek Krejci9b042892019-02-13 14:28:44 +0100945 /* TODO use the destructor, this just suppress warning about unused parameter */
946 (void) private_destructor;
947
Radek Krejci19a96102018-11-15 13:38:09 +0100948 if (module) {
949 lysc_module_free_(module);
950 }
951}
952
953void
954lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
955{
956 if (!module) {
957 return;
958 }
959
960 lysc_module_free(module->compiled, private_destructor);
Radek Krejci80d281e2020-09-14 17:42:54 +0200961 FREE_ARRAY(module->ctx, module->identities, lysc_ident_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100962 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100963
Michal Vasko7f45cf22020-10-01 12:49:44 +0200964 LY_ARRAY_FREE(module->augmented_by);
965 LY_ARRAY_FREE(module->deviated_by);
966
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100967 FREE_STRING(module->ctx, module->name);
Radek Krejci0af46292019-01-11 16:02:31 +0100968 FREE_STRING(module->ctx, module->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100969 FREE_STRING(module->ctx, module->ns);
970 FREE_STRING(module->ctx, module->prefix);
971 FREE_STRING(module->ctx, module->filepath);
972 FREE_STRING(module->ctx, module->org);
973 FREE_STRING(module->ctx, module->contact);
974 FREE_STRING(module->ctx, module->dsc);
975 FREE_STRING(module->ctx, module->ref);
976
Radek Krejci19a96102018-11-15 13:38:09 +0100977 free(module);
978}
Michal Vasko33ff9422020-07-03 09:50:39 +0200979
Radek Krejci38d85362019-09-05 16:26:38 +0200980API void
981lysc_extension_instance_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
982{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200983 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
Radek Krejci38d85362019-09-05 16:26:38 +0200984 if (!substmts[u].storage) {
985 continue;
986 }
987
Michal Vaskod989ba02020-08-24 10:59:24 +0200988 switch (substmts[u].stmt) {
Radek Krejci38d85362019-09-05 16:26:38 +0200989 case LY_STMT_TYPE:
990 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
991 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +0200992 struct lysc_type *type = *((struct lysc_type **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200993 if (!type) {
994 break;
995 }
996 lysc_type_free(ctx, type);
997 } else {
998 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +0200999 struct lysc_type **types = *((struct lysc_type ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001000 if (!types) {
1001 break;
1002 }
1003 FREE_ARRAY(ctx, types, lysc_type2_free);
1004 }
1005 break;
1006 case LY_STMT_UNITS:
1007 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1008 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02001009 const char *str = *((const char **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001010 if (!str) {
1011 break;
1012 }
1013 FREE_STRING(ctx, str);
1014 } else {
1015 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +02001016 const char **strs = *((const char ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001017 if (!strs) {
1018 break;
1019 }
1020 FREE_STRINGS(ctx, strs);
1021 }
1022 break;
1023 case LY_STMT_STATUS:
Radek Krejciad5963b2019-09-06 16:03:05 +02001024 case LY_STMT_CONFIG:
Radek Krejci38d85362019-09-05 16:26:38 +02001025 /* nothing to do */
1026 break;
1027 case LY_STMT_IF_FEATURE: {
Michal Vasko22df3f02020-08-24 13:29:22 +02001028 struct lysc_iffeature *iff = *((struct lysc_iffeature **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001029 if (!iff) {
1030 break;
1031 }
1032 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1033 /* single item */
1034 lysc_iffeature_free(ctx, iff);
1035 free(iff);
1036 } else {
1037 /* multiple items */
1038 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
1039 }
1040 break;
1041 }
1042
Radek Krejci0f969882020-08-21 16:56:47 +02001043 /* TODO other statements */
Radek Krejci38d85362019-09-05 16:26:38 +02001044 default:
1045 LOGINT(ctx);
1046 }
1047 }
1048}
David Sedlákebd3acf2019-07-26 15:04:32 +02001049
1050void
Michal Vaskob36053d2020-03-26 15:49:30 +01001051yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001052{
1053 if (ctx) {
1054 free(ctx);
1055 }
1056}
1057
1058void
Michal Vaskob36053d2020-03-26 15:49:30 +01001059yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001060{
1061 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001062 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +02001063 free(ctx);
1064 }
1065}