blob: 283ba29a3936c61244178d88c6cdd7bd79339353 [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
15#include "common.h"
16
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <stdlib.h>
18
19#include "dict.h"
20#include "tree.h"
21#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010022#include "tree_schema_internal.h"
23#include "xpath.h"
24
Radek Krejci19a96102018-11-15 13:38:09 +010025static void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
26static void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
27
28static void
29lysp_stmt_free(struct ly_ctx *ctx, struct lysp_stmt *stmt)
30{
31 struct lysp_stmt *child, *next;
32
33 FREE_STRING(ctx, stmt->stmt);
34 FREE_STRING(ctx, stmt->arg);
35
36 LY_LIST_FOR_SAFE(stmt->child, next, child) {
37 lysp_stmt_free(ctx, child);
38 }
39
40 free(stmt);
41}
42
43static void
44lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext)
45{
46 struct lysp_stmt *stmt, *next;
47
48 FREE_STRING(ctx, ext->name);
49 FREE_STRING(ctx, ext->argument);
50
51 LY_LIST_FOR_SAFE(ext->child, next, stmt) {
52 lysp_stmt_free(ctx, stmt);
53 }
54}
55
56static void
57lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import)
58{
59 /* imported module is freed directly from the context's list */
60 FREE_STRING(ctx, import->name);
61 FREE_STRING(ctx, import->prefix);
62 FREE_STRING(ctx, import->dsc);
63 FREE_STRING(ctx, import->ref);
64 FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free);
65}
66
67static void
68lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include)
69{
70 if (include->submodule) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +010071 lysp_submodule_free(ctx, include->submodule);
Radek Krejci19a96102018-11-15 13:38:09 +010072 }
73 FREE_STRING(ctx, include->name);
74 FREE_STRING(ctx, include->dsc);
75 FREE_STRING(ctx, include->ref);
76 FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free);
77}
78
79static void
80lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev)
81{
82 FREE_STRING(ctx, rev->dsc);
83 FREE_STRING(ctx, rev->ref);
84 FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free);
85}
86
87static void
88lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext)
89{
90 FREE_STRING(ctx, ext->name);
91 FREE_STRING(ctx, ext->argument);
92 FREE_STRING(ctx, ext->dsc);
93 FREE_STRING(ctx, ext->ref);
94 FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free);
95}
96
97static void
98lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat)
99{
100 FREE_STRING(ctx, feat->name);
101 FREE_STRINGS(ctx, feat->iffeatures);
102 FREE_STRING(ctx, feat->dsc);
103 FREE_STRING(ctx, feat->ref);
104 FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
105}
106
107static void
108lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident)
109{
110 FREE_STRING(ctx, ident->name);
111 FREE_STRINGS(ctx, ident->iffeatures);
112 FREE_STRINGS(ctx, ident->bases);
113 FREE_STRING(ctx, ident->dsc);
114 FREE_STRING(ctx, ident->ref);
115 FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
116}
117
118static void
119lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
120{
121 FREE_STRING(ctx, restr->arg);
122 FREE_STRING(ctx, restr->emsg);
123 FREE_STRING(ctx, restr->eapptag);
124 FREE_STRING(ctx, restr->dsc);
125 FREE_STRING(ctx, restr->ref);
126 FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
127}
128
129static void
130lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item)
131{
132 FREE_STRING(ctx, item->name);
133 FREE_STRING(ctx, item->dsc);
134 FREE_STRING(ctx, item->ref);
135 FREE_STRINGS(ctx, item->iffeatures);
136 FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
137}
138
Radek Krejcicdfecd92018-11-26 11:27:32 +0100139void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type);
Radek Krejci19a96102018-11-15 13:38:09 +0100140static void
141lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type)
142{
143 FREE_STRING(ctx, type->name);
144 FREE_MEMBER(ctx, type->range, lysp_restr_free);
145 FREE_MEMBER(ctx, type->length, lysp_restr_free);
146 FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
147 FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
148 FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
149 FREE_STRING(ctx, type->path);
150 FREE_STRINGS(ctx, type->bases);
151 FREE_ARRAY(ctx, type->types, lysp_type_free);
152 FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
153 if (type->compiled) {
154 lysc_type_free(ctx, type->compiled);
155 }
156}
157
158static void
159lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
160{
161 FREE_STRING(ctx, tpdf->name);
162 FREE_STRING(ctx, tpdf->units);
163 FREE_STRING(ctx, tpdf->dflt);
164 FREE_STRING(ctx, tpdf->dsc);
165 FREE_STRING(ctx, tpdf->ref);
166 FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
167
168 lysp_type_free(ctx, &tpdf->type);
169
170}
171
Radek Krejcif538ce52019-03-05 10:46:14 +0100172void
Radek Krejci19a96102018-11-15 13:38:09 +0100173lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout)
174{
175 struct lysp_node *node, *next;
176
177 FREE_ARRAY(ctx, inout->musts, lysp_restr_free);
178 FREE_ARRAY(ctx, inout->typedefs, lysp_tpdf_free);
179 FREE_ARRAY(ctx, inout->groupings, lysp_grp_free);
180 LY_LIST_FOR_SAFE(inout->data, next, node) {
181 lysp_node_free(ctx, node);
182 }
183 FREE_ARRAY(ctx, inout->exts, lysp_ext_instance_free);
184
185}
186
187static void
188lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action)
189{
190 FREE_STRING(ctx, action->name);
191 FREE_STRING(ctx, action->dsc);
192 FREE_STRING(ctx, action->ref);
193 FREE_STRINGS(ctx, action->iffeatures);
194 FREE_ARRAY(ctx, action->typedefs, lysp_tpdf_free);
195 FREE_ARRAY(ctx, action->groupings, lysp_grp_free);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100196 lysp_action_inout_free(ctx, &action->input);
197 lysp_action_inout_free(ctx, &action->output);
Radek Krejci19a96102018-11-15 13:38:09 +0100198 FREE_ARRAY(ctx, action->exts, lysp_ext_instance_free);
199}
200
201static void
202lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif)
203{
204 struct lysp_node *node, *next;
205
206 FREE_STRING(ctx, notif->name);
207 FREE_STRING(ctx, notif->dsc);
208 FREE_STRING(ctx, notif->ref);
209 FREE_STRINGS(ctx, notif->iffeatures);
210 FREE_ARRAY(ctx, notif->musts, lysp_restr_free);
211 FREE_ARRAY(ctx, notif->typedefs, lysp_tpdf_free);
212 FREE_ARRAY(ctx, notif->groupings, lysp_grp_free);
213 LY_LIST_FOR_SAFE(notif->data, next, node) {
214 lysp_node_free(ctx, node);
215 }
216 FREE_ARRAY(ctx, notif->exts, lysp_ext_instance_free);
217}
218
219static void
220lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp)
221{
222 struct lysp_node *node, *next;
223
224 FREE_STRING(ctx, grp->name);
225 FREE_STRING(ctx, grp->dsc);
226 FREE_STRING(ctx, grp->ref);
227 FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
228 FREE_ARRAY(ctx, grp->groupings, lysp_grp_free);
229 LY_LIST_FOR_SAFE(grp->data, next, node) {
230 lysp_node_free(ctx, node);
231 }
232 FREE_ARRAY(ctx, grp->actions, lysp_action_free);
233 FREE_ARRAY(ctx, grp->notifs, lysp_notif_free);
234 FREE_ARRAY(ctx, grp->exts, lysp_ext_instance_free);
235}
236
237static void
238lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
239{
240 FREE_STRING(ctx, when->cond);
241 FREE_STRING(ctx, when->dsc);
242 FREE_STRING(ctx, when->ref);
243 FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
244}
245
246static void
247lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment)
248{
249 struct lysp_node *node, *next;
250
251 FREE_STRING(ctx, augment->nodeid);
252 FREE_STRING(ctx, augment->dsc);
253 FREE_STRING(ctx, augment->ref);
254 FREE_MEMBER(ctx, augment->when, lysp_when_free);
255 FREE_STRINGS(ctx, augment->iffeatures);
256 LY_LIST_FOR_SAFE(augment->child, next, node) {
257 lysp_node_free(ctx, node);
258 }
259 FREE_ARRAY(ctx, augment->actions, lysp_action_free);
260 FREE_ARRAY(ctx, augment->notifs, lysp_notif_free);
261 FREE_ARRAY(ctx, augment->exts, lysp_ext_instance_free);
262}
263
264static void
265lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
266{
267 struct lysp_deviate_add *add = (struct lysp_deviate_add*)d;
268 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl*)d;
269
270 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
271 switch(d->mod) {
272 case LYS_DEV_NOT_SUPPORTED:
273 /* nothing to do */
274 break;
275 case LYS_DEV_ADD:
276 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
277 FREE_STRING(ctx, add->units);
278 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
279 FREE_STRINGS(ctx, add->uniques);
280 FREE_STRINGS(ctx, add->dflts);
281 break;
282 case LYS_DEV_REPLACE:
283 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
284 FREE_STRING(ctx, rpl->units);
285 FREE_STRING(ctx, rpl->dflt);
286 break;
287 default:
288 LOGINT(ctx);
289 break;
290 }
291}
292
293static void
294lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
295{
296 struct lysp_deviate *next, *iter;
297
298 FREE_STRING(ctx, dev->nodeid);
299 FREE_STRING(ctx, dev->dsc);
300 FREE_STRING(ctx, dev->ref);
301 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
302 lysp_deviate_free(ctx, iter);
303 free(iter);
304 }
305 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
306}
307
308static void
309lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
310{
311 FREE_STRING(ctx, ref->nodeid);
312 FREE_STRING(ctx, ref->dsc);
313 FREE_STRING(ctx, ref->ref);
314 FREE_STRINGS(ctx, ref->iffeatures);
315 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
316 FREE_STRING(ctx, ref->presence);
317 FREE_STRINGS(ctx, ref->dflts);
318 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
319}
320
321static void
322lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
323{
324 struct lysp_node *child, *next;
325
326 FREE_STRING(ctx, node->name);
327 FREE_STRING(ctx, node->dsc);
328 FREE_STRING(ctx, node->ref);
329 FREE_MEMBER(ctx, node->when, lysp_when_free);
330 FREE_STRINGS(ctx, node->iffeatures);
331 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
332
333 switch(node->nodetype) {
334 case LYS_CONTAINER:
335 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->musts, lysp_restr_free);
336 FREE_STRING(ctx, ((struct lysp_node_container*)node)->presence);
337 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->typedefs, lysp_tpdf_free);
338 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->groupings, lysp_grp_free);
339 LY_LIST_FOR_SAFE(((struct lysp_node_container*)node)->child, next, child) {
340 lysp_node_free(ctx, child);
341 }
342 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->actions, lysp_action_free);
343 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->notifs, lysp_notif_free);
344 break;
345 case LYS_LEAF:
346 FREE_ARRAY(ctx, ((struct lysp_node_leaf*)node)->musts, lysp_restr_free);
347 lysp_type_free(ctx, &((struct lysp_node_leaf*)node)->type);
348 FREE_STRING(ctx, ((struct lysp_node_leaf*)node)->units);
349 FREE_STRING(ctx, ((struct lysp_node_leaf*)node)->dflt);
350 break;
351 case LYS_LEAFLIST:
352 FREE_ARRAY(ctx, ((struct lysp_node_leaflist*)node)->musts, lysp_restr_free);
353 lysp_type_free(ctx, &((struct lysp_node_leaflist*)node)->type);
354 FREE_STRING(ctx, ((struct lysp_node_leaflist*)node)->units);
355 FREE_STRINGS(ctx, ((struct lysp_node_leaflist*)node)->dflts);
356 break;
357 case LYS_LIST:
358 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->musts, lysp_restr_free);
359 FREE_STRING(ctx, ((struct lysp_node_list*)node)->key);
360 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->typedefs, lysp_tpdf_free);
361 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->groupings, lysp_grp_free);
362 LY_LIST_FOR_SAFE(((struct lysp_node_list*)node)->child, next, child) {
363 lysp_node_free(ctx, child);
364 }
365 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->actions, lysp_action_free);
366 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->notifs, lysp_notif_free);
367 FREE_STRINGS(ctx, ((struct lysp_node_list*)node)->uniques);
368 break;
369 case LYS_CHOICE:
370 LY_LIST_FOR_SAFE(((struct lysp_node_choice*)node)->child, next, child) {
371 lysp_node_free(ctx, child);
372 }
373 FREE_STRING(ctx, ((struct lysp_node_choice*)node)->dflt);
374 break;
375 case LYS_CASE:
376 LY_LIST_FOR_SAFE(((struct lysp_node_case*)node)->child, next, child) {
377 lysp_node_free(ctx, child);
378 }
379 break;
380 case LYS_ANYDATA:
381 case LYS_ANYXML:
382 FREE_ARRAY(ctx, ((struct lysp_node_anydata*)node)->musts, lysp_restr_free);
383 break;
384 case LYS_USES:
385 FREE_ARRAY(ctx, ((struct lysp_node_uses*)node)->refines, lysp_refine_free);
386 FREE_ARRAY(ctx, ((struct lysp_node_uses*)node)->augments, lysp_augment_free);
387 break;
388 default:
389 LOGINT(ctx);
390 }
391
392 free(node);
393}
394
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100395void
396lysp_submodule_free(struct ly_ctx *ctx, struct lysp_submodule *submod)
397{
398 struct lysp_node *node, *next;
399
Radek Krejci40544fa2019-01-11 09:38:37 +0100400 if (!submod) {
401 return;
402 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100403
404 FREE_ARRAY(ctx, submod->imports, lysp_import_free);
405 FREE_ARRAY(ctx, submod->includes, lysp_include_free);
406
407 FREE_ARRAY(ctx, submod->revs, lysp_revision_free);
408 FREE_ARRAY(ctx, submod->extensions, lysp_ext_free);
409 FREE_ARRAY(ctx, submod->features, lysp_feature_free);
410 FREE_ARRAY(ctx, submod->identities, lysp_ident_free);
411 FREE_ARRAY(ctx, submod->typedefs, lysp_tpdf_free);
412 FREE_ARRAY(ctx, submod->groupings, lysp_grp_free);
413 LY_LIST_FOR_SAFE(submod->data, next, node) {
414 lysp_node_free(ctx, node);
415 }
416 FREE_ARRAY(ctx, submod->augments, lysp_augment_free);
417 FREE_ARRAY(ctx, submod->rpcs, lysp_action_free);
418 FREE_ARRAY(ctx, submod->notifs, lysp_notif_free);
419 FREE_ARRAY(ctx, submod->deviations, lysp_deviation_free);
420 FREE_ARRAY(ctx, submod->exts, lysp_ext_instance_free);
421
422 FREE_STRING(ctx, submod->belongsto);
423 FREE_STRING(ctx, submod->name);
424 FREE_STRING(ctx, submod->filepath);
425 FREE_STRING(ctx, submod->prefix);
426 FREE_STRING(ctx, submod->org);
427 FREE_STRING(ctx, submod->contact);
428 FREE_STRING(ctx, submod->dsc);
429 FREE_STRING(ctx, submod->ref);
430
431 free(submod);
432}
433
Radek Krejci19a96102018-11-15 13:38:09 +0100434API void
435lysp_module_free(struct lysp_module *module)
436{
437 struct ly_ctx *ctx;
438 struct lysp_node *node, *next;
439
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100440 if (!module) {
441 return;
442 }
443 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100444
445 FREE_ARRAY(ctx, module->imports, lysp_import_free);
446 FREE_ARRAY(ctx, module->includes, lysp_include_free);
447
Radek Krejci19a96102018-11-15 13:38:09 +0100448 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
449 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
450 FREE_ARRAY(ctx, module->features, lysp_feature_free);
451 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
452 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
453 FREE_ARRAY(ctx, module->groupings, lysp_grp_free);
454 LY_LIST_FOR_SAFE(module->data, next, node) {
455 lysp_node_free(ctx, node);
456 }
457 FREE_ARRAY(ctx, module->augments, lysp_augment_free);
458 FREE_ARRAY(ctx, module->rpcs, lysp_action_free);
459 FREE_ARRAY(ctx, module->notifs, lysp_notif_free);
460 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
461 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
462
463 free(module);
464}
465
Radek Krejci0af46292019-01-11 16:02:31 +0100466void
Radek Krejci19a96102018-11-15 13:38:09 +0100467lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
468{
469 FREE_STRING(ctx, ext->argument);
470 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
471}
472
Radek Krejci0af46292019-01-11 16:02:31 +0100473void
Radek Krejci19a96102018-11-15 13:38:09 +0100474lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
475{
476 LY_ARRAY_FREE(iff->features);
477 free(iff->expr);
478}
479
480static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100481lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100482{
Radek Krejci00b874b2019-02-12 10:54:50 +0100483 if (--(*w)->refcount) {
484 return;
485 }
486 lyxp_expr_free(ctx, (*w)->cond);
487 FREE_STRING(ctx, (*w)->dsc);
488 FREE_STRING(ctx, (*w)->ref);
489 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
490 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100491}
492
Radek Krejciccd20f12019-02-15 14:12:27 +0100493void
Radek Krejci58d171e2018-11-23 13:50:55 +0100494lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
495{
496 lyxp_expr_free(ctx, must->cond);
497 FREE_STRING(ctx, must->emsg);
498 FREE_STRING(ctx, must->eapptag);
Radek Krejcic8b31002019-01-08 10:24:45 +0100499 FREE_STRING(ctx, must->dsc);
500 FREE_STRING(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100501 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
502}
503
504static void
Radek Krejci19a96102018-11-15 13:38:09 +0100505lysc_import_free(struct ly_ctx *ctx, struct lysc_import *import)
506{
507 /* imported module is freed directly from the context's list */
508 FREE_STRING(ctx, import->prefix);
509 FREE_ARRAY(ctx, import->exts, lysc_ext_instance_free);
510}
511
512static void
513lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
514{
515 FREE_STRING(ctx, ident->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100516 FREE_STRING(ctx, ident->dsc);
517 FREE_STRING(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100518 FREE_ARRAY(ctx, ident->iffeatures, lysc_iffeature_free);
519 LY_ARRAY_FREE(ident->derived);
520 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
521}
522
523static void
524lysc_feature_free(struct ly_ctx *ctx, struct lysc_feature *feat)
525{
526 FREE_STRING(ctx, feat->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100527 FREE_STRING(ctx, feat->dsc);
528 FREE_STRING(ctx, feat->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100529 FREE_ARRAY(ctx, feat->iffeatures, lysc_iffeature_free);
530 LY_ARRAY_FREE(feat->depfeatures);
531 FREE_ARRAY(ctx, feat->exts, lysc_ext_instance_free);
532}
533
534static void
535lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
536{
537 LY_ARRAY_FREE(range->parts);
538 FREE_STRING(ctx, range->eapptag);
539 FREE_STRING(ctx, range->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100540 FREE_STRING(ctx, range->dsc);
541 FREE_STRING(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100542 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
543}
544
545static void
546lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
547{
548 if (--(*pattern)->refcount) {
549 return;
550 }
Radek Krejci54579462019-04-30 12:47:06 +0200551 pcre2_code_free((*pattern)->code);
552 FREE_STRING(ctx, (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +0100553 FREE_STRING(ctx, (*pattern)->eapptag);
554 FREE_STRING(ctx, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100555 FREE_STRING(ctx, (*pattern)->dsc);
556 FREE_STRING(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100557 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
558 free(*pattern);
559}
560
561static void
Radek Krejci693262f2019-04-29 15:23:20 +0200562lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100563{
564 FREE_STRING(ctx, item->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100565 FREE_STRING(ctx, item->dsc);
566 FREE_STRING(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100567 FREE_ARRAY(ctx, item->iffeatures, lysc_iffeature_free);
568 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
569}
570
Radek Krejcia3045382018-11-22 14:30:31 +0100571static void
572lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
573{
574 lysc_type_free(ctx, *type);
575}
Radek Krejcicdfecd92018-11-26 11:27:32 +0100576void
Radek Krejci19a96102018-11-15 13:38:09 +0100577lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
578{
579 if (--type->refcount) {
580 return;
581 }
582 switch(type->basetype) {
583 case LY_TYPE_BINARY:
584 FREE_MEMBER(ctx, ((struct lysc_type_bin*)type)->length, lysc_range_free);
585 break;
586 case LY_TYPE_BITS:
Radek Krejci693262f2019-04-29 15:23:20 +0200587 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 +0100588 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100589 case LY_TYPE_DEC64:
590 FREE_MEMBER(ctx, ((struct lysc_type_dec*)type)->range, lysc_range_free);
591 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100592 case LY_TYPE_STRING:
593 FREE_MEMBER(ctx, ((struct lysc_type_str*)type)->length, lysc_range_free);
594 FREE_ARRAY(ctx, ((struct lysc_type_str*)type)->patterns, lysc_pattern_free);
595 break;
596 case LY_TYPE_ENUM:
597 FREE_ARRAY(ctx, ((struct lysc_type_enum*)type)->enums, lysc_enum_item_free);
598 break;
599 case LY_TYPE_INT8:
600 case LY_TYPE_UINT8:
601 case LY_TYPE_INT16:
602 case LY_TYPE_UINT16:
603 case LY_TYPE_INT32:
604 case LY_TYPE_UINT32:
605 case LY_TYPE_INT64:
606 case LY_TYPE_UINT64:
607 FREE_MEMBER(ctx, ((struct lysc_type_num*)type)->range, lysc_range_free);
608 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100609 case LY_TYPE_IDENT:
610 LY_ARRAY_FREE(((struct lysc_type_identityref*)type)->bases);
611 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100612 case LY_TYPE_UNION:
613 FREE_ARRAY(ctx, ((struct lysc_type_union*)type)->types, lysc_type2_free);
614 break;
615 case LY_TYPE_LEAFREF:
616 FREE_STRING(ctx, ((struct lysc_type_leafref*)type)->path);
617 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100618 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100619 case LY_TYPE_BOOL:
620 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100621 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100622 /* nothing to do */
623 break;
624 }
625 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejci01342af2019-01-03 15:18:08 +0100626 FREE_STRING(ctx, type->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +0100627 free(type);
628}
629
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100630void
Radek Krejcif538ce52019-03-05 10:46:14 +0100631lysc_action_inout_free(struct ly_ctx *ctx, struct lysc_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100632{
633 struct lysc_node *child, *child_next;
634
Radek Krejcif538ce52019-03-05 10:46:14 +0100635 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
636 LY_LIST_FOR_SAFE(inout->data, child_next, child) {
637 lysc_node_free(ctx, child);
638 }
639}
640
641void
642lysc_action_free(struct ly_ctx *ctx, struct lysc_action *action)
643{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100644 FREE_STRING(ctx, action->name);
645 FREE_STRING(ctx, action->dsc);
646 FREE_STRING(ctx, action->ref);
647 FREE_ARRAY(ctx, action->iffeatures, lysc_iffeature_free);
648 FREE_ARRAY(ctx, action->exts, lysc_ext_instance_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200649 FREE_ARRAY(ctx, action->input_exts, lysc_ext_instance_free);
Radek Krejcif538ce52019-03-05 10:46:14 +0100650 lysc_action_inout_free(ctx, &action->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200651 FREE_ARRAY(ctx, action->output_exts, lysc_ext_instance_free);
Radek Krejcif538ce52019-03-05 10:46:14 +0100652 lysc_action_inout_free(ctx, &action->output);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100653}
654
Radek Krejcifc11bd72019-04-11 16:00:05 +0200655void
656lysc_notif_free(struct ly_ctx *ctx, struct lysc_notif *notif)
657{
658 struct lysc_node *child, *child_next;
659
660 FREE_STRING(ctx, notif->name);
661 FREE_STRING(ctx, notif->dsc);
662 FREE_STRING(ctx, notif->ref);
663 FREE_ARRAY(ctx, notif->iffeatures, lysc_iffeature_free);
664 FREE_ARRAY(ctx, notif->exts, lysc_ext_instance_free);
665 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
666 LY_LIST_FOR_SAFE(notif->data, child_next, child) {
667 lysc_node_free(ctx, child);
668 }
669}
670
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200671void
Radek Krejci19a96102018-11-15 13:38:09 +0100672lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
673{
674 struct lysc_node *child, *child_next;
675
676 LY_LIST_FOR_SAFE(node->child, child_next, child) {
677 lysc_node_free(ctx, child);
678 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100679 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100680 FREE_ARRAY(ctx, node->actions, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200681 FREE_ARRAY(ctx, node->notifs, lysc_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100682}
683
684static void
685lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
686{
Radek Krejci58d171e2018-11-23 13:50:55 +0100687 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100688 if (node->type) {
689 lysc_type_free(ctx, node->type);
690 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100691 FREE_STRING(ctx, node->units);
692 FREE_STRING(ctx, node->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +0100693}
694
Radek Krejci42452ac2018-11-28 17:09:52 +0100695static void
696lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
697{
698 unsigned int u;
699
Radek Krejci42452ac2018-11-28 17:09:52 +0100700 FREE_ARRAY(ctx, node->musts, lysc_must_free);
701 if (node->type) {
702 lysc_type_free(ctx, node->type);
703 }
704 FREE_STRING(ctx, node->units);
705 LY_ARRAY_FOR(node->dflts, u) {
706 lydict_remove(ctx, node->dflts[u]);
707 }
708 LY_ARRAY_FREE(node->dflts);
709}
710
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100711static void
712lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
713{
714 unsigned int u;
715 struct lysc_node *child, *child_next;
716
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100717 LY_LIST_FOR_SAFE(node->child, child_next, child) {
718 lysc_node_free(ctx, child);
719 }
720 FREE_ARRAY(ctx, node->musts, lysc_must_free);
721
722 LY_ARRAY_FREE(node->keys);
723 LY_ARRAY_FOR(node->uniques, u) {
724 LY_ARRAY_FREE(node->uniques[u]);
725 }
726 LY_ARRAY_FREE(node->uniques);
727
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100728 FREE_ARRAY(ctx, node->actions, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200729 FREE_ARRAY(ctx, node->notifs, lysc_notif_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100730}
731
Radek Krejci056d0a82018-12-06 16:57:25 +0100732static void
733lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
734{
735 struct lysc_node *child, *child_next;
736
Radek Krejci056d0a82018-12-06 16:57:25 +0100737 if (node->cases) {
738 LY_LIST_FOR_SAFE(node->cases->child, child_next, child) {
739 lysc_node_free(ctx, child);
740 }
741 LY_LIST_FOR_SAFE((struct lysc_node*)node->cases, child_next, child) {
742 lysc_node_free(ctx, child);
743 }
744 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100745}
Radek Krejci056d0a82018-12-06 16:57:25 +0100746
Radek Krejci9800fb82018-12-13 14:26:23 +0100747static void
748lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
749{
750 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100751}
752
Radek Krejci19a96102018-11-15 13:38:09 +0100753void
754lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node)
755{
756 /* common part */
757 FREE_STRING(ctx, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +0100758 FREE_STRING(ctx, node->dsc);
759 FREE_STRING(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100760
761 /* nodetype-specific part */
762 switch(node->nodetype) {
763 case LYS_CONTAINER:
764 lysc_node_container_free(ctx, (struct lysc_node_container*)node);
765 break;
766 case LYS_LEAF:
767 lysc_node_leaf_free(ctx, (struct lysc_node_leaf*)node);
768 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100769 case LYS_LEAFLIST:
770 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist*)node);
771 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100772 case LYS_LIST:
773 lysc_node_list_free(ctx, (struct lysc_node_list*)node);
774 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100775 case LYS_CHOICE:
776 lysc_node_choice_free(ctx, (struct lysc_node_choice*)node);
777 break;
778 case LYS_CASE:
779 /* nothing specific */
780 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100781 case LYS_ANYDATA:
782 case LYS_ANYXML:
783 lysc_node_anydata_free(ctx, (struct lysc_node_anydata*)node);
784 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100785 default:
786 LOGINT(ctx);
787 }
788
Radek Krejci00b874b2019-02-12 10:54:50 +0100789 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100790 FREE_ARRAY(ctx, node->iffeatures, lysc_iffeature_free);
791 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100792 free(node);
793}
794
795static void
796lysc_module_free_(struct lysc_module *module)
797{
798 struct ly_ctx *ctx;
799 struct lysc_node *node, *node_next;
800
801 LY_CHECK_ARG_RET(NULL, module,);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100802 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100803
Radek Krejci19a96102018-11-15 13:38:09 +0100804 FREE_ARRAY(ctx, module->imports, lysc_import_free);
805 FREE_ARRAY(ctx, module->features, lysc_feature_free);
806 FREE_ARRAY(ctx, module->identities, lysc_ident_free);
807
808 LY_LIST_FOR_SAFE(module->data, node_next, node) {
809 lysc_node_free(ctx, node);
810 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100811 FREE_ARRAY(ctx, module->rpcs, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200812 FREE_ARRAY(ctx, module->notifs, lysc_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100813
814 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
815
816 free(module);
817}
818
819void
820lysc_module_free(struct lysc_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
821{
Radek Krejci9b042892019-02-13 14:28:44 +0100822 /* TODO use the destructor, this just suppress warning about unused parameter */
823 (void) private_destructor;
824
Radek Krejci19a96102018-11-15 13:38:09 +0100825 if (module) {
826 lysc_module_free_(module);
827 }
828}
829
830void
831lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
832{
833 if (!module) {
834 return;
835 }
836
837 lysc_module_free(module->compiled, private_destructor);
Radek Krejci0af46292019-01-11 16:02:31 +0100838 FREE_ARRAY(module->ctx, module->off_features, lysc_feature_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100839 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100840
841 FREE_STRING(module->ctx, module->name);
Radek Krejci0af46292019-01-11 16:02:31 +0100842 FREE_STRING(module->ctx, module->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100843 FREE_STRING(module->ctx, module->ns);
844 FREE_STRING(module->ctx, module->prefix);
845 FREE_STRING(module->ctx, module->filepath);
846 FREE_STRING(module->ctx, module->org);
847 FREE_STRING(module->ctx, module->contact);
848 FREE_STRING(module->ctx, module->dsc);
849 FREE_STRING(module->ctx, module->ref);
850
Radek Krejci19a96102018-11-15 13:38:09 +0100851 free(module);
852}