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 |
| 29 | lyd_value_free_path(struct ly_ctx *ctx, struct lyd_value_path *path) |
| 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 LY_ERR |
| 46 | lyd_unlink_tree(struct lyd_node *node) |
| 47 | { |
| 48 | struct lyd_node *iter; |
| 49 | struct lyd_node **first_sibling; |
| 50 | |
| 51 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
| 52 | |
| 53 | if (node->parent) { |
| 54 | first_sibling = lyd_node_children_p((struct lyd_node*)node->parent); |
| 55 | } |
| 56 | |
| 57 | /* unlink from siblings */ |
| 58 | if (node->prev->next) { |
| 59 | node->prev->next = node->next; |
| 60 | } |
| 61 | if (node->next) { |
| 62 | node->next->prev = node->prev; |
| 63 | } else { |
| 64 | /* unlinking the last node */ |
| 65 | if (node->parent) { |
| 66 | iter = *first_sibling; |
| 67 | } else { |
| 68 | iter = node->prev; |
| 69 | while (iter->prev != node) { |
| 70 | iter = iter->prev; |
| 71 | } |
| 72 | } |
| 73 | /* update the "last" pointer from the first node */ |
| 74 | iter->prev = node->prev; |
| 75 | } |
| 76 | |
| 77 | /* unlink from parent */ |
| 78 | if (node->parent) { |
| 79 | if (*first_sibling == node) { |
| 80 | /* the node is the first child */ |
| 81 | *first_sibling = node->next; |
| 82 | } |
| 83 | lyd_unlink_hash(node); |
| 84 | node->parent = NULL; |
| 85 | } |
| 86 | |
| 87 | node->next = NULL; |
| 88 | node->prev = node; |
| 89 | |
| 90 | return EXIT_SUCCESS; |
| 91 | } |
| 92 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 93 | API void |
| 94 | lyd_free_attr(struct ly_ctx *ctx, struct lyd_attr *attr, int recursive) |
| 95 | { |
| 96 | struct lyd_attr *iter; |
| 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) { |
| 105 | if (recursive) { |
| 106 | attr->parent->attr = NULL; |
| 107 | } else { |
| 108 | attr->parent->attr = attr->next; |
| 109 | } |
| 110 | } else { |
| 111 | for (iter = attr->parent->attr; iter->next != attr; iter = iter->next); |
| 112 | if (iter->next) { |
| 113 | if (recursive) { |
| 114 | iter->next = NULL; |
| 115 | } else { |
| 116 | iter->next = attr->next; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (!recursive) { |
| 123 | attr->next = NULL; |
| 124 | } |
| 125 | |
| 126 | for(iter = attr; iter; ) { |
| 127 | attr = iter; |
| 128 | iter = iter->next; |
| 129 | |
| 130 | FREE_STRING(ctx, attr->name); |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 131 | /* TODO type->plugin->free(ctx, type, &attr->value); */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 132 | free(attr); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @brief Free Data (sub)tree. |
| 138 | * @param[in] ctx libyang context. |
| 139 | * @param[in] node Data node to be freed. |
| 140 | * @param[in] top Recursion flag to unlink the root of the subtree being freed. |
| 141 | */ |
| 142 | static void |
| 143 | lyd_free_subtree(struct ly_ctx *ctx, struct lyd_node *node, int top) |
| 144 | { |
| 145 | struct lyd_node *iter, *next; |
| 146 | struct lyd_node *children; |
| 147 | |
| 148 | assert(node); |
| 149 | |
| 150 | /* remove children hash table in case of inner data node */ |
| 151 | if (node->schema->nodetype & LYD_NODE_INNER) { |
| 152 | lyht_free(((struct lyd_node_inner*)node)->children_ht); |
| 153 | ((struct lyd_node_inner*)node)->children_ht = NULL; |
| 154 | |
| 155 | /* free the children */ |
| 156 | children = (struct lyd_node*)lyd_node_children(node); |
| 157 | LY_LIST_FOR_SAFE(children, next, iter) { |
| 158 | lyd_free_subtree(ctx, iter, 0); |
| 159 | } |
| 160 | } else if (node->schema->nodetype & LYD_NODE_ANY) { |
| 161 | /* TODO anydata */ |
| 162 | #if 0 |
| 163 | switch (((struct lyd_node_anydata *)node)->value_type) { |
| 164 | case LYD_ANYDATA_CONSTSTRING: |
| 165 | case LYD_ANYDATA_SXML: |
| 166 | case LYD_ANYDATA_JSON: |
| 167 | FREE_STRING(node->schema->module->ctx, ((struct lyd_node_anydata *)node)->value.str); |
| 168 | break; |
| 169 | case LYD_ANYDATA_DATATREE: |
| 170 | lyd_free_withsiblings(((struct lyd_node_anydata *)node)->value.tree); |
| 171 | break; |
| 172 | case LYD_ANYDATA_XML: |
| 173 | lyxml_free_withsiblings(node->schema->module->ctx, ((struct lyd_node_anydata *)node)->value.xml); |
| 174 | break; |
| 175 | case LYD_ANYDATA_LYB: |
| 176 | free(((struct lyd_node_anydata *)node)->value.mem); |
| 177 | break; |
| 178 | case LYD_ANYDATA_STRING: |
| 179 | case LYD_ANYDATA_SXMLD: |
| 180 | case LYD_ANYDATA_JSOND: |
| 181 | case LYD_ANYDATA_LYBD: |
| 182 | /* dynamic strings are used only as input parameters */ |
| 183 | assert(0); |
| 184 | break; |
| 185 | } |
| 186 | #endif |
| 187 | } else if (node->schema->nodetype & LYD_NODE_TERM) { |
Radek Krejci | 62903c3 | 2019-07-15 14:42:05 +0200 | [diff] [blame^] | 188 | ((struct lysc_node_leaf*)node->schema)->type->plugin->free(ctx, &((struct lyd_node_term*)node)->value); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* free the node's attributes */ |
| 192 | lyd_free_attr(ctx, node->attr, 1); |
| 193 | |
| 194 | /* unlink only the nodes from the first level, nodes in subtree are freed all, so no unlink is needed */ |
| 195 | if (top) { |
| 196 | lyd_unlink_tree(node); |
| 197 | } |
| 198 | |
| 199 | free(node); |
| 200 | } |
| 201 | |
| 202 | API void |
| 203 | lyd_free_tree(struct lyd_node *node) |
| 204 | { |
| 205 | if (!node) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | lyd_free_subtree(node->schema->module->ctx, node, 1); |
| 210 | } |
| 211 | |
| 212 | API void |
| 213 | lyd_free_all(struct lyd_node *node) |
| 214 | { |
| 215 | struct lyd_node *iter, *next; |
| 216 | |
| 217 | if (!node) { |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | /* get the first top-level sibling */ |
| 222 | for (; node->parent; node = (struct lyd_node*)node->parent); |
| 223 | while (node->prev->next) { |
| 224 | node = node->prev; |
| 225 | } |
| 226 | |
| 227 | LY_LIST_FOR_SAFE(node, next, iter) { |
| 228 | /* in case of the top-level nodes (node->parent is NULL), no unlinking needed */ |
Radek Krejci | 3567630 | 2019-05-17 10:50:57 +0200 | [diff] [blame] | 229 | lyd_free_subtree(iter->schema->module->ctx, iter, iter->parent ? 1 : 0); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 230 | } |
| 231 | } |