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