libyang  4.0.2
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "libyang.h"
24 #include "ly_common.h"
25 #include "tree_data_sorted.h"
26 
27 static void lyplg_type_free_lyds(const struct ly_ctx *ctx, struct lyd_value *value);
28 
29 static void
30 lyplg_type_lyb_size_lyds(const struct lysc_type *UNUSED(type), enum lyplg_lyb_size_type *size_type,
31  uint32_t *fixed_size_bits)
32 {
33  *size_type = LYPLG_LYB_SIZE_FIXED_BITS;
34  *fixed_size_bits = 0;
35 }
36 
37 static LY_ERR
38 lyplg_type_store_lyds(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
39  uint32_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), const struct lysc_ext_instance *UNUSED(top_ext),
41  struct lyd_value *storage, 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. */
48  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
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 
68 cleanup:
69  if (ret) {
70  lyplg_type_free_lyds(ctx, storage);
71  }
72 
73  return ret;
74 }
75 
76 static void
77 lyplg_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 
91 static LY_ERR
92 lyplg_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 
103 static LY_ERR
104 lyplg_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 
110 static int
111 lyplg_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 
117 static const void *
118 lyplg_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, uint32_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 };
struct lysc_type * realtype
Definition: tree_data.h:567
Compiled YANG data node.
Definition: tree_schema.h:1434
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
struct lyplg_type_record plugins_lyds_tree[]
Plugin information for lyds_tree type implementation.
Definition: lyds_tree.c:138
lyplg_lyb_size_type
Type of the LYB size of a value of a particular type.
Generic structure for a data node.
Definition: tree_data.h:792
lyds_free_tree(val->rbt)
YANG extension compiled instance.
Definition: plugins_exts.h:436
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:240
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:28
Definition: log.h:254
#define LYPLG_TYPE_STORE_DYNAMIC
struct rb_node * rbt
Definition: tree_data.h:720
LYPLG_TYPE_VAL_INLINE_DESTROY(val)
Definition: log.h:242
The main libyang public header.
YANG data representation.
Definition: tree_data.h:563
const char * _canonical
Definition: tree_data.h:564
Libyang full error structure.
Definition: log.h:285
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:606
Definition: log.h:248
const char * module
Special lyd_value structure for lyds tree value.
Definition: tree_data.h:719
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:234
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
API for (user) types plugins.
libyang context handler.
assert(!value->_canonical)