blob: 673c4d9ff3c8a53f2343166768f9826fd89afc27 [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
Michal Vaskoc5a22832020-08-20 13:21:33 +020017#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include "common.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include "plugins_exts.h"
20#include "plugins_types.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010024#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "xml.h"
Radek Krejci19a96102018-11-15 13:38:09 +010026#include "xpath.h"
27
Radek Krejci2d7a47b2019-05-16 13:34:10 +020028void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
Michal Vasko5fe75f12020-03-02 13:52:37 +010029void lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext);
Radek Krejci19a96102018-11-15 13:38:09 +010030
31static void
32lysp_stmt_free(struct ly_ctx *ctx, struct lysp_stmt *stmt)
33{
34 struct lysp_stmt *child, *next;
35
36 FREE_STRING(ctx, stmt->stmt);
37 FREE_STRING(ctx, stmt->arg);
38
39 LY_LIST_FOR_SAFE(stmt->child, next, child) {
40 lysp_stmt_free(ctx, child);
41 }
42
43 free(stmt);
44}
45
Radek Krejci2d7a47b2019-05-16 13:34:10 +020046void
Radek Krejci19a96102018-11-15 13:38:09 +010047lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext)
48{
49 struct lysp_stmt *stmt, *next;
50
51 FREE_STRING(ctx, ext->name);
52 FREE_STRING(ctx, ext->argument);
53
54 LY_LIST_FOR_SAFE(ext->child, next, stmt) {
55 lysp_stmt_free(ctx, stmt);
56 }
57}
58
David Sedlák298ff6d2019-07-26 14:29:03 +020059void
Radek Krejci19a96102018-11-15 13:38:09 +010060lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import)
61{
62 /* imported module is freed directly from the context's list */
63 FREE_STRING(ctx, import->name);
64 FREE_STRING(ctx, import->prefix);
65 FREE_STRING(ctx, import->dsc);
66 FREE_STRING(ctx, import->ref);
67 FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free);
68}
69
David Sedlák0c2bab92019-07-22 15:33:19 +020070void
Radek Krejci19a96102018-11-15 13:38:09 +010071lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include)
72{
73 if (include->submodule) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +010074 lysp_submodule_free(ctx, include->submodule);
Radek Krejci19a96102018-11-15 13:38:09 +010075 }
76 FREE_STRING(ctx, include->name);
77 FREE_STRING(ctx, include->dsc);
78 FREE_STRING(ctx, include->ref);
79 FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free);
80}
81
David Sedlákaa854b02019-07-22 14:17:10 +020082void
Radek Krejci19a96102018-11-15 13:38:09 +010083lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev)
84{
85 FREE_STRING(ctx, rev->dsc);
86 FREE_STRING(ctx, rev->ref);
87 FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free);
88}
89
David Sedlák986cb412019-07-04 13:10:11 +020090void
Radek Krejci19a96102018-11-15 13:38:09 +010091lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext)
92{
93 FREE_STRING(ctx, ext->name);
94 FREE_STRING(ctx, ext->argument);
95 FREE_STRING(ctx, ext->dsc);
96 FREE_STRING(ctx, ext->ref);
97 FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free);
Michal Vasko5fe75f12020-03-02 13:52:37 +010098 if (ext->compiled) {
99 lysc_extension_free(ctx, &ext->compiled);
100 }
Radek Krejci19a96102018-11-15 13:38:09 +0100101}
102
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200103void
Radek Krejci19a96102018-11-15 13:38:09 +0100104lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat)
105{
106 FREE_STRING(ctx, feat->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200107 FREE_ARRAY(ctx, feat->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100108 FREE_STRING(ctx, feat->dsc);
109 FREE_STRING(ctx, feat->ref);
110 FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
111}
112
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200113void
Radek Krejci19a96102018-11-15 13:38:09 +0100114lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident)
115{
116 FREE_STRING(ctx, ident->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200117 FREE_ARRAY(ctx, ident->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100118 FREE_STRINGS(ctx, ident->bases);
119 FREE_STRING(ctx, ident->dsc);
120 FREE_STRING(ctx, ident->ref);
121 FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
122}
123
Michal Vasko7f45cf22020-10-01 12:49:44 +0200124void
Radek Krejci19a96102018-11-15 13:38:09 +0100125lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
126{
Michal Vasko7f45cf22020-10-01 12:49:44 +0200127 FREE_STRING(ctx, restr->arg.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100128 FREE_STRING(ctx, restr->emsg);
129 FREE_STRING(ctx, restr->eapptag);
130 FREE_STRING(ctx, restr->dsc);
131 FREE_STRING(ctx, restr->ref);
132 FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
133}
134
135static void
136lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item)
137{
138 FREE_STRING(ctx, item->name);
139 FREE_STRING(ctx, item->dsc);
140 FREE_STRING(ctx, item->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200141 FREE_ARRAY(ctx, item->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100142 FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
143}
144
Radek Krejcicdfecd92018-11-26 11:27:32 +0100145void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type);
Michal Vasko004d3152020-06-11 19:59:22 +0200146
David Sedlák32488102019-07-15 17:44:10 +0200147void
Radek Krejci19a96102018-11-15 13:38:09 +0100148lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type)
149{
150 FREE_STRING(ctx, type->name);
151 FREE_MEMBER(ctx, type->range, lysp_restr_free);
152 FREE_MEMBER(ctx, type->length, lysp_restr_free);
153 FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
154 FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
155 FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
Michal Vasko004d3152020-06-11 19:59:22 +0200156 lyxp_expr_free(ctx, type->path);
Radek Krejci19a96102018-11-15 13:38:09 +0100157 FREE_STRINGS(ctx, type->bases);
158 FREE_ARRAY(ctx, type->types, lysp_type_free);
159 FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
160 if (type->compiled) {
161 lysc_type_free(ctx, type->compiled);
162 }
163}
164
David Sedlák04e17b22019-07-19 15:29:48 +0200165void
Radek Krejci19a96102018-11-15 13:38:09 +0100166lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
167{
168 FREE_STRING(ctx, tpdf->name);
169 FREE_STRING(ctx, tpdf->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200170 FREE_STRING(ctx, tpdf->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100171 FREE_STRING(ctx, tpdf->dsc);
172 FREE_STRING(ctx, tpdf->ref);
173 FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
174
175 lysp_type_free(ctx, &tpdf->type);
176
177}
178
Radek Krejcif538ce52019-03-05 10:46:14 +0100179void
Radek Krejci19a96102018-11-15 13:38:09 +0100180lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout)
181{
182 struct lysp_node *node, *next;
183
184 FREE_ARRAY(ctx, inout->musts, lysp_restr_free);
185 FREE_ARRAY(ctx, inout->typedefs, lysp_tpdf_free);
186 FREE_ARRAY(ctx, inout->groupings, lysp_grp_free);
187 LY_LIST_FOR_SAFE(inout->data, next, node) {
188 lysp_node_free(ctx, node);
189 }
190 FREE_ARRAY(ctx, inout->exts, lysp_ext_instance_free);
191
192}
193
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200194void
Radek Krejci19a96102018-11-15 13:38:09 +0100195lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action)
196{
197 FREE_STRING(ctx, action->name);
198 FREE_STRING(ctx, action->dsc);
199 FREE_STRING(ctx, action->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200200 FREE_ARRAY(ctx, action->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100201 FREE_ARRAY(ctx, action->typedefs, lysp_tpdf_free);
202 FREE_ARRAY(ctx, action->groupings, lysp_grp_free);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100203 lysp_action_inout_free(ctx, &action->input);
204 lysp_action_inout_free(ctx, &action->output);
Radek Krejci19a96102018-11-15 13:38:09 +0100205 FREE_ARRAY(ctx, action->exts, lysp_ext_instance_free);
206}
207
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200208void
Radek Krejci19a96102018-11-15 13:38:09 +0100209lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif)
210{
211 struct lysp_node *node, *next;
212
213 FREE_STRING(ctx, notif->name);
214 FREE_STRING(ctx, notif->dsc);
215 FREE_STRING(ctx, notif->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200216 FREE_ARRAY(ctx, notif->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100217 FREE_ARRAY(ctx, notif->musts, lysp_restr_free);
218 FREE_ARRAY(ctx, notif->typedefs, lysp_tpdf_free);
219 FREE_ARRAY(ctx, notif->groupings, lysp_grp_free);
220 LY_LIST_FOR_SAFE(notif->data, next, node) {
221 lysp_node_free(ctx, node);
222 }
223 FREE_ARRAY(ctx, notif->exts, lysp_ext_instance_free);
224}
225
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200226void
Radek Krejci19a96102018-11-15 13:38:09 +0100227lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp)
228{
229 struct lysp_node *node, *next;
230
231 FREE_STRING(ctx, grp->name);
232 FREE_STRING(ctx, grp->dsc);
233 FREE_STRING(ctx, grp->ref);
234 FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
235 FREE_ARRAY(ctx, grp->groupings, lysp_grp_free);
236 LY_LIST_FOR_SAFE(grp->data, next, node) {
237 lysp_node_free(ctx, node);
238 }
239 FREE_ARRAY(ctx, grp->actions, lysp_action_free);
240 FREE_ARRAY(ctx, grp->notifs, lysp_notif_free);
241 FREE_ARRAY(ctx, grp->exts, lysp_ext_instance_free);
242}
243
Radek Krejcif09e4e82019-06-14 15:08:11 +0200244void
Radek Krejci19a96102018-11-15 13:38:09 +0100245lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
246{
247 FREE_STRING(ctx, when->cond);
248 FREE_STRING(ctx, when->dsc);
249 FREE_STRING(ctx, when->ref);
250 FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
251}
252
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200253void
Radek Krejci19a96102018-11-15 13:38:09 +0100254lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment)
255{
256 struct lysp_node *node, *next;
257
258 FREE_STRING(ctx, augment->nodeid);
259 FREE_STRING(ctx, augment->dsc);
260 FREE_STRING(ctx, augment->ref);
261 FREE_MEMBER(ctx, augment->when, lysp_when_free);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200262 FREE_ARRAY(ctx, augment->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100263 LY_LIST_FOR_SAFE(augment->child, next, node) {
264 lysp_node_free(ctx, node);
265 }
266 FREE_ARRAY(ctx, augment->actions, lysp_action_free);
267 FREE_ARRAY(ctx, augment->notifs, lysp_notif_free);
268 FREE_ARRAY(ctx, augment->exts, lysp_ext_instance_free);
269}
270
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200271void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200272lysp_qname_free(struct ly_ctx *ctx, struct lysp_qname *qname)
273{
274 if (qname) {
275 FREE_STRING(ctx, qname->str);
276 }
277}
278
279void
Radek Krejci19a96102018-11-15 13:38:09 +0100280lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
281{
Michal Vasko22df3f02020-08-24 13:29:22 +0200282 struct lysp_deviate_add *add = (struct lysp_deviate_add *)d;
283 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl *)d;
Radek Krejci19a96102018-11-15 13:38:09 +0100284
285 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
Michal Vaskod989ba02020-08-24 10:59:24 +0200286 switch (d->mod) {
Radek Krejci19a96102018-11-15 13:38:09 +0100287 case LYS_DEV_NOT_SUPPORTED:
288 /* nothing to do */
289 break;
290 case LYS_DEV_ADD:
291 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
292 FREE_STRING(ctx, add->units);
293 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
294 FREE_STRINGS(ctx, add->uniques);
295 FREE_STRINGS(ctx, add->dflts);
296 break;
297 case LYS_DEV_REPLACE:
298 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
299 FREE_STRING(ctx, rpl->units);
300 FREE_STRING(ctx, rpl->dflt);
301 break;
302 default:
303 LOGINT(ctx);
304 break;
305 }
306}
307
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200308void
Radek Krejci19a96102018-11-15 13:38:09 +0100309lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
310{
311 struct lysp_deviate *next, *iter;
312
313 FREE_STRING(ctx, dev->nodeid);
314 FREE_STRING(ctx, dev->dsc);
315 FREE_STRING(ctx, dev->ref);
316 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
317 lysp_deviate_free(ctx, iter);
318 free(iter);
319 }
320 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
321}
322
David Sedlákd2d676a2019-07-22 11:28:19 +0200323void
Radek Krejci19a96102018-11-15 13:38:09 +0100324lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
325{
326 FREE_STRING(ctx, ref->nodeid);
327 FREE_STRING(ctx, ref->dsc);
328 FREE_STRING(ctx, ref->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200329 FREE_ARRAY(ctx, ref->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100330 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
331 FREE_STRING(ctx, ref->presence);
332 FREE_STRINGS(ctx, ref->dflts);
333 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
334}
335
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200336void
Radek Krejci19a96102018-11-15 13:38:09 +0100337lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
338{
339 struct lysp_node *child, *next;
340
341 FREE_STRING(ctx, node->name);
342 FREE_STRING(ctx, node->dsc);
343 FREE_STRING(ctx, node->ref);
344 FREE_MEMBER(ctx, node->when, lysp_when_free);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200345 FREE_ARRAY(ctx, node->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100346 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
347
Michal Vaskod989ba02020-08-24 10:59:24 +0200348 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100349 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200350 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->musts, lysp_restr_free);
351 FREE_STRING(ctx, ((struct lysp_node_container *)node)->presence);
352 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->typedefs, lysp_tpdf_free);
353 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->groupings, lysp_grp_free);
354 LY_LIST_FOR_SAFE(((struct lysp_node_container *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100355 lysp_node_free(ctx, child);
356 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200357 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->actions, lysp_action_free);
358 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->notifs, lysp_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100359 break;
360 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200361 FREE_ARRAY(ctx, ((struct lysp_node_leaf *)node)->musts, lysp_restr_free);
362 lysp_type_free(ctx, &((struct lysp_node_leaf *)node)->type);
363 FREE_STRING(ctx, ((struct lysp_node_leaf *)node)->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200364 FREE_STRING(ctx, ((struct lysp_node_leaf *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100365 break;
366 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200367 FREE_ARRAY(ctx, ((struct lysp_node_leaflist *)node)->musts, lysp_restr_free);
368 lysp_type_free(ctx, &((struct lysp_node_leaflist *)node)->type);
369 FREE_STRING(ctx, ((struct lysp_node_leaflist *)node)->units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200370 FREE_ARRAY(ctx, ((struct lysp_node_leaflist *)node)->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100371 break;
372 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200373 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->musts, lysp_restr_free);
374 FREE_STRING(ctx, ((struct lysp_node_list *)node)->key);
375 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->typedefs, lysp_tpdf_free);
376 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->groupings, lysp_grp_free);
377 LY_LIST_FOR_SAFE(((struct lysp_node_list *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100378 lysp_node_free(ctx, child);
379 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200380 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->actions, lysp_action_free);
381 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->notifs, lysp_notif_free);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200382 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->uniques, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100383 break;
384 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200385 LY_LIST_FOR_SAFE(((struct lysp_node_choice *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100386 lysp_node_free(ctx, child);
387 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200388 FREE_STRING(ctx, ((struct lysp_node_choice *)node)->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100389 break;
390 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200391 LY_LIST_FOR_SAFE(((struct lysp_node_case *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100392 lysp_node_free(ctx, child);
393 }
394 break;
395 case LYS_ANYDATA:
396 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200397 FREE_ARRAY(ctx, ((struct lysp_node_anydata *)node)->musts, lysp_restr_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100398 break;
399 case LYS_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +0200400 FREE_ARRAY(ctx, ((struct lysp_node_uses *)node)->refines, lysp_refine_free);
401 FREE_ARRAY(ctx, ((struct lysp_node_uses *)node)->augments, lysp_augment_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100402 break;
403 default:
404 LOGINT(ctx);
405 }
406
407 free(node);
408}
409
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100410void
411lysp_submodule_free(struct ly_ctx *ctx, struct lysp_submodule *submod)
412{
413 struct lysp_node *node, *next;
414
Radek Krejci40544fa2019-01-11 09:38:37 +0100415 if (!submod) {
416 return;
417 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100418
419 FREE_ARRAY(ctx, submod->imports, lysp_import_free);
420 FREE_ARRAY(ctx, submod->includes, lysp_include_free);
421
422 FREE_ARRAY(ctx, submod->revs, lysp_revision_free);
423 FREE_ARRAY(ctx, submod->extensions, lysp_ext_free);
424 FREE_ARRAY(ctx, submod->features, lysp_feature_free);
425 FREE_ARRAY(ctx, submod->identities, lysp_ident_free);
426 FREE_ARRAY(ctx, submod->typedefs, lysp_tpdf_free);
427 FREE_ARRAY(ctx, submod->groupings, lysp_grp_free);
428 LY_LIST_FOR_SAFE(submod->data, next, node) {
429 lysp_node_free(ctx, node);
430 }
431 FREE_ARRAY(ctx, submod->augments, lysp_augment_free);
432 FREE_ARRAY(ctx, submod->rpcs, lysp_action_free);
433 FREE_ARRAY(ctx, submod->notifs, lysp_notif_free);
434 FREE_ARRAY(ctx, submod->deviations, lysp_deviation_free);
435 FREE_ARRAY(ctx, submod->exts, lysp_ext_instance_free);
436
437 FREE_STRING(ctx, submod->belongsto);
438 FREE_STRING(ctx, submod->name);
439 FREE_STRING(ctx, submod->filepath);
440 FREE_STRING(ctx, submod->prefix);
441 FREE_STRING(ctx, submod->org);
442 FREE_STRING(ctx, submod->contact);
443 FREE_STRING(ctx, submod->dsc);
444 FREE_STRING(ctx, submod->ref);
445
446 free(submod);
447}
448
Radek Krejci19a96102018-11-15 13:38:09 +0100449API void
450lysp_module_free(struct lysp_module *module)
451{
452 struct ly_ctx *ctx;
453 struct lysp_node *node, *next;
454
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100455 if (!module) {
456 return;
457 }
458 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100459
460 FREE_ARRAY(ctx, module->imports, lysp_import_free);
461 FREE_ARRAY(ctx, module->includes, lysp_include_free);
462
Radek Krejci19a96102018-11-15 13:38:09 +0100463 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
464 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
465 FREE_ARRAY(ctx, module->features, lysp_feature_free);
466 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
467 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
468 FREE_ARRAY(ctx, module->groupings, lysp_grp_free);
469 LY_LIST_FOR_SAFE(module->data, next, node) {
470 lysp_node_free(ctx, node);
471 }
472 FREE_ARRAY(ctx, module->augments, lysp_augment_free);
473 FREE_ARRAY(ctx, module->rpcs, lysp_action_free);
474 FREE_ARRAY(ctx, module->notifs, lysp_notif_free);
475 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
476 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
477
478 free(module);
479}
480
Radek Krejci0af46292019-01-11 16:02:31 +0100481void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100482lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
483{
484 if (--(*ext)->refcount) {
485 return;
486 }
487 FREE_STRING(ctx, (*ext)->name);
488 FREE_STRING(ctx, (*ext)->argument);
489 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
490 free(*ext);
491}
492
493void
Radek Krejci19a96102018-11-15 13:38:09 +0100494lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
495{
fredganebc50572019-10-31 15:39:23 +0800496 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200497 ext->def->plugin->free(ctx, ext);
498 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100499 if (ext->def) {
500 lysc_extension_free(ctx, &ext->def);
501 }
Radek Krejci0935f412019-08-20 16:15:18 +0200502 FREE_STRING(ctx, ext->argument);
503 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
504}
505
506void
Radek Krejci19a96102018-11-15 13:38:09 +0100507lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
508{
509 LY_ARRAY_FREE(iff->features);
510 free(iff->expr);
511}
512
513static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100514lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100515{
Radek Krejci00b874b2019-02-12 10:54:50 +0100516 if (--(*w)->refcount) {
517 return;
518 }
519 lyxp_expr_free(ctx, (*w)->cond);
520 FREE_STRING(ctx, (*w)->dsc);
521 FREE_STRING(ctx, (*w)->ref);
522 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
523 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100524}
525
Radek Krejciccd20f12019-02-15 14:12:27 +0100526void
Radek Krejci58d171e2018-11-23 13:50:55 +0100527lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
528{
529 lyxp_expr_free(ctx, must->cond);
530 FREE_STRING(ctx, must->emsg);
531 FREE_STRING(ctx, must->eapptag);
Radek Krejcic8b31002019-01-08 10:24:45 +0100532 FREE_STRING(ctx, must->dsc);
533 FREE_STRING(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100534 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
535}
536
537static void
Radek Krejci19a96102018-11-15 13:38:09 +0100538lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
539{
540 FREE_STRING(ctx, ident->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100541 FREE_STRING(ctx, ident->dsc);
542 FREE_STRING(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100543 FREE_ARRAY(ctx, ident->iffeatures, lysc_iffeature_free);
544 LY_ARRAY_FREE(ident->derived);
545 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
546}
547
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200548void
Radek Krejci19a96102018-11-15 13:38:09 +0100549lysc_feature_free(struct ly_ctx *ctx, struct lysc_feature *feat)
550{
551 FREE_STRING(ctx, feat->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100552 FREE_STRING(ctx, feat->dsc);
553 FREE_STRING(ctx, feat->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100554 FREE_ARRAY(ctx, feat->iffeatures, lysc_iffeature_free);
555 LY_ARRAY_FREE(feat->depfeatures);
556 FREE_ARRAY(ctx, feat->exts, lysc_ext_instance_free);
557}
558
559static void
560lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
561{
562 LY_ARRAY_FREE(range->parts);
563 FREE_STRING(ctx, range->eapptag);
564 FREE_STRING(ctx, range->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100565 FREE_STRING(ctx, range->dsc);
566 FREE_STRING(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100567 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
568}
569
570static void
571lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
572{
573 if (--(*pattern)->refcount) {
574 return;
575 }
Radek Krejci54579462019-04-30 12:47:06 +0200576 pcre2_code_free((*pattern)->code);
577 FREE_STRING(ctx, (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +0100578 FREE_STRING(ctx, (*pattern)->eapptag);
579 FREE_STRING(ctx, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100580 FREE_STRING(ctx, (*pattern)->dsc);
581 FREE_STRING(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100582 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
583 free(*pattern);
584}
585
586static void
Radek Krejci693262f2019-04-29 15:23:20 +0200587lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100588{
589 FREE_STRING(ctx, item->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100590 FREE_STRING(ctx, item->dsc);
591 FREE_STRING(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100592 FREE_ARRAY(ctx, item->iffeatures, lysc_iffeature_free);
593 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
594}
595
Radek Krejcia3045382018-11-22 14:30:31 +0100596static void
597lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
598{
599 lysc_type_free(ctx, *type);
600}
Radek Krejci0f969882020-08-21 16:56:47 +0200601
Radek Krejcicdfecd92018-11-26 11:27:32 +0100602void
Radek Krejci19a96102018-11-15 13:38:09 +0100603lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
604{
605 if (--type->refcount) {
606 return;
607 }
Radek Krejcib915ac92020-08-14 23:31:04 +0200608
Michal Vaskod989ba02020-08-24 10:59:24 +0200609 switch (type->basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100610 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +0200611 FREE_MEMBER(ctx, ((struct lysc_type_bin *)type)->length, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100612 break;
613 case LY_TYPE_BITS:
Michal Vasko22df3f02020-08-24 13:29:22 +0200614 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 +0100615 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100616 case LY_TYPE_DEC64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200617 FREE_MEMBER(ctx, ((struct lysc_type_dec *)type)->range, lysc_range_free);
Radek Krejci6cba4292018-11-15 17:33:29 +0100618 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100619 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +0200620 FREE_MEMBER(ctx, ((struct lysc_type_str *)type)->length, lysc_range_free);
621 FREE_ARRAY(ctx, ((struct lysc_type_str *)type)->patterns, lysc_pattern_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100622 break;
623 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +0200624 FREE_ARRAY(ctx, ((struct lysc_type_enum *)type)->enums, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100625 break;
626 case LY_TYPE_INT8:
627 case LY_TYPE_UINT8:
628 case LY_TYPE_INT16:
629 case LY_TYPE_UINT16:
630 case LY_TYPE_INT32:
631 case LY_TYPE_UINT32:
632 case LY_TYPE_INT64:
633 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200634 FREE_MEMBER(ctx, ((struct lysc_type_num *)type)->range, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100635 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100636 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +0200637 LY_ARRAY_FREE(((struct lysc_type_identityref *)type)->bases);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100638 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100639 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +0200640 FREE_ARRAY(ctx, ((struct lysc_type_union *)type)->types, lysc_type2_free);
Radek Krejcia3045382018-11-22 14:30:31 +0100641 break;
642 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200643 lyxp_expr_free(ctx, ((struct lysc_type_leafref *)type)->path);
Radek Krejcia3045382018-11-22 14:30:31 +0100644 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100645 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100646 case LY_TYPE_BOOL:
647 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100648 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100649 /* nothing to do */
650 break;
651 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200652
653 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100654 free(type);
655}
656
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100657void
Radek Krejcif538ce52019-03-05 10:46:14 +0100658lysc_action_inout_free(struct ly_ctx *ctx, struct lysc_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100659{
660 struct lysc_node *child, *child_next;
661
Radek Krejcif538ce52019-03-05 10:46:14 +0100662 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
663 LY_LIST_FOR_SAFE(inout->data, child_next, child) {
664 lysc_node_free(ctx, child);
665 }
666}
667
668void
669lysc_action_free(struct ly_ctx *ctx, struct lysc_action *action)
670{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100671 FREE_STRING(ctx, action->name);
672 FREE_STRING(ctx, action->dsc);
673 FREE_STRING(ctx, action->ref);
674 FREE_ARRAY(ctx, action->iffeatures, lysc_iffeature_free);
675 FREE_ARRAY(ctx, action->exts, lysc_ext_instance_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200676 FREE_ARRAY(ctx, action->input_exts, lysc_ext_instance_free);
Radek Krejcif538ce52019-03-05 10:46:14 +0100677 lysc_action_inout_free(ctx, &action->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200678 FREE_ARRAY(ctx, action->output_exts, lysc_ext_instance_free);
Radek Krejcif538ce52019-03-05 10:46:14 +0100679 lysc_action_inout_free(ctx, &action->output);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100680}
681
Radek Krejcifc11bd72019-04-11 16:00:05 +0200682void
683lysc_notif_free(struct ly_ctx *ctx, struct lysc_notif *notif)
684{
685 struct lysc_node *child, *child_next;
686
687 FREE_STRING(ctx, notif->name);
688 FREE_STRING(ctx, notif->dsc);
689 FREE_STRING(ctx, notif->ref);
690 FREE_ARRAY(ctx, notif->iffeatures, lysc_iffeature_free);
691 FREE_ARRAY(ctx, notif->exts, lysc_ext_instance_free);
692 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
693 LY_LIST_FOR_SAFE(notif->data, child_next, child) {
694 lysc_node_free(ctx, child);
695 }
696}
697
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200698void
Radek Krejci19a96102018-11-15 13:38:09 +0100699lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
700{
701 struct lysc_node *child, *child_next;
702
703 LY_LIST_FOR_SAFE(node->child, child_next, child) {
704 lysc_node_free(ctx, child);
705 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100706 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100707 FREE_ARRAY(ctx, node->actions, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200708 FREE_ARRAY(ctx, node->notifs, lysc_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100709}
710
711static void
712lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
713{
Radek Krejci58d171e2018-11-23 13:50:55 +0100714 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100715 if (node->type) {
716 lysc_type_free(ctx, node->type);
717 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100718 FREE_STRING(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200719 if (node->dflt) {
720 node->dflt->realtype->plugin->free(ctx, node->dflt);
721 lysc_type_free(ctx, node->dflt->realtype);
722 free(node->dflt);
723 }
Radek Krejci19a96102018-11-15 13:38:09 +0100724}
725
Radek Krejci42452ac2018-11-28 17:09:52 +0100726static void
727lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
728{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200729 LY_ARRAY_COUNT_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100730
Radek Krejci42452ac2018-11-28 17:09:52 +0100731 FREE_ARRAY(ctx, node->musts, lysc_must_free);
732 if (node->type) {
733 lysc_type_free(ctx, node->type);
734 }
735 FREE_STRING(ctx, node->units);
736 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200737 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
738 lysc_type_free(ctx, node->dflts[u]->realtype);
739 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100740 }
741 LY_ARRAY_FREE(node->dflts);
742}
743
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100744static void
745lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
746{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200747 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100748 struct lysc_node *child, *child_next;
749
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100750 LY_LIST_FOR_SAFE(node->child, child_next, child) {
751 lysc_node_free(ctx, child);
752 }
753 FREE_ARRAY(ctx, node->musts, lysc_must_free);
754
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100755 LY_ARRAY_FOR(node->uniques, u) {
756 LY_ARRAY_FREE(node->uniques[u]);
757 }
758 LY_ARRAY_FREE(node->uniques);
759
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100760 FREE_ARRAY(ctx, node->actions, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200761 FREE_ARRAY(ctx, node->notifs, lysc_notif_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100762}
763
Radek Krejci056d0a82018-12-06 16:57:25 +0100764static void
765lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
766{
767 struct lysc_node *child, *child_next;
768
Michal Vasko20424b42020-08-31 12:29:38 +0200769 LY_LIST_FOR_SAFE((struct lysc_node *)node->cases, child_next, child) {
770 lysc_node_free(ctx, child);
771 }
772}
773
774static void
775lysc_node_case_free(struct ly_ctx *ctx, struct lysc_node_case *node)
776{
777 struct lysc_node *child, *child_next;
778
779 LY_LIST_FOR_SAFE(node->child, child_next, child) {
780 lysc_node_free(ctx, child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100781 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100782}
Radek Krejci056d0a82018-12-06 16:57:25 +0100783
Radek Krejci9800fb82018-12-13 14:26:23 +0100784static void
785lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
786{
787 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100788}
789
Radek Krejci19a96102018-11-15 13:38:09 +0100790void
791lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node)
792{
793 /* common part */
794 FREE_STRING(ctx, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +0100795 FREE_STRING(ctx, node->dsc);
796 FREE_STRING(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100797
798 /* nodetype-specific part */
Michal Vaskod989ba02020-08-24 10:59:24 +0200799 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100800 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200801 lysc_node_container_free(ctx, (struct lysc_node_container *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100802 break;
803 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200804 lysc_node_leaf_free(ctx, (struct lysc_node_leaf *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100805 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100806 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200807 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist *)node);
Radek Krejci42452ac2018-11-28 17:09:52 +0100808 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100809 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200810 lysc_node_list_free(ctx, (struct lysc_node_list *)node);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100811 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100812 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200813 lysc_node_choice_free(ctx, (struct lysc_node_choice *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100814 break;
815 case LYS_CASE:
Michal Vasko20424b42020-08-31 12:29:38 +0200816 lysc_node_case_free(ctx, (struct lysc_node_case *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100817 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100818 case LYS_ANYDATA:
819 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200820 lysc_node_anydata_free(ctx, (struct lysc_node_anydata *)node);
Radek Krejci9800fb82018-12-13 14:26:23 +0100821 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100822 default:
823 LOGINT(ctx);
824 }
825
Radek Krejci00b874b2019-02-12 10:54:50 +0100826 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100827 FREE_ARRAY(ctx, node->iffeatures, lysc_iffeature_free);
828 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100829 free(node);
830}
831
832static void
833lysc_module_free_(struct lysc_module *module)
834{
835 struct ly_ctx *ctx;
836 struct lysc_node *node, *node_next;
837
Michal Vaskod989ba02020-08-24 10:59:24 +0200838 LY_CHECK_ARG_RET(NULL, module, );
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100839 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100840
Radek Krejci19a96102018-11-15 13:38:09 +0100841 LY_LIST_FOR_SAFE(module->data, node_next, node) {
842 lysc_node_free(ctx, node);
843 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100844 FREE_ARRAY(ctx, module->rpcs, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200845 FREE_ARRAY(ctx, module->notifs, lysc_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100846 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
847
848 free(module);
849}
850
851void
852lysc_module_free(struct lysc_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
853{
Radek Krejci9b042892019-02-13 14:28:44 +0100854 /* TODO use the destructor, this just suppress warning about unused parameter */
855 (void) private_destructor;
856
Radek Krejci19a96102018-11-15 13:38:09 +0100857 if (module) {
858 lysc_module_free_(module);
859 }
860}
861
862void
863lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
864{
865 if (!module) {
866 return;
867 }
868
869 lysc_module_free(module->compiled, private_destructor);
Radek Krejci14915cc2020-09-14 17:28:13 +0200870 FREE_ARRAY(module->ctx, module->features, lysc_feature_free);
Radek Krejci80d281e2020-09-14 17:42:54 +0200871 FREE_ARRAY(module->ctx, module->identities, lysc_ident_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100872 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100873
Michal Vasko7f45cf22020-10-01 12:49:44 +0200874 LY_ARRAY_FREE(module->augmented_by);
875 LY_ARRAY_FREE(module->deviated_by);
876
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100877 FREE_STRING(module->ctx, module->name);
Radek Krejci0af46292019-01-11 16:02:31 +0100878 FREE_STRING(module->ctx, module->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100879 FREE_STRING(module->ctx, module->ns);
880 FREE_STRING(module->ctx, module->prefix);
881 FREE_STRING(module->ctx, module->filepath);
882 FREE_STRING(module->ctx, module->org);
883 FREE_STRING(module->ctx, module->contact);
884 FREE_STRING(module->ctx, module->dsc);
885 FREE_STRING(module->ctx, module->ref);
886
Radek Krejci19a96102018-11-15 13:38:09 +0100887 free(module);
888}
Michal Vasko33ff9422020-07-03 09:50:39 +0200889
Radek Krejci38d85362019-09-05 16:26:38 +0200890API void
891lysc_extension_instance_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
892{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200893 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
Radek Krejci38d85362019-09-05 16:26:38 +0200894 if (!substmts[u].storage) {
895 continue;
896 }
897
Michal Vaskod989ba02020-08-24 10:59:24 +0200898 switch (substmts[u].stmt) {
Radek Krejci38d85362019-09-05 16:26:38 +0200899 case LY_STMT_TYPE:
900 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
901 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +0200902 struct lysc_type *type = *((struct lysc_type **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200903 if (!type) {
904 break;
905 }
906 lysc_type_free(ctx, type);
907 } else {
908 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +0200909 struct lysc_type **types = *((struct lysc_type ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200910 if (!types) {
911 break;
912 }
913 FREE_ARRAY(ctx, types, lysc_type2_free);
914 }
915 break;
916 case LY_STMT_UNITS:
917 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
918 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +0200919 const char *str = *((const char **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200920 if (!str) {
921 break;
922 }
923 FREE_STRING(ctx, str);
924 } else {
925 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +0200926 const char **strs = *((const char ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200927 if (!strs) {
928 break;
929 }
930 FREE_STRINGS(ctx, strs);
931 }
932 break;
933 case LY_STMT_STATUS:
Radek Krejciad5963b2019-09-06 16:03:05 +0200934 case LY_STMT_CONFIG:
Radek Krejci38d85362019-09-05 16:26:38 +0200935 /* nothing to do */
936 break;
937 case LY_STMT_IF_FEATURE: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200938 struct lysc_iffeature *iff = *((struct lysc_iffeature **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200939 if (!iff) {
940 break;
941 }
942 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
943 /* single item */
944 lysc_iffeature_free(ctx, iff);
945 free(iff);
946 } else {
947 /* multiple items */
948 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
949 }
950 break;
951 }
952
Radek Krejci0f969882020-08-21 16:56:47 +0200953 /* TODO other statements */
Radek Krejci38d85362019-09-05 16:26:38 +0200954 default:
955 LOGINT(ctx);
956 }
957 }
958}
David Sedlákebd3acf2019-07-26 15:04:32 +0200959
960void
Michal Vaskob36053d2020-03-26 15:49:30 +0100961yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +0200962{
963 if (ctx) {
964 free(ctx);
965 }
966}
967
968void
Michal Vaskob36053d2020-03-26 15:49:30 +0100969yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +0200970{
971 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100972 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +0200973 free(ctx);
974 }
975}