libyang 5.4.9
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
15
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 "libyang.h"
24#include "ly_common.h"
25#include "tree_data_sorted.h"
26
27static void lyplg_type_free_lyds(const struct ly_ctx *ctx, struct lyd_value *value);
28
29static void
30lyplg_type_lyb_size_lyds(const struct lysc_type *UNUSED(type), enum lyplg_lyb_size_type *size_type,
31 uint64_t *fixed_size_bits)
32{
33 *size_type = LYPLG_LYB_SIZE_FIXED_BITS;
34 *fixed_size_bits = 0;
35}
36
37static LY_ERR
38lyplg_type_store_lyds(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
39 uint64_t UNUSED(value_size_bits), uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data),
40 uint32_t UNUSED(hints), const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage,
41 struct lys_glob_unres *UNUSED(unres), struct ly_err_item **UNUSED(err))
42{
43 LY_ERR ret = LY_SUCCESS;
44 struct rb_node *rbt = NULL;
45 struct lyd_value_lyds_tree *val;
46
47 /* Prepare value memory. */
49 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
50
51 if (format == LY_VALUE_CANON) {
52 /* The canonical value for lyds_tree type is the empty string, so @p value is like NULL. */
53 memset(storage->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE);
54 storage->realtype = type;
55 return LY_SUCCESS;
56 } else if ((format != LY_VALUE_LYB) || (options & LYPLG_TYPE_STORE_DYNAMIC)) {
57 return LY_EVALID;
58 }
59
60 /* Create a new Red-black tree. The insertion of additional data nodes should be done via lyds_insert(). */
61 ret = lyds_create_node((struct lyd_node *)value, &rbt);
62 LY_CHECK_GOTO(ret, cleanup);
63
64 /* Set the root of the Red-black tree. */
65 storage->realtype = type;
66 val->rbt = rbt;
67
68cleanup:
69 if (ret) {
70 lyplg_type_free_lyds(ctx, storage);
71 }
72
73 return ret;
74}
75
76static void
77lyplg_type_free_lyds(const struct ly_ctx *UNUSED(ctx), struct lyd_value *value)
78{
79 struct lyd_value_lyds_tree *val = NULL;
80
81 /* The canonical value is not used at all. */
82 assert(!value->_canonical);
83 LYD_VALUE_GET(value, val);
84
85 /* Release Red-black tree. */
86 lyds_free_tree(val->rbt);
88 memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE);
89}
90
91static LY_ERR
92lyplg_type_dupl_lyds(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *original, struct lyd_value *dup)
93{
94 /* The duplicate is not created here, but at the caller, which creates a duplicate lyds tree
95 * implicitly by inserting duplicate nodes into the data tree.
96 */
97 memset(dup, 0, sizeof *dup);
98 dup->realtype = original->realtype;
99
100 return LY_SUCCESS;
101}
102
103static LY_ERR
104lyplg_type_compare_lyds(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *UNUSED(val1),
105 const struct lyd_value *UNUSED(val2))
106{
107 return LY_ENOT;
108}
109
110static int
111lyplg_type_sort_lyds(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *UNUSED(val1),
112 const struct lyd_value *UNUSED(val2))
113{
114 return 0;
115}
116
117static const void *
118lyplg_type_print_lyds(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *UNUSED(value),
119 LY_VALUE_FORMAT UNUSED(format), void *UNUSED(prefix_data), ly_bool *dynamic, uint64_t *value_size_bits)
120{
121 if (dynamic) {
122 *dynamic = 0;
123 }
124 if (value_size_bits) {
125 *value_size_bits = 0;
126 }
127
128 return "";
129}
130
139 {
140 .module = "yang",
141 .revision = NULL,
142 .name = "lyds_tree",
143
144 .plugin.id = "ly2 lyds_tree",
145 .plugin.lyb_size = lyplg_type_lyb_size_lyds,
146 .plugin.store = lyplg_type_store_lyds,
147 .plugin.validate_value = NULL,
148 .plugin.validate_tree = NULL,
149 .plugin.compare = lyplg_type_compare_lyds,
150 .plugin.sort = lyplg_type_sort_lyds,
151 .plugin.print = lyplg_type_print_lyds,
152 .plugin.duplicate = lyplg_type_dupl_lyds,
153 .plugin.free = lyplg_type_free_lyds,
154 },
155 {0}
156};
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:297
#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.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
@ LY_VALUE_CANON
Definition tree.h:235
@ LY_VALUE_LYB
Definition tree.h:240
The main libyang public header.
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:138
API for (user) types plugins.
struct rb_node * rbt
Definition tree_data.h:711
const struct lysc_type * realtype
Definition tree_data.h:527
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition tree_data.h:566
const char * _canonical
Definition tree_data.h:524
Generic structure for a data node.
Definition tree_data.h:783
YANG data representation.
Definition tree_data.h:523
Special lyd_value structure for lyds tree value.
Definition tree_data.h:710