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