blob: 8bcfb5a52242797424d196bbf20fd5bafb3ec219 [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"
Michal Vaskofc2cd072021-02-24 13:17:17 +010024#include "tree_data_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010026#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "xml.h"
Radek Krejci19a96102018-11-15 13:38:09 +010028#include "xpath.h"
29
Radek Krejci2a9fc652021-01-22 17:44:34 +010030void lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp);
Michal Vasko5fe75f12020-03-02 13:52:37 +010031void lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010032static void lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node);
Radek Krejci19a96102018-11-15 13:38:09 +010033
34static void
35lysp_stmt_free(struct ly_ctx *ctx, struct lysp_stmt *stmt)
36{
37 struct lysp_stmt *child, *next;
38
Michal Vaskoe180ed02021-02-05 16:31:20 +010039 lydict_remove(ctx, stmt->stmt);
40 lydict_remove(ctx, stmt->arg);
Michal Vaskofc2cd072021-02-24 13:17:17 +010041 ly_free_prefix_data(stmt->format, stmt->prefix_data);
Radek Krejci19a96102018-11-15 13:38:09 +010042
43 LY_LIST_FOR_SAFE(stmt->child, next, child) {
44 lysp_stmt_free(ctx, child);
45 }
46
47 free(stmt);
48}
49
Radek Krejci2d7a47b2019-05-16 13:34:10 +020050void
Radek Krejci19a96102018-11-15 13:38:09 +010051lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext)
52{
53 struct lysp_stmt *stmt, *next;
54
Michal Vaskoe180ed02021-02-05 16:31:20 +010055 lydict_remove(ctx, ext->name);
56 lydict_remove(ctx, ext->argument);
Michal Vaskofc2cd072021-02-24 13:17:17 +010057 ly_free_prefix_data(ext->format, ext->prefix_data);
Radek Krejci19a96102018-11-15 13:38:09 +010058
59 LY_LIST_FOR_SAFE(ext->child, next, stmt) {
60 lysp_stmt_free(ctx, stmt);
61 }
62}
63
David Sedlák298ff6d2019-07-26 14:29:03 +020064void
Radek Krejci19a96102018-11-15 13:38:09 +010065lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import)
66{
67 /* imported module is freed directly from the context's list */
Michal Vaskoe180ed02021-02-05 16:31:20 +010068 lydict_remove(ctx, import->name);
69 lydict_remove(ctx, import->prefix);
70 lydict_remove(ctx, import->dsc);
71 lydict_remove(ctx, import->ref);
Radek Krejci19a96102018-11-15 13:38:09 +010072 FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free);
73}
74
Radek Krejci771928a2021-01-19 13:42:36 +010075/**
76 * @brief Common function to erase include record in main module and submodule.
77 *
78 * There is a difference since the main module is expected to have the complete list if the included submodules and
79 * the parsed submodule is shared with any include in a submodule. Therefore, the referenced submodules in the include
80 * record are freed only from main module's records.
81 *
82 * @param[in] ctx libyang context
83 * @param[in] include The include record to be erased, the record itself is not freed.
84 * @param[in] main_module Flag to get know if the include record is placed in main module so also the referenced submodule
85 * is supposed to be freed.
86 */
87static void
88lysp_include_free_(struct ly_ctx *ctx, struct lysp_include *include, ly_bool main_module)
Radek Krejci19a96102018-11-15 13:38:09 +010089{
Radek Krejci771928a2021-01-19 13:42:36 +010090 if (main_module && include->submodule) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +020091 lysp_module_free((struct lysp_module *)include->submodule);
Radek Krejci19a96102018-11-15 13:38:09 +010092 }
Michal Vaskoe180ed02021-02-05 16:31:20 +010093 lydict_remove(ctx, include->name);
94 lydict_remove(ctx, include->dsc);
95 lydict_remove(ctx, include->ref);
Radek Krejci19a96102018-11-15 13:38:09 +010096 FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free);
97}
98
David Sedlákaa854b02019-07-22 14:17:10 +020099void
Radek Krejci771928a2021-01-19 13:42:36 +0100100lysp_include_free_submodule(struct ly_ctx *ctx, struct lysp_include *include)
101{
102 return lysp_include_free_(ctx, include, 0);
103}
104
105void
106lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include)
107{
108 return lysp_include_free_(ctx, include, 1);
109}
110
111void
Radek Krejci19a96102018-11-15 13:38:09 +0100112lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev)
113{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100114 lydict_remove(ctx, rev->dsc);
115 lydict_remove(ctx, rev->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100116 FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free);
117}
118
David Sedlák986cb412019-07-04 13:10:11 +0200119void
Radek Krejci19a96102018-11-15 13:38:09 +0100120lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext)
121{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100122 lydict_remove(ctx, ext->name);
123 lydict_remove(ctx, ext->argument);
124 lydict_remove(ctx, ext->dsc);
125 lydict_remove(ctx, ext->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100126 FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free);
Michal Vasko5fe75f12020-03-02 13:52:37 +0100127 if (ext->compiled) {
128 lysc_extension_free(ctx, &ext->compiled);
129 }
Radek Krejci19a96102018-11-15 13:38:09 +0100130}
131
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200132void
Radek Krejci19a96102018-11-15 13:38:09 +0100133lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat)
134{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100135 lydict_remove(ctx, feat->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200136 FREE_ARRAY(ctx, feat->iffeatures, lysp_qname_free);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100137 FREE_ARRAY(ctx, feat->iffeatures_c, lysc_iffeature_free);
138 LY_ARRAY_FREE(feat->depfeatures);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100139 lydict_remove(ctx, feat->dsc);
140 lydict_remove(ctx, feat->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100141 FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
142}
143
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200144void
Radek Krejci19a96102018-11-15 13:38:09 +0100145lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident)
146{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100147 lydict_remove(ctx, ident->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200148 FREE_ARRAY(ctx, ident->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100149 FREE_STRINGS(ctx, ident->bases);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100150 lydict_remove(ctx, ident->dsc);
151 lydict_remove(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100152 FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
153}
154
Michal Vasko7f45cf22020-10-01 12:49:44 +0200155void
Radek Krejci19a96102018-11-15 13:38:09 +0100156lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
157{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100158 lydict_remove(ctx, restr->arg.str);
159 lydict_remove(ctx, restr->emsg);
160 lydict_remove(ctx, restr->eapptag);
161 lydict_remove(ctx, restr->dsc);
162 lydict_remove(ctx, restr->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100163 FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
164}
165
166static void
167lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item)
168{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100169 lydict_remove(ctx, item->name);
170 lydict_remove(ctx, item->dsc);
171 lydict_remove(ctx, item->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200172 FREE_ARRAY(ctx, item->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100173 FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
174}
175
Radek Krejcicdfecd92018-11-26 11:27:32 +0100176void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type);
Michal Vasko004d3152020-06-11 19:59:22 +0200177
David Sedlák32488102019-07-15 17:44:10 +0200178void
Radek Krejci19a96102018-11-15 13:38:09 +0100179lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type)
180{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100181 lydict_remove(ctx, type->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100182 FREE_MEMBER(ctx, type->range, lysp_restr_free);
183 FREE_MEMBER(ctx, type->length, lysp_restr_free);
184 FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
185 FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
186 FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
Michal Vasko004d3152020-06-11 19:59:22 +0200187 lyxp_expr_free(ctx, type->path);
Radek Krejci19a96102018-11-15 13:38:09 +0100188 FREE_STRINGS(ctx, type->bases);
189 FREE_ARRAY(ctx, type->types, lysp_type_free);
190 FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
191 if (type->compiled) {
192 lysc_type_free(ctx, type->compiled);
193 }
194}
195
David Sedlák04e17b22019-07-19 15:29:48 +0200196void
Radek Krejci19a96102018-11-15 13:38:09 +0100197lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
198{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100199 lydict_remove(ctx, tpdf->name);
200 lydict_remove(ctx, tpdf->units);
201 lydict_remove(ctx, tpdf->dflt.str);
202 lydict_remove(ctx, tpdf->dsc);
203 lydict_remove(ctx, tpdf->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100204 FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
205
206 lysp_type_free(ctx, &tpdf->type);
207
208}
209
Radek Krejcif538ce52019-03-05 10:46:14 +0100210void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100211lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp)
Radek Krejci19a96102018-11-15 13:38:09 +0100212{
213 struct lysp_node *node, *next;
214
Radek Krejci19a96102018-11-15 13:38:09 +0100215 FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100216 LY_LIST_FOR_SAFE((struct lysp_node *)grp->groupings, next, node) {
217 lysp_node_free(ctx, node);
218 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100219 LY_LIST_FOR_SAFE(grp->child, next, node) {
Radek Krejci19a96102018-11-15 13:38:09 +0100220 lysp_node_free(ctx, node);
221 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100222 LY_LIST_FOR_SAFE((struct lysp_node *)grp->actions, next, node) {
223 lysp_node_free(ctx, node);
224 }
225 LY_LIST_FOR_SAFE((struct lysp_node *)grp->notifs, next, node) {
226 lysp_node_free(ctx, node);
227 }
Radek Krejci19a96102018-11-15 13:38:09 +0100228}
229
Radek Krejcif09e4e82019-06-14 15:08:11 +0200230void
Radek Krejci19a96102018-11-15 13:38:09 +0100231lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
232{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100233 lydict_remove(ctx, when->cond);
234 lydict_remove(ctx, when->dsc);
235 lydict_remove(ctx, when->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100236 FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
237}
238
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200239void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100240lysp_augment_free(struct ly_ctx *ctx, struct lysp_node_augment *augment)
Radek Krejci19a96102018-11-15 13:38:09 +0100241{
242 struct lysp_node *node, *next;
243
Radek Krejci19a96102018-11-15 13:38:09 +0100244 LY_LIST_FOR_SAFE(augment->child, next, node) {
245 lysp_node_free(ctx, node);
246 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100247 LY_LIST_FOR_SAFE((struct lysp_node *)augment->actions, next, node) {
248 lysp_node_free(ctx, node);
249 }
250 LY_LIST_FOR_SAFE((struct lysp_node *)augment->notifs, next, node) {
251 lysp_node_free(ctx, node);
252 }
Radek Krejci19a96102018-11-15 13:38:09 +0100253}
254
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200255void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200256lysp_qname_free(struct ly_ctx *ctx, struct lysp_qname *qname)
257{
258 if (qname) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100259 lydict_remove(ctx, qname->str);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200260 }
261}
262
263void
Radek Krejci19a96102018-11-15 13:38:09 +0100264lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
265{
Michal Vasko22df3f02020-08-24 13:29:22 +0200266 struct lysp_deviate_add *add = (struct lysp_deviate_add *)d;
267 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl *)d;
Radek Krejci19a96102018-11-15 13:38:09 +0100268
269 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
Michal Vaskod989ba02020-08-24 10:59:24 +0200270 switch (d->mod) {
Radek Krejci19a96102018-11-15 13:38:09 +0100271 case LYS_DEV_NOT_SUPPORTED:
272 /* nothing to do */
273 break;
274 case LYS_DEV_ADD:
275 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100276 lydict_remove(ctx, add->units);
Radek Krejci19a96102018-11-15 13:38:09 +0100277 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200278 FREE_ARRAY(ctx, add->uniques, lysp_qname_free);
279 FREE_ARRAY(ctx, add->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100280 break;
281 case LYS_DEV_REPLACE:
282 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100283 lydict_remove(ctx, rpl->units);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200284 lysp_qname_free(ctx, &rpl->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +0100285 break;
286 default:
287 LOGINT(ctx);
288 break;
289 }
290}
291
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200292void
Radek Krejci19a96102018-11-15 13:38:09 +0100293lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
294{
295 struct lysp_deviate *next, *iter;
296
Michal Vaskoe180ed02021-02-05 16:31:20 +0100297 lydict_remove(ctx, dev->nodeid);
298 lydict_remove(ctx, dev->dsc);
299 lydict_remove(ctx, dev->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100300 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
301 lysp_deviate_free(ctx, iter);
302 free(iter);
303 }
304 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
305}
306
David Sedlákd2d676a2019-07-22 11:28:19 +0200307void
Radek Krejci19a96102018-11-15 13:38:09 +0100308lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
309{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100310 lydict_remove(ctx, ref->nodeid);
311 lydict_remove(ctx, ref->dsc);
312 lydict_remove(ctx, ref->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200313 FREE_ARRAY(ctx, ref->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100314 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100315 lydict_remove(ctx, ref->presence);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200316 FREE_ARRAY(ctx, ref->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100317 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
318}
319
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200320void
Radek Krejci19a96102018-11-15 13:38:09 +0100321lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
322{
323 struct lysp_node *child, *next;
Radek Krejci9a3823e2021-01-27 20:26:46 +0100324 struct lysp_restr *musts = lysp_node_musts(node);
325 struct lysp_when *when = lysp_node_when(node);
Radek Krejci19a96102018-11-15 13:38:09 +0100326
Michal Vaskoe180ed02021-02-05 16:31:20 +0100327 lydict_remove(ctx, node->name);
328 lydict_remove(ctx, node->dsc);
329 lydict_remove(ctx, node->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200330 FREE_ARRAY(ctx, node->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100331 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
332
Radek Krejci9a3823e2021-01-27 20:26:46 +0100333 FREE_MEMBER(ctx, when, lysp_when_free);
334 FREE_ARRAY(ctx, musts, lysp_restr_free);
335
Michal Vaskod989ba02020-08-24 10:59:24 +0200336 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100337 case LYS_CONTAINER:
Michal Vaskoe180ed02021-02-05 16:31:20 +0100338 lydict_remove(ctx, ((struct lysp_node_container *)node)->presence);
Michal Vasko22df3f02020-08-24 13:29:22 +0200339 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100340 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100341 lysp_node_free(ctx, child);
342 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200343 LY_LIST_FOR_SAFE(((struct lysp_node_container *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100344 lysp_node_free(ctx, child);
345 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100346 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->actions->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100347 lysp_node_free(ctx, child);
348 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100349 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->notifs->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100350 lysp_node_free(ctx, child);
351 }
Radek Krejci19a96102018-11-15 13:38:09 +0100352 break;
353 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200354 lysp_type_free(ctx, &((struct lysp_node_leaf *)node)->type);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100355 lydict_remove(ctx, ((struct lysp_node_leaf *)node)->units);
356 lydict_remove(ctx, ((struct lysp_node_leaf *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100357 break;
358 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200359 lysp_type_free(ctx, &((struct lysp_node_leaflist *)node)->type);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100360 lydict_remove(ctx, ((struct lysp_node_leaflist *)node)->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200361 FREE_ARRAY(ctx, ((struct lysp_node_leaflist *)node)->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100362 break;
363 case LYS_LIST:
Michal Vaskoe180ed02021-02-05 16:31:20 +0100364 lydict_remove(ctx, ((struct lysp_node_list *)node)->key);
Michal Vasko22df3f02020-08-24 13:29:22 +0200365 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100366 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100367 lysp_node_free(ctx, child);
368 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200369 LY_LIST_FOR_SAFE(((struct lysp_node_list *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100370 lysp_node_free(ctx, child);
371 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100372 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->actions->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100373 lysp_node_free(ctx, child);
374 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100375 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->notifs->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100376 lysp_node_free(ctx, child);
377 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200378 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->uniques, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100379 break;
380 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200381 LY_LIST_FOR_SAFE(((struct lysp_node_choice *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100382 lysp_node_free(ctx, child);
383 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100384 lydict_remove(ctx, ((struct lysp_node_choice *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100385 break;
386 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200387 LY_LIST_FOR_SAFE(((struct lysp_node_case *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100388 lysp_node_free(ctx, child);
389 }
390 break;
391 case LYS_ANYDATA:
392 case LYS_ANYXML:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100393 /* nothing special to do */
Radek Krejci19a96102018-11-15 13:38:09 +0100394 break;
395 case LYS_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +0200396 FREE_ARRAY(ctx, ((struct lysp_node_uses *)node)->refines, lysp_refine_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100397 LY_LIST_FOR_SAFE(&((struct lysp_node_uses *)node)->augments->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100398 lysp_node_free(ctx, child);
399 }
400 break;
401 case LYS_RPC:
402 case LYS_ACTION:
403 FREE_ARRAY(ctx, ((struct lysp_node_action *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100404 LY_LIST_FOR_SAFE(&((struct lysp_node_action *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100405 lysp_node_free(ctx, child);
406 }
407 if (((struct lysp_node_action *)node)->input.nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100408 lysp_node_free(ctx, &((struct lysp_node_action *)node)->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100409 }
410 if (((struct lysp_node_action *)node)->output.nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100411 lysp_node_free(ctx, &((struct lysp_node_action *)node)->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100412 }
413 break;
414 case LYS_INPUT:
415 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100416 FREE_ARRAY(ctx, ((struct lysp_node_action_inout *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100417 LY_LIST_FOR_SAFE(&((struct lysp_node_action_inout *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100418 lysp_node_free(ctx, child);
419 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100420 LY_LIST_FOR_SAFE(((struct lysp_node_action_inout *)node)->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100421 lysp_node_free(ctx, child);
422 }
423 /* do not free the node, it is never standalone but part of the action node */
424 return;
425 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100426 FREE_ARRAY(ctx, ((struct lysp_node_notif *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100427 LY_LIST_FOR_SAFE(&((struct lysp_node_notif *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100428 lysp_node_free(ctx, child);
429 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100430 LY_LIST_FOR_SAFE(((struct lysp_node_notif *)node)->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100431 lysp_node_free(ctx, child);
432 }
433 break;
434 case LYS_GROUPING:
435 lysp_grp_free(ctx, (struct lysp_node_grp *)node);
436 break;
437 case LYS_AUGMENT:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100438 lysp_augment_free(ctx, ((struct lysp_node_augment *)node));
Radek Krejci19a96102018-11-15 13:38:09 +0100439 break;
440 default:
441 LOGINT(ctx);
442 }
443
444 free(node);
445}
446
Radek Krejci15f10ab2020-11-03 14:14:14 +0100447void
Radek Krejci19a96102018-11-15 13:38:09 +0100448lysp_module_free(struct lysp_module *module)
449{
450 struct ly_ctx *ctx;
451 struct lysp_node *node, *next;
452
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100453 if (!module) {
454 return;
455 }
456 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100457
458 FREE_ARRAY(ctx, module->imports, lysp_import_free);
Radek Krejci771928a2021-01-19 13:42:36 +0100459 FREE_ARRAY(ctx, module->includes, module->is_submod ? lysp_include_free_submodule : lysp_include_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100460
Radek Krejci19a96102018-11-15 13:38:09 +0100461 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
462 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
463 FREE_ARRAY(ctx, module->features, lysp_feature_free);
464 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
465 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100466 LY_LIST_FOR_SAFE((struct lysp_node *)module->groupings, next, node) {
467 lysp_node_free(ctx, node);
468 }
Radek Krejci19a96102018-11-15 13:38:09 +0100469 LY_LIST_FOR_SAFE(module->data, next, node) {
470 lysp_node_free(ctx, node);
471 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100472 LY_LIST_FOR_SAFE((struct lysp_node *)module->augments, next, node) {
473 lysp_node_free(ctx, node);
474 }
475 LY_LIST_FOR_SAFE((struct lysp_node *)module->rpcs, next, node) {
476 lysp_node_free(ctx, node);
477 }
478 LY_LIST_FOR_SAFE((struct lysp_node *)module->notifs, next, node) {
479 lysp_node_free(ctx, node);
480 }
Radek Krejci19a96102018-11-15 13:38:09 +0100481 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
482 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
483
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200484 if (module->is_submod) {
485 struct lysp_submodule *submod = (struct lysp_submodule *)module;
486
Michal Vaskoe180ed02021-02-05 16:31:20 +0100487 lydict_remove(ctx, submod->name);
488 lydict_remove(ctx, submod->filepath);
489 lydict_remove(ctx, submod->prefix);
490 lydict_remove(ctx, submod->org);
491 lydict_remove(ctx, submod->contact);
492 lydict_remove(ctx, submod->dsc);
493 lydict_remove(ctx, submod->ref);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200494 }
495
Radek Krejci19a96102018-11-15 13:38:09 +0100496 free(module);
497}
498
Radek Krejci0af46292019-01-11 16:02:31 +0100499void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100500lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
501{
502 if (--(*ext)->refcount) {
503 return;
504 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100505 lydict_remove(ctx, (*ext)->name);
506 lydict_remove(ctx, (*ext)->argument);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100507 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
508 free(*ext);
509}
510
511void
Radek Krejci19a96102018-11-15 13:38:09 +0100512lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
513{
fredganebc50572019-10-31 15:39:23 +0800514 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200515 ext->def->plugin->free(ctx, ext);
516 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100517 if (ext->def) {
518 lysc_extension_free(ctx, &ext->def);
519 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100520 lydict_remove(ctx, ext->argument);
Radek Krejci0935f412019-08-20 16:15:18 +0200521 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
522}
523
524void
Radek Krejci19a96102018-11-15 13:38:09 +0100525lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
526{
527 LY_ARRAY_FREE(iff->features);
528 free(iff->expr);
529}
530
531static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100532lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100533{
Radek Krejci00b874b2019-02-12 10:54:50 +0100534 if (--(*w)->refcount) {
535 return;
536 }
537 lyxp_expr_free(ctx, (*w)->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200538 lysc_prefixes_free((*w)->prefixes);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100539 lydict_remove(ctx, (*w)->dsc);
540 lydict_remove(ctx, (*w)->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +0100541 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
542 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100543}
544
Radek Krejciccd20f12019-02-15 14:12:27 +0100545void
Radek Krejci58d171e2018-11-23 13:50:55 +0100546lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
547{
548 lyxp_expr_free(ctx, must->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200549 lysc_prefixes_free(must->prefixes);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100550 lydict_remove(ctx, must->emsg);
551 lydict_remove(ctx, must->eapptag);
552 lydict_remove(ctx, must->dsc);
553 lydict_remove(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100554 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
555}
556
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100557void
Radek Krejci19a96102018-11-15 13:38:09 +0100558lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
559{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100560 lydict_remove(ctx, ident->name);
561 lydict_remove(ctx, ident->dsc);
562 lydict_remove(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100563 LY_ARRAY_FREE(ident->derived);
564 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
565}
566
Radek Krejci19a96102018-11-15 13:38:09 +0100567static void
568lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
569{
570 LY_ARRAY_FREE(range->parts);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100571 lydict_remove(ctx, range->eapptag);
572 lydict_remove(ctx, range->emsg);
573 lydict_remove(ctx, range->dsc);
574 lydict_remove(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100575 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
576}
577
578static void
579lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
580{
581 if (--(*pattern)->refcount) {
582 return;
583 }
Radek Krejci54579462019-04-30 12:47:06 +0200584 pcre2_code_free((*pattern)->code);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100585 lydict_remove(ctx, (*pattern)->expr);
586 lydict_remove(ctx, (*pattern)->eapptag);
587 lydict_remove(ctx, (*pattern)->emsg);
588 lydict_remove(ctx, (*pattern)->dsc);
589 lydict_remove(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100590 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
591 free(*pattern);
592}
593
594static void
Radek Krejci693262f2019-04-29 15:23:20 +0200595lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100596{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100597 lydict_remove(ctx, item->name);
598 lydict_remove(ctx, item->dsc);
599 lydict_remove(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100600 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
601}
602
Radek Krejcia3045382018-11-22 14:30:31 +0100603static void
604lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
605{
606 lysc_type_free(ctx, *type);
607}
Radek Krejci0f969882020-08-21 16:56:47 +0200608
Radek Krejcicdfecd92018-11-26 11:27:32 +0100609void
Radek Krejci19a96102018-11-15 13:38:09 +0100610lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
611{
612 if (--type->refcount) {
613 return;
614 }
Radek Krejcib915ac92020-08-14 23:31:04 +0200615
Michal Vaskod989ba02020-08-24 10:59:24 +0200616 switch (type->basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100617 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +0200618 FREE_MEMBER(ctx, ((struct lysc_type_bin *)type)->length, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100619 break;
620 case LY_TYPE_BITS:
Michal Vasko22df3f02020-08-24 13:29:22 +0200621 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 +0100622 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100623 case LY_TYPE_DEC64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200624 FREE_MEMBER(ctx, ((struct lysc_type_dec *)type)->range, lysc_range_free);
Radek Krejci6cba4292018-11-15 17:33:29 +0100625 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100626 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +0200627 FREE_MEMBER(ctx, ((struct lysc_type_str *)type)->length, lysc_range_free);
628 FREE_ARRAY(ctx, ((struct lysc_type_str *)type)->patterns, lysc_pattern_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100629 break;
630 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +0200631 FREE_ARRAY(ctx, ((struct lysc_type_enum *)type)->enums, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100632 break;
633 case LY_TYPE_INT8:
634 case LY_TYPE_UINT8:
635 case LY_TYPE_INT16:
636 case LY_TYPE_UINT16:
637 case LY_TYPE_INT32:
638 case LY_TYPE_UINT32:
639 case LY_TYPE_INT64:
640 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200641 FREE_MEMBER(ctx, ((struct lysc_type_num *)type)->range, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100642 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100643 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +0200644 LY_ARRAY_FREE(((struct lysc_type_identityref *)type)->bases);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100645 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100646 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +0200647 FREE_ARRAY(ctx, ((struct lysc_type_union *)type)->types, lysc_type2_free);
Radek Krejcia3045382018-11-22 14:30:31 +0100648 break;
649 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200650 lyxp_expr_free(ctx, ((struct lysc_type_leafref *)type)->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200651 lysc_prefixes_free(((struct lysc_type_leafref *)type)->prefixes);
Radek Krejcia3045382018-11-22 14:30:31 +0100652 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100653 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100654 case LY_TYPE_BOOL:
655 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100656 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100657 /* nothing to do */
658 break;
659 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200660
661 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100662 free(type);
663}
664
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100665void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100666lysc_node_action_inout_free(struct ly_ctx *ctx, struct lysc_node_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100667{
668 struct lysc_node *child, *child_next;
669
Radek Krejcif538ce52019-03-05 10:46:14 +0100670 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100671 LY_LIST_FOR_SAFE(inout->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100672 lysc_node_free_(ctx, child);
Radek Krejcif538ce52019-03-05 10:46:14 +0100673 }
674}
675
676void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100677lysc_node_action_free(struct ly_ctx *ctx, struct lysc_node_action *action)
Radek Krejcif538ce52019-03-05 10:46:14 +0100678{
Michal Vasko37a0fe62021-02-03 09:53:04 +0100679 FREE_ARRAY(ctx, action->when, lysc_when_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100680 if (action->input.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100681 lysc_node_free_(ctx, &action->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100682 }
683 if (action->output.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100684 lysc_node_free_(ctx, &action->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100685 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100686}
687
Radek Krejcifc11bd72019-04-11 16:00:05 +0200688void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100689lysc_node_notif_free(struct ly_ctx *ctx, struct lysc_node_notif *notif)
Radek Krejcifc11bd72019-04-11 16:00:05 +0200690{
691 struct lysc_node *child, *child_next;
692
Michal Vasko37a0fe62021-02-03 09:53:04 +0100693 FREE_ARRAY(ctx, notif->when, lysc_when_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200694 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100695 LY_LIST_FOR_SAFE(notif->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100696 lysc_node_free_(ctx, child);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200697 }
698}
699
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200700void
Radek Krejci19a96102018-11-15 13:38:09 +0100701lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
702{
703 struct lysc_node *child, *child_next;
704
705 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100706 lysc_node_free_(ctx, child);
Radek Krejci19a96102018-11-15 13:38:09 +0100707 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100708 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
709 lysc_node_free_(ctx, child);
710 }
711 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
712 lysc_node_free_(ctx, child);
713 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100714 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100715 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100716}
717
718static void
719lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
720{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100721 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100722 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100723 if (node->type) {
724 lysc_type_free(ctx, node->type);
725 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100726 lydict_remove(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200727 if (node->dflt) {
728 node->dflt->realtype->plugin->free(ctx, node->dflt);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200729 lysc_type_free(ctx, (struct lysc_type *)node->dflt->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200730 free(node->dflt);
731 }
Radek Krejci19a96102018-11-15 13:38:09 +0100732}
733
Radek Krejci42452ac2018-11-28 17:09:52 +0100734static void
735lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
736{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200737 LY_ARRAY_COUNT_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100738
Radek Krejci9a3823e2021-01-27 20:26:46 +0100739 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci42452ac2018-11-28 17:09:52 +0100740 FREE_ARRAY(ctx, node->musts, lysc_must_free);
741 if (node->type) {
742 lysc_type_free(ctx, node->type);
743 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100744 lydict_remove(ctx, node->units);
Radek Krejci42452ac2018-11-28 17:09:52 +0100745 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200746 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200747 lysc_type_free(ctx, (struct lysc_type *)node->dflts[u]->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200748 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100749 }
750 LY_ARRAY_FREE(node->dflts);
751}
752
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100753static void
754lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
755{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200756 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100757 struct lysc_node *child, *child_next;
758
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100759 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100760 lysc_node_free_(ctx, child);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100761 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100762 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100763 FREE_ARRAY(ctx, node->musts, lysc_must_free);
764
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100765 LY_ARRAY_FOR(node->uniques, u) {
766 LY_ARRAY_FREE(node->uniques[u]);
767 }
768 LY_ARRAY_FREE(node->uniques);
769
Radek Krejci2a9fc652021-01-22 17:44:34 +0100770 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
771 lysc_node_free_(ctx, child);
772 }
773 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
774 lysc_node_free_(ctx, child);
775 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100776}
777
Radek Krejci056d0a82018-12-06 16:57:25 +0100778static void
779lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
780{
781 struct lysc_node *child, *child_next;
782
Radek Krejci9a3823e2021-01-27 20:26:46 +0100783 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200784 LY_LIST_FOR_SAFE((struct lysc_node *)node->cases, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100785 lysc_node_free_(ctx, child);
Michal Vasko20424b42020-08-31 12:29:38 +0200786 }
787}
788
789static void
790lysc_node_case_free(struct ly_ctx *ctx, struct lysc_node_case *node)
791{
792 struct lysc_node *child, *child_next;
793
Radek Krejci9a3823e2021-01-27 20:26:46 +0100794 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200795 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100796 lysc_node_free_(ctx, child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100797 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100798}
Radek Krejci056d0a82018-12-06 16:57:25 +0100799
Radek Krejci9800fb82018-12-13 14:26:23 +0100800static void
801lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
802{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100803 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9800fb82018-12-13 14:26:23 +0100804 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100805}
806
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100807static void
808lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +0100809{
Radek Krejci2a9fc652021-01-22 17:44:34 +0100810 ly_bool inout = 0;
811
Radek Krejci19a96102018-11-15 13:38:09 +0100812 /* common part */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100813 lydict_remove(ctx, node->name);
814 lydict_remove(ctx, node->dsc);
815 lydict_remove(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100816
817 /* nodetype-specific part */
Michal Vaskod989ba02020-08-24 10:59:24 +0200818 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100819 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200820 lysc_node_container_free(ctx, (struct lysc_node_container *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100821 break;
822 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200823 lysc_node_leaf_free(ctx, (struct lysc_node_leaf *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100824 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100825 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200826 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist *)node);
Radek Krejci42452ac2018-11-28 17:09:52 +0100827 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100828 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200829 lysc_node_list_free(ctx, (struct lysc_node_list *)node);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100830 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100831 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200832 lysc_node_choice_free(ctx, (struct lysc_node_choice *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100833 break;
834 case LYS_CASE:
Michal Vasko20424b42020-08-31 12:29:38 +0200835 lysc_node_case_free(ctx, (struct lysc_node_case *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100836 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100837 case LYS_ANYDATA:
838 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200839 lysc_node_anydata_free(ctx, (struct lysc_node_anydata *)node);
Radek Krejci9800fb82018-12-13 14:26:23 +0100840 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100841 case LYS_RPC:
842 case LYS_ACTION:
843 lysc_node_action_free(ctx, (struct lysc_node_action *)node);
844 break;
845 case LYS_INPUT:
846 case LYS_OUTPUT:
847 lysc_node_action_inout_free(ctx, (struct lysc_node_action_inout *)node);
848 inout = 1;
849 break;
850 case LYS_NOTIF:
851 lysc_node_notif_free(ctx, (struct lysc_node_notif *)node);
852 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100853 default:
854 LOGINT(ctx);
855 }
856
Radek Krejci056d0a82018-12-06 16:57:25 +0100857 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100858
859 if (!inout) {
860 free(node);
861 }
Radek Krejci19a96102018-11-15 13:38:09 +0100862}
863
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100864void
865lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node, ly_bool unlink)
866{
867 struct lysc_node *iter, **child_p;
868
Radek Krejci2a9fc652021-01-22 17:44:34 +0100869 if (node->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
870 /* nothing to do - inouts are part of actions and cannot be unlinked/freed separately */
871 return;
872 }
873
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100874 if (unlink) {
875 /* unlink from siblings */
876 if (node->prev->next) {
877 node->prev->next = node->next;
878 }
879 if (node->next) {
880 node->next->prev = node->prev;
881 } else {
882 /* unlinking the last node */
883 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100884 if (node->nodetype == LYS_ACTION) {
885 iter = (struct lysc_node *)lysc_node_actions(node->parent);
886 } else if (node->nodetype == LYS_NOTIF) {
887 iter = (struct lysc_node *)lysc_node_notifs(node->parent);
888 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +0100889 iter = (struct lysc_node *)lysc_node_child(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100890 }
Michal Vasko539c4a62020-11-03 17:21:34 +0100891 LY_CHECK_ERR_RET(!iter, LOGINT(ctx), );
Radek Krejci2a9fc652021-01-22 17:44:34 +0100892 } else if (node->nodetype == LYS_RPC) {
893 iter = (struct lysc_node *)node->module->compiled->rpcs;
894 } else if (node->nodetype == LYS_NOTIF) {
895 iter = (struct lysc_node *)node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100896 } else {
897 iter = node->module->compiled->data;
898 }
899 /* update the "last" pointer from the first node */
900 iter->prev = node->prev;
901 }
902
903 /* unlink from parent */
904 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100905 if (node->nodetype == LYS_ACTION) {
906 child_p = (struct lysc_node **)lysc_node_actions_p(node->parent);
907 } else if (node->nodetype == LYS_NOTIF) {
908 child_p = (struct lysc_node **)lysc_node_notifs_p(node->parent);
909 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +0100910 child_p = lysc_node_child_p(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100911 }
912 } else if (node->nodetype == LYS_RPC) {
913 child_p = (struct lysc_node **)&node->module->compiled->rpcs;
914 } else if (node->nodetype == LYS_NOTIF) {
915 child_p = (struct lysc_node **)&node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100916 } else {
917 child_p = &node->module->compiled->data;
918 }
919 if (child_p && (*child_p == node)) {
920 /* the node is the first child */
921 *child_p = node->next;
922 }
923 }
924
925 lysc_node_free_(ctx, node);
926}
927
Radek Krejci19a96102018-11-15 13:38:09 +0100928static void
929lysc_module_free_(struct lysc_module *module)
930{
931 struct ly_ctx *ctx;
932 struct lysc_node *node, *node_next;
933
Michal Vaskod989ba02020-08-24 10:59:24 +0200934 LY_CHECK_ARG_RET(NULL, module, );
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100935 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100936
Radek Krejci19a96102018-11-15 13:38:09 +0100937 LY_LIST_FOR_SAFE(module->data, node_next, node) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100938 lysc_node_free_(ctx, node);
Radek Krejci19a96102018-11-15 13:38:09 +0100939 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100940 LY_LIST_FOR_SAFE((struct lysc_node *)module->rpcs, node_next, node) {
941 lysc_node_free_(ctx, node);
942 }
943 LY_LIST_FOR_SAFE((struct lysc_node *)module->notifs, node_next, node) {
944 lysc_node_free_(ctx, node);
945 }
Radek Krejci19a96102018-11-15 13:38:09 +0100946 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
947
948 free(module);
949}
950
951void
952lysc_module_free(struct lysc_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
953{
Radek Krejci9b042892019-02-13 14:28:44 +0100954 /* TODO use the destructor, this just suppress warning about unused parameter */
955 (void) private_destructor;
956
Radek Krejci19a96102018-11-15 13:38:09 +0100957 if (module) {
958 lysc_module_free_(module);
959 }
960}
961
962void
963lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
964{
965 if (!module) {
966 return;
967 }
968
969 lysc_module_free(module->compiled, private_destructor);
Radek Krejci80d281e2020-09-14 17:42:54 +0200970 FREE_ARRAY(module->ctx, module->identities, lysc_ident_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100971 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100972
Michal Vasko7f45cf22020-10-01 12:49:44 +0200973 LY_ARRAY_FREE(module->augmented_by);
974 LY_ARRAY_FREE(module->deviated_by);
975
Michal Vaskoe180ed02021-02-05 16:31:20 +0100976 lydict_remove(module->ctx, module->name);
977 lydict_remove(module->ctx, module->revision);
978 lydict_remove(module->ctx, module->ns);
979 lydict_remove(module->ctx, module->prefix);
980 lydict_remove(module->ctx, module->filepath);
981 lydict_remove(module->ctx, module->org);
982 lydict_remove(module->ctx, module->contact);
983 lydict_remove(module->ctx, module->dsc);
984 lydict_remove(module->ctx, module->ref);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100985
Radek Krejci19a96102018-11-15 13:38:09 +0100986 free(module);
987}
Michal Vasko33ff9422020-07-03 09:50:39 +0200988
Radek Krejci38d85362019-09-05 16:26:38 +0200989API void
Radek Krejci1b2eef82021-02-17 11:17:27 +0100990lysc_extension_instance_substatements_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
Radek Krejci38d85362019-09-05 16:26:38 +0200991{
Radek Krejci1b2eef82021-02-17 11:17:27 +0100992 LY_ARRAY_COUNT_TYPE u;
993
994 LY_ARRAY_FOR(substmts, u) {
Radek Krejci38d85362019-09-05 16:26:38 +0200995 if (!substmts[u].storage) {
996 continue;
997 }
998
Michal Vaskod989ba02020-08-24 10:59:24 +0200999 switch (substmts[u].stmt) {
Radek Krejci38d85362019-09-05 16:26:38 +02001000 case LY_STMT_TYPE:
1001 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1002 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02001003 struct lysc_type *type = *((struct lysc_type **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001004 if (!type) {
1005 break;
1006 }
1007 lysc_type_free(ctx, type);
1008 } else {
1009 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +02001010 struct lysc_type **types = *((struct lysc_type ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001011 if (!types) {
1012 break;
1013 }
1014 FREE_ARRAY(ctx, types, lysc_type2_free);
1015 }
1016 break;
Radek Krejci1b2eef82021-02-17 11:17:27 +01001017 case LY_STMT_DESCRIPTION:
1018 case LY_STMT_REFERENCE:
Radek Krejci38d85362019-09-05 16:26:38 +02001019 case LY_STMT_UNITS:
1020 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1021 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02001022 const char *str = *((const char **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001023 if (!str) {
1024 break;
1025 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001026 lydict_remove(ctx, str);
Radek Krejci38d85362019-09-05 16:26:38 +02001027 } else {
1028 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +02001029 const char **strs = *((const char ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001030 if (!strs) {
1031 break;
1032 }
1033 FREE_STRINGS(ctx, strs);
1034 }
1035 break;
1036 case LY_STMT_STATUS:
Radek Krejciad5963b2019-09-06 16:03:05 +02001037 case LY_STMT_CONFIG:
Radek Krejci38d85362019-09-05 16:26:38 +02001038 /* nothing to do */
1039 break;
1040 case LY_STMT_IF_FEATURE: {
Michal Vasko22df3f02020-08-24 13:29:22 +02001041 struct lysc_iffeature *iff = *((struct lysc_iffeature **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001042 if (!iff) {
1043 break;
1044 }
1045 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1046 /* single item */
1047 lysc_iffeature_free(ctx, iff);
1048 free(iff);
1049 } else {
1050 /* multiple items */
1051 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
1052 }
1053 break;
1054 }
1055
Radek Krejci0f969882020-08-21 16:56:47 +02001056 /* TODO other statements */
Radek Krejci38d85362019-09-05 16:26:38 +02001057 default:
1058 LOGINT(ctx);
1059 }
1060 }
Radek Krejci1b2eef82021-02-17 11:17:27 +01001061
1062 LY_ARRAY_FREE(substmts);
Radek Krejci38d85362019-09-05 16:26:38 +02001063}
David Sedlákebd3acf2019-07-26 15:04:32 +02001064
1065void
Michal Vaskob36053d2020-03-26 15:49:30 +01001066yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001067{
1068 if (ctx) {
1069 free(ctx);
1070 }
1071}
1072
1073void
Michal Vaskob36053d2020-03-26 15:49:30 +01001074yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001075{
1076 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001077 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +02001078 free(ctx);
1079 }
1080}