blob: 60d91e83b28f8687d2b452b674f69bf55685876d [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"
18#include "config.h"
Michal Vasko004d3152020-06-11 19:59:22 +020019#include "path.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);
30void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
Michal Vasko5fe75f12020-03-02 13:52:37 +010031void lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext);
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) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +010076 lysp_submodule_free(ctx, 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);
109 FREE_STRINGS(ctx, feat->iffeatures);
110 FREE_STRING(ctx, feat->dsc);
111 FREE_STRING(ctx, feat->ref);
112 FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
113}
114
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200115void
Radek Krejci19a96102018-11-15 13:38:09 +0100116lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident)
117{
118 FREE_STRING(ctx, ident->name);
119 FREE_STRINGS(ctx, ident->iffeatures);
120 FREE_STRINGS(ctx, ident->bases);
121 FREE_STRING(ctx, ident->dsc);
122 FREE_STRING(ctx, ident->ref);
123 FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
124}
125
126static void
127lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
128{
129 FREE_STRING(ctx, restr->arg);
130 FREE_STRING(ctx, restr->emsg);
131 FREE_STRING(ctx, restr->eapptag);
132 FREE_STRING(ctx, restr->dsc);
133 FREE_STRING(ctx, restr->ref);
134 FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
135}
136
137static void
138lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item)
139{
140 FREE_STRING(ctx, item->name);
141 FREE_STRING(ctx, item->dsc);
142 FREE_STRING(ctx, item->ref);
143 FREE_STRINGS(ctx, item->iffeatures);
144 FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
145}
146
Radek Krejcicdfecd92018-11-26 11:27:32 +0100147void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type);
Michal Vasko004d3152020-06-11 19:59:22 +0200148
David Sedlák32488102019-07-15 17:44:10 +0200149void
Radek Krejci19a96102018-11-15 13:38:09 +0100150lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type)
151{
152 FREE_STRING(ctx, type->name);
153 FREE_MEMBER(ctx, type->range, lysp_restr_free);
154 FREE_MEMBER(ctx, type->length, lysp_restr_free);
155 FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
156 FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
157 FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
Michal Vasko004d3152020-06-11 19:59:22 +0200158 lyxp_expr_free(ctx, type->path);
Radek Krejci19a96102018-11-15 13:38:09 +0100159 FREE_STRINGS(ctx, type->bases);
160 FREE_ARRAY(ctx, type->types, lysp_type_free);
161 FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
162 if (type->compiled) {
163 lysc_type_free(ctx, type->compiled);
164 }
165}
166
David Sedlák04e17b22019-07-19 15:29:48 +0200167void
Radek Krejci19a96102018-11-15 13:38:09 +0100168lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
169{
170 FREE_STRING(ctx, tpdf->name);
171 FREE_STRING(ctx, tpdf->units);
172 FREE_STRING(ctx, tpdf->dflt);
173 FREE_STRING(ctx, tpdf->dsc);
174 FREE_STRING(ctx, tpdf->ref);
175 FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
176
177 lysp_type_free(ctx, &tpdf->type);
178
179}
180
Radek Krejcif538ce52019-03-05 10:46:14 +0100181void
Radek Krejci19a96102018-11-15 13:38:09 +0100182lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout)
183{
184 struct lysp_node *node, *next;
185
186 FREE_ARRAY(ctx, inout->musts, lysp_restr_free);
187 FREE_ARRAY(ctx, inout->typedefs, lysp_tpdf_free);
188 FREE_ARRAY(ctx, inout->groupings, lysp_grp_free);
189 LY_LIST_FOR_SAFE(inout->data, next, node) {
190 lysp_node_free(ctx, node);
191 }
192 FREE_ARRAY(ctx, inout->exts, lysp_ext_instance_free);
193
194}
195
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200196void
Radek Krejci19a96102018-11-15 13:38:09 +0100197lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action)
198{
199 FREE_STRING(ctx, action->name);
200 FREE_STRING(ctx, action->dsc);
201 FREE_STRING(ctx, action->ref);
202 FREE_STRINGS(ctx, action->iffeatures);
203 FREE_ARRAY(ctx, action->typedefs, lysp_tpdf_free);
204 FREE_ARRAY(ctx, action->groupings, lysp_grp_free);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100205 lysp_action_inout_free(ctx, &action->input);
206 lysp_action_inout_free(ctx, &action->output);
Radek Krejci19a96102018-11-15 13:38:09 +0100207 FREE_ARRAY(ctx, action->exts, lysp_ext_instance_free);
208}
209
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200210void
Radek Krejci19a96102018-11-15 13:38:09 +0100211lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif)
212{
213 struct lysp_node *node, *next;
214
215 FREE_STRING(ctx, notif->name);
216 FREE_STRING(ctx, notif->dsc);
217 FREE_STRING(ctx, notif->ref);
218 FREE_STRINGS(ctx, notif->iffeatures);
219 FREE_ARRAY(ctx, notif->musts, lysp_restr_free);
220 FREE_ARRAY(ctx, notif->typedefs, lysp_tpdf_free);
221 FREE_ARRAY(ctx, notif->groupings, lysp_grp_free);
222 LY_LIST_FOR_SAFE(notif->data, next, node) {
223 lysp_node_free(ctx, node);
224 }
225 FREE_ARRAY(ctx, notif->exts, lysp_ext_instance_free);
226}
227
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200228void
Radek Krejci19a96102018-11-15 13:38:09 +0100229lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp)
230{
231 struct lysp_node *node, *next;
232
233 FREE_STRING(ctx, grp->name);
234 FREE_STRING(ctx, grp->dsc);
235 FREE_STRING(ctx, grp->ref);
236 FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
237 FREE_ARRAY(ctx, grp->groupings, lysp_grp_free);
238 LY_LIST_FOR_SAFE(grp->data, next, node) {
239 lysp_node_free(ctx, node);
240 }
241 FREE_ARRAY(ctx, grp->actions, lysp_action_free);
242 FREE_ARRAY(ctx, grp->notifs, lysp_notif_free);
243 FREE_ARRAY(ctx, grp->exts, lysp_ext_instance_free);
244}
245
Radek Krejcif09e4e82019-06-14 15:08:11 +0200246void
Radek Krejci19a96102018-11-15 13:38:09 +0100247lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
248{
249 FREE_STRING(ctx, when->cond);
250 FREE_STRING(ctx, when->dsc);
251 FREE_STRING(ctx, when->ref);
252 FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
253}
254
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200255void
Radek Krejci19a96102018-11-15 13:38:09 +0100256lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment)
257{
258 struct lysp_node *node, *next;
259
260 FREE_STRING(ctx, augment->nodeid);
261 FREE_STRING(ctx, augment->dsc);
262 FREE_STRING(ctx, augment->ref);
263 FREE_MEMBER(ctx, augment->when, lysp_when_free);
264 FREE_STRINGS(ctx, augment->iffeatures);
265 LY_LIST_FOR_SAFE(augment->child, next, node) {
266 lysp_node_free(ctx, node);
267 }
268 FREE_ARRAY(ctx, augment->actions, lysp_action_free);
269 FREE_ARRAY(ctx, augment->notifs, lysp_notif_free);
270 FREE_ARRAY(ctx, augment->exts, lysp_ext_instance_free);
271}
272
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200273void
Radek Krejci19a96102018-11-15 13:38:09 +0100274lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
275{
276 struct lysp_deviate_add *add = (struct lysp_deviate_add*)d;
277 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl*)d;
278
279 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
280 switch(d->mod) {
281 case LYS_DEV_NOT_SUPPORTED:
282 /* nothing to do */
283 break;
284 case LYS_DEV_ADD:
285 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
286 FREE_STRING(ctx, add->units);
287 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
288 FREE_STRINGS(ctx, add->uniques);
289 FREE_STRINGS(ctx, add->dflts);
290 break;
291 case LYS_DEV_REPLACE:
292 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
293 FREE_STRING(ctx, rpl->units);
294 FREE_STRING(ctx, rpl->dflt);
295 break;
296 default:
297 LOGINT(ctx);
298 break;
299 }
300}
301
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200302void
Radek Krejci19a96102018-11-15 13:38:09 +0100303lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
304{
305 struct lysp_deviate *next, *iter;
306
307 FREE_STRING(ctx, dev->nodeid);
308 FREE_STRING(ctx, dev->dsc);
309 FREE_STRING(ctx, dev->ref);
310 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
311 lysp_deviate_free(ctx, iter);
312 free(iter);
313 }
314 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
315}
316
David Sedlákd2d676a2019-07-22 11:28:19 +0200317void
Radek Krejci19a96102018-11-15 13:38:09 +0100318lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
319{
320 FREE_STRING(ctx, ref->nodeid);
321 FREE_STRING(ctx, ref->dsc);
322 FREE_STRING(ctx, ref->ref);
323 FREE_STRINGS(ctx, ref->iffeatures);
324 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
325 FREE_STRING(ctx, ref->presence);
326 FREE_STRINGS(ctx, ref->dflts);
327 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
328}
329
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200330void
Radek Krejci19a96102018-11-15 13:38:09 +0100331lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
332{
333 struct lysp_node *child, *next;
334
335 FREE_STRING(ctx, node->name);
336 FREE_STRING(ctx, node->dsc);
337 FREE_STRING(ctx, node->ref);
338 FREE_MEMBER(ctx, node->when, lysp_when_free);
339 FREE_STRINGS(ctx, node->iffeatures);
340 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
341
342 switch(node->nodetype) {
343 case LYS_CONTAINER:
344 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->musts, lysp_restr_free);
345 FREE_STRING(ctx, ((struct lysp_node_container*)node)->presence);
346 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->typedefs, lysp_tpdf_free);
347 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->groupings, lysp_grp_free);
348 LY_LIST_FOR_SAFE(((struct lysp_node_container*)node)->child, next, child) {
349 lysp_node_free(ctx, child);
350 }
351 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->actions, lysp_action_free);
352 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->notifs, lysp_notif_free);
353 break;
354 case LYS_LEAF:
355 FREE_ARRAY(ctx, ((struct lysp_node_leaf*)node)->musts, lysp_restr_free);
356 lysp_type_free(ctx, &((struct lysp_node_leaf*)node)->type);
357 FREE_STRING(ctx, ((struct lysp_node_leaf*)node)->units);
358 FREE_STRING(ctx, ((struct lysp_node_leaf*)node)->dflt);
359 break;
360 case LYS_LEAFLIST:
361 FREE_ARRAY(ctx, ((struct lysp_node_leaflist*)node)->musts, lysp_restr_free);
362 lysp_type_free(ctx, &((struct lysp_node_leaflist*)node)->type);
363 FREE_STRING(ctx, ((struct lysp_node_leaflist*)node)->units);
364 FREE_STRINGS(ctx, ((struct lysp_node_leaflist*)node)->dflts);
365 break;
366 case LYS_LIST:
367 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->musts, lysp_restr_free);
368 FREE_STRING(ctx, ((struct lysp_node_list*)node)->key);
369 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->typedefs, lysp_tpdf_free);
370 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->groupings, lysp_grp_free);
371 LY_LIST_FOR_SAFE(((struct lysp_node_list*)node)->child, next, child) {
372 lysp_node_free(ctx, child);
373 }
374 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->actions, lysp_action_free);
375 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->notifs, lysp_notif_free);
376 FREE_STRINGS(ctx, ((struct lysp_node_list*)node)->uniques);
377 break;
378 case LYS_CHOICE:
379 LY_LIST_FOR_SAFE(((struct lysp_node_choice*)node)->child, next, child) {
380 lysp_node_free(ctx, child);
381 }
382 FREE_STRING(ctx, ((struct lysp_node_choice*)node)->dflt);
383 break;
384 case LYS_CASE:
385 LY_LIST_FOR_SAFE(((struct lysp_node_case*)node)->child, next, child) {
386 lysp_node_free(ctx, child);
387 }
388 break;
389 case LYS_ANYDATA:
390 case LYS_ANYXML:
391 FREE_ARRAY(ctx, ((struct lysp_node_anydata*)node)->musts, lysp_restr_free);
392 break;
393 case LYS_USES:
394 FREE_ARRAY(ctx, ((struct lysp_node_uses*)node)->refines, lysp_refine_free);
395 FREE_ARRAY(ctx, ((struct lysp_node_uses*)node)->augments, lysp_augment_free);
396 break;
397 default:
398 LOGINT(ctx);
399 }
400
401 free(node);
402}
403
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100404void
405lysp_submodule_free(struct ly_ctx *ctx, struct lysp_submodule *submod)
406{
407 struct lysp_node *node, *next;
408
Radek Krejci40544fa2019-01-11 09:38:37 +0100409 if (!submod) {
410 return;
411 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100412
413 FREE_ARRAY(ctx, submod->imports, lysp_import_free);
414 FREE_ARRAY(ctx, submod->includes, lysp_include_free);
415
416 FREE_ARRAY(ctx, submod->revs, lysp_revision_free);
417 FREE_ARRAY(ctx, submod->extensions, lysp_ext_free);
418 FREE_ARRAY(ctx, submod->features, lysp_feature_free);
419 FREE_ARRAY(ctx, submod->identities, lysp_ident_free);
420 FREE_ARRAY(ctx, submod->typedefs, lysp_tpdf_free);
421 FREE_ARRAY(ctx, submod->groupings, lysp_grp_free);
422 LY_LIST_FOR_SAFE(submod->data, next, node) {
423 lysp_node_free(ctx, node);
424 }
425 FREE_ARRAY(ctx, submod->augments, lysp_augment_free);
426 FREE_ARRAY(ctx, submod->rpcs, lysp_action_free);
427 FREE_ARRAY(ctx, submod->notifs, lysp_notif_free);
428 FREE_ARRAY(ctx, submod->deviations, lysp_deviation_free);
429 FREE_ARRAY(ctx, submod->exts, lysp_ext_instance_free);
430
431 FREE_STRING(ctx, submod->belongsto);
432 FREE_STRING(ctx, submod->name);
433 FREE_STRING(ctx, submod->filepath);
434 FREE_STRING(ctx, submod->prefix);
435 FREE_STRING(ctx, submod->org);
436 FREE_STRING(ctx, submod->contact);
437 FREE_STRING(ctx, submod->dsc);
438 FREE_STRING(ctx, submod->ref);
439
440 free(submod);
441}
442
Radek Krejci19a96102018-11-15 13:38:09 +0100443API void
444lysp_module_free(struct lysp_module *module)
445{
446 struct ly_ctx *ctx;
447 struct lysp_node *node, *next;
448
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100449 if (!module) {
450 return;
451 }
452 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100453
454 FREE_ARRAY(ctx, module->imports, lysp_import_free);
455 FREE_ARRAY(ctx, module->includes, lysp_include_free);
456
Radek Krejci19a96102018-11-15 13:38:09 +0100457 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
458 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
459 FREE_ARRAY(ctx, module->features, lysp_feature_free);
460 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
461 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
462 FREE_ARRAY(ctx, module->groupings, lysp_grp_free);
463 LY_LIST_FOR_SAFE(module->data, next, node) {
464 lysp_node_free(ctx, node);
465 }
466 FREE_ARRAY(ctx, module->augments, lysp_augment_free);
467 FREE_ARRAY(ctx, module->rpcs, lysp_action_free);
468 FREE_ARRAY(ctx, module->notifs, lysp_notif_free);
469 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
470 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
471
472 free(module);
473}
474
Radek Krejci0af46292019-01-11 16:02:31 +0100475void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100476lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
477{
478 if (--(*ext)->refcount) {
479 return;
480 }
481 FREE_STRING(ctx, (*ext)->name);
482 FREE_STRING(ctx, (*ext)->argument);
483 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
484 free(*ext);
485}
486
487void
Radek Krejci19a96102018-11-15 13:38:09 +0100488lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
489{
fredganebc50572019-10-31 15:39:23 +0800490 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200491 ext->def->plugin->free(ctx, ext);
492 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100493 if (ext->def) {
494 lysc_extension_free(ctx, &ext->def);
495 }
Radek Krejci0935f412019-08-20 16:15:18 +0200496 FREE_STRING(ctx, ext->argument);
497 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
498}
499
500void
Radek Krejci19a96102018-11-15 13:38:09 +0100501lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
502{
503 LY_ARRAY_FREE(iff->features);
504 free(iff->expr);
505}
506
507static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100508lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100509{
Radek Krejci00b874b2019-02-12 10:54:50 +0100510 if (--(*w)->refcount) {
511 return;
512 }
513 lyxp_expr_free(ctx, (*w)->cond);
514 FREE_STRING(ctx, (*w)->dsc);
515 FREE_STRING(ctx, (*w)->ref);
516 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
517 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100518}
519
Radek Krejciccd20f12019-02-15 14:12:27 +0100520void
Radek Krejci58d171e2018-11-23 13:50:55 +0100521lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
522{
523 lyxp_expr_free(ctx, must->cond);
524 FREE_STRING(ctx, must->emsg);
525 FREE_STRING(ctx, must->eapptag);
Radek Krejcic8b31002019-01-08 10:24:45 +0100526 FREE_STRING(ctx, must->dsc);
527 FREE_STRING(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100528 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
529}
530
531static void
Radek Krejci19a96102018-11-15 13:38:09 +0100532lysc_import_free(struct ly_ctx *ctx, struct lysc_import *import)
533{
534 /* imported module is freed directly from the context's list */
535 FREE_STRING(ctx, import->prefix);
536 FREE_ARRAY(ctx, import->exts, lysc_ext_instance_free);
537}
538
539static void
540lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
541{
542 FREE_STRING(ctx, ident->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100543 FREE_STRING(ctx, ident->dsc);
544 FREE_STRING(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100545 FREE_ARRAY(ctx, ident->iffeatures, lysc_iffeature_free);
546 LY_ARRAY_FREE(ident->derived);
547 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
548}
549
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200550void
Radek Krejci19a96102018-11-15 13:38:09 +0100551lysc_feature_free(struct ly_ctx *ctx, struct lysc_feature *feat)
552{
553 FREE_STRING(ctx, feat->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100554 FREE_STRING(ctx, feat->dsc);
555 FREE_STRING(ctx, feat->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100556 FREE_ARRAY(ctx, feat->iffeatures, lysc_iffeature_free);
557 LY_ARRAY_FREE(feat->depfeatures);
558 FREE_ARRAY(ctx, feat->exts, lysc_ext_instance_free);
559}
560
561static void
562lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
563{
564 LY_ARRAY_FREE(range->parts);
565 FREE_STRING(ctx, range->eapptag);
566 FREE_STRING(ctx, range->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100567 FREE_STRING(ctx, range->dsc);
568 FREE_STRING(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100569 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
570}
571
572static void
573lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
574{
575 if (--(*pattern)->refcount) {
576 return;
577 }
Radek Krejci54579462019-04-30 12:47:06 +0200578 pcre2_code_free((*pattern)->code);
579 FREE_STRING(ctx, (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +0100580 FREE_STRING(ctx, (*pattern)->eapptag);
581 FREE_STRING(ctx, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100582 FREE_STRING(ctx, (*pattern)->dsc);
583 FREE_STRING(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100584 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
585 free(*pattern);
586}
587
588static void
Radek Krejci693262f2019-04-29 15:23:20 +0200589lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100590{
591 FREE_STRING(ctx, item->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100592 FREE_STRING(ctx, item->dsc);
593 FREE_STRING(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100594 FREE_ARRAY(ctx, item->iffeatures, lysc_iffeature_free);
595 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
596}
597
Radek Krejcia3045382018-11-22 14:30:31 +0100598static void
599lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
600{
601 lysc_type_free(ctx, *type);
602}
Radek Krejcicdfecd92018-11-26 11:27:32 +0100603void
Radek Krejci19a96102018-11-15 13:38:09 +0100604lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
605{
606 if (--type->refcount) {
607 return;
608 }
609 switch(type->basetype) {
610 case LY_TYPE_BINARY:
611 FREE_MEMBER(ctx, ((struct lysc_type_bin*)type)->length, lysc_range_free);
612 break;
613 case LY_TYPE_BITS:
Radek Krejci693262f2019-04-29 15:23:20 +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:
617 FREE_MEMBER(ctx, ((struct lysc_type_dec*)type)->range, lysc_range_free);
618 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100619 case LY_TYPE_STRING:
620 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);
622 break;
623 case LY_TYPE_ENUM:
624 FREE_ARRAY(ctx, ((struct lysc_type_enum*)type)->enums, lysc_enum_item_free);
625 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:
634 FREE_MEMBER(ctx, ((struct lysc_type_num*)type)->range, lysc_range_free);
635 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100636 case LY_TYPE_IDENT:
637 LY_ARRAY_FREE(((struct lysc_type_identityref*)type)->bases);
638 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100639 case LY_TYPE_UNION:
640 FREE_ARRAY(ctx, ((struct lysc_type_union*)type)->types, lysc_type2_free);
641 break;
642 case LY_TYPE_LEAFREF:
Michal Vasko004d3152020-06-11 19:59: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 }
652 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejcia1911222019-07-22 17:24:50 +0200653 if (type->dflt) {
654 type->plugin->free(ctx, type->dflt);
655 lysc_type_free(ctx, type->dflt->realtype);
656 free(type->dflt);
657 }
Radek Krejci19a96102018-11-15 13:38:09 +0100658 free(type);
659}
660
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100661void
Radek Krejcif538ce52019-03-05 10:46:14 +0100662lysc_action_inout_free(struct ly_ctx *ctx, struct lysc_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100663{
664 struct lysc_node *child, *child_next;
665
Radek Krejcif538ce52019-03-05 10:46:14 +0100666 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
667 LY_LIST_FOR_SAFE(inout->data, child_next, child) {
668 lysc_node_free(ctx, child);
669 }
670}
671
672void
673lysc_action_free(struct ly_ctx *ctx, struct lysc_action *action)
674{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100675 FREE_STRING(ctx, action->name);
676 FREE_STRING(ctx, action->dsc);
677 FREE_STRING(ctx, action->ref);
678 FREE_ARRAY(ctx, action->iffeatures, lysc_iffeature_free);
679 FREE_ARRAY(ctx, action->exts, lysc_ext_instance_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200680 FREE_ARRAY(ctx, action->input_exts, lysc_ext_instance_free);
Radek Krejcif538ce52019-03-05 10:46:14 +0100681 lysc_action_inout_free(ctx, &action->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200682 FREE_ARRAY(ctx, action->output_exts, lysc_ext_instance_free);
Radek Krejcif538ce52019-03-05 10:46:14 +0100683 lysc_action_inout_free(ctx, &action->output);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100684}
685
Radek Krejcifc11bd72019-04-11 16:00:05 +0200686void
687lysc_notif_free(struct ly_ctx *ctx, struct lysc_notif *notif)
688{
689 struct lysc_node *child, *child_next;
690
691 FREE_STRING(ctx, notif->name);
692 FREE_STRING(ctx, notif->dsc);
693 FREE_STRING(ctx, notif->ref);
694 FREE_ARRAY(ctx, notif->iffeatures, lysc_iffeature_free);
695 FREE_ARRAY(ctx, notif->exts, lysc_ext_instance_free);
696 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
697 LY_LIST_FOR_SAFE(notif->data, child_next, child) {
698 lysc_node_free(ctx, child);
699 }
700}
701
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200702void
Radek Krejci19a96102018-11-15 13:38:09 +0100703lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
704{
705 struct lysc_node *child, *child_next;
706
707 LY_LIST_FOR_SAFE(node->child, child_next, child) {
708 lysc_node_free(ctx, child);
709 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100710 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100711 FREE_ARRAY(ctx, node->actions, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200712 FREE_ARRAY(ctx, node->notifs, lysc_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100713}
714
715static void
716lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
717{
Radek Krejci58d171e2018-11-23 13:50:55 +0100718 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100719 if (node->type) {
720 lysc_type_free(ctx, node->type);
721 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100722 FREE_STRING(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200723 if (node->dflt) {
724 node->dflt->realtype->plugin->free(ctx, node->dflt);
725 lysc_type_free(ctx, node->dflt->realtype);
726 free(node->dflt);
727 }
Radek Krejci19a96102018-11-15 13:38:09 +0100728}
729
Radek Krejci42452ac2018-11-28 17:09:52 +0100730static void
731lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
732{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200733 LY_ARRAY_SIZE_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100734
Radek Krejci42452ac2018-11-28 17:09:52 +0100735 FREE_ARRAY(ctx, node->musts, lysc_must_free);
736 if (node->type) {
737 lysc_type_free(ctx, node->type);
738 }
739 FREE_STRING(ctx, node->units);
740 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200741 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
742 lysc_type_free(ctx, node->dflts[u]->realtype);
743 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100744 }
745 LY_ARRAY_FREE(node->dflts);
Radek Krejcid0ef1af2019-07-23 12:22:05 +0200746 LY_ARRAY_FREE(node->dflts_mods);
Radek Krejci42452ac2018-11-28 17:09:52 +0100747}
748
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100749static void
750lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
751{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200752 LY_ARRAY_SIZE_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100753 struct lysc_node *child, *child_next;
754
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100755 LY_LIST_FOR_SAFE(node->child, child_next, child) {
756 lysc_node_free(ctx, child);
757 }
758 FREE_ARRAY(ctx, node->musts, lysc_must_free);
759
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100760 LY_ARRAY_FOR(node->uniques, u) {
761 LY_ARRAY_FREE(node->uniques[u]);
762 }
763 LY_ARRAY_FREE(node->uniques);
764
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100765 FREE_ARRAY(ctx, node->actions, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200766 FREE_ARRAY(ctx, node->notifs, lysc_notif_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100767}
768
Radek Krejci056d0a82018-12-06 16:57:25 +0100769static void
770lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
771{
772 struct lysc_node *child, *child_next;
773
Radek Krejci056d0a82018-12-06 16:57:25 +0100774 if (node->cases) {
775 LY_LIST_FOR_SAFE(node->cases->child, child_next, child) {
776 lysc_node_free(ctx, child);
777 }
778 LY_LIST_FOR_SAFE((struct lysc_node*)node->cases, child_next, child) {
779 lysc_node_free(ctx, child);
780 }
781 }
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 */
799 switch(node->nodetype) {
800 case LYS_CONTAINER:
801 lysc_node_container_free(ctx, (struct lysc_node_container*)node);
802 break;
803 case LYS_LEAF:
804 lysc_node_leaf_free(ctx, (struct lysc_node_leaf*)node);
805 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100806 case LYS_LEAFLIST:
807 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist*)node);
808 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100809 case LYS_LIST:
810 lysc_node_list_free(ctx, (struct lysc_node_list*)node);
811 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100812 case LYS_CHOICE:
813 lysc_node_choice_free(ctx, (struct lysc_node_choice*)node);
814 break;
815 case LYS_CASE:
816 /* nothing specific */
817 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100818 case LYS_ANYDATA:
819 case LYS_ANYXML:
820 lysc_node_anydata_free(ctx, (struct lysc_node_anydata*)node);
821 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
838 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 FREE_ARRAY(ctx, module->imports, lysc_import_free);
842 FREE_ARRAY(ctx, module->features, lysc_feature_free);
843 FREE_ARRAY(ctx, module->identities, lysc_ident_free);
844
845 LY_LIST_FOR_SAFE(module->data, node_next, node) {
846 lysc_node_free(ctx, node);
847 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100848 FREE_ARRAY(ctx, module->rpcs, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200849 FREE_ARRAY(ctx, module->notifs, lysc_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100850 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
Michal Vasko57c10cd2020-05-27 15:57:11 +0200851 LY_ARRAY_FREE(module->deviated_by);
Radek Krejci19a96102018-11-15 13:38:09 +0100852
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 Krejci0af46292019-01-11 16:02:31 +0100875 FREE_ARRAY(module->ctx, module->off_features, lysc_feature_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100876 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100877
878 FREE_STRING(module->ctx, module->name);
Radek Krejci0af46292019-01-11 16:02:31 +0100879 FREE_STRING(module->ctx, module->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100880 FREE_STRING(module->ctx, module->ns);
881 FREE_STRING(module->ctx, module->prefix);
882 FREE_STRING(module->ctx, module->filepath);
883 FREE_STRING(module->ctx, module->org);
884 FREE_STRING(module->ctx, module->contact);
885 FREE_STRING(module->ctx, module->dsc);
886 FREE_STRING(module->ctx, module->ref);
887
Radek Krejci19a96102018-11-15 13:38:09 +0100888 free(module);
889}
Radek Krejci38d85362019-09-05 16:26:38 +0200890API void
891lysc_extension_instance_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
892{
893 for (unsigned int u = 0; substmts[u].stmt; ++u) {
894 if (!substmts[u].storage) {
895 continue;
896 }
897
898 switch(substmts[u].stmt) {
899 case LY_STMT_TYPE:
900 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
901 /* single item */
902 struct lysc_type *type = *((struct lysc_type**)substmts[u].storage);
903 if (!type) {
904 break;
905 }
906 lysc_type_free(ctx, type);
907 } else {
908 /* multiple items */
909 struct lysc_type **types = *((struct lysc_type***)substmts[u].storage);
910 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 */
919 const char *str = *((const char**)substmts[u].storage);
920 if (!str) {
921 break;
922 }
923 FREE_STRING(ctx, str);
924 } else {
925 /* multiple items */
926 const char **strs = *((const char***)substmts[u].storage);
927 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: {
938 struct lysc_iffeature *iff = *((struct lysc_iffeature**)substmts[u].storage);
939 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
953 /* TODO other statements */
954 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}