blob: f39c2967c520bc4511bf2f26746254f9b4376d0b [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 Krejci77114102021-03-10 15:21:57 +010019#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010020#include "log.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include "plugins_exts.h"
22#include "plugins_types.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "tree_data.h"
Michal Vaskofc2cd072021-02-24 13:17:17 +010025#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010026#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010028#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "xml.h"
Radek Krejci19a96102018-11-15 13:38:09 +010030#include "xpath.h"
31
Radek Krejci2a9fc652021-01-22 17:44:34 +010032void lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010033static void lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node);
Radek Krejci19a96102018-11-15 13:38:09 +010034
35static void
36lysp_stmt_free(struct ly_ctx *ctx, struct lysp_stmt *stmt)
37{
38 struct lysp_stmt *child, *next;
39
Michal Vaskoe180ed02021-02-05 16:31:20 +010040 lydict_remove(ctx, stmt->stmt);
41 lydict_remove(ctx, stmt->arg);
Michal Vaskofc2cd072021-02-24 13:17:17 +010042 ly_free_prefix_data(stmt->format, stmt->prefix_data);
Radek Krejci19a96102018-11-15 13:38:09 +010043
44 LY_LIST_FOR_SAFE(stmt->child, next, child) {
45 lysp_stmt_free(ctx, child);
46 }
47
48 free(stmt);
49}
50
Radek Krejci2d7a47b2019-05-16 13:34:10 +020051void
Radek Krejci19a96102018-11-15 13:38:09 +010052lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext)
53{
54 struct lysp_stmt *stmt, *next;
55
Michal Vaskoe180ed02021-02-05 16:31:20 +010056 lydict_remove(ctx, ext->name);
57 lydict_remove(ctx, ext->argument);
Michal Vaskofc2cd072021-02-24 13:17:17 +010058 ly_free_prefix_data(ext->format, ext->prefix_data);
Radek Krejci19a96102018-11-15 13:38:09 +010059
60 LY_LIST_FOR_SAFE(ext->child, next, stmt) {
61 lysp_stmt_free(ctx, stmt);
62 }
63}
64
David Sedlák298ff6d2019-07-26 14:29:03 +020065void
Radek Krejci19a96102018-11-15 13:38:09 +010066lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import)
67{
68 /* imported module is freed directly from the context's list */
Michal Vaskoe180ed02021-02-05 16:31:20 +010069 lydict_remove(ctx, import->name);
70 lydict_remove(ctx, import->prefix);
71 lydict_remove(ctx, import->dsc);
72 lydict_remove(ctx, import->ref);
Radek Krejci19a96102018-11-15 13:38:09 +010073 FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free);
74}
75
Radek Krejci771928a2021-01-19 13:42:36 +010076/**
77 * @brief Common function to erase include record in main module and submodule.
78 *
79 * There is a difference since the main module is expected to have the complete list if the included submodules and
80 * the parsed submodule is shared with any include in a submodule. Therefore, the referenced submodules in the include
81 * record are freed only from main module's records.
82 *
83 * @param[in] ctx libyang context
84 * @param[in] include The include record to be erased, the record itself is not freed.
85 * @param[in] main_module Flag to get know if the include record is placed in main module so also the referenced submodule
86 * is supposed to be freed.
87 */
88static void
89lysp_include_free_(struct ly_ctx *ctx, struct lysp_include *include, ly_bool main_module)
Radek Krejci19a96102018-11-15 13:38:09 +010090{
Radek Krejci771928a2021-01-19 13:42:36 +010091 if (main_module && include->submodule) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +020092 lysp_module_free((struct lysp_module *)include->submodule);
Radek Krejci19a96102018-11-15 13:38:09 +010093 }
Michal Vaskoe180ed02021-02-05 16:31:20 +010094 lydict_remove(ctx, include->name);
95 lydict_remove(ctx, include->dsc);
96 lydict_remove(ctx, include->ref);
Radek Krejci19a96102018-11-15 13:38:09 +010097 FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free);
98}
99
David Sedlákaa854b02019-07-22 14:17:10 +0200100void
Radek Krejci771928a2021-01-19 13:42:36 +0100101lysp_include_free_submodule(struct ly_ctx *ctx, struct lysp_include *include)
102{
103 return lysp_include_free_(ctx, include, 0);
104}
105
106void
107lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include)
108{
109 return lysp_include_free_(ctx, include, 1);
110}
111
112void
Radek Krejci19a96102018-11-15 13:38:09 +0100113lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev)
114{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100115 lydict_remove(ctx, rev->dsc);
116 lydict_remove(ctx, rev->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100117 FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free);
118}
119
David Sedlák986cb412019-07-04 13:10:11 +0200120void
Radek Krejci19a96102018-11-15 13:38:09 +0100121lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext)
122{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100123 lydict_remove(ctx, ext->name);
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100124 lydict_remove(ctx, ext->argname);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100125 lydict_remove(ctx, ext->dsc);
126 lydict_remove(ctx, ext->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100127 FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free);
Michal Vasko5fe75f12020-03-02 13:52:37 +0100128 if (ext->compiled) {
129 lysc_extension_free(ctx, &ext->compiled);
130 }
Radek Krejci19a96102018-11-15 13:38:09 +0100131}
132
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200133void
Radek Krejci19a96102018-11-15 13:38:09 +0100134lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat)
135{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100136 lydict_remove(ctx, feat->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200137 FREE_ARRAY(ctx, feat->iffeatures, lysp_qname_free);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100138 FREE_ARRAY(ctx, feat->iffeatures_c, lysc_iffeature_free);
139 LY_ARRAY_FREE(feat->depfeatures);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100140 lydict_remove(ctx, feat->dsc);
141 lydict_remove(ctx, feat->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100142 FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
143}
144
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200145void
Radek Krejci19a96102018-11-15 13:38:09 +0100146lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident)
147{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100148 lydict_remove(ctx, ident->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200149 FREE_ARRAY(ctx, ident->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100150 FREE_STRINGS(ctx, ident->bases);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100151 lydict_remove(ctx, ident->dsc);
152 lydict_remove(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100153 FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
154}
155
Michal Vasko7f45cf22020-10-01 12:49:44 +0200156void
Radek Krejci19a96102018-11-15 13:38:09 +0100157lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
158{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100159 lydict_remove(ctx, restr->arg.str);
160 lydict_remove(ctx, restr->emsg);
161 lydict_remove(ctx, restr->eapptag);
162 lydict_remove(ctx, restr->dsc);
163 lydict_remove(ctx, restr->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100164 FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
165}
166
167static void
168lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item)
169{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100170 lydict_remove(ctx, item->name);
171 lydict_remove(ctx, item->dsc);
172 lydict_remove(ctx, item->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200173 FREE_ARRAY(ctx, item->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100174 FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
175}
176
Radek Krejcicdfecd92018-11-26 11:27:32 +0100177void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type);
Michal Vasko004d3152020-06-11 19:59:22 +0200178
David Sedlák32488102019-07-15 17:44:10 +0200179void
Radek Krejci19a96102018-11-15 13:38:09 +0100180lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type)
181{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100182 lydict_remove(ctx, type->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100183 FREE_MEMBER(ctx, type->range, lysp_restr_free);
184 FREE_MEMBER(ctx, type->length, lysp_restr_free);
185 FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
186 FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
187 FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
Michal Vasko004d3152020-06-11 19:59:22 +0200188 lyxp_expr_free(ctx, type->path);
Radek Krejci19a96102018-11-15 13:38:09 +0100189 FREE_STRINGS(ctx, type->bases);
190 FREE_ARRAY(ctx, type->types, lysp_type_free);
191 FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
192 if (type->compiled) {
193 lysc_type_free(ctx, type->compiled);
194 }
195}
196
David Sedlák04e17b22019-07-19 15:29:48 +0200197void
Radek Krejci19a96102018-11-15 13:38:09 +0100198lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
199{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100200 lydict_remove(ctx, tpdf->name);
201 lydict_remove(ctx, tpdf->units);
202 lydict_remove(ctx, tpdf->dflt.str);
203 lydict_remove(ctx, tpdf->dsc);
204 lydict_remove(ctx, tpdf->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100205 FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
206
207 lysp_type_free(ctx, &tpdf->type);
208
209}
210
Radek Krejcif538ce52019-03-05 10:46:14 +0100211void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100212lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp)
Radek Krejci19a96102018-11-15 13:38:09 +0100213{
214 struct lysp_node *node, *next;
215
Radek Krejci19a96102018-11-15 13:38:09 +0100216 FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100217 LY_LIST_FOR_SAFE((struct lysp_node *)grp->groupings, next, node) {
218 lysp_node_free(ctx, node);
219 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100220 LY_LIST_FOR_SAFE(grp->child, next, node) {
Radek Krejci19a96102018-11-15 13:38:09 +0100221 lysp_node_free(ctx, node);
222 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100223 LY_LIST_FOR_SAFE((struct lysp_node *)grp->actions, next, node) {
224 lysp_node_free(ctx, node);
225 }
226 LY_LIST_FOR_SAFE((struct lysp_node *)grp->notifs, next, node) {
227 lysp_node_free(ctx, node);
228 }
Radek Krejci19a96102018-11-15 13:38:09 +0100229}
230
Radek Krejcif09e4e82019-06-14 15:08:11 +0200231void
Radek Krejci19a96102018-11-15 13:38:09 +0100232lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
233{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100234 lydict_remove(ctx, when->cond);
235 lydict_remove(ctx, when->dsc);
236 lydict_remove(ctx, when->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100237 FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
238}
239
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200240void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100241lysp_augment_free(struct ly_ctx *ctx, struct lysp_node_augment *augment)
Radek Krejci19a96102018-11-15 13:38:09 +0100242{
243 struct lysp_node *node, *next;
244
Radek Krejci19a96102018-11-15 13:38:09 +0100245 LY_LIST_FOR_SAFE(augment->child, next, node) {
246 lysp_node_free(ctx, node);
247 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100248 LY_LIST_FOR_SAFE((struct lysp_node *)augment->actions, next, node) {
249 lysp_node_free(ctx, node);
250 }
251 LY_LIST_FOR_SAFE((struct lysp_node *)augment->notifs, next, node) {
252 lysp_node_free(ctx, node);
253 }
Radek Krejci19a96102018-11-15 13:38:09 +0100254}
255
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200256void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200257lysp_qname_free(struct ly_ctx *ctx, struct lysp_qname *qname)
258{
259 if (qname) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100260 lydict_remove(ctx, qname->str);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200261 }
262}
263
264void
Radek Krejci19a96102018-11-15 13:38:09 +0100265lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
266{
Michal Vasko22df3f02020-08-24 13:29:22 +0200267 struct lysp_deviate_add *add = (struct lysp_deviate_add *)d;
268 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl *)d;
Radek Krejci19a96102018-11-15 13:38:09 +0100269
270 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
Michal Vaskod989ba02020-08-24 10:59:24 +0200271 switch (d->mod) {
Radek Krejci19a96102018-11-15 13:38:09 +0100272 case LYS_DEV_NOT_SUPPORTED:
273 /* nothing to do */
274 break;
275 case LYS_DEV_ADD:
276 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100277 lydict_remove(ctx, add->units);
Radek Krejci19a96102018-11-15 13:38:09 +0100278 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200279 FREE_ARRAY(ctx, add->uniques, lysp_qname_free);
280 FREE_ARRAY(ctx, add->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100281 break;
282 case LYS_DEV_REPLACE:
283 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100284 lydict_remove(ctx, rpl->units);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200285 lysp_qname_free(ctx, &rpl->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +0100286 break;
287 default:
288 LOGINT(ctx);
289 break;
290 }
291}
292
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200293void
Radek Krejci19a96102018-11-15 13:38:09 +0100294lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
295{
296 struct lysp_deviate *next, *iter;
297
Michal Vaskoe180ed02021-02-05 16:31:20 +0100298 lydict_remove(ctx, dev->nodeid);
299 lydict_remove(ctx, dev->dsc);
300 lydict_remove(ctx, dev->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100301 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
302 lysp_deviate_free(ctx, iter);
303 free(iter);
304 }
305 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
306}
307
David Sedlákd2d676a2019-07-22 11:28:19 +0200308void
Radek Krejci19a96102018-11-15 13:38:09 +0100309lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
310{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100311 lydict_remove(ctx, ref->nodeid);
312 lydict_remove(ctx, ref->dsc);
313 lydict_remove(ctx, ref->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200314 FREE_ARRAY(ctx, ref->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100315 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100316 lydict_remove(ctx, ref->presence);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200317 FREE_ARRAY(ctx, ref->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100318 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
319}
320
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200321void
Radek Krejci19a96102018-11-15 13:38:09 +0100322lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
323{
324 struct lysp_node *child, *next;
Radek Krejci9a3823e2021-01-27 20:26:46 +0100325 struct lysp_restr *musts = lysp_node_musts(node);
326 struct lysp_when *when = lysp_node_when(node);
Radek Krejci19a96102018-11-15 13:38:09 +0100327
Michal Vaskoe180ed02021-02-05 16:31:20 +0100328 lydict_remove(ctx, node->name);
329 lydict_remove(ctx, node->dsc);
330 lydict_remove(ctx, node->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200331 FREE_ARRAY(ctx, node->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100332 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
333
Radek Krejci9a3823e2021-01-27 20:26:46 +0100334 FREE_MEMBER(ctx, when, lysp_when_free);
335 FREE_ARRAY(ctx, musts, lysp_restr_free);
336
Michal Vaskod989ba02020-08-24 10:59:24 +0200337 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100338 case LYS_CONTAINER:
Michal Vaskoe180ed02021-02-05 16:31:20 +0100339 lydict_remove(ctx, ((struct lysp_node_container *)node)->presence);
Michal Vasko22df3f02020-08-24 13:29:22 +0200340 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100341 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100342 lysp_node_free(ctx, child);
343 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200344 LY_LIST_FOR_SAFE(((struct lysp_node_container *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100345 lysp_node_free(ctx, child);
346 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100347 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->actions->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100348 lysp_node_free(ctx, child);
349 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100350 LY_LIST_FOR_SAFE(&((struct lysp_node_container *)node)->notifs->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100351 lysp_node_free(ctx, child);
352 }
Radek Krejci19a96102018-11-15 13:38:09 +0100353 break;
354 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200355 lysp_type_free(ctx, &((struct lysp_node_leaf *)node)->type);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100356 lydict_remove(ctx, ((struct lysp_node_leaf *)node)->units);
357 lydict_remove(ctx, ((struct lysp_node_leaf *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100358 break;
359 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200360 lysp_type_free(ctx, &((struct lysp_node_leaflist *)node)->type);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100361 lydict_remove(ctx, ((struct lysp_node_leaflist *)node)->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200362 FREE_ARRAY(ctx, ((struct lysp_node_leaflist *)node)->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100363 break;
364 case LYS_LIST:
Michal Vaskoe180ed02021-02-05 16:31:20 +0100365 lydict_remove(ctx, ((struct lysp_node_list *)node)->key);
Michal Vasko22df3f02020-08-24 13:29:22 +0200366 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100367 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100368 lysp_node_free(ctx, child);
369 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200370 LY_LIST_FOR_SAFE(((struct lysp_node_list *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100371 lysp_node_free(ctx, child);
372 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100373 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->actions->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100374 lysp_node_free(ctx, child);
375 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100376 LY_LIST_FOR_SAFE(&((struct lysp_node_list *)node)->notifs->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100377 lysp_node_free(ctx, child);
378 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200379 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->uniques, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100380 break;
381 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200382 LY_LIST_FOR_SAFE(((struct lysp_node_choice *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100383 lysp_node_free(ctx, child);
384 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100385 lydict_remove(ctx, ((struct lysp_node_choice *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100386 break;
387 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200388 LY_LIST_FOR_SAFE(((struct lysp_node_case *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100389 lysp_node_free(ctx, child);
390 }
391 break;
392 case LYS_ANYDATA:
393 case LYS_ANYXML:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100394 /* nothing special to do */
Radek Krejci19a96102018-11-15 13:38:09 +0100395 break;
396 case LYS_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +0200397 FREE_ARRAY(ctx, ((struct lysp_node_uses *)node)->refines, lysp_refine_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100398 LY_LIST_FOR_SAFE(&((struct lysp_node_uses *)node)->augments->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100399 lysp_node_free(ctx, child);
400 }
401 break;
402 case LYS_RPC:
403 case LYS_ACTION:
404 FREE_ARRAY(ctx, ((struct lysp_node_action *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100405 LY_LIST_FOR_SAFE(&((struct lysp_node_action *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100406 lysp_node_free(ctx, child);
407 }
408 if (((struct lysp_node_action *)node)->input.nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100409 lysp_node_free(ctx, &((struct lysp_node_action *)node)->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100410 }
411 if (((struct lysp_node_action *)node)->output.nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100412 lysp_node_free(ctx, &((struct lysp_node_action *)node)->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100413 }
414 break;
415 case LYS_INPUT:
416 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100417 FREE_ARRAY(ctx, ((struct lysp_node_action_inout *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100418 LY_LIST_FOR_SAFE(&((struct lysp_node_action_inout *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100419 lysp_node_free(ctx, child);
420 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100421 LY_LIST_FOR_SAFE(((struct lysp_node_action_inout *)node)->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100422 lysp_node_free(ctx, child);
423 }
424 /* do not free the node, it is never standalone but part of the action node */
425 return;
426 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100427 FREE_ARRAY(ctx, ((struct lysp_node_notif *)node)->typedefs, lysp_tpdf_free);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100428 LY_LIST_FOR_SAFE(&((struct lysp_node_notif *)node)->groupings->node, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100429 lysp_node_free(ctx, child);
430 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100431 LY_LIST_FOR_SAFE(((struct lysp_node_notif *)node)->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100432 lysp_node_free(ctx, child);
433 }
434 break;
435 case LYS_GROUPING:
436 lysp_grp_free(ctx, (struct lysp_node_grp *)node);
437 break;
438 case LYS_AUGMENT:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100439 lysp_augment_free(ctx, ((struct lysp_node_augment *)node));
Radek Krejci19a96102018-11-15 13:38:09 +0100440 break;
441 default:
442 LOGINT(ctx);
443 }
444
445 free(node);
446}
447
Radek Krejci15f10ab2020-11-03 14:14:14 +0100448void
Radek Krejci19a96102018-11-15 13:38:09 +0100449lysp_module_free(struct lysp_module *module)
450{
451 struct ly_ctx *ctx;
452 struct lysp_node *node, *next;
453
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100454 if (!module) {
455 return;
456 }
457 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100458
459 FREE_ARRAY(ctx, module->imports, lysp_import_free);
Radek Krejci771928a2021-01-19 13:42:36 +0100460 FREE_ARRAY(ctx, module->includes, module->is_submod ? lysp_include_free_submodule : lysp_include_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100461
Radek Krejci19a96102018-11-15 13:38:09 +0100462 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
463 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
464 FREE_ARRAY(ctx, module->features, lysp_feature_free);
465 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
466 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100467 LY_LIST_FOR_SAFE((struct lysp_node *)module->groupings, next, node) {
468 lysp_node_free(ctx, node);
469 }
Radek Krejci19a96102018-11-15 13:38:09 +0100470 LY_LIST_FOR_SAFE(module->data, next, node) {
471 lysp_node_free(ctx, node);
472 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100473 LY_LIST_FOR_SAFE((struct lysp_node *)module->augments, next, node) {
474 lysp_node_free(ctx, node);
475 }
476 LY_LIST_FOR_SAFE((struct lysp_node *)module->rpcs, next, node) {
477 lysp_node_free(ctx, node);
478 }
479 LY_LIST_FOR_SAFE((struct lysp_node *)module->notifs, next, node) {
480 lysp_node_free(ctx, node);
481 }
Radek Krejci19a96102018-11-15 13:38:09 +0100482 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
483 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
484
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200485 if (module->is_submod) {
486 struct lysp_submodule *submod = (struct lysp_submodule *)module;
487
Michal Vaskoe180ed02021-02-05 16:31:20 +0100488 lydict_remove(ctx, submod->name);
489 lydict_remove(ctx, submod->filepath);
490 lydict_remove(ctx, submod->prefix);
491 lydict_remove(ctx, submod->org);
492 lydict_remove(ctx, submod->contact);
493 lydict_remove(ctx, submod->dsc);
494 lydict_remove(ctx, submod->ref);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200495 }
496
Radek Krejci19a96102018-11-15 13:38:09 +0100497 free(module);
498}
499
Radek Krejci0af46292019-01-11 16:02:31 +0100500void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100501lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
502{
503 if (--(*ext)->refcount) {
504 return;
505 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100506 lydict_remove(ctx, (*ext)->name);
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100507 lydict_remove(ctx, (*ext)->argname);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100508 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
509 free(*ext);
Radek Krejci720d2612021-03-03 19:44:22 +0100510 *ext = NULL;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100511}
512
513void
Radek Krejci19a96102018-11-15 13:38:09 +0100514lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
515{
fredganebc50572019-10-31 15:39:23 +0800516 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200517 ext->def->plugin->free(ctx, ext);
518 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100519 if (ext->def) {
520 lysc_extension_free(ctx, &ext->def);
521 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100522 lydict_remove(ctx, ext->argument);
Radek Krejci0935f412019-08-20 16:15:18 +0200523 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
524}
525
526void
Radek Krejci19a96102018-11-15 13:38:09 +0100527lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
528{
529 LY_ARRAY_FREE(iff->features);
530 free(iff->expr);
531}
532
533static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100534lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100535{
Radek Krejci00b874b2019-02-12 10:54:50 +0100536 if (--(*w)->refcount) {
537 return;
538 }
539 lyxp_expr_free(ctx, (*w)->cond);
Radek Krejci2926c1b2021-03-16 14:45:45 +0100540 ly_free_prefix_data(LY_PREF_SCHEMA_RESOLVED, (*w)->prefixes);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100541 lydict_remove(ctx, (*w)->dsc);
542 lydict_remove(ctx, (*w)->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +0100543 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
544 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100545}
546
Radek Krejciccd20f12019-02-15 14:12:27 +0100547void
Radek Krejci58d171e2018-11-23 13:50:55 +0100548lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
549{
550 lyxp_expr_free(ctx, must->cond);
Radek Krejci2926c1b2021-03-16 14:45:45 +0100551 ly_free_prefix_data(LY_PREF_SCHEMA_RESOLVED, must->prefixes);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100552 lydict_remove(ctx, must->emsg);
553 lydict_remove(ctx, must->eapptag);
554 lydict_remove(ctx, must->dsc);
555 lydict_remove(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100556 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
557}
558
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100559void
Radek Krejci19a96102018-11-15 13:38:09 +0100560lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
561{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100562 lydict_remove(ctx, ident->name);
563 lydict_remove(ctx, ident->dsc);
564 lydict_remove(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100565 LY_ARRAY_FREE(ident->derived);
566 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
567}
568
Radek Krejci19a96102018-11-15 13:38:09 +0100569static void
570lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
571{
572 LY_ARRAY_FREE(range->parts);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100573 lydict_remove(ctx, range->eapptag);
574 lydict_remove(ctx, range->emsg);
575 lydict_remove(ctx, range->dsc);
576 lydict_remove(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100577 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
578}
579
580static void
581lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
582{
583 if (--(*pattern)->refcount) {
584 return;
585 }
Radek Krejci54579462019-04-30 12:47:06 +0200586 pcre2_code_free((*pattern)->code);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100587 lydict_remove(ctx, (*pattern)->expr);
588 lydict_remove(ctx, (*pattern)->eapptag);
589 lydict_remove(ctx, (*pattern)->emsg);
590 lydict_remove(ctx, (*pattern)->dsc);
591 lydict_remove(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100592 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
593 free(*pattern);
594}
595
596static void
Radek Krejci693262f2019-04-29 15:23:20 +0200597lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100598{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100599 lydict_remove(ctx, item->name);
600 lydict_remove(ctx, item->dsc);
601 lydict_remove(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100602 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
603}
604
Radek Krejcia3045382018-11-22 14:30:31 +0100605static void
606lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
607{
608 lysc_type_free(ctx, *type);
609}
Radek Krejci0f969882020-08-21 16:56:47 +0200610
Radek Krejcicdfecd92018-11-26 11:27:32 +0100611void
Radek Krejci19a96102018-11-15 13:38:09 +0100612lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
613{
614 if (--type->refcount) {
615 return;
616 }
Radek Krejcib915ac92020-08-14 23:31:04 +0200617
Michal Vaskod989ba02020-08-24 10:59:24 +0200618 switch (type->basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100619 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +0200620 FREE_MEMBER(ctx, ((struct lysc_type_bin *)type)->length, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100621 break;
622 case LY_TYPE_BITS:
Michal Vasko22df3f02020-08-24 13:29:22 +0200623 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 +0100624 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100625 case LY_TYPE_DEC64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200626 FREE_MEMBER(ctx, ((struct lysc_type_dec *)type)->range, lysc_range_free);
Radek Krejci6cba4292018-11-15 17:33:29 +0100627 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100628 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +0200629 FREE_MEMBER(ctx, ((struct lysc_type_str *)type)->length, lysc_range_free);
630 FREE_ARRAY(ctx, ((struct lysc_type_str *)type)->patterns, lysc_pattern_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100631 break;
632 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +0200633 FREE_ARRAY(ctx, ((struct lysc_type_enum *)type)->enums, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100634 break;
635 case LY_TYPE_INT8:
636 case LY_TYPE_UINT8:
637 case LY_TYPE_INT16:
638 case LY_TYPE_UINT16:
639 case LY_TYPE_INT32:
640 case LY_TYPE_UINT32:
641 case LY_TYPE_INT64:
642 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200643 FREE_MEMBER(ctx, ((struct lysc_type_num *)type)->range, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100644 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100645 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +0200646 LY_ARRAY_FREE(((struct lysc_type_identityref *)type)->bases);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100647 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100648 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +0200649 FREE_ARRAY(ctx, ((struct lysc_type_union *)type)->types, lysc_type2_free);
Radek Krejcia3045382018-11-22 14:30:31 +0100650 break;
651 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200652 lyxp_expr_free(ctx, ((struct lysc_type_leafref *)type)->path);
Radek Krejci2926c1b2021-03-16 14:45:45 +0100653 ly_free_prefix_data(LY_PREF_SCHEMA_RESOLVED, ((struct lysc_type_leafref *)type)->prefixes);
Radek Krejcia3045382018-11-22 14:30:31 +0100654 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100655 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100656 case LY_TYPE_BOOL:
657 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100658 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100659 /* nothing to do */
660 break;
661 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200662
663 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100664 free(type);
665}
666
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100667void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100668lysc_node_action_inout_free(struct ly_ctx *ctx, struct lysc_node_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100669{
670 struct lysc_node *child, *child_next;
671
Radek Krejcif538ce52019-03-05 10:46:14 +0100672 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100673 LY_LIST_FOR_SAFE(inout->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100674 lysc_node_free_(ctx, child);
Radek Krejcif538ce52019-03-05 10:46:14 +0100675 }
676}
677
678void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100679lysc_node_action_free(struct ly_ctx *ctx, struct lysc_node_action *action)
Radek Krejcif538ce52019-03-05 10:46:14 +0100680{
Michal Vasko37a0fe62021-02-03 09:53:04 +0100681 FREE_ARRAY(ctx, action->when, lysc_when_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100682 if (action->input.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100683 lysc_node_free_(ctx, &action->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100684 }
685 if (action->output.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100686 lysc_node_free_(ctx, &action->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100687 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100688}
689
Radek Krejcifc11bd72019-04-11 16:00:05 +0200690void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100691lysc_node_notif_free(struct ly_ctx *ctx, struct lysc_node_notif *notif)
Radek Krejcifc11bd72019-04-11 16:00:05 +0200692{
693 struct lysc_node *child, *child_next;
694
Michal Vasko37a0fe62021-02-03 09:53:04 +0100695 FREE_ARRAY(ctx, notif->when, lysc_when_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200696 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100697 LY_LIST_FOR_SAFE(notif->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100698 lysc_node_free_(ctx, child);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200699 }
700}
701
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200702void
Radek Krejci19a96102018-11-15 13:38:09 +0100703lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
704{
705 struct lysc_node *child, *child_next;
706
707 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100708 lysc_node_free_(ctx, child);
Radek Krejci19a96102018-11-15 13:38:09 +0100709 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100710 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
711 lysc_node_free_(ctx, child);
712 }
713 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
714 lysc_node_free_(ctx, child);
715 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100716 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100717 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100718}
719
720static void
721lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
722{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100723 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100724 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100725 if (node->type) {
726 lysc_type_free(ctx, node->type);
727 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100728 lydict_remove(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200729 if (node->dflt) {
730 node->dflt->realtype->plugin->free(ctx, node->dflt);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200731 lysc_type_free(ctx, (struct lysc_type *)node->dflt->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200732 free(node->dflt);
733 }
Radek Krejci19a96102018-11-15 13:38:09 +0100734}
735
Radek Krejci42452ac2018-11-28 17:09:52 +0100736static void
737lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
738{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200739 LY_ARRAY_COUNT_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100740
Radek Krejci9a3823e2021-01-27 20:26:46 +0100741 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci42452ac2018-11-28 17:09:52 +0100742 FREE_ARRAY(ctx, node->musts, lysc_must_free);
743 if (node->type) {
744 lysc_type_free(ctx, node->type);
745 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100746 lydict_remove(ctx, node->units);
Radek Krejci42452ac2018-11-28 17:09:52 +0100747 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200748 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200749 lysc_type_free(ctx, (struct lysc_type *)node->dflts[u]->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200750 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100751 }
752 LY_ARRAY_FREE(node->dflts);
753}
754
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100755static void
756lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
757{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200758 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100759 struct lysc_node *child, *child_next;
760
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100761 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100762 lysc_node_free_(ctx, child);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100763 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100764 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100765 FREE_ARRAY(ctx, node->musts, lysc_must_free);
766
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100767 LY_ARRAY_FOR(node->uniques, u) {
768 LY_ARRAY_FREE(node->uniques[u]);
769 }
770 LY_ARRAY_FREE(node->uniques);
771
Radek Krejci2a9fc652021-01-22 17:44:34 +0100772 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
773 lysc_node_free_(ctx, child);
774 }
775 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
776 lysc_node_free_(ctx, child);
777 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100778}
779
Radek Krejci056d0a82018-12-06 16:57:25 +0100780static void
781lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
782{
783 struct lysc_node *child, *child_next;
784
Radek Krejci9a3823e2021-01-27 20:26:46 +0100785 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200786 LY_LIST_FOR_SAFE((struct lysc_node *)node->cases, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100787 lysc_node_free_(ctx, child);
Michal Vasko20424b42020-08-31 12:29:38 +0200788 }
789}
790
791static void
792lysc_node_case_free(struct ly_ctx *ctx, struct lysc_node_case *node)
793{
794 struct lysc_node *child, *child_next;
795
Radek Krejci9a3823e2021-01-27 20:26:46 +0100796 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200797 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100798 lysc_node_free_(ctx, child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100799 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100800}
Radek Krejci056d0a82018-12-06 16:57:25 +0100801
Radek Krejci9800fb82018-12-13 14:26:23 +0100802static void
803lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
804{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100805 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9800fb82018-12-13 14:26:23 +0100806 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100807}
808
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100809static void
810lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +0100811{
Radek Krejci2a9fc652021-01-22 17:44:34 +0100812 ly_bool inout = 0;
813
Radek Krejci19a96102018-11-15 13:38:09 +0100814 /* common part */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100815 lydict_remove(ctx, node->name);
816 lydict_remove(ctx, node->dsc);
817 lydict_remove(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100818
819 /* nodetype-specific part */
Michal Vaskod989ba02020-08-24 10:59:24 +0200820 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100821 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200822 lysc_node_container_free(ctx, (struct lysc_node_container *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100823 break;
824 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200825 lysc_node_leaf_free(ctx, (struct lysc_node_leaf *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100826 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100827 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200828 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist *)node);
Radek Krejci42452ac2018-11-28 17:09:52 +0100829 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100830 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200831 lysc_node_list_free(ctx, (struct lysc_node_list *)node);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100832 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100833 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200834 lysc_node_choice_free(ctx, (struct lysc_node_choice *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100835 break;
836 case LYS_CASE:
Michal Vasko20424b42020-08-31 12:29:38 +0200837 lysc_node_case_free(ctx, (struct lysc_node_case *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100838 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100839 case LYS_ANYDATA:
840 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200841 lysc_node_anydata_free(ctx, (struct lysc_node_anydata *)node);
Radek Krejci9800fb82018-12-13 14:26:23 +0100842 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100843 case LYS_RPC:
844 case LYS_ACTION:
845 lysc_node_action_free(ctx, (struct lysc_node_action *)node);
846 break;
847 case LYS_INPUT:
848 case LYS_OUTPUT:
849 lysc_node_action_inout_free(ctx, (struct lysc_node_action_inout *)node);
850 inout = 1;
851 break;
852 case LYS_NOTIF:
853 lysc_node_notif_free(ctx, (struct lysc_node_notif *)node);
854 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100855 default:
856 LOGINT(ctx);
857 }
858
Radek Krejci056d0a82018-12-06 16:57:25 +0100859 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100860
861 if (!inout) {
862 free(node);
863 }
Radek Krejci19a96102018-11-15 13:38:09 +0100864}
865
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100866void
867lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node, ly_bool unlink)
868{
869 struct lysc_node *iter, **child_p;
870
Radek Krejci2a9fc652021-01-22 17:44:34 +0100871 if (node->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
872 /* nothing to do - inouts are part of actions and cannot be unlinked/freed separately */
873 return;
874 }
875
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100876 if (unlink) {
877 /* unlink from siblings */
878 if (node->prev->next) {
879 node->prev->next = node->next;
880 }
881 if (node->next) {
882 node->next->prev = node->prev;
883 } else {
884 /* unlinking the last node */
885 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100886 if (node->nodetype == LYS_ACTION) {
887 iter = (struct lysc_node *)lysc_node_actions(node->parent);
888 } else if (node->nodetype == LYS_NOTIF) {
889 iter = (struct lysc_node *)lysc_node_notifs(node->parent);
890 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +0100891 iter = (struct lysc_node *)lysc_node_child(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100892 }
Michal Vasko539c4a62020-11-03 17:21:34 +0100893 LY_CHECK_ERR_RET(!iter, LOGINT(ctx), );
Radek Krejci2a9fc652021-01-22 17:44:34 +0100894 } else if (node->nodetype == LYS_RPC) {
895 iter = (struct lysc_node *)node->module->compiled->rpcs;
896 } else if (node->nodetype == LYS_NOTIF) {
897 iter = (struct lysc_node *)node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100898 } else {
899 iter = node->module->compiled->data;
900 }
901 /* update the "last" pointer from the first node */
902 iter->prev = node->prev;
903 }
904
905 /* unlink from parent */
906 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100907 if (node->nodetype == LYS_ACTION) {
908 child_p = (struct lysc_node **)lysc_node_actions_p(node->parent);
909 } else if (node->nodetype == LYS_NOTIF) {
910 child_p = (struct lysc_node **)lysc_node_notifs_p(node->parent);
911 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +0100912 child_p = lysc_node_child_p(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100913 }
914 } else if (node->nodetype == LYS_RPC) {
915 child_p = (struct lysc_node **)&node->module->compiled->rpcs;
916 } else if (node->nodetype == LYS_NOTIF) {
917 child_p = (struct lysc_node **)&node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100918 } else {
919 child_p = &node->module->compiled->data;
920 }
921 if (child_p && (*child_p == node)) {
922 /* the node is the first child */
923 *child_p = node->next;
924 }
925 }
926
927 lysc_node_free_(ctx, node);
928}
929
Radek Krejci19a96102018-11-15 13:38:09 +0100930static void
931lysc_module_free_(struct lysc_module *module)
932{
933 struct ly_ctx *ctx;
934 struct lysc_node *node, *node_next;
935
Michal Vaskod989ba02020-08-24 10:59:24 +0200936 LY_CHECK_ARG_RET(NULL, module, );
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100937 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100938
Radek Krejci19a96102018-11-15 13:38:09 +0100939 LY_LIST_FOR_SAFE(module->data, node_next, node) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100940 lysc_node_free_(ctx, node);
Radek Krejci19a96102018-11-15 13:38:09 +0100941 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100942 LY_LIST_FOR_SAFE((struct lysc_node *)module->rpcs, node_next, node) {
943 lysc_node_free_(ctx, node);
944 }
945 LY_LIST_FOR_SAFE((struct lysc_node *)module->notifs, node_next, node) {
946 lysc_node_free_(ctx, node);
947 }
Radek Krejci19a96102018-11-15 13:38:09 +0100948 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
949
950 free(module);
951}
952
953void
954lysc_module_free(struct lysc_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
955{
Radek Krejci9b042892019-02-13 14:28:44 +0100956 /* TODO use the destructor, this just suppress warning about unused parameter */
957 (void) private_destructor;
958
Radek Krejci19a96102018-11-15 13:38:09 +0100959 if (module) {
960 lysc_module_free_(module);
961 }
962}
963
964void
965lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
966{
967 if (!module) {
968 return;
969 }
970
971 lysc_module_free(module->compiled, private_destructor);
Radek Krejci80d281e2020-09-14 17:42:54 +0200972 FREE_ARRAY(module->ctx, module->identities, lysc_ident_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100973 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100974
Michal Vasko7f45cf22020-10-01 12:49:44 +0200975 LY_ARRAY_FREE(module->augmented_by);
976 LY_ARRAY_FREE(module->deviated_by);
977
Michal Vaskoe180ed02021-02-05 16:31:20 +0100978 lydict_remove(module->ctx, module->name);
979 lydict_remove(module->ctx, module->revision);
980 lydict_remove(module->ctx, module->ns);
981 lydict_remove(module->ctx, module->prefix);
982 lydict_remove(module->ctx, module->filepath);
983 lydict_remove(module->ctx, module->org);
984 lydict_remove(module->ctx, module->contact);
985 lydict_remove(module->ctx, module->dsc);
986 lydict_remove(module->ctx, module->ref);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100987
Radek Krejci19a96102018-11-15 13:38:09 +0100988 free(module);
989}
Michal Vasko33ff9422020-07-03 09:50:39 +0200990
Radek Krejci38d85362019-09-05 16:26:38 +0200991API void
Radek Krejci0b013302021-03-29 15:22:32 +0200992lyplg_ext_instance_substatements_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
Radek Krejci38d85362019-09-05 16:26:38 +0200993{
Radek Krejci1b2eef82021-02-17 11:17:27 +0100994 LY_ARRAY_COUNT_TYPE u;
995
996 LY_ARRAY_FOR(substmts, u) {
Radek Krejci38d85362019-09-05 16:26:38 +0200997 if (!substmts[u].storage) {
998 continue;
999 }
1000
Michal Vaskod989ba02020-08-24 10:59:24 +02001001 switch (substmts[u].stmt) {
Radek Krejci6b88a462021-02-17 12:39:34 +01001002 case LY_STMT_ACTION:
1003 case LY_STMT_ANYDATA:
1004 case LY_STMT_ANYXML:
1005 case LY_STMT_CONTAINER:
1006 case LY_STMT_CHOICE:
1007 case LY_STMT_LEAF:
1008 case LY_STMT_LEAF_LIST:
1009 case LY_STMT_LIST:
1010 case LY_STMT_NOTIFICATION:
1011 case LY_STMT_RPC:
1012 case LY_STMT_USES: {
1013 struct lysc_node *child, *child_next;
1014
1015 LY_LIST_FOR_SAFE(*((struct lysc_node **)substmts[u].storage), child_next, child) {
1016 lysc_node_free_(ctx, child);
Radek Krejci38d85362019-09-05 16:26:38 +02001017 }
1018 break;
Radek Krejci6b88a462021-02-17 12:39:34 +01001019 }
1020 case LY_STMT_CONFIG:
1021 case LY_STMT_STATUS:
1022 /* nothing to do */
1023 break;
Radek Krejci1b2eef82021-02-17 11:17:27 +01001024 case LY_STMT_DESCRIPTION:
1025 case LY_STMT_REFERENCE:
Radek Krejci38d85362019-09-05 16:26:38 +02001026 case LY_STMT_UNITS:
1027 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1028 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02001029 const char *str = *((const char **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001030 if (!str) {
1031 break;
1032 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001033 lydict_remove(ctx, str);
Radek Krejci38d85362019-09-05 16:26:38 +02001034 } else {
1035 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +02001036 const char **strs = *((const char ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001037 if (!strs) {
1038 break;
1039 }
1040 FREE_STRINGS(ctx, strs);
1041 }
1042 break;
Radek Krejci38d85362019-09-05 16:26:38 +02001043 case LY_STMT_IF_FEATURE: {
Michal Vasko22df3f02020-08-24 13:29:22 +02001044 struct lysc_iffeature *iff = *((struct lysc_iffeature **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001045 if (!iff) {
1046 break;
1047 }
1048 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1049 /* single item */
1050 lysc_iffeature_free(ctx, iff);
1051 free(iff);
1052 } else {
1053 /* multiple items */
1054 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
1055 }
1056 break;
Radek Krejci6b88a462021-02-17 12:39:34 +01001057 case LY_STMT_TYPE:
1058 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1059 /* single item */
1060 struct lysc_type *type = *((struct lysc_type **)substmts[u].storage);
1061 if (!type) {
1062 break;
1063 }
1064 lysc_type_free(ctx, type);
1065 } else {
1066 /* multiple items */
1067 struct lysc_type **types = *((struct lysc_type ***)substmts[u].storage);
1068 if (!types) {
1069 break;
1070 }
1071 FREE_ARRAY(ctx, types, lysc_type2_free);
1072 }
1073 break;
Radek Krejci38d85362019-09-05 16:26:38 +02001074 }
1075
Radek Krejci0f969882020-08-21 16:56:47 +02001076 /* TODO other statements */
Radek Krejci38d85362019-09-05 16:26:38 +02001077 default:
1078 LOGINT(ctx);
1079 }
1080 }
Radek Krejci1b2eef82021-02-17 11:17:27 +01001081
1082 LY_ARRAY_FREE(substmts);
Radek Krejci38d85362019-09-05 16:26:38 +02001083}
David Sedlákebd3acf2019-07-26 15:04:32 +02001084
1085void
Michal Vaskob36053d2020-03-26 15:49:30 +01001086yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001087{
1088 if (ctx) {
1089 free(ctx);
1090 }
1091}
1092
1093void
Michal Vaskob36053d2020-03-26 15:49:30 +01001094yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001095{
1096 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001097 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +02001098 free(ctx);
1099 }
1100}