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