blob: dbb2d11e82df2c7931520aaf71de8550daaa8c0c [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
Radek Krejcie7b95092019-05-15 11:03:07 +02002 * @file tree_schema_free.c
Radek Krejci19a96102018-11-15 13:38:09 +01003 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejcie7b95092019-05-15 11:03:07 +02004 * @brief Freeing functions for schema tree structures.
Radek Krejci19a96102018-11-15 13:38:09 +01005 *
Radek Krejcie7b95092019-05-15 11:03:07 +02006 * Copyright (c) 2019 CESNET, z.s.p.o.
Radek Krejci19a96102018-11-15 13:38:09 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejcie7b95092019-05-15 11:03:07 +020015#include <stdlib.h>
16
Radek Krejci535ea9f2020-05-29 16:01:05 +020017#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020018#include "compat.h"
Radek Krejci47fab892020-11-05 17:02:41 +010019#include "log.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include "plugins_exts.h"
21#include "plugins_types.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010025#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "xml.h"
Radek Krejci19a96102018-11-15 13:38:09 +010027#include "xpath.h"
28
Radek Krejci2d7a47b2019-05-16 13:34:10 +020029void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
Michal Vasko5fe75f12020-03-02 13:52:37 +010030void lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010031static void lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node);
Radek Krejci19a96102018-11-15 13:38:09 +010032
33static void
34lysp_stmt_free(struct ly_ctx *ctx, struct lysp_stmt *stmt)
35{
36 struct lysp_stmt *child, *next;
37
38 FREE_STRING(ctx, stmt->stmt);
39 FREE_STRING(ctx, stmt->arg);
40
41 LY_LIST_FOR_SAFE(stmt->child, next, child) {
42 lysp_stmt_free(ctx, child);
43 }
44
45 free(stmt);
46}
47
Radek Krejci2d7a47b2019-05-16 13:34:10 +020048void
Radek Krejci19a96102018-11-15 13:38:09 +010049lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext)
50{
51 struct lysp_stmt *stmt, *next;
52
53 FREE_STRING(ctx, ext->name);
54 FREE_STRING(ctx, ext->argument);
55
56 LY_LIST_FOR_SAFE(ext->child, next, stmt) {
57 lysp_stmt_free(ctx, stmt);
58 }
59}
60
David Sedlák298ff6d2019-07-26 14:29:03 +020061void
Radek Krejci19a96102018-11-15 13:38:09 +010062lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import)
63{
64 /* imported module is freed directly from the context's list */
65 FREE_STRING(ctx, import->name);
66 FREE_STRING(ctx, import->prefix);
67 FREE_STRING(ctx, import->dsc);
68 FREE_STRING(ctx, import->ref);
69 FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free);
70}
71
David Sedlák0c2bab92019-07-22 15:33:19 +020072void
Radek Krejci19a96102018-11-15 13:38:09 +010073lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include)
74{
75 if (include->submodule) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +020076 lysp_module_free((struct lysp_module *)include->submodule);
Radek Krejci19a96102018-11-15 13:38:09 +010077 }
78 FREE_STRING(ctx, include->name);
79 FREE_STRING(ctx, include->dsc);
80 FREE_STRING(ctx, include->ref);
81 FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free);
82}
83
David Sedlákaa854b02019-07-22 14:17:10 +020084void
Radek Krejci19a96102018-11-15 13:38:09 +010085lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev)
86{
87 FREE_STRING(ctx, rev->dsc);
88 FREE_STRING(ctx, rev->ref);
89 FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free);
90}
91
David Sedlák986cb412019-07-04 13:10:11 +020092void
Radek Krejci19a96102018-11-15 13:38:09 +010093lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext)
94{
95 FREE_STRING(ctx, ext->name);
96 FREE_STRING(ctx, ext->argument);
97 FREE_STRING(ctx, ext->dsc);
98 FREE_STRING(ctx, ext->ref);
99 FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free);
Michal Vasko5fe75f12020-03-02 13:52:37 +0100100 if (ext->compiled) {
101 lysc_extension_free(ctx, &ext->compiled);
102 }
Radek Krejci19a96102018-11-15 13:38:09 +0100103}
104
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200105void
Radek Krejci19a96102018-11-15 13:38:09 +0100106lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat)
107{
108 FREE_STRING(ctx, feat->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200109 FREE_ARRAY(ctx, feat->iffeatures, lysp_qname_free);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100110 FREE_ARRAY(ctx, feat->iffeatures_c, lysc_iffeature_free);
111 LY_ARRAY_FREE(feat->depfeatures);
Radek Krejci19a96102018-11-15 13:38:09 +0100112 FREE_STRING(ctx, feat->dsc);
113 FREE_STRING(ctx, feat->ref);
114 FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
115}
116
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200117void
Radek Krejci19a96102018-11-15 13:38:09 +0100118lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident)
119{
120 FREE_STRING(ctx, ident->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200121 FREE_ARRAY(ctx, ident->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100122 FREE_STRINGS(ctx, ident->bases);
123 FREE_STRING(ctx, ident->dsc);
124 FREE_STRING(ctx, ident->ref);
125 FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
126}
127
Michal Vasko7f45cf22020-10-01 12:49:44 +0200128void
Radek Krejci19a96102018-11-15 13:38:09 +0100129lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
130{
Michal Vasko7f45cf22020-10-01 12:49:44 +0200131 FREE_STRING(ctx, restr->arg.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100132 FREE_STRING(ctx, restr->emsg);
133 FREE_STRING(ctx, restr->eapptag);
134 FREE_STRING(ctx, restr->dsc);
135 FREE_STRING(ctx, restr->ref);
136 FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
137}
138
139static void
140lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item)
141{
142 FREE_STRING(ctx, item->name);
143 FREE_STRING(ctx, item->dsc);
144 FREE_STRING(ctx, item->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200145 FREE_ARRAY(ctx, item->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100146 FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
147}
148
Radek Krejcicdfecd92018-11-26 11:27:32 +0100149void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type);
Michal Vasko004d3152020-06-11 19:59:22 +0200150
David Sedlák32488102019-07-15 17:44:10 +0200151void
Radek Krejci19a96102018-11-15 13:38:09 +0100152lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type)
153{
154 FREE_STRING(ctx, type->name);
155 FREE_MEMBER(ctx, type->range, lysp_restr_free);
156 FREE_MEMBER(ctx, type->length, lysp_restr_free);
157 FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
158 FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
159 FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
Michal Vasko004d3152020-06-11 19:59:22 +0200160 lyxp_expr_free(ctx, type->path);
Radek Krejci19a96102018-11-15 13:38:09 +0100161 FREE_STRINGS(ctx, type->bases);
162 FREE_ARRAY(ctx, type->types, lysp_type_free);
163 FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
164 if (type->compiled) {
165 lysc_type_free(ctx, type->compiled);
166 }
167}
168
David Sedlák04e17b22019-07-19 15:29:48 +0200169void
Radek Krejci19a96102018-11-15 13:38:09 +0100170lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
171{
172 FREE_STRING(ctx, tpdf->name);
173 FREE_STRING(ctx, tpdf->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200174 FREE_STRING(ctx, tpdf->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100175 FREE_STRING(ctx, tpdf->dsc);
176 FREE_STRING(ctx, tpdf->ref);
177 FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
178
179 lysp_type_free(ctx, &tpdf->type);
180
181}
182
Radek Krejcif538ce52019-03-05 10:46:14 +0100183void
Radek Krejci19a96102018-11-15 13:38:09 +0100184lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout)
185{
186 struct lysp_node *node, *next;
187
188 FREE_ARRAY(ctx, inout->musts, lysp_restr_free);
189 FREE_ARRAY(ctx, inout->typedefs, lysp_tpdf_free);
190 FREE_ARRAY(ctx, inout->groupings, lysp_grp_free);
191 LY_LIST_FOR_SAFE(inout->data, next, node) {
192 lysp_node_free(ctx, node);
193 }
194 FREE_ARRAY(ctx, inout->exts, lysp_ext_instance_free);
195
196}
197
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200198void
Radek Krejci19a96102018-11-15 13:38:09 +0100199lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action)
200{
201 FREE_STRING(ctx, action->name);
202 FREE_STRING(ctx, action->dsc);
203 FREE_STRING(ctx, action->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200204 FREE_ARRAY(ctx, action->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100205 FREE_ARRAY(ctx, action->typedefs, lysp_tpdf_free);
206 FREE_ARRAY(ctx, action->groupings, lysp_grp_free);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100207 lysp_action_inout_free(ctx, &action->input);
208 lysp_action_inout_free(ctx, &action->output);
Radek Krejci19a96102018-11-15 13:38:09 +0100209 FREE_ARRAY(ctx, action->exts, lysp_ext_instance_free);
210}
211
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200212void
Radek Krejci19a96102018-11-15 13:38:09 +0100213lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif)
214{
215 struct lysp_node *node, *next;
216
217 FREE_STRING(ctx, notif->name);
218 FREE_STRING(ctx, notif->dsc);
219 FREE_STRING(ctx, notif->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200220 FREE_ARRAY(ctx, notif->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100221 FREE_ARRAY(ctx, notif->musts, lysp_restr_free);
222 FREE_ARRAY(ctx, notif->typedefs, lysp_tpdf_free);
223 FREE_ARRAY(ctx, notif->groupings, lysp_grp_free);
224 LY_LIST_FOR_SAFE(notif->data, next, node) {
225 lysp_node_free(ctx, node);
226 }
227 FREE_ARRAY(ctx, notif->exts, lysp_ext_instance_free);
228}
229
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200230void
Radek Krejci19a96102018-11-15 13:38:09 +0100231lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp)
232{
233 struct lysp_node *node, *next;
234
235 FREE_STRING(ctx, grp->name);
236 FREE_STRING(ctx, grp->dsc);
237 FREE_STRING(ctx, grp->ref);
238 FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
239 FREE_ARRAY(ctx, grp->groupings, lysp_grp_free);
240 LY_LIST_FOR_SAFE(grp->data, next, node) {
241 lysp_node_free(ctx, node);
242 }
243 FREE_ARRAY(ctx, grp->actions, lysp_action_free);
244 FREE_ARRAY(ctx, grp->notifs, lysp_notif_free);
245 FREE_ARRAY(ctx, grp->exts, lysp_ext_instance_free);
246}
247
Radek Krejcif09e4e82019-06-14 15:08:11 +0200248void
Radek Krejci19a96102018-11-15 13:38:09 +0100249lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
250{
251 FREE_STRING(ctx, when->cond);
252 FREE_STRING(ctx, when->dsc);
253 FREE_STRING(ctx, when->ref);
254 FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
255}
256
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200257void
Radek Krejci19a96102018-11-15 13:38:09 +0100258lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment)
259{
260 struct lysp_node *node, *next;
261
262 FREE_STRING(ctx, augment->nodeid);
263 FREE_STRING(ctx, augment->dsc);
264 FREE_STRING(ctx, augment->ref);
265 FREE_MEMBER(ctx, augment->when, lysp_when_free);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200266 FREE_ARRAY(ctx, augment->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100267 LY_LIST_FOR_SAFE(augment->child, next, node) {
268 lysp_node_free(ctx, node);
269 }
270 FREE_ARRAY(ctx, augment->actions, lysp_action_free);
271 FREE_ARRAY(ctx, augment->notifs, lysp_notif_free);
272 FREE_ARRAY(ctx, augment->exts, lysp_ext_instance_free);
273}
274
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200275void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200276lysp_qname_free(struct ly_ctx *ctx, struct lysp_qname *qname)
277{
278 if (qname) {
279 FREE_STRING(ctx, qname->str);
280 }
281}
282
283void
Radek Krejci19a96102018-11-15 13:38:09 +0100284lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
285{
Michal Vasko22df3f02020-08-24 13:29:22 +0200286 struct lysp_deviate_add *add = (struct lysp_deviate_add *)d;
287 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl *)d;
Radek Krejci19a96102018-11-15 13:38:09 +0100288
289 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
Michal Vaskod989ba02020-08-24 10:59:24 +0200290 switch (d->mod) {
Radek Krejci19a96102018-11-15 13:38:09 +0100291 case LYS_DEV_NOT_SUPPORTED:
292 /* nothing to do */
293 break;
294 case LYS_DEV_ADD:
295 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
296 FREE_STRING(ctx, add->units);
297 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200298 FREE_ARRAY(ctx, add->uniques, lysp_qname_free);
299 FREE_ARRAY(ctx, add->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100300 break;
301 case LYS_DEV_REPLACE:
302 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
303 FREE_STRING(ctx, rpl->units);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200304 lysp_qname_free(ctx, &rpl->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +0100305 break;
306 default:
307 LOGINT(ctx);
308 break;
309 }
310}
311
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200312void
Radek Krejci19a96102018-11-15 13:38:09 +0100313lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
314{
315 struct lysp_deviate *next, *iter;
316
317 FREE_STRING(ctx, dev->nodeid);
318 FREE_STRING(ctx, dev->dsc);
319 FREE_STRING(ctx, dev->ref);
320 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
321 lysp_deviate_free(ctx, iter);
322 free(iter);
323 }
324 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
325}
326
David Sedlákd2d676a2019-07-22 11:28:19 +0200327void
Radek Krejci19a96102018-11-15 13:38:09 +0100328lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
329{
330 FREE_STRING(ctx, ref->nodeid);
331 FREE_STRING(ctx, ref->dsc);
332 FREE_STRING(ctx, ref->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200333 FREE_ARRAY(ctx, ref->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100334 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
335 FREE_STRING(ctx, ref->presence);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200336 FREE_ARRAY(ctx, ref->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100337 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
338}
339
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200340void
Radek Krejci19a96102018-11-15 13:38:09 +0100341lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
342{
343 struct lysp_node *child, *next;
344
345 FREE_STRING(ctx, node->name);
346 FREE_STRING(ctx, node->dsc);
347 FREE_STRING(ctx, node->ref);
348 FREE_MEMBER(ctx, node->when, lysp_when_free);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200349 FREE_ARRAY(ctx, node->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100350 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
351
Michal Vaskod989ba02020-08-24 10:59:24 +0200352 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100353 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200354 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->musts, lysp_restr_free);
355 FREE_STRING(ctx, ((struct lysp_node_container *)node)->presence);
356 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->typedefs, lysp_tpdf_free);
357 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->groupings, lysp_grp_free);
358 LY_LIST_FOR_SAFE(((struct lysp_node_container *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100359 lysp_node_free(ctx, child);
360 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200361 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->actions, lysp_action_free);
362 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->notifs, lysp_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100363 break;
364 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200365 FREE_ARRAY(ctx, ((struct lysp_node_leaf *)node)->musts, lysp_restr_free);
366 lysp_type_free(ctx, &((struct lysp_node_leaf *)node)->type);
367 FREE_STRING(ctx, ((struct lysp_node_leaf *)node)->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200368 FREE_STRING(ctx, ((struct lysp_node_leaf *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100369 break;
370 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200371 FREE_ARRAY(ctx, ((struct lysp_node_leaflist *)node)->musts, lysp_restr_free);
372 lysp_type_free(ctx, &((struct lysp_node_leaflist *)node)->type);
373 FREE_STRING(ctx, ((struct lysp_node_leaflist *)node)->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200374 FREE_ARRAY(ctx, ((struct lysp_node_leaflist *)node)->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100375 break;
376 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200377 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->musts, lysp_restr_free);
378 FREE_STRING(ctx, ((struct lysp_node_list *)node)->key);
379 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->typedefs, lysp_tpdf_free);
380 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->groupings, lysp_grp_free);
381 LY_LIST_FOR_SAFE(((struct lysp_node_list *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100382 lysp_node_free(ctx, child);
383 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200384 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->actions, lysp_action_free);
385 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->notifs, lysp_notif_free);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200386 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->uniques, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100387 break;
388 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200389 LY_LIST_FOR_SAFE(((struct lysp_node_choice *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100390 lysp_node_free(ctx, child);
391 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200392 FREE_STRING(ctx, ((struct lysp_node_choice *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100393 break;
394 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200395 LY_LIST_FOR_SAFE(((struct lysp_node_case *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100396 lysp_node_free(ctx, child);
397 }
398 break;
399 case LYS_ANYDATA:
400 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200401 FREE_ARRAY(ctx, ((struct lysp_node_anydata *)node)->musts, lysp_restr_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100402 break;
403 case LYS_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +0200404 FREE_ARRAY(ctx, ((struct lysp_node_uses *)node)->refines, lysp_refine_free);
405 FREE_ARRAY(ctx, ((struct lysp_node_uses *)node)->augments, lysp_augment_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100406 break;
407 default:
408 LOGINT(ctx);
409 }
410
411 free(node);
412}
413
Radek Krejci15f10ab2020-11-03 14:14:14 +0100414void
Radek Krejci19a96102018-11-15 13:38:09 +0100415lysp_module_free(struct lysp_module *module)
416{
417 struct ly_ctx *ctx;
418 struct lysp_node *node, *next;
419
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100420 if (!module) {
421 return;
422 }
423 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100424
425 FREE_ARRAY(ctx, module->imports, lysp_import_free);
426 FREE_ARRAY(ctx, module->includes, lysp_include_free);
427
Radek Krejci19a96102018-11-15 13:38:09 +0100428 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
429 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
430 FREE_ARRAY(ctx, module->features, lysp_feature_free);
431 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
432 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
433 FREE_ARRAY(ctx, module->groupings, lysp_grp_free);
434 LY_LIST_FOR_SAFE(module->data, next, node) {
435 lysp_node_free(ctx, node);
436 }
437 FREE_ARRAY(ctx, module->augments, lysp_augment_free);
438 FREE_ARRAY(ctx, module->rpcs, lysp_action_free);
439 FREE_ARRAY(ctx, module->notifs, lysp_notif_free);
440 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
441 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
442
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200443 if (module->is_submod) {
444 struct lysp_submodule *submod = (struct lysp_submodule *)module;
445
446 FREE_STRING(ctx, submod->name);
447 FREE_STRING(ctx, submod->filepath);
448 FREE_STRING(ctx, submod->prefix);
449 FREE_STRING(ctx, submod->org);
450 FREE_STRING(ctx, submod->contact);
451 FREE_STRING(ctx, submod->dsc);
452 FREE_STRING(ctx, submod->ref);
453 }
454
Radek Krejci19a96102018-11-15 13:38:09 +0100455 free(module);
456}
457
Radek Krejci0af46292019-01-11 16:02:31 +0100458void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100459lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
460{
461 if (--(*ext)->refcount) {
462 return;
463 }
464 FREE_STRING(ctx, (*ext)->name);
465 FREE_STRING(ctx, (*ext)->argument);
466 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
467 free(*ext);
468}
469
470void
Radek Krejci19a96102018-11-15 13:38:09 +0100471lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
472{
fredganebc50572019-10-31 15:39:23 +0800473 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200474 ext->def->plugin->free(ctx, ext);
475 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100476 if (ext->def) {
477 lysc_extension_free(ctx, &ext->def);
478 }
Radek Krejci0935f412019-08-20 16:15:18 +0200479 FREE_STRING(ctx, ext->argument);
480 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
481}
482
483void
Radek Krejci19a96102018-11-15 13:38:09 +0100484lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
485{
486 LY_ARRAY_FREE(iff->features);
487 free(iff->expr);
488}
489
490static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100491lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100492{
Radek Krejci00b874b2019-02-12 10:54:50 +0100493 if (--(*w)->refcount) {
494 return;
495 }
496 lyxp_expr_free(ctx, (*w)->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200497 lysc_prefixes_free((*w)->prefixes);
Radek Krejci00b874b2019-02-12 10:54:50 +0100498 FREE_STRING(ctx, (*w)->dsc);
499 FREE_STRING(ctx, (*w)->ref);
500 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
501 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100502}
503
Radek Krejciccd20f12019-02-15 14:12:27 +0100504void
Radek Krejci58d171e2018-11-23 13:50:55 +0100505lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
506{
507 lyxp_expr_free(ctx, must->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200508 lysc_prefixes_free(must->prefixes);
Radek Krejci58d171e2018-11-23 13:50:55 +0100509 FREE_STRING(ctx, must->emsg);
510 FREE_STRING(ctx, must->eapptag);
Radek Krejcic8b31002019-01-08 10:24:45 +0100511 FREE_STRING(ctx, must->dsc);
512 FREE_STRING(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100513 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
514}
515
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100516void
Radek Krejci19a96102018-11-15 13:38:09 +0100517lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
518{
519 FREE_STRING(ctx, ident->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100520 FREE_STRING(ctx, ident->dsc);
521 FREE_STRING(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100522 LY_ARRAY_FREE(ident->derived);
523 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
524}
525
Radek Krejci19a96102018-11-15 13:38:09 +0100526static void
527lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
528{
529 LY_ARRAY_FREE(range->parts);
530 FREE_STRING(ctx, range->eapptag);
531 FREE_STRING(ctx, range->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100532 FREE_STRING(ctx, range->dsc);
533 FREE_STRING(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100534 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
535}
536
537static void
538lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
539{
540 if (--(*pattern)->refcount) {
541 return;
542 }
Radek Krejci54579462019-04-30 12:47:06 +0200543 pcre2_code_free((*pattern)->code);
544 FREE_STRING(ctx, (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +0100545 FREE_STRING(ctx, (*pattern)->eapptag);
546 FREE_STRING(ctx, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100547 FREE_STRING(ctx, (*pattern)->dsc);
548 FREE_STRING(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100549 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
550 free(*pattern);
551}
552
553static void
Radek Krejci693262f2019-04-29 15:23:20 +0200554lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100555{
556 FREE_STRING(ctx, item->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100557 FREE_STRING(ctx, item->dsc);
558 FREE_STRING(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100559 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
560}
561
Radek Krejcia3045382018-11-22 14:30:31 +0100562static void
563lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
564{
565 lysc_type_free(ctx, *type);
566}
Radek Krejci0f969882020-08-21 16:56:47 +0200567
Radek Krejcicdfecd92018-11-26 11:27:32 +0100568void
Radek Krejci19a96102018-11-15 13:38:09 +0100569lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
570{
571 if (--type->refcount) {
572 return;
573 }
Radek Krejcib915ac92020-08-14 23:31:04 +0200574
Michal Vaskod989ba02020-08-24 10:59:24 +0200575 switch (type->basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100576 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +0200577 FREE_MEMBER(ctx, ((struct lysc_type_bin *)type)->length, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100578 break;
579 case LY_TYPE_BITS:
Michal Vasko22df3f02020-08-24 13:29:22 +0200580 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 +0100581 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100582 case LY_TYPE_DEC64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200583 FREE_MEMBER(ctx, ((struct lysc_type_dec *)type)->range, lysc_range_free);
Radek Krejci6cba4292018-11-15 17:33:29 +0100584 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100585 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +0200586 FREE_MEMBER(ctx, ((struct lysc_type_str *)type)->length, lysc_range_free);
587 FREE_ARRAY(ctx, ((struct lysc_type_str *)type)->patterns, lysc_pattern_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100588 break;
589 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +0200590 FREE_ARRAY(ctx, ((struct lysc_type_enum *)type)->enums, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100591 break;
592 case LY_TYPE_INT8:
593 case LY_TYPE_UINT8:
594 case LY_TYPE_INT16:
595 case LY_TYPE_UINT16:
596 case LY_TYPE_INT32:
597 case LY_TYPE_UINT32:
598 case LY_TYPE_INT64:
599 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200600 FREE_MEMBER(ctx, ((struct lysc_type_num *)type)->range, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100601 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100602 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +0200603 LY_ARRAY_FREE(((struct lysc_type_identityref *)type)->bases);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100604 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100605 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +0200606 FREE_ARRAY(ctx, ((struct lysc_type_union *)type)->types, lysc_type2_free);
Radek Krejcia3045382018-11-22 14:30:31 +0100607 break;
608 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200609 lyxp_expr_free(ctx, ((struct lysc_type_leafref *)type)->path);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200610 lysc_prefixes_free(((struct lysc_type_leafref *)type)->prefixes);
Radek Krejcia3045382018-11-22 14:30:31 +0100611 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100612 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100613 case LY_TYPE_BOOL:
614 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100615 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100616 /* nothing to do */
617 break;
618 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200619
620 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100621 free(type);
622}
623
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100624void
Radek Krejcif538ce52019-03-05 10:46:14 +0100625lysc_action_inout_free(struct ly_ctx *ctx, struct lysc_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100626{
627 struct lysc_node *child, *child_next;
628
Radek Krejcif538ce52019-03-05 10:46:14 +0100629 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
630 LY_LIST_FOR_SAFE(inout->data, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100631 lysc_node_free_(ctx, child);
Radek Krejcif538ce52019-03-05 10:46:14 +0100632 }
633}
634
635void
636lysc_action_free(struct ly_ctx *ctx, struct lysc_action *action)
637{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100638 FREE_STRING(ctx, action->name);
639 FREE_STRING(ctx, action->dsc);
640 FREE_STRING(ctx, action->ref);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100641 FREE_ARRAY(ctx, action->exts, lysc_ext_instance_free);
Michal Vaskoceab6dd2020-10-09 16:53:36 +0200642 FREE_ARRAY(ctx, action->when, lysc_when_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200643 FREE_ARRAY(ctx, action->input_exts, lysc_ext_instance_free);
Radek Krejcif538ce52019-03-05 10:46:14 +0100644 lysc_action_inout_free(ctx, &action->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200645 FREE_ARRAY(ctx, action->output_exts, lysc_ext_instance_free);
Radek Krejcif538ce52019-03-05 10:46:14 +0100646 lysc_action_inout_free(ctx, &action->output);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100647}
648
Radek Krejcifc11bd72019-04-11 16:00:05 +0200649void
650lysc_notif_free(struct ly_ctx *ctx, struct lysc_notif *notif)
651{
652 struct lysc_node *child, *child_next;
653
654 FREE_STRING(ctx, notif->name);
655 FREE_STRING(ctx, notif->dsc);
656 FREE_STRING(ctx, notif->ref);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200657 FREE_ARRAY(ctx, notif->exts, lysc_ext_instance_free);
Michal Vaskoceab6dd2020-10-09 16:53:36 +0200658 FREE_ARRAY(ctx, notif->when, lysc_when_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200659 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
660 LY_LIST_FOR_SAFE(notif->data, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100661 lysc_node_free_(ctx, child);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200662 }
663}
664
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200665void
Radek Krejci19a96102018-11-15 13:38:09 +0100666lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
667{
668 struct lysc_node *child, *child_next;
669
670 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100671 lysc_node_free_(ctx, child);
Radek Krejci19a96102018-11-15 13:38:09 +0100672 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100673 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100674 FREE_ARRAY(ctx, node->actions, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200675 FREE_ARRAY(ctx, node->notifs, lysc_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100676}
677
678static void
679lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
680{
Radek Krejci58d171e2018-11-23 13:50:55 +0100681 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100682 if (node->type) {
683 lysc_type_free(ctx, node->type);
684 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100685 FREE_STRING(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200686 if (node->dflt) {
687 node->dflt->realtype->plugin->free(ctx, node->dflt);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200688 lysc_type_free(ctx, (struct lysc_type *)node->dflt->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200689 free(node->dflt);
690 }
Radek Krejci19a96102018-11-15 13:38:09 +0100691}
692
Radek Krejci42452ac2018-11-28 17:09:52 +0100693static void
694lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
695{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200696 LY_ARRAY_COUNT_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100697
Radek Krejci42452ac2018-11-28 17:09:52 +0100698 FREE_ARRAY(ctx, node->musts, lysc_must_free);
699 if (node->type) {
700 lysc_type_free(ctx, node->type);
701 }
702 FREE_STRING(ctx, node->units);
703 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200704 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200705 lysc_type_free(ctx, (struct lysc_type *)node->dflts[u]->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200706 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100707 }
708 LY_ARRAY_FREE(node->dflts);
709}
710
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100711static void
712lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
713{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200714 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100715 struct lysc_node *child, *child_next;
716
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100717 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100718 lysc_node_free_(ctx, child);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100719 }
720 FREE_ARRAY(ctx, node->musts, lysc_must_free);
721
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100722 LY_ARRAY_FOR(node->uniques, u) {
723 LY_ARRAY_FREE(node->uniques[u]);
724 }
725 LY_ARRAY_FREE(node->uniques);
726
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100727 FREE_ARRAY(ctx, node->actions, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200728 FREE_ARRAY(ctx, node->notifs, lysc_notif_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100729}
730
Radek Krejci056d0a82018-12-06 16:57:25 +0100731static void
732lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
733{
734 struct lysc_node *child, *child_next;
735
Michal Vasko20424b42020-08-31 12:29:38 +0200736 LY_LIST_FOR_SAFE((struct lysc_node *)node->cases, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100737 lysc_node_free_(ctx, child);
Michal Vasko20424b42020-08-31 12:29:38 +0200738 }
739}
740
741static void
742lysc_node_case_free(struct ly_ctx *ctx, struct lysc_node_case *node)
743{
744 struct lysc_node *child, *child_next;
745
746 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100747 lysc_node_free_(ctx, child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100748 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100749}
Radek Krejci056d0a82018-12-06 16:57:25 +0100750
Radek Krejci9800fb82018-12-13 14:26:23 +0100751static void
752lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
753{
754 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100755}
756
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100757static void
758lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +0100759{
760 /* common part */
761 FREE_STRING(ctx, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +0100762 FREE_STRING(ctx, node->dsc);
763 FREE_STRING(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100764
765 /* nodetype-specific part */
Michal Vaskod989ba02020-08-24 10:59:24 +0200766 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100767 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200768 lysc_node_container_free(ctx, (struct lysc_node_container *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100769 break;
770 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200771 lysc_node_leaf_free(ctx, (struct lysc_node_leaf *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100772 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100773 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200774 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist *)node);
Radek Krejci42452ac2018-11-28 17:09:52 +0100775 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100776 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200777 lysc_node_list_free(ctx, (struct lysc_node_list *)node);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100778 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100779 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200780 lysc_node_choice_free(ctx, (struct lysc_node_choice *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100781 break;
782 case LYS_CASE:
Michal Vasko20424b42020-08-31 12:29:38 +0200783 lysc_node_case_free(ctx, (struct lysc_node_case *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100784 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100785 case LYS_ANYDATA:
786 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200787 lysc_node_anydata_free(ctx, (struct lysc_node_anydata *)node);
Radek Krejci9800fb82018-12-13 14:26:23 +0100788 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100789 default:
790 LOGINT(ctx);
791 }
792
Radek Krejci00b874b2019-02-12 10:54:50 +0100793 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100794 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100795 free(node);
796}
797
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100798void
799lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node, ly_bool unlink)
800{
801 struct lysc_node *iter, **child_p;
802
803 if (unlink) {
804 /* unlink from siblings */
805 if (node->prev->next) {
806 node->prev->next = node->next;
807 }
808 if (node->next) {
809 node->next->prev = node->prev;
810 } else {
811 /* unlinking the last node */
812 if (node->parent) {
813 iter = (struct lysc_node *)lysc_node_children(node->parent, node->flags & LYS_CONFIG_MASK);
Michal Vasko539c4a62020-11-03 17:21:34 +0100814 LY_CHECK_ERR_RET(!iter, LOGINT(ctx), );
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100815 } else {
816 iter = node->module->compiled->data;
817 }
818 /* update the "last" pointer from the first node */
819 iter->prev = node->prev;
820 }
821
822 /* unlink from parent */
823 if (node->parent) {
824 child_p = lysc_node_children_p(node->parent, node->flags & LYS_CONFIG_MASK);
825 } else {
826 child_p = &node->module->compiled->data;
827 }
828 if (child_p && (*child_p == node)) {
829 /* the node is the first child */
830 *child_p = node->next;
831 }
832 }
833
834 lysc_node_free_(ctx, node);
835}
836
Radek Krejci19a96102018-11-15 13:38:09 +0100837static void
838lysc_module_free_(struct lysc_module *module)
839{
840 struct ly_ctx *ctx;
841 struct lysc_node *node, *node_next;
842
Michal Vaskod989ba02020-08-24 10:59:24 +0200843 LY_CHECK_ARG_RET(NULL, module, );
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100844 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100845
Radek Krejci19a96102018-11-15 13:38:09 +0100846 LY_LIST_FOR_SAFE(module->data, node_next, node) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100847 lysc_node_free_(ctx, node);
Radek Krejci19a96102018-11-15 13:38:09 +0100848 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100849 FREE_ARRAY(ctx, module->rpcs, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200850 FREE_ARRAY(ctx, module->notifs, lysc_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100851 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
852
853 free(module);
854}
855
856void
857lysc_module_free(struct lysc_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
858{
Radek Krejci9b042892019-02-13 14:28:44 +0100859 /* TODO use the destructor, this just suppress warning about unused parameter */
860 (void) private_destructor;
861
Radek Krejci19a96102018-11-15 13:38:09 +0100862 if (module) {
863 lysc_module_free_(module);
864 }
865}
866
867void
868lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
869{
870 if (!module) {
871 return;
872 }
873
874 lysc_module_free(module->compiled, private_destructor);
Radek Krejci80d281e2020-09-14 17:42:54 +0200875 FREE_ARRAY(module->ctx, module->identities, lysc_ident_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100876 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100877
Michal Vasko7f45cf22020-10-01 12:49:44 +0200878 LY_ARRAY_FREE(module->augmented_by);
879 LY_ARRAY_FREE(module->deviated_by);
880
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100881 FREE_STRING(module->ctx, module->name);
Radek Krejci0af46292019-01-11 16:02:31 +0100882 FREE_STRING(module->ctx, module->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100883 FREE_STRING(module->ctx, module->ns);
884 FREE_STRING(module->ctx, module->prefix);
885 FREE_STRING(module->ctx, module->filepath);
886 FREE_STRING(module->ctx, module->org);
887 FREE_STRING(module->ctx, module->contact);
888 FREE_STRING(module->ctx, module->dsc);
889 FREE_STRING(module->ctx, module->ref);
890
Radek Krejci19a96102018-11-15 13:38:09 +0100891 free(module);
892}
Michal Vasko33ff9422020-07-03 09:50:39 +0200893
Radek Krejci38d85362019-09-05 16:26:38 +0200894API void
895lysc_extension_instance_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
896{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200897 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
Radek Krejci38d85362019-09-05 16:26:38 +0200898 if (!substmts[u].storage) {
899 continue;
900 }
901
Michal Vaskod989ba02020-08-24 10:59:24 +0200902 switch (substmts[u].stmt) {
Radek Krejci38d85362019-09-05 16:26:38 +0200903 case LY_STMT_TYPE:
904 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
905 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +0200906 struct lysc_type *type = *((struct lysc_type **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200907 if (!type) {
908 break;
909 }
910 lysc_type_free(ctx, type);
911 } else {
912 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +0200913 struct lysc_type **types = *((struct lysc_type ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200914 if (!types) {
915 break;
916 }
917 FREE_ARRAY(ctx, types, lysc_type2_free);
918 }
919 break;
920 case LY_STMT_UNITS:
921 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
922 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +0200923 const char *str = *((const char **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200924 if (!str) {
925 break;
926 }
927 FREE_STRING(ctx, str);
928 } else {
929 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +0200930 const char **strs = *((const char ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200931 if (!strs) {
932 break;
933 }
934 FREE_STRINGS(ctx, strs);
935 }
936 break;
937 case LY_STMT_STATUS:
Radek Krejciad5963b2019-09-06 16:03:05 +0200938 case LY_STMT_CONFIG:
Radek Krejci38d85362019-09-05 16:26:38 +0200939 /* nothing to do */
940 break;
941 case LY_STMT_IF_FEATURE: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200942 struct lysc_iffeature *iff = *((struct lysc_iffeature **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200943 if (!iff) {
944 break;
945 }
946 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
947 /* single item */
948 lysc_iffeature_free(ctx, iff);
949 free(iff);
950 } else {
951 /* multiple items */
952 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
953 }
954 break;
955 }
956
Radek Krejci0f969882020-08-21 16:56:47 +0200957 /* TODO other statements */
Radek Krejci38d85362019-09-05 16:26:38 +0200958 default:
959 LOGINT(ctx);
960 }
961 }
962}
David Sedlákebd3acf2019-07-26 15:04:32 +0200963
964void
Michal Vaskob36053d2020-03-26 15:49:30 +0100965yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +0200966{
967 if (ctx) {
968 free(ctx);
969 }
970}
971
972void
Michal Vaskob36053d2020-03-26 15:49:30 +0100973yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +0200974{
975 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100976 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +0200977 free(ctx);
978 }
979}