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