libyang  6.3.1
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 "dict.h"
24 #include "ly_common.h"
25 #include "plugins_internal.h"
26 #include "tree_data_sorted.h"
27 
28 static void lyplg_type_free_lyds(const struct ly_ctx *ctx, struct lyd_value *value);
29 
30 static void
31 lyplg_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 
38 static LY_ERR
39 lyplg_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. */
49  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
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 
69 cleanup:
70  if (ret) {
71  lyplg_type_free_lyds(ctx, storage);
72  }
73 
74  return ret;
75 }
76 
77 static void
78 lyplg_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 
92 static LY_ERR
93 lyplg_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 
104 static LY_ERR
105 lyplg_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 
111 static int
112 lyplg_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 
118 static const void *
119 lyplg_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 };
struct lysc_type * realtype
Definition: tree_data.h:615
Compiled YANG data node.
Definition: tree_schema.h:1430
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:139
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:878
lyds_free_tree(val->rbt)
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:255
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:29
libyang dictionary
Definition: log.h:269
#define LYPLG_TYPE_STORE_DYNAMIC
struct rb_node * rbt
Definition: tree_data.h:799
LYPLG_TYPE_VAL_INLINE_DESTROY(val)
Definition: log.h:257
YANG data representation.
Definition: tree_data.h:611
const char * _canonical
Definition: tree_data.h:612
Libyang full error structure.
Definition: log.h:301
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:654
Definition: log.h:263
const char * module
Special lyd_value structure for lyds tree value.
Definition: tree_data.h:798
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: utils.h:93
#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)