Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_data_free.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Freeing functions for data tree structures |
| 5 | * |
| 6 | * Copyright (c) 2019 CESNET, z.s.p.o. |
| 7 | * |
| 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 Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 15 | #include <assert.h> |
| 16 | #include <stdlib.h> |
| 17 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 18 | #include "common.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 19 | #include "dict.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | #include "hash_table.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 21 | #include "log.h" |
Radek Krejci | 849a62a | 2019-05-22 15:29:05 +0200 | [diff] [blame] | 22 | #include "plugins_types.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 23 | #include "tree.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 24 | #include "tree_data.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 25 | #include "tree_data_internal.h" |
| 26 | #include "tree_schema.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 27 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 28 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 29 | lyd_free_meta(struct lyd_meta *meta, ly_bool siblings) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 30 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 31 | struct lyd_meta *iter; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 32 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 33 | if (!meta) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 34 | return; |
| 35 | } |
| 36 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 37 | if (meta->parent) { |
| 38 | if (meta->parent->meta == meta) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 39 | if (siblings) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 40 | meta->parent->meta = NULL; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 41 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 42 | meta->parent->meta = meta->next; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 43 | } |
| 44 | } else { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 45 | for (iter = meta->parent->meta; iter->next != meta; iter = iter->next) {} |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 46 | if (iter->next) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 47 | if (siblings) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 48 | iter->next = NULL; |
| 49 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 50 | iter->next = meta->next; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 56 | if (!siblings) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 57 | meta->next = NULL; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 58 | } |
| 59 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 60 | for (iter = meta; iter; ) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 61 | meta = iter; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 62 | iter = iter->next; |
| 63 | |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 64 | lydict_remove(meta->annotation->module->ctx, meta->name); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 65 | meta->value.realtype->plugin->free(meta->annotation->module->ctx, &meta->value); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 66 | free(meta); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 70 | API void |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 71 | lyd_free_meta_single(struct lyd_meta *meta) |
| 72 | { |
| 73 | lyd_free_meta(meta, 0); |
| 74 | } |
| 75 | |
| 76 | API void |
| 77 | lyd_free_meta_siblings(struct lyd_meta *meta) |
| 78 | { |
| 79 | lyd_free_meta(meta, 1); |
| 80 | } |
| 81 | |
| 82 | static void |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 83 | lyd_free_attr(const struct ly_ctx *ctx, struct lyd_attr *attr, ly_bool siblings) |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 84 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 85 | struct lyd_attr *iter; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 86 | |
| 87 | LY_CHECK_ARG_RET(NULL, ctx, ); |
| 88 | if (!attr) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | if (attr->parent) { |
| 93 | if (attr->parent->attr == attr) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 94 | if (siblings) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 95 | attr->parent->attr = NULL; |
| 96 | } else { |
| 97 | attr->parent->attr = attr->next; |
| 98 | } |
| 99 | } else { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 100 | for (iter = attr->parent->attr; iter->next != attr; iter = iter->next) {} |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 101 | if (iter->next) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 102 | if (siblings) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 103 | iter->next = NULL; |
| 104 | } else { |
| 105 | iter->next = attr->next; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 111 | if (!siblings) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 112 | attr->next = NULL; |
| 113 | } |
| 114 | |
| 115 | for (iter = attr; iter; ) { |
| 116 | attr = iter; |
| 117 | iter = iter->next; |
| 118 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 119 | ly_free_prefix_data(attr->format, attr->val_prefix_data); |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 120 | lydict_remove(ctx, attr->name.name); |
| 121 | lydict_remove(ctx, attr->name.prefix); |
| 122 | lydict_remove(ctx, attr->name.module_ns); |
| 123 | lydict_remove(ctx, attr->value); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 124 | free(attr); |
| 125 | } |
| 126 | } |
| 127 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 128 | API void |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 129 | lyd_free_attr_single(const struct ly_ctx *ctx, struct lyd_attr *attr) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 130 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 131 | lyd_free_attr(ctx, attr, 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | API void |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 135 | lyd_free_attr_siblings(const struct ly_ctx *ctx, struct lyd_attr *attr) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 136 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 137 | lyd_free_attr(ctx, attr, 1); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 140 | /** |
| 141 | * @brief Free Data (sub)tree. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 142 | * @param[in] node Data node to be freed. |
| 143 | * @param[in] top Recursion flag to unlink the root of the subtree being freed. |
| 144 | */ |
| 145 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 146 | lyd_free_subtree(struct lyd_node *node, ly_bool top) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 147 | { |
| 148 | struct lyd_node *iter, *next; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 149 | struct lyd_node_opaq *opaq = NULL; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 150 | |
| 151 | assert(node); |
| 152 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 153 | if (!node->schema) { |
| 154 | opaq = (struct lyd_node_opaq *)node; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 155 | |
| 156 | /* free the children */ |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 157 | LY_LIST_FOR_SAFE(lyd_child(node), next, iter) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 158 | lyd_free_subtree(iter, 0); |
| 159 | } |
| 160 | |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 161 | lydict_remove(LYD_CTX(opaq), opaq->name.name); |
| 162 | lydict_remove(LYD_CTX(opaq), opaq->name.prefix); |
| 163 | lydict_remove(LYD_CTX(opaq), opaq->name.module_ns); |
| 164 | lydict_remove(LYD_CTX(opaq), opaq->value); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 165 | ly_free_prefix_data(opaq->format, opaq->val_prefix_data); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 166 | } else if (node->schema->nodetype & LYD_NODE_INNER) { |
| 167 | /* remove children hash table in case of inner data node */ |
| 168 | lyht_free(((struct lyd_node_inner *)node)->children_ht); |
| 169 | ((struct lyd_node_inner *)node)->children_ht = NULL; |
| 170 | |
| 171 | /* free the children */ |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 172 | LY_LIST_FOR_SAFE(lyd_child(node), next, iter) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 173 | lyd_free_subtree(iter, 0); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 174 | } |
| 175 | } else if (node->schema->nodetype & LYD_NODE_ANY) { |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 176 | /* only frees the value this way */ |
| 177 | lyd_any_copy_value(node, NULL, 0); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 178 | } else if (node->schema->nodetype & LYD_NODE_TERM) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 179 | ((struct lysc_node_leaf *)node->schema)->type->plugin->free(LYD_CTX(node), &((struct lyd_node_term *)node)->value); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 180 | } |
| 181 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 182 | if (!node->schema) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 183 | lyd_free_attr_siblings(LYD_CTX(node), opaq->attr); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 184 | } else { |
| 185 | /* free the node's metadata */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 186 | lyd_free_meta_siblings(node->meta); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 187 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 188 | |
| 189 | /* unlink only the nodes from the first level, nodes in subtree are freed all, so no unlink is needed */ |
| 190 | if (top) { |
| 191 | lyd_unlink_tree(node); |
| 192 | } |
| 193 | |
| 194 | free(node); |
| 195 | } |
| 196 | |
| 197 | API void |
| 198 | lyd_free_tree(struct lyd_node *node) |
| 199 | { |
| 200 | if (!node) { |
| 201 | return; |
| 202 | } |
| 203 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 204 | lyd_free_subtree(node, 1); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 207 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 208 | lyd_free_(struct lyd_node *node, ly_bool top) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 209 | { |
| 210 | struct lyd_node *iter, *next; |
| 211 | |
| 212 | if (!node) { |
| 213 | return; |
| 214 | } |
| 215 | |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 216 | /* get the first (top-level) sibling */ |
| 217 | if (top) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 218 | for ( ; node->parent; node = lyd_parent(node)) {} |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 219 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 220 | while (node->prev->next) { |
| 221 | node = node->prev; |
| 222 | } |
| 223 | |
| 224 | LY_LIST_FOR_SAFE(node, next, iter) { |
| 225 | /* in case of the top-level nodes (node->parent is NULL), no unlinking needed */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 226 | lyd_free_subtree(iter, iter->parent ? 1 : 0); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 227 | } |
| 228 | } |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 229 | |
| 230 | API void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 231 | lyd_free_siblings(struct lyd_node *node) |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 232 | { |
| 233 | lyd_free_(node, 0); |
| 234 | } |
| 235 | |
| 236 | API void |
| 237 | lyd_free_all(struct lyd_node *node) |
| 238 | { |
| 239 | lyd_free_(node, 1); |
| 240 | } |