blob: 7416e6b7723bd3628990b730a20ea904077c7488 [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"
19#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);
29void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
Michal Vasko5fe75f12020-03-02 13:52:37 +010030void lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext);
Radek Krejci19a96102018-11-15 13:38:09 +010031
32static void
33lysp_stmt_free(struct ly_ctx *ctx, struct lysp_stmt *stmt)
34{
35 struct lysp_stmt *child, *next;
36
37 FREE_STRING(ctx, stmt->stmt);
38 FREE_STRING(ctx, stmt->arg);
39
40 LY_LIST_FOR_SAFE(stmt->child, next, child) {
41 lysp_stmt_free(ctx, child);
42 }
43
44 free(stmt);
45}
46
Radek Krejci2d7a47b2019-05-16 13:34:10 +020047void
Radek Krejci19a96102018-11-15 13:38:09 +010048lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext)
49{
50 struct lysp_stmt *stmt, *next;
51
52 FREE_STRING(ctx, ext->name);
53 FREE_STRING(ctx, ext->argument);
54
55 LY_LIST_FOR_SAFE(ext->child, next, stmt) {
56 lysp_stmt_free(ctx, stmt);
57 }
58}
59
David Sedlák298ff6d2019-07-26 14:29:03 +020060void
Radek Krejci19a96102018-11-15 13:38:09 +010061lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import)
62{
63 /* imported module is freed directly from the context's list */
64 FREE_STRING(ctx, import->name);
65 FREE_STRING(ctx, import->prefix);
66 FREE_STRING(ctx, import->dsc);
67 FREE_STRING(ctx, import->ref);
68 FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free);
69}
70
David Sedlák0c2bab92019-07-22 15:33:19 +020071void
Radek Krejci19a96102018-11-15 13:38:09 +010072lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include)
73{
74 if (include->submodule) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +010075 lysp_submodule_free(ctx, include->submodule);
Radek Krejci19a96102018-11-15 13:38:09 +010076 }
77 FREE_STRING(ctx, include->name);
78 FREE_STRING(ctx, include->dsc);
79 FREE_STRING(ctx, include->ref);
80 FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free);
81}
82
David Sedlákaa854b02019-07-22 14:17:10 +020083void
Radek Krejci19a96102018-11-15 13:38:09 +010084lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev)
85{
86 FREE_STRING(ctx, rev->dsc);
87 FREE_STRING(ctx, rev->ref);
88 FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free);
89}
90
David Sedlák986cb412019-07-04 13:10:11 +020091void
Radek Krejci19a96102018-11-15 13:38:09 +010092lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext)
93{
94 FREE_STRING(ctx, ext->name);
95 FREE_STRING(ctx, ext->argument);
96 FREE_STRING(ctx, ext->dsc);
97 FREE_STRING(ctx, ext->ref);
98 FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free);
Michal Vasko5fe75f12020-03-02 13:52:37 +010099 if (ext->compiled) {
100 lysc_extension_free(ctx, &ext->compiled);
101 }
Radek Krejci19a96102018-11-15 13:38:09 +0100102}
103
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200104void
Radek Krejci19a96102018-11-15 13:38:09 +0100105lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat)
106{
107 FREE_STRING(ctx, feat->name);
108 FREE_STRINGS(ctx, feat->iffeatures);
109 FREE_STRING(ctx, feat->dsc);
110 FREE_STRING(ctx, feat->ref);
111 FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
112}
113
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200114void
Radek Krejci19a96102018-11-15 13:38:09 +0100115lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident)
116{
117 FREE_STRING(ctx, ident->name);
118 FREE_STRINGS(ctx, ident->iffeatures);
119 FREE_STRINGS(ctx, ident->bases);
120 FREE_STRING(ctx, ident->dsc);
121 FREE_STRING(ctx, ident->ref);
122 FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
123}
124
125static void
126lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
127{
128 FREE_STRING(ctx, restr->arg);
129 FREE_STRING(ctx, restr->emsg);
130 FREE_STRING(ctx, restr->eapptag);
131 FREE_STRING(ctx, restr->dsc);
132 FREE_STRING(ctx, restr->ref);
133 FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
134}
135
136static void
137lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item)
138{
139 FREE_STRING(ctx, item->name);
140 FREE_STRING(ctx, item->dsc);
141 FREE_STRING(ctx, item->ref);
142 FREE_STRINGS(ctx, item->iffeatures);
143 FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
144}
145
Radek Krejcicdfecd92018-11-26 11:27:32 +0100146void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type);
Michal Vasko004d3152020-06-11 19:59:22 +0200147
David Sedlák32488102019-07-15 17:44:10 +0200148void
Radek Krejci19a96102018-11-15 13:38:09 +0100149lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type)
150{
151 FREE_STRING(ctx, type->name);
152 FREE_MEMBER(ctx, type->range, lysp_restr_free);
153 FREE_MEMBER(ctx, type->length, lysp_restr_free);
154 FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
155 FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
156 FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
Michal Vasko004d3152020-06-11 19:59:22 +0200157 lyxp_expr_free(ctx, type->path);
Radek Krejci19a96102018-11-15 13:38:09 +0100158 FREE_STRINGS(ctx, type->bases);
159 FREE_ARRAY(ctx, type->types, lysp_type_free);
160 FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
161 if (type->compiled) {
162 lysc_type_free(ctx, type->compiled);
163 }
164}
165
David Sedlák04e17b22019-07-19 15:29:48 +0200166void
Radek Krejci19a96102018-11-15 13:38:09 +0100167lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
168{
169 FREE_STRING(ctx, tpdf->name);
170 FREE_STRING(ctx, tpdf->units);
171 FREE_STRING(ctx, tpdf->dflt);
172 FREE_STRING(ctx, tpdf->dsc);
173 FREE_STRING(ctx, tpdf->ref);
174 FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
175
176 lysp_type_free(ctx, &tpdf->type);
177
178}
179
Radek Krejcif538ce52019-03-05 10:46:14 +0100180void
Radek Krejci19a96102018-11-15 13:38:09 +0100181lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout)
182{
183 struct lysp_node *node, *next;
184
185 FREE_ARRAY(ctx, inout->musts, lysp_restr_free);
186 FREE_ARRAY(ctx, inout->typedefs, lysp_tpdf_free);
187 FREE_ARRAY(ctx, inout->groupings, lysp_grp_free);
188 LY_LIST_FOR_SAFE(inout->data, next, node) {
189 lysp_node_free(ctx, node);
190 }
191 FREE_ARRAY(ctx, inout->exts, lysp_ext_instance_free);
192
193}
194
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200195void
Radek Krejci19a96102018-11-15 13:38:09 +0100196lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action)
197{
198 FREE_STRING(ctx, action->name);
199 FREE_STRING(ctx, action->dsc);
200 FREE_STRING(ctx, action->ref);
201 FREE_STRINGS(ctx, action->iffeatures);
202 FREE_ARRAY(ctx, action->typedefs, lysp_tpdf_free);
203 FREE_ARRAY(ctx, action->groupings, lysp_grp_free);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100204 lysp_action_inout_free(ctx, &action->input);
205 lysp_action_inout_free(ctx, &action->output);
Radek Krejci19a96102018-11-15 13:38:09 +0100206 FREE_ARRAY(ctx, action->exts, lysp_ext_instance_free);
207}
208
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200209void
Radek Krejci19a96102018-11-15 13:38:09 +0100210lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif)
211{
212 struct lysp_node *node, *next;
213
214 FREE_STRING(ctx, notif->name);
215 FREE_STRING(ctx, notif->dsc);
216 FREE_STRING(ctx, notif->ref);
217 FREE_STRINGS(ctx, notif->iffeatures);
218 FREE_ARRAY(ctx, notif->musts, lysp_restr_free);
219 FREE_ARRAY(ctx, notif->typedefs, lysp_tpdf_free);
220 FREE_ARRAY(ctx, notif->groupings, lysp_grp_free);
221 LY_LIST_FOR_SAFE(notif->data, next, node) {
222 lysp_node_free(ctx, node);
223 }
224 FREE_ARRAY(ctx, notif->exts, lysp_ext_instance_free);
225}
226
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200227void
Radek Krejci19a96102018-11-15 13:38:09 +0100228lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp)
229{
230 struct lysp_node *node, *next;
231
232 FREE_STRING(ctx, grp->name);
233 FREE_STRING(ctx, grp->dsc);
234 FREE_STRING(ctx, grp->ref);
235 FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
236 FREE_ARRAY(ctx, grp->groupings, lysp_grp_free);
237 LY_LIST_FOR_SAFE(grp->data, next, node) {
238 lysp_node_free(ctx, node);
239 }
240 FREE_ARRAY(ctx, grp->actions, lysp_action_free);
241 FREE_ARRAY(ctx, grp->notifs, lysp_notif_free);
242 FREE_ARRAY(ctx, grp->exts, lysp_ext_instance_free);
243}
244
Radek Krejcif09e4e82019-06-14 15:08:11 +0200245void
Radek Krejci19a96102018-11-15 13:38:09 +0100246lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
247{
248 FREE_STRING(ctx, when->cond);
249 FREE_STRING(ctx, when->dsc);
250 FREE_STRING(ctx, when->ref);
251 FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
252}
253
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200254void
Radek Krejci19a96102018-11-15 13:38:09 +0100255lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment)
256{
257 struct lysp_node *node, *next;
258
259 FREE_STRING(ctx, augment->nodeid);
260 FREE_STRING(ctx, augment->dsc);
261 FREE_STRING(ctx, augment->ref);
262 FREE_MEMBER(ctx, augment->when, lysp_when_free);
263 FREE_STRINGS(ctx, augment->iffeatures);
264 LY_LIST_FOR_SAFE(augment->child, next, node) {
265 lysp_node_free(ctx, node);
266 }
267 FREE_ARRAY(ctx, augment->actions, lysp_action_free);
268 FREE_ARRAY(ctx, augment->notifs, lysp_notif_free);
269 FREE_ARRAY(ctx, augment->exts, lysp_ext_instance_free);
270}
271
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200272void
Radek Krejci19a96102018-11-15 13:38:09 +0100273lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
274{
275 struct lysp_deviate_add *add = (struct lysp_deviate_add*)d;
276 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl*)d;
277
278 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
279 switch(d->mod) {
280 case LYS_DEV_NOT_SUPPORTED:
281 /* nothing to do */
282 break;
283 case LYS_DEV_ADD:
284 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
285 FREE_STRING(ctx, add->units);
286 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
287 FREE_STRINGS(ctx, add->uniques);
288 FREE_STRINGS(ctx, add->dflts);
289 break;
290 case LYS_DEV_REPLACE:
291 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
292 FREE_STRING(ctx, rpl->units);
293 FREE_STRING(ctx, rpl->dflt);
294 break;
295 default:
296 LOGINT(ctx);
297 break;
298 }
299}
300
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200301void
Radek Krejci19a96102018-11-15 13:38:09 +0100302lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
303{
304 struct lysp_deviate *next, *iter;
305
306 FREE_STRING(ctx, dev->nodeid);
307 FREE_STRING(ctx, dev->dsc);
308 FREE_STRING(ctx, dev->ref);
309 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
310 lysp_deviate_free(ctx, iter);
311 free(iter);
312 }
313 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
314}
315
David Sedlákd2d676a2019-07-22 11:28:19 +0200316void
Radek Krejci19a96102018-11-15 13:38:09 +0100317lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
318{
319 FREE_STRING(ctx, ref->nodeid);
320 FREE_STRING(ctx, ref->dsc);
321 FREE_STRING(ctx, ref->ref);
322 FREE_STRINGS(ctx, ref->iffeatures);
323 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
324 FREE_STRING(ctx, ref->presence);
325 FREE_STRINGS(ctx, ref->dflts);
326 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
327}
328
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200329void
Radek Krejci19a96102018-11-15 13:38:09 +0100330lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
331{
332 struct lysp_node *child, *next;
333
334 FREE_STRING(ctx, node->name);
335 FREE_STRING(ctx, node->dsc);
336 FREE_STRING(ctx, node->ref);
337 FREE_MEMBER(ctx, node->when, lysp_when_free);
338 FREE_STRINGS(ctx, node->iffeatures);
339 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
340
341 switch(node->nodetype) {
342 case LYS_CONTAINER:
343 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->musts, lysp_restr_free);
344 FREE_STRING(ctx, ((struct lysp_node_container*)node)->presence);
345 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->typedefs, lysp_tpdf_free);
346 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->groupings, lysp_grp_free);
347 LY_LIST_FOR_SAFE(((struct lysp_node_container*)node)->child, next, child) {
348 lysp_node_free(ctx, child);
349 }
350 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->actions, lysp_action_free);
351 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->notifs, lysp_notif_free);
352 break;
353 case LYS_LEAF:
354 FREE_ARRAY(ctx, ((struct lysp_node_leaf*)node)->musts, lysp_restr_free);
355 lysp_type_free(ctx, &((struct lysp_node_leaf*)node)->type);
356 FREE_STRING(ctx, ((struct lysp_node_leaf*)node)->units);
357 FREE_STRING(ctx, ((struct lysp_node_leaf*)node)->dflt);
358 break;
359 case LYS_LEAFLIST:
360 FREE_ARRAY(ctx, ((struct lysp_node_leaflist*)node)->musts, lysp_restr_free);
361 lysp_type_free(ctx, &((struct lysp_node_leaflist*)node)->type);
362 FREE_STRING(ctx, ((struct lysp_node_leaflist*)node)->units);
363 FREE_STRINGS(ctx, ((struct lysp_node_leaflist*)node)->dflts);
364 break;
365 case LYS_LIST:
366 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->musts, lysp_restr_free);
367 FREE_STRING(ctx, ((struct lysp_node_list*)node)->key);
368 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->typedefs, lysp_tpdf_free);
369 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->groupings, lysp_grp_free);
370 LY_LIST_FOR_SAFE(((struct lysp_node_list*)node)->child, next, child) {
371 lysp_node_free(ctx, child);
372 }
373 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->actions, lysp_action_free);
374 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->notifs, lysp_notif_free);
375 FREE_STRINGS(ctx, ((struct lysp_node_list*)node)->uniques);
376 break;
377 case LYS_CHOICE:
378 LY_LIST_FOR_SAFE(((struct lysp_node_choice*)node)->child, next, child) {
379 lysp_node_free(ctx, child);
380 }
381 FREE_STRING(ctx, ((struct lysp_node_choice*)node)->dflt);
382 break;
383 case LYS_CASE:
384 LY_LIST_FOR_SAFE(((struct lysp_node_case*)node)->child, next, child) {
385 lysp_node_free(ctx, child);
386 }
387 break;
388 case LYS_ANYDATA:
389 case LYS_ANYXML:
390 FREE_ARRAY(ctx, ((struct lysp_node_anydata*)node)->musts, lysp_restr_free);
391 break;
392 case LYS_USES:
393 FREE_ARRAY(ctx, ((struct lysp_node_uses*)node)->refines, lysp_refine_free);
394 FREE_ARRAY(ctx, ((struct lysp_node_uses*)node)->augments, lysp_augment_free);
395 break;
396 default:
397 LOGINT(ctx);
398 }
399
400 free(node);
401}
402
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100403void
404lysp_submodule_free(struct ly_ctx *ctx, struct lysp_submodule *submod)
405{
406 struct lysp_node *node, *next;
407
Radek Krejci40544fa2019-01-11 09:38:37 +0100408 if (!submod) {
409 return;
410 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100411
412 FREE_ARRAY(ctx, submod->imports, lysp_import_free);
413 FREE_ARRAY(ctx, submod->includes, lysp_include_free);
414
415 FREE_ARRAY(ctx, submod->revs, lysp_revision_free);
416 FREE_ARRAY(ctx, submod->extensions, lysp_ext_free);
417 FREE_ARRAY(ctx, submod->features, lysp_feature_free);
418 FREE_ARRAY(ctx, submod->identities, lysp_ident_free);
419 FREE_ARRAY(ctx, submod->typedefs, lysp_tpdf_free);
420 FREE_ARRAY(ctx, submod->groupings, lysp_grp_free);
421 LY_LIST_FOR_SAFE(submod->data, next, node) {
422 lysp_node_free(ctx, node);
423 }
424 FREE_ARRAY(ctx, submod->augments, lysp_augment_free);
425 FREE_ARRAY(ctx, submod->rpcs, lysp_action_free);
426 FREE_ARRAY(ctx, submod->notifs, lysp_notif_free);
427 FREE_ARRAY(ctx, submod->deviations, lysp_deviation_free);
428 FREE_ARRAY(ctx, submod->exts, lysp_ext_instance_free);
429
430 FREE_STRING(ctx, submod->belongsto);
431 FREE_STRING(ctx, submod->name);
432 FREE_STRING(ctx, submod->filepath);
433 FREE_STRING(ctx, submod->prefix);
434 FREE_STRING(ctx, submod->org);
435 FREE_STRING(ctx, submod->contact);
436 FREE_STRING(ctx, submod->dsc);
437 FREE_STRING(ctx, submod->ref);
438
439 free(submod);
440}
441
Radek Krejci19a96102018-11-15 13:38:09 +0100442API void
443lysp_module_free(struct lysp_module *module)
444{
445 struct ly_ctx *ctx;
446 struct lysp_node *node, *next;
447
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100448 if (!module) {
449 return;
450 }
451 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100452
453 FREE_ARRAY(ctx, module->imports, lysp_import_free);
454 FREE_ARRAY(ctx, module->includes, lysp_include_free);
455
Radek Krejci19a96102018-11-15 13:38:09 +0100456 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
457 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
458 FREE_ARRAY(ctx, module->features, lysp_feature_free);
459 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
460 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
461 FREE_ARRAY(ctx, module->groupings, lysp_grp_free);
462 LY_LIST_FOR_SAFE(module->data, next, node) {
463 lysp_node_free(ctx, node);
464 }
465 FREE_ARRAY(ctx, module->augments, lysp_augment_free);
466 FREE_ARRAY(ctx, module->rpcs, lysp_action_free);
467 FREE_ARRAY(ctx, module->notifs, lysp_notif_free);
468 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
469 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
470
471 free(module);
472}
473
Radek Krejci0af46292019-01-11 16:02:31 +0100474void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100475lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
476{
477 if (--(*ext)->refcount) {
478 return;
479 }
480 FREE_STRING(ctx, (*ext)->name);
481 FREE_STRING(ctx, (*ext)->argument);
482 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
483 free(*ext);
484}
485
486void
Radek Krejci19a96102018-11-15 13:38:09 +0100487lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
488{
fredganebc50572019-10-31 15:39:23 +0800489 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200490 ext->def->plugin->free(ctx, ext);
491 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100492 if (ext->def) {
493 lysc_extension_free(ctx, &ext->def);
494 }
Radek Krejci0935f412019-08-20 16:15:18 +0200495 FREE_STRING(ctx, ext->argument);
496 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
497}
498
499void
Radek Krejci19a96102018-11-15 13:38:09 +0100500lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
501{
502 LY_ARRAY_FREE(iff->features);
503 free(iff->expr);
504}
505
506static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100507lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100508{
Radek Krejci00b874b2019-02-12 10:54:50 +0100509 if (--(*w)->refcount) {
510 return;
511 }
512 lyxp_expr_free(ctx, (*w)->cond);
513 FREE_STRING(ctx, (*w)->dsc);
514 FREE_STRING(ctx, (*w)->ref);
515 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
516 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100517}
518
Radek Krejciccd20f12019-02-15 14:12:27 +0100519void
Radek Krejci58d171e2018-11-23 13:50:55 +0100520lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
521{
522 lyxp_expr_free(ctx, must->cond);
523 FREE_STRING(ctx, must->emsg);
524 FREE_STRING(ctx, must->eapptag);
Radek Krejcic8b31002019-01-08 10:24:45 +0100525 FREE_STRING(ctx, must->dsc);
526 FREE_STRING(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100527 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
528}
529
530static void
Radek Krejci19a96102018-11-15 13:38:09 +0100531lysc_import_free(struct ly_ctx *ctx, struct lysc_import *import)
532{
533 /* imported module is freed directly from the context's list */
534 FREE_STRING(ctx, import->prefix);
535 FREE_ARRAY(ctx, import->exts, lysc_ext_instance_free);
536}
537
538static void
539lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
540{
541 FREE_STRING(ctx, ident->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100542 FREE_STRING(ctx, ident->dsc);
543 FREE_STRING(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100544 FREE_ARRAY(ctx, ident->iffeatures, lysc_iffeature_free);
545 LY_ARRAY_FREE(ident->derived);
546 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
547}
548
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200549void
Radek Krejci19a96102018-11-15 13:38:09 +0100550lysc_feature_free(struct ly_ctx *ctx, struct lysc_feature *feat)
551{
552 FREE_STRING(ctx, feat->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100553 FREE_STRING(ctx, feat->dsc);
554 FREE_STRING(ctx, feat->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100555 FREE_ARRAY(ctx, feat->iffeatures, lysc_iffeature_free);
556 LY_ARRAY_FREE(feat->depfeatures);
557 FREE_ARRAY(ctx, feat->exts, lysc_ext_instance_free);
558}
559
560static void
561lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
562{
563 LY_ARRAY_FREE(range->parts);
564 FREE_STRING(ctx, range->eapptag);
565 FREE_STRING(ctx, range->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100566 FREE_STRING(ctx, range->dsc);
567 FREE_STRING(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100568 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
569}
570
571static void
572lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
573{
574 if (--(*pattern)->refcount) {
575 return;
576 }
Radek Krejci54579462019-04-30 12:47:06 +0200577 pcre2_code_free((*pattern)->code);
578 FREE_STRING(ctx, (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +0100579 FREE_STRING(ctx, (*pattern)->eapptag);
580 FREE_STRING(ctx, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100581 FREE_STRING(ctx, (*pattern)->dsc);
582 FREE_STRING(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100583 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
584 free(*pattern);
585}
586
587static void
Radek Krejci693262f2019-04-29 15:23:20 +0200588lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100589{
590 FREE_STRING(ctx, item->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100591 FREE_STRING(ctx, item->dsc);
592 FREE_STRING(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100593 FREE_ARRAY(ctx, item->iffeatures, lysc_iffeature_free);
594 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
595}
596
Radek Krejcia3045382018-11-22 14:30:31 +0100597static void
598lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
599{
600 lysc_type_free(ctx, *type);
601}
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 }
608 switch(type->basetype) {
609 case LY_TYPE_BINARY:
610 FREE_MEMBER(ctx, ((struct lysc_type_bin*)type)->length, lysc_range_free);
611 break;
612 case LY_TYPE_BITS:
Radek Krejci693262f2019-04-29 15:23:20 +0200613 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 +0100614 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100615 case LY_TYPE_DEC64:
616 FREE_MEMBER(ctx, ((struct lysc_type_dec*)type)->range, lysc_range_free);
617 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100618 case LY_TYPE_STRING:
619 FREE_MEMBER(ctx, ((struct lysc_type_str*)type)->length, lysc_range_free);
620 FREE_ARRAY(ctx, ((struct lysc_type_str*)type)->patterns, lysc_pattern_free);
621 break;
622 case LY_TYPE_ENUM:
623 FREE_ARRAY(ctx, ((struct lysc_type_enum*)type)->enums, lysc_enum_item_free);
624 break;
625 case LY_TYPE_INT8:
626 case LY_TYPE_UINT8:
627 case LY_TYPE_INT16:
628 case LY_TYPE_UINT16:
629 case LY_TYPE_INT32:
630 case LY_TYPE_UINT32:
631 case LY_TYPE_INT64:
632 case LY_TYPE_UINT64:
633 FREE_MEMBER(ctx, ((struct lysc_type_num*)type)->range, lysc_range_free);
634 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100635 case LY_TYPE_IDENT:
636 LY_ARRAY_FREE(((struct lysc_type_identityref*)type)->bases);
637 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100638 case LY_TYPE_UNION:
639 FREE_ARRAY(ctx, ((struct lysc_type_union*)type)->types, lysc_type2_free);
640 break;
641 case LY_TYPE_LEAFREF:
Michal Vasko004d3152020-06-11 19:59:22 +0200642 lyxp_expr_free(ctx, ((struct lysc_type_leafref*)type)->path);
Radek Krejcia3045382018-11-22 14:30:31 +0100643 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100644 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100645 case LY_TYPE_BOOL:
646 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100647 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100648 /* nothing to do */
649 break;
650 }
651 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejcia1911222019-07-22 17:24:50 +0200652 if (type->dflt) {
653 type->plugin->free(ctx, type->dflt);
654 lysc_type_free(ctx, type->dflt->realtype);
655 free(type->dflt);
656 }
Radek Krejci19a96102018-11-15 13:38:09 +0100657 free(type);
658}
659
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100660void
Radek Krejcif538ce52019-03-05 10:46:14 +0100661lysc_action_inout_free(struct ly_ctx *ctx, struct lysc_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100662{
663 struct lysc_node *child, *child_next;
664
Radek Krejcif538ce52019-03-05 10:46:14 +0100665 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
666 LY_LIST_FOR_SAFE(inout->data, child_next, child) {
667 lysc_node_free(ctx, child);
668 }
669}
670
671void
672lysc_action_free(struct ly_ctx *ctx, struct lysc_action *action)
673{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100674 FREE_STRING(ctx, action->name);
675 FREE_STRING(ctx, action->dsc);
676 FREE_STRING(ctx, action->ref);
677 FREE_ARRAY(ctx, action->iffeatures, lysc_iffeature_free);
678 FREE_ARRAY(ctx, action->exts, lysc_ext_instance_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200679 FREE_ARRAY(ctx, action->input_exts, lysc_ext_instance_free);
Radek Krejcif538ce52019-03-05 10:46:14 +0100680 lysc_action_inout_free(ctx, &action->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200681 FREE_ARRAY(ctx, action->output_exts, lysc_ext_instance_free);
Radek Krejcif538ce52019-03-05 10:46:14 +0100682 lysc_action_inout_free(ctx, &action->output);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100683}
684
Radek Krejcifc11bd72019-04-11 16:00:05 +0200685void
686lysc_notif_free(struct ly_ctx *ctx, struct lysc_notif *notif)
687{
688 struct lysc_node *child, *child_next;
689
690 FREE_STRING(ctx, notif->name);
691 FREE_STRING(ctx, notif->dsc);
692 FREE_STRING(ctx, notif->ref);
693 FREE_ARRAY(ctx, notif->iffeatures, lysc_iffeature_free);
694 FREE_ARRAY(ctx, notif->exts, lysc_ext_instance_free);
695 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
696 LY_LIST_FOR_SAFE(notif->data, child_next, child) {
697 lysc_node_free(ctx, child);
698 }
699}
700
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200701void
Radek Krejci19a96102018-11-15 13:38:09 +0100702lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
703{
704 struct lysc_node *child, *child_next;
705
706 LY_LIST_FOR_SAFE(node->child, child_next, child) {
707 lysc_node_free(ctx, child);
708 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100709 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100710 FREE_ARRAY(ctx, node->actions, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200711 FREE_ARRAY(ctx, node->notifs, lysc_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100712}
713
714static void
715lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
716{
Radek Krejci58d171e2018-11-23 13:50:55 +0100717 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100718 if (node->type) {
719 lysc_type_free(ctx, node->type);
720 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100721 FREE_STRING(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200722 if (node->dflt) {
723 node->dflt->realtype->plugin->free(ctx, node->dflt);
724 lysc_type_free(ctx, node->dflt->realtype);
725 free(node->dflt);
726 }
Radek Krejci19a96102018-11-15 13:38:09 +0100727}
728
Radek Krejci42452ac2018-11-28 17:09:52 +0100729static void
730lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
731{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200732 LY_ARRAY_COUNT_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100733
Radek Krejci42452ac2018-11-28 17:09:52 +0100734 FREE_ARRAY(ctx, node->musts, lysc_must_free);
735 if (node->type) {
736 lysc_type_free(ctx, node->type);
737 }
738 FREE_STRING(ctx, node->units);
739 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200740 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
741 lysc_type_free(ctx, node->dflts[u]->realtype);
742 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100743 }
744 LY_ARRAY_FREE(node->dflts);
Radek Krejcid0ef1af2019-07-23 12:22:05 +0200745 LY_ARRAY_FREE(node->dflts_mods);
Radek Krejci42452ac2018-11-28 17:09:52 +0100746}
747
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100748static void
749lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
750{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200751 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100752 struct lysc_node *child, *child_next;
753
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100754 LY_LIST_FOR_SAFE(node->child, child_next, child) {
755 lysc_node_free(ctx, child);
756 }
757 FREE_ARRAY(ctx, node->musts, lysc_must_free);
758
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100759 LY_ARRAY_FOR(node->uniques, u) {
760 LY_ARRAY_FREE(node->uniques[u]);
761 }
762 LY_ARRAY_FREE(node->uniques);
763
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100764 FREE_ARRAY(ctx, node->actions, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200765 FREE_ARRAY(ctx, node->notifs, lysc_notif_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100766}
767
Radek Krejci056d0a82018-12-06 16:57:25 +0100768static void
769lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
770{
771 struct lysc_node *child, *child_next;
772
Radek Krejci056d0a82018-12-06 16:57:25 +0100773 if (node->cases) {
774 LY_LIST_FOR_SAFE(node->cases->child, child_next, child) {
775 lysc_node_free(ctx, child);
776 }
777 LY_LIST_FOR_SAFE((struct lysc_node*)node->cases, child_next, child) {
778 lysc_node_free(ctx, child);
779 }
780 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100781}
Radek Krejci056d0a82018-12-06 16:57:25 +0100782
Radek Krejci9800fb82018-12-13 14:26:23 +0100783static void
784lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
785{
786 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100787}
788
Radek Krejci19a96102018-11-15 13:38:09 +0100789void
790lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node)
791{
792 /* common part */
793 FREE_STRING(ctx, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +0100794 FREE_STRING(ctx, node->dsc);
795 FREE_STRING(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100796
797 /* nodetype-specific part */
798 switch(node->nodetype) {
799 case LYS_CONTAINER:
800 lysc_node_container_free(ctx, (struct lysc_node_container*)node);
801 break;
802 case LYS_LEAF:
803 lysc_node_leaf_free(ctx, (struct lysc_node_leaf*)node);
804 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100805 case LYS_LEAFLIST:
806 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist*)node);
807 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100808 case LYS_LIST:
809 lysc_node_list_free(ctx, (struct lysc_node_list*)node);
810 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100811 case LYS_CHOICE:
812 lysc_node_choice_free(ctx, (struct lysc_node_choice*)node);
813 break;
814 case LYS_CASE:
815 /* nothing specific */
816 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100817 case LYS_ANYDATA:
818 case LYS_ANYXML:
819 lysc_node_anydata_free(ctx, (struct lysc_node_anydata*)node);
820 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100821 default:
822 LOGINT(ctx);
823 }
824
Radek Krejci00b874b2019-02-12 10:54:50 +0100825 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100826 FREE_ARRAY(ctx, node->iffeatures, lysc_iffeature_free);
827 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100828 free(node);
829}
830
831static void
832lysc_module_free_(struct lysc_module *module)
833{
834 struct ly_ctx *ctx;
835 struct lysc_node *node, *node_next;
836
837 LY_CHECK_ARG_RET(NULL, module,);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100838 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100839
Radek Krejci19a96102018-11-15 13:38:09 +0100840 FREE_ARRAY(ctx, module->imports, lysc_import_free);
841 FREE_ARRAY(ctx, module->features, lysc_feature_free);
842 FREE_ARRAY(ctx, module->identities, lysc_ident_free);
843
844 LY_LIST_FOR_SAFE(module->data, node_next, node) {
845 lysc_node_free(ctx, node);
846 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100847 FREE_ARRAY(ctx, module->rpcs, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200848 FREE_ARRAY(ctx, module->notifs, lysc_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100849 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
Michal Vasko57c10cd2020-05-27 15:57:11 +0200850 LY_ARRAY_FREE(module->deviated_by);
Michal Vaskoa3af5982020-06-29 11:51:37 +0200851 LY_ARRAY_FREE(module->augmented_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);
Michal Vasko33ff9422020-07-03 09:50:39 +0200875 FREE_ARRAY(module->ctx, module->dis_features, lysc_feature_free);
876 FREE_ARRAY(module->ctx, module->dis_identities, lysc_ident_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100877 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100878
879 FREE_STRING(module->ctx, module->name);
Radek Krejci0af46292019-01-11 16:02:31 +0100880 FREE_STRING(module->ctx, module->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100881 FREE_STRING(module->ctx, module->ns);
882 FREE_STRING(module->ctx, module->prefix);
883 FREE_STRING(module->ctx, module->filepath);
884 FREE_STRING(module->ctx, module->org);
885 FREE_STRING(module->ctx, module->contact);
886 FREE_STRING(module->ctx, module->dsc);
887 FREE_STRING(module->ctx, module->ref);
888
Radek Krejci19a96102018-11-15 13:38:09 +0100889 free(module);
890}
Michal Vasko33ff9422020-07-03 09:50:39 +0200891
Radek Krejci38d85362019-09-05 16:26:38 +0200892API void
893lysc_extension_instance_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
894{
895 for (unsigned int u = 0; substmts[u].stmt; ++u) {
896 if (!substmts[u].storage) {
897 continue;
898 }
899
900 switch(substmts[u].stmt) {
901 case LY_STMT_TYPE:
902 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
903 /* single item */
904 struct lysc_type *type = *((struct lysc_type**)substmts[u].storage);
905 if (!type) {
906 break;
907 }
908 lysc_type_free(ctx, type);
909 } else {
910 /* multiple items */
911 struct lysc_type **types = *((struct lysc_type***)substmts[u].storage);
912 if (!types) {
913 break;
914 }
915 FREE_ARRAY(ctx, types, lysc_type2_free);
916 }
917 break;
918 case LY_STMT_UNITS:
919 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
920 /* single item */
921 const char *str = *((const char**)substmts[u].storage);
922 if (!str) {
923 break;
924 }
925 FREE_STRING(ctx, str);
926 } else {
927 /* multiple items */
928 const char **strs = *((const char***)substmts[u].storage);
929 if (!strs) {
930 break;
931 }
932 FREE_STRINGS(ctx, strs);
933 }
934 break;
935 case LY_STMT_STATUS:
Radek Krejciad5963b2019-09-06 16:03:05 +0200936 case LY_STMT_CONFIG:
Radek Krejci38d85362019-09-05 16:26:38 +0200937 /* nothing to do */
938 break;
939 case LY_STMT_IF_FEATURE: {
940 struct lysc_iffeature *iff = *((struct lysc_iffeature**)substmts[u].storage);
941 if (!iff) {
942 break;
943 }
944 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
945 /* single item */
946 lysc_iffeature_free(ctx, iff);
947 free(iff);
948 } else {
949 /* multiple items */
950 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
951 }
952 break;
953 }
954
955 /* TODO other statements */
956 default:
957 LOGINT(ctx);
958 }
959 }
960}
David Sedlákebd3acf2019-07-26 15:04:32 +0200961
962void
Michal Vaskob36053d2020-03-26 15:49:30 +0100963yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +0200964{
965 if (ctx) {
966 free(ctx);
967 }
968}
969
970void
Michal Vaskob36053d2020-03-26 15:49:30 +0100971yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +0200972{
973 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100974 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +0200975 free(ctx);
976 }
977}