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