libyang 6.4.3
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
lyds_tree.c
Go to the documentation of this file.
1
16#include "plugins_types.h"
17
18#include <assert.h> /* assert */
19#include <stddef.h> /* NULL */
20#include <string.h> /* memset */
21
22#include "compat.h"
23#include "dict.h"
24#include "ly_common.h"
25#include "plugins_internal.h"
26#include "tree_data_sorted.h"
27
28static void lyplg_type_free_lyds(const struct ly_ctx *ctx, struct lyd_value *value);
29
30static void
31lyplg_type_lyb_size_lyds(const struct lysc_type *UNUSED(type), enum lyplg_lyb_size_type *size_type,
32 uint64_t *fixed_size_bits)
33{
34 *size_type = LYPLG_LYB_SIZE_FIXED_BITS;
35 *fixed_size_bits = 0;
36}
37
38static LY_ERR
39lyplg_type_store_lyds(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
40 uint64_t UNUSED(value_size_bits), uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data),
41 uint32_t UNUSED(hints), const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage,
42 struct lys_glob_unres *UNUSED(unres), struct ly_err_item **UNUSED(err))
43{
44 LY_ERR ret = LY_SUCCESS;
45 struct rb_node *rbt = NULL;
46 struct lyd_value_lyds_tree *val;
47
48 /* Prepare value memory. */
50 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
51
52 if (format == LY_VALUE_CANON) {
53 /* The canonical value for lyds_tree type is the empty string, so @p value is like NULL. */
54 memset(storage->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE);
55 storage->realtype = type;
56 return LY_SUCCESS;
57 } else if ((format != LY_VALUE_LYB) || (options & LYPLG_TYPE_STORE_DYNAMIC)) {
58 return LY_EVALID;
59 }
60
61 /* Create a new Red-black tree. The insertion of additional data nodes should be done via lyds_insert(). */
62 ret = lyds_create_node((struct lyd_node *)value, &rbt);
63 LY_CHECK_GOTO(ret, cleanup);
64
65 /* Set the root of the Red-black tree. */
66 storage->realtype = type;
67 val->rbt = rbt;
68
69cleanup:
70 if (ret) {
71 lyplg_type_free_lyds(ctx, storage);
72 }
73
74 return ret;
75}
76
77static void
78lyplg_type_free_lyds(const struct ly_ctx *UNUSED(ctx), struct lyd_value *value)
79{
80 struct lyd_value_lyds_tree *val = NULL;
81
82 /* The canonical value is not used at all. */
83 assert(!value->_canonical);
84 LYD_VALUE_GET(value, val);
85
86 /* Release Red-black tree. */
87 lyds_free_tree(val->rbt);
89 memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE);
90}
91
92static LY_ERR
93lyplg_type_dupl_lyds(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *original, struct lyd_value *dup)
94{
95 /* The duplicate is not created here, but at the caller, which creates a duplicate lyds tree
96 * implicitly by inserting duplicate nodes into the data tree.
97 */
98 memset(dup, 0, sizeof *dup);
99 dup->realtype = original->realtype;
100
101 return LY_SUCCESS;
102}
103
104static LY_ERR
105lyplg_type_compare_lyds(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *UNUSED(val1),
106 const struct lyd_value *UNUSED(val2))
107{
108 return LY_ENOT;
109}
110
111static int
112lyplg_type_sort_lyds(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *UNUSED(val1),
113 const struct lyd_value *UNUSED(val2))
114{
115 return 0;
116}
117
118static const void *
119lyplg_type_print_lyds(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *UNUSED(value),
120 LY_VALUE_FORMAT UNUSED(format), void *UNUSED(prefix_data), ly_bool *dynamic, uint64_t *value_size_bits)
121{
122 if (dynamic) {
123 *dynamic = 0;
124 }
125 if (value_size_bits) {
126 *value_size_bits = 0;
127 }
128
129 return "";
130}
131
140 {
141 .module = "yang",
142 .revision = NULL,
143 .name = "lyds_tree",
144
145 .plugin.id = "ly2 lyds_tree",
146 .plugin.lyb_size = lyplg_type_lyb_size_lyds,
147 .plugin.store = lyplg_type_store_lyds,
148 .plugin.validate_value = NULL,
149 .plugin.validate_tree = NULL,
150 .plugin.compare = lyplg_type_compare_lyds,
151 .plugin.sort = lyplg_type_sort_lyds,
152 .plugin.print = lyplg_type_print_lyds,
153 .plugin.duplicate = lyplg_type_dupl_lyds,
154 .plugin.free = lyplg_type_free_lyds,
155 },
156 {0}
157};
libyang dictionary
libyang context handler.
LY_ERR
libyang's error codes returned by the libyang functions.
Definition log.h:252
@ LY_EMEM
Definition log.h:254
@ LY_ENOT
Definition log.h:266
@ LY_EVALID
Definition log.h:260
@ LY_SUCCESS
Definition log.h:253
Libyang full error structure.
Definition log.h:298
const char *const char * revision
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
lyplg_lyb_size_type
Type of the LYB size of a value of a particular type.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
@ LYPLG_LYB_SIZE_FIXED_BITS
#define LYPLG_TYPE_STORE_DYNAMIC
Compiled YANG data node.
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:36
const struct lyplg_type_record plugins_lyds_tree[]
Plugin information for lyds_tree type implementation.
Definition lyds_tree.c:139
API for (user) types plugins.
struct rb_node * rbt
Definition tree_data.h:798
const struct lysc_type * realtype
Definition tree_data.h:614
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition tree_data.h:653
const char * _canonical
Definition tree_data.h:611
Generic structure for a data node.
Definition tree_data.h:875
YANG data representation.
Definition tree_data.h:610
Special lyd_value structure for lyds tree value.
Definition tree_data.h:797
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition utils.h:93
@ LY_VALUE_CANON
Definition utils.h:94
@ LY_VALUE_LYB
Definition utils.h:100