blob: 2dadee551337a12f0054cab4146d65745990deac [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);
107 FREE_STRINGS(ctx, feat->iffeatures);
108 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);
117 FREE_STRINGS(ctx, ident->iffeatures);
118 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
124static void
125lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
126{
127 FREE_STRING(ctx, restr->arg);
128 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);
141 FREE_STRINGS(ctx, item->iffeatures);
142 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);
170 FREE_STRING(ctx, tpdf->dflt);
171 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);
200 FREE_STRINGS(ctx, action->iffeatures);
201 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);
216 FREE_STRINGS(ctx, notif->iffeatures);
217 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);
262 FREE_STRINGS(ctx, augment->iffeatures);
263 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
Radek Krejci19a96102018-11-15 13:38:09 +0100272lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
273{
Michal Vasko22df3f02020-08-24 13:29:22 +0200274 struct lysp_deviate_add *add = (struct lysp_deviate_add *)d;
275 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl *)d;
Radek Krejci19a96102018-11-15 13:38:09 +0100276
277 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
Michal Vaskod989ba02020-08-24 10:59:24 +0200278 switch (d->mod) {
Radek Krejci19a96102018-11-15 13:38:09 +0100279 case LYS_DEV_NOT_SUPPORTED:
280 /* nothing to do */
281 break;
282 case LYS_DEV_ADD:
283 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
284 FREE_STRING(ctx, add->units);
285 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
286 FREE_STRINGS(ctx, add->uniques);
287 FREE_STRINGS(ctx, add->dflts);
288 break;
289 case LYS_DEV_REPLACE:
290 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
291 FREE_STRING(ctx, rpl->units);
292 FREE_STRING(ctx, rpl->dflt);
293 break;
294 default:
295 LOGINT(ctx);
296 break;
297 }
298}
299
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200300void
Radek Krejci19a96102018-11-15 13:38:09 +0100301lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
302{
303 struct lysp_deviate *next, *iter;
304
305 FREE_STRING(ctx, dev->nodeid);
306 FREE_STRING(ctx, dev->dsc);
307 FREE_STRING(ctx, dev->ref);
308 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
309 lysp_deviate_free(ctx, iter);
310 free(iter);
311 }
312 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
313}
314
David Sedlákd2d676a2019-07-22 11:28:19 +0200315void
Radek Krejci19a96102018-11-15 13:38:09 +0100316lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
317{
318 FREE_STRING(ctx, ref->nodeid);
319 FREE_STRING(ctx, ref->dsc);
320 FREE_STRING(ctx, ref->ref);
321 FREE_STRINGS(ctx, ref->iffeatures);
322 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
323 FREE_STRING(ctx, ref->presence);
324 FREE_STRINGS(ctx, ref->dflts);
325 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
326}
327
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200328void
Radek Krejci19a96102018-11-15 13:38:09 +0100329lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
330{
331 struct lysp_node *child, *next;
332
333 FREE_STRING(ctx, node->name);
334 FREE_STRING(ctx, node->dsc);
335 FREE_STRING(ctx, node->ref);
336 FREE_MEMBER(ctx, node->when, lysp_when_free);
337 FREE_STRINGS(ctx, node->iffeatures);
338 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
339
Michal Vaskod989ba02020-08-24 10:59:24 +0200340 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100341 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200342 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->musts, lysp_restr_free);
343 FREE_STRING(ctx, ((struct lysp_node_container *)node)->presence);
344 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->typedefs, lysp_tpdf_free);
345 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->groupings, lysp_grp_free);
346 LY_LIST_FOR_SAFE(((struct lysp_node_container *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100347 lysp_node_free(ctx, child);
348 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200349 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->actions, lysp_action_free);
350 FREE_ARRAY(ctx, ((struct lysp_node_container *)node)->notifs, lysp_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100351 break;
352 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200353 FREE_ARRAY(ctx, ((struct lysp_node_leaf *)node)->musts, lysp_restr_free);
354 lysp_type_free(ctx, &((struct lysp_node_leaf *)node)->type);
355 FREE_STRING(ctx, ((struct lysp_node_leaf *)node)->units);
356 FREE_STRING(ctx, ((struct lysp_node_leaf *)node)->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +0100357 break;
358 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200359 FREE_ARRAY(ctx, ((struct lysp_node_leaflist *)node)->musts, lysp_restr_free);
360 lysp_type_free(ctx, &((struct lysp_node_leaflist *)node)->type);
361 FREE_STRING(ctx, ((struct lysp_node_leaflist *)node)->units);
362 FREE_STRINGS(ctx, ((struct lysp_node_leaflist *)node)->dflts);
Radek Krejci19a96102018-11-15 13:38:09 +0100363 break;
364 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200365 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->musts, lysp_restr_free);
366 FREE_STRING(ctx, ((struct lysp_node_list *)node)->key);
367 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->typedefs, lysp_tpdf_free);
368 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->groupings, lysp_grp_free);
369 LY_LIST_FOR_SAFE(((struct lysp_node_list *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100370 lysp_node_free(ctx, child);
371 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200372 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->actions, lysp_action_free);
373 FREE_ARRAY(ctx, ((struct lysp_node_list *)node)->notifs, lysp_notif_free);
374 FREE_STRINGS(ctx, ((struct lysp_node_list *)node)->uniques);
Radek Krejci19a96102018-11-15 13:38:09 +0100375 break;
376 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200377 LY_LIST_FOR_SAFE(((struct lysp_node_choice *)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_STRING(ctx, ((struct lysp_node_choice *)node)->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +0100381 break;
382 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200383 LY_LIST_FOR_SAFE(((struct lysp_node_case *)node)->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100384 lysp_node_free(ctx, child);
385 }
386 break;
387 case LYS_ANYDATA:
388 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200389 FREE_ARRAY(ctx, ((struct lysp_node_anydata *)node)->musts, lysp_restr_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100390 break;
391 case LYS_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +0200392 FREE_ARRAY(ctx, ((struct lysp_node_uses *)node)->refines, lysp_refine_free);
393 FREE_ARRAY(ctx, ((struct lysp_node_uses *)node)->augments, lysp_augment_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100394 break;
395 default:
396 LOGINT(ctx);
397 }
398
399 free(node);
400}
401
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100402void
403lysp_submodule_free(struct ly_ctx *ctx, struct lysp_submodule *submod)
404{
405 struct lysp_node *node, *next;
406
Radek Krejci40544fa2019-01-11 09:38:37 +0100407 if (!submod) {
408 return;
409 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100410
411 FREE_ARRAY(ctx, submod->imports, lysp_import_free);
412 FREE_ARRAY(ctx, submod->includes, lysp_include_free);
413
414 FREE_ARRAY(ctx, submod->revs, lysp_revision_free);
415 FREE_ARRAY(ctx, submod->extensions, lysp_ext_free);
416 FREE_ARRAY(ctx, submod->features, lysp_feature_free);
417 FREE_ARRAY(ctx, submod->identities, lysp_ident_free);
418 FREE_ARRAY(ctx, submod->typedefs, lysp_tpdf_free);
419 FREE_ARRAY(ctx, submod->groupings, lysp_grp_free);
420 LY_LIST_FOR_SAFE(submod->data, next, node) {
421 lysp_node_free(ctx, node);
422 }
423 FREE_ARRAY(ctx, submod->augments, lysp_augment_free);
424 FREE_ARRAY(ctx, submod->rpcs, lysp_action_free);
425 FREE_ARRAY(ctx, submod->notifs, lysp_notif_free);
426 FREE_ARRAY(ctx, submod->deviations, lysp_deviation_free);
427 FREE_ARRAY(ctx, submod->exts, lysp_ext_instance_free);
428
429 FREE_STRING(ctx, submod->belongsto);
430 FREE_STRING(ctx, submod->name);
431 FREE_STRING(ctx, submod->filepath);
432 FREE_STRING(ctx, submod->prefix);
433 FREE_STRING(ctx, submod->org);
434 FREE_STRING(ctx, submod->contact);
435 FREE_STRING(ctx, submod->dsc);
436 FREE_STRING(ctx, submod->ref);
437
438 free(submod);
439}
440
Radek Krejci19a96102018-11-15 13:38:09 +0100441API void
442lysp_module_free(struct lysp_module *module)
443{
444 struct ly_ctx *ctx;
445 struct lysp_node *node, *next;
446
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100447 if (!module) {
448 return;
449 }
450 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100451
452 FREE_ARRAY(ctx, module->imports, lysp_import_free);
453 FREE_ARRAY(ctx, module->includes, lysp_include_free);
454
Radek Krejci19a96102018-11-15 13:38:09 +0100455 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
456 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
457 FREE_ARRAY(ctx, module->features, lysp_feature_free);
458 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
459 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
460 FREE_ARRAY(ctx, module->groupings, lysp_grp_free);
461 LY_LIST_FOR_SAFE(module->data, next, node) {
462 lysp_node_free(ctx, node);
463 }
464 FREE_ARRAY(ctx, module->augments, lysp_augment_free);
465 FREE_ARRAY(ctx, module->rpcs, lysp_action_free);
466 FREE_ARRAY(ctx, module->notifs, lysp_notif_free);
467 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
468 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
469
470 free(module);
471}
472
Radek Krejci0af46292019-01-11 16:02:31 +0100473void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100474lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
475{
476 if (--(*ext)->refcount) {
477 return;
478 }
479 FREE_STRING(ctx, (*ext)->name);
480 FREE_STRING(ctx, (*ext)->argument);
481 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
482 free(*ext);
483}
484
485void
Radek Krejci19a96102018-11-15 13:38:09 +0100486lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
487{
fredganebc50572019-10-31 15:39:23 +0800488 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200489 ext->def->plugin->free(ctx, ext);
490 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100491 if (ext->def) {
492 lysc_extension_free(ctx, &ext->def);
493 }
Radek Krejci0935f412019-08-20 16:15:18 +0200494 FREE_STRING(ctx, ext->argument);
495 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
496}
497
498void
Radek Krejci19a96102018-11-15 13:38:09 +0100499lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
500{
501 LY_ARRAY_FREE(iff->features);
502 free(iff->expr);
503}
504
505static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100506lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100507{
Radek Krejci00b874b2019-02-12 10:54:50 +0100508 if (--(*w)->refcount) {
509 return;
510 }
511 lyxp_expr_free(ctx, (*w)->cond);
512 FREE_STRING(ctx, (*w)->dsc);
513 FREE_STRING(ctx, (*w)->ref);
514 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
515 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100516}
517
Radek Krejciccd20f12019-02-15 14:12:27 +0100518void
Radek Krejci58d171e2018-11-23 13:50:55 +0100519lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
520{
521 lyxp_expr_free(ctx, must->cond);
522 FREE_STRING(ctx, must->emsg);
523 FREE_STRING(ctx, must->eapptag);
Radek Krejcic8b31002019-01-08 10:24:45 +0100524 FREE_STRING(ctx, must->dsc);
525 FREE_STRING(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100526 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
527}
528
529static void
Radek Krejci19a96102018-11-15 13:38:09 +0100530lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
531{
532 FREE_STRING(ctx, ident->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100533 FREE_STRING(ctx, ident->dsc);
534 FREE_STRING(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100535 FREE_ARRAY(ctx, ident->iffeatures, lysc_iffeature_free);
536 LY_ARRAY_FREE(ident->derived);
537 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
538}
539
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200540void
Radek Krejci19a96102018-11-15 13:38:09 +0100541lysc_feature_free(struct ly_ctx *ctx, struct lysc_feature *feat)
542{
543 FREE_STRING(ctx, feat->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100544 FREE_STRING(ctx, feat->dsc);
545 FREE_STRING(ctx, feat->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100546 FREE_ARRAY(ctx, feat->iffeatures, lysc_iffeature_free);
547 LY_ARRAY_FREE(feat->depfeatures);
548 FREE_ARRAY(ctx, feat->exts, lysc_ext_instance_free);
549}
550
551static void
552lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
553{
554 LY_ARRAY_FREE(range->parts);
555 FREE_STRING(ctx, range->eapptag);
556 FREE_STRING(ctx, range->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100557 FREE_STRING(ctx, range->dsc);
558 FREE_STRING(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100559 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
560}
561
562static void
563lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
564{
565 if (--(*pattern)->refcount) {
566 return;
567 }
Radek Krejci54579462019-04-30 12:47:06 +0200568 pcre2_code_free((*pattern)->code);
569 FREE_STRING(ctx, (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +0100570 FREE_STRING(ctx, (*pattern)->eapptag);
571 FREE_STRING(ctx, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100572 FREE_STRING(ctx, (*pattern)->dsc);
573 FREE_STRING(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100574 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
575 free(*pattern);
576}
577
578static void
Radek Krejci693262f2019-04-29 15:23:20 +0200579lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100580{
581 FREE_STRING(ctx, item->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100582 FREE_STRING(ctx, item->dsc);
583 FREE_STRING(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100584 FREE_ARRAY(ctx, item->iffeatures, lysc_iffeature_free);
585 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
586}
587
Radek Krejcia3045382018-11-22 14:30:31 +0100588static void
589lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
590{
591 lysc_type_free(ctx, *type);
592}
Radek Krejci0f969882020-08-21 16:56:47 +0200593
Radek Krejcicdfecd92018-11-26 11:27:32 +0100594void
Radek Krejci19a96102018-11-15 13:38:09 +0100595lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
596{
597 if (--type->refcount) {
598 return;
599 }
Radek Krejcib915ac92020-08-14 23:31:04 +0200600
Michal Vaskod989ba02020-08-24 10:59:24 +0200601 switch (type->basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100602 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +0200603 FREE_MEMBER(ctx, ((struct lysc_type_bin *)type)->length, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100604 break;
605 case LY_TYPE_BITS:
Michal Vasko22df3f02020-08-24 13:29:22 +0200606 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 +0100607 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100608 case LY_TYPE_DEC64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200609 FREE_MEMBER(ctx, ((struct lysc_type_dec *)type)->range, lysc_range_free);
Radek Krejci6cba4292018-11-15 17:33:29 +0100610 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100611 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +0200612 FREE_MEMBER(ctx, ((struct lysc_type_str *)type)->length, lysc_range_free);
613 FREE_ARRAY(ctx, ((struct lysc_type_str *)type)->patterns, lysc_pattern_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100614 break;
615 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +0200616 FREE_ARRAY(ctx, ((struct lysc_type_enum *)type)->enums, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100617 break;
618 case LY_TYPE_INT8:
619 case LY_TYPE_UINT8:
620 case LY_TYPE_INT16:
621 case LY_TYPE_UINT16:
622 case LY_TYPE_INT32:
623 case LY_TYPE_UINT32:
624 case LY_TYPE_INT64:
625 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200626 FREE_MEMBER(ctx, ((struct lysc_type_num *)type)->range, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100627 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100628 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +0200629 LY_ARRAY_FREE(((struct lysc_type_identityref *)type)->bases);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100630 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100631 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +0200632 FREE_ARRAY(ctx, ((struct lysc_type_union *)type)->types, lysc_type2_free);
Radek Krejcia3045382018-11-22 14:30:31 +0100633 break;
634 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200635 lyxp_expr_free(ctx, ((struct lysc_type_leafref *)type)->path);
Radek Krejcia3045382018-11-22 14:30:31 +0100636 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100637 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100638 case LY_TYPE_BOOL:
639 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100640 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100641 /* nothing to do */
642 break;
643 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200644
645 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100646 free(type);
647}
648
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100649void
Radek Krejcif538ce52019-03-05 10:46:14 +0100650lysc_action_inout_free(struct ly_ctx *ctx, struct lysc_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100651{
652 struct lysc_node *child, *child_next;
653
Radek Krejcif538ce52019-03-05 10:46:14 +0100654 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
655 LY_LIST_FOR_SAFE(inout->data, child_next, child) {
656 lysc_node_free(ctx, child);
657 }
658}
659
660void
661lysc_action_free(struct ly_ctx *ctx, struct lysc_action *action)
662{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100663 FREE_STRING(ctx, action->name);
664 FREE_STRING(ctx, action->dsc);
665 FREE_STRING(ctx, action->ref);
666 FREE_ARRAY(ctx, action->iffeatures, lysc_iffeature_free);
667 FREE_ARRAY(ctx, action->exts, lysc_ext_instance_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200668 FREE_ARRAY(ctx, action->input_exts, lysc_ext_instance_free);
Radek Krejcif538ce52019-03-05 10:46:14 +0100669 lysc_action_inout_free(ctx, &action->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200670 FREE_ARRAY(ctx, action->output_exts, lysc_ext_instance_free);
Radek Krejcif538ce52019-03-05 10:46:14 +0100671 lysc_action_inout_free(ctx, &action->output);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100672}
673
Radek Krejcifc11bd72019-04-11 16:00:05 +0200674void
675lysc_notif_free(struct ly_ctx *ctx, struct lysc_notif *notif)
676{
677 struct lysc_node *child, *child_next;
678
679 FREE_STRING(ctx, notif->name);
680 FREE_STRING(ctx, notif->dsc);
681 FREE_STRING(ctx, notif->ref);
682 FREE_ARRAY(ctx, notif->iffeatures, lysc_iffeature_free);
683 FREE_ARRAY(ctx, notif->exts, lysc_ext_instance_free);
684 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
685 LY_LIST_FOR_SAFE(notif->data, child_next, child) {
686 lysc_node_free(ctx, child);
687 }
688}
689
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200690void
Radek Krejci19a96102018-11-15 13:38:09 +0100691lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
692{
693 struct lysc_node *child, *child_next;
694
695 LY_LIST_FOR_SAFE(node->child, child_next, child) {
696 lysc_node_free(ctx, child);
697 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100698 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100699 FREE_ARRAY(ctx, node->actions, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200700 FREE_ARRAY(ctx, node->notifs, lysc_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100701}
702
703static void
704lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
705{
Radek Krejci58d171e2018-11-23 13:50:55 +0100706 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100707 if (node->type) {
708 lysc_type_free(ctx, node->type);
709 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100710 FREE_STRING(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200711 if (node->dflt) {
712 node->dflt->realtype->plugin->free(ctx, node->dflt);
713 lysc_type_free(ctx, node->dflt->realtype);
714 free(node->dflt);
715 }
Radek Krejci19a96102018-11-15 13:38:09 +0100716}
717
Radek Krejci42452ac2018-11-28 17:09:52 +0100718static void
719lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
720{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200721 LY_ARRAY_COUNT_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100722
Radek Krejci42452ac2018-11-28 17:09:52 +0100723 FREE_ARRAY(ctx, node->musts, lysc_must_free);
724 if (node->type) {
725 lysc_type_free(ctx, node->type);
726 }
727 FREE_STRING(ctx, node->units);
728 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200729 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
730 lysc_type_free(ctx, node->dflts[u]->realtype);
731 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100732 }
733 LY_ARRAY_FREE(node->dflts);
734}
735
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100736static void
737lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
738{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200739 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100740 struct lysc_node *child, *child_next;
741
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100742 LY_LIST_FOR_SAFE(node->child, child_next, child) {
743 lysc_node_free(ctx, child);
744 }
745 FREE_ARRAY(ctx, node->musts, lysc_must_free);
746
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100747 LY_ARRAY_FOR(node->uniques, u) {
748 LY_ARRAY_FREE(node->uniques[u]);
749 }
750 LY_ARRAY_FREE(node->uniques);
751
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100752 FREE_ARRAY(ctx, node->actions, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200753 FREE_ARRAY(ctx, node->notifs, lysc_notif_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100754}
755
Radek Krejci056d0a82018-12-06 16:57:25 +0100756static void
757lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
758{
759 struct lysc_node *child, *child_next;
760
Michal Vasko20424b42020-08-31 12:29:38 +0200761 LY_LIST_FOR_SAFE((struct lysc_node *)node->cases, child_next, child) {
762 lysc_node_free(ctx, child);
763 }
764}
765
766static void
767lysc_node_case_free(struct ly_ctx *ctx, struct lysc_node_case *node)
768{
769 struct lysc_node *child, *child_next;
770
771 LY_LIST_FOR_SAFE(node->child, child_next, child) {
772 lysc_node_free(ctx, child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100773 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100774}
Radek Krejci056d0a82018-12-06 16:57:25 +0100775
Radek Krejci9800fb82018-12-13 14:26:23 +0100776static void
777lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
778{
779 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100780}
781
Radek Krejci19a96102018-11-15 13:38:09 +0100782void
783lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node)
784{
785 /* common part */
786 FREE_STRING(ctx, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +0100787 FREE_STRING(ctx, node->dsc);
788 FREE_STRING(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100789
790 /* nodetype-specific part */
Michal Vaskod989ba02020-08-24 10:59:24 +0200791 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100792 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200793 lysc_node_container_free(ctx, (struct lysc_node_container *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100794 break;
795 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200796 lysc_node_leaf_free(ctx, (struct lysc_node_leaf *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100797 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100798 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200799 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist *)node);
Radek Krejci42452ac2018-11-28 17:09:52 +0100800 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100801 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200802 lysc_node_list_free(ctx, (struct lysc_node_list *)node);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100803 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100804 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200805 lysc_node_choice_free(ctx, (struct lysc_node_choice *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100806 break;
807 case LYS_CASE:
Michal Vasko20424b42020-08-31 12:29:38 +0200808 lysc_node_case_free(ctx, (struct lysc_node_case *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100809 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100810 case LYS_ANYDATA:
811 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200812 lysc_node_anydata_free(ctx, (struct lysc_node_anydata *)node);
Radek Krejci9800fb82018-12-13 14:26:23 +0100813 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100814 default:
815 LOGINT(ctx);
816 }
817
Radek Krejci00b874b2019-02-12 10:54:50 +0100818 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100819 FREE_ARRAY(ctx, node->iffeatures, lysc_iffeature_free);
820 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100821 free(node);
822}
823
824static void
825lysc_module_free_(struct lysc_module *module)
826{
827 struct ly_ctx *ctx;
828 struct lysc_node *node, *node_next;
829
Michal Vaskod989ba02020-08-24 10:59:24 +0200830 LY_CHECK_ARG_RET(NULL, module, );
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100831 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100832
Radek Krejci19a96102018-11-15 13:38:09 +0100833 FREE_ARRAY(ctx, module->identities, lysc_ident_free);
834
835 LY_LIST_FOR_SAFE(module->data, node_next, node) {
836 lysc_node_free(ctx, node);
837 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100838 FREE_ARRAY(ctx, module->rpcs, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200839 FREE_ARRAY(ctx, module->notifs, lysc_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100840 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
Michal Vasko57c10cd2020-05-27 15:57:11 +0200841 LY_ARRAY_FREE(module->deviated_by);
Michal Vaskoa3af5982020-06-29 11:51:37 +0200842 LY_ARRAY_FREE(module->augmented_by);
Radek Krejci19a96102018-11-15 13:38:09 +0100843
844 free(module);
845}
846
847void
848lysc_module_free(struct lysc_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
849{
Radek Krejci9b042892019-02-13 14:28:44 +0100850 /* TODO use the destructor, this just suppress warning about unused parameter */
851 (void) private_destructor;
852
Radek Krejci19a96102018-11-15 13:38:09 +0100853 if (module) {
854 lysc_module_free_(module);
855 }
856}
857
858void
859lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
860{
861 if (!module) {
862 return;
863 }
864
865 lysc_module_free(module->compiled, private_destructor);
Radek Krejci14915cc2020-09-14 17:28:13 +0200866 FREE_ARRAY(module->ctx, module->features, lysc_feature_free);
Michal Vasko33ff9422020-07-03 09:50:39 +0200867 FREE_ARRAY(module->ctx, module->dis_identities, lysc_ident_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100868 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100869
870 FREE_STRING(module->ctx, module->name);
Radek Krejci0af46292019-01-11 16:02:31 +0100871 FREE_STRING(module->ctx, module->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100872 FREE_STRING(module->ctx, module->ns);
873 FREE_STRING(module->ctx, module->prefix);
874 FREE_STRING(module->ctx, module->filepath);
875 FREE_STRING(module->ctx, module->org);
876 FREE_STRING(module->ctx, module->contact);
877 FREE_STRING(module->ctx, module->dsc);
878 FREE_STRING(module->ctx, module->ref);
879
Radek Krejci19a96102018-11-15 13:38:09 +0100880 free(module);
881}
Michal Vasko33ff9422020-07-03 09:50:39 +0200882
Radek Krejci38d85362019-09-05 16:26:38 +0200883API void
884lysc_extension_instance_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
885{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200886 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
Radek Krejci38d85362019-09-05 16:26:38 +0200887 if (!substmts[u].storage) {
888 continue;
889 }
890
Michal Vaskod989ba02020-08-24 10:59:24 +0200891 switch (substmts[u].stmt) {
Radek Krejci38d85362019-09-05 16:26:38 +0200892 case LY_STMT_TYPE:
893 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
894 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +0200895 struct lysc_type *type = *((struct lysc_type **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200896 if (!type) {
897 break;
898 }
899 lysc_type_free(ctx, type);
900 } else {
901 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +0200902 struct lysc_type **types = *((struct lysc_type ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200903 if (!types) {
904 break;
905 }
906 FREE_ARRAY(ctx, types, lysc_type2_free);
907 }
908 break;
909 case LY_STMT_UNITS:
910 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
911 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +0200912 const char *str = *((const char **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200913 if (!str) {
914 break;
915 }
916 FREE_STRING(ctx, str);
917 } else {
918 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +0200919 const char **strs = *((const char ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200920 if (!strs) {
921 break;
922 }
923 FREE_STRINGS(ctx, strs);
924 }
925 break;
926 case LY_STMT_STATUS:
Radek Krejciad5963b2019-09-06 16:03:05 +0200927 case LY_STMT_CONFIG:
Radek Krejci38d85362019-09-05 16:26:38 +0200928 /* nothing to do */
929 break;
930 case LY_STMT_IF_FEATURE: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200931 struct lysc_iffeature *iff = *((struct lysc_iffeature **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +0200932 if (!iff) {
933 break;
934 }
935 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
936 /* single item */
937 lysc_iffeature_free(ctx, iff);
938 free(iff);
939 } else {
940 /* multiple items */
941 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
942 }
943 break;
944 }
945
Radek Krejci0f969882020-08-21 16:56:47 +0200946 /* TODO other statements */
Radek Krejci38d85362019-09-05 16:26:38 +0200947 default:
948 LOGINT(ctx);
949 }
950 }
951}
David Sedlákebd3acf2019-07-26 15:04:32 +0200952
953void
Michal Vaskob36053d2020-03-26 15:49:30 +0100954yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +0200955{
956 if (ctx) {
957 free(ctx);
958 }
959}
960
961void
Michal Vaskob36053d2020-03-26 15:49:30 +0100962yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +0200963{
964 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100965 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +0200966 free(ctx);
967 }
968}