From 545473073552cd4cf5e0fc839e7170d4678ef646 Mon Sep 17 00:00:00 2001
From: Michal Vasko <mvasko@cesnet.cz>
Date: Wed, 10 Dec 2025 09:52:13 +0100
Subject: [PATCH 1/6] plugins ext BUGFIX printed context align all sizes and
 mem
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Organization: Wires

Refs #2455

Signed-off-by: Mattias Walström <lazzer@gmail.com>
---
 src/plugins_exts.h              |  17 +++-
 src/plugins_exts/metadata.c     |   4 +-
 src/plugins_exts/schema_mount.c |  24 +++---
 src/plugins_exts/structure.c    |   8 +-
 src/printer_context.c           | 148 +++++++++++++++-----------------
 5 files changed, 100 insertions(+), 101 deletions(-)

diff --git a/src/plugins_exts.h b/src/plugins_exts.h
index f260ff787..e912fe908 100644
--- a/src/plugins_exts.h
+++ b/src/plugins_exts.h
@@ -953,13 +953,26 @@ LIBYANG_API_DECL void lyplg_ext_cfree_instance_substatements(const struct ly_ctx
  * compiled size
  */
 
+/**
+ * @brief Alignment of all the printed data, ensures all memory access is aligned to this number (B)
+ */
+#define LY_CTXP_MEM_ALIGN 8
+
+/**
+ * @brief Adjust data size to an aligned size to make sure the following data is aligned.
+ *
+ * @param[in,out] SIZE Data size that is adjusted.
+ */
+#define LY_CTXP_MEM_SIZE(SIZE) ((SIZE) + ((~(SIZE) + 1) & (LY_CTXP_MEM_ALIGN - 1)))
+
 /**
  * @brief Callback to return the size of the custom compiled structure and substmts array. If there are none, do not
  * define this callback.
  *
  * @param[in] ext Compiled extension structure.
  * @param[in,out] addr_ht Hash table with addresses of shared structures that were already accounted for, can be added to.
- * @return Total size of the custom compiled structures and the substmts array;
+ * @return Total size of the custom compiled structures and the substmts array. Always use ::LY_CTXP_MEM_SIZE when
+ * adding to the size for correct alignment.
  * @return -1 on error.
  */
 typedef int (*lyplg_ext_compiled_size_clb)(const struct lysc_ext_instance *ext, struct ly_ht *addr_ht);
@@ -984,7 +997,7 @@ LIBYANG_API_DECL int lyplg_ext_compiled_stmts_storage_size(const struct lysc_ext
  * @param[in] orig_ext Compiled extension structure to print.
  * @param[in,out] ext Printed extension structure to modify.
  * @param[in,out] mem Memory chunk of the size returned by ::lyplg_ext_compiled_size_clb() to print into, is moved
- * after all the printed data.
+ * after all the printed data. Always use ::LY_CTXP_MEM_SIZE when moving @p mem for correct alignment.
  * @param[in,out] ptr_set Set with pointers to set to printed addresses.
  * @param[in,out] addr_ht Hash table with pairs of addresses of shared structures to be printed and their printed
  * addresses, can be added to.
diff --git a/src/plugins_exts/metadata.c b/src/plugins_exts/metadata.c
index d5492839e..c73c8f9a8 100644
--- a/src/plugins_exts/metadata.c
+++ b/src/plugins_exts/metadata.c
@@ -220,7 +220,7 @@ annotation_compiled_size(const struct lysc_ext_instance *ext, struct ly_ht *addr
 {
     int size = 0;
 
-    size += sizeof(struct lysc_ext_metadata);
+    size += LY_CTXP_MEM_SIZE(sizeof(struct lysc_ext_metadata));
     size += lyplg_ext_compiled_stmts_storage_size(ext->substmts, addr_ht);
 
     return size;
@@ -233,7 +233,7 @@ annotation_compiled_print(const struct lysc_ext_instance *orig_ext, struct lysc_
     struct lysc_ext_metadata *ann_cdata;
 
     ann_cdata = ext->compiled = *mem;
-    *mem = (char *)*mem + sizeof *ann_cdata;
+    *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *ann_cdata);
     memset(ann_cdata, 0, sizeof *ann_cdata);
 
     ext->substmts[1].storage_p = (void **)&ann_cdata->units;
diff --git a/src/plugins_exts/schema_mount.c b/src/plugins_exts/schema_mount.c
index df736e5ad..3dd23f992 100644
--- a/src/plugins_exts/schema_mount.c
+++ b/src/plugins_exts/schema_mount.c
@@ -79,8 +79,6 @@ struct sprinter_tree_priv {
         lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EINT, "Internal error (%s:%d).", __FILE__, __LINE__); \
         return LY_EINT
 
-#define CTXP_MEM_SIZE(SIZE) ((SIZE) + ((~(SIZE) + 1) & (8 - 1)))
-
 /**
  * @brief Check if given mount point is unique among its siblings
  *
@@ -1468,7 +1466,7 @@ schema_mount_compiled_size(const struct lysc_ext_instance *ext, struct ly_ht *ad
         return 0;
     }
 
-    size += CTXP_MEM_SIZE(sizeof *sm_data);
+    size += LY_CTXP_MEM_SIZE(sizeof *sm_data);
 
     /* ht addr check, make sure the shared context is stored only once */
     hash = lyht_hash((const char *)&sm_data->shared, sizeof sm_data->shared);
@@ -1476,12 +1474,12 @@ schema_mount_compiled_size(const struct lysc_ext_instance *ext, struct ly_ht *ad
         return size;
     }
 
-    size += CTXP_MEM_SIZE(sizeof *sm_data->shared);
-    size += CTXP_MEM_SIZE(sm_data->shared->schema_count * sizeof *sm_data->shared->schemas);
+    size += LY_CTXP_MEM_SIZE(sizeof *sm_data->shared);
+    size += LY_CTXP_MEM_SIZE(sm_data->shared->schema_count * sizeof *sm_data->shared->schemas);
     for (i = 0; i < sm_data->shared->schema_count; ++i) {
-        size += CTXP_MEM_SIZE(ly_ctx_compiled_size(sm_data->shared->schemas[i].ctx));
-        size += CTXP_MEM_SIZE(strlen(sm_data->shared->schemas[i].mount_point) + 1);
-        size += CTXP_MEM_SIZE(strlen(sm_data->shared->schemas[i].content_id) + 1);
+        size += LY_CTXP_MEM_SIZE(ly_ctx_compiled_size(sm_data->shared->schemas[i].ctx));
+        size += LY_CTXP_MEM_SIZE(strlen(sm_data->shared->schemas[i].mount_point) + 1);
+        size += LY_CTXP_MEM_SIZE(strlen(sm_data->shared->schemas[i].content_id) + 1);
     }
 
     /* inlined contexts cannot be reused and will not be printed */
@@ -1501,7 +1499,7 @@ schema_mount_compiled_print(const struct lysc_ext_instance *orig_ext, struct lys
 
     /* sm_data */
     ext->compiled = sm_data = *mem;
-    *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *sm_data);
+    *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *sm_data);
 
     pthread_mutexattr_init(&attr);
     pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
@@ -1517,7 +1515,7 @@ schema_mount_compiled_print(const struct lysc_ext_instance *orig_ext, struct lys
 
     /* sm_data->shared */
     sm_data->shared = *mem;
-    *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *sm_data->shared);
+    *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *sm_data->shared);
 
     sm_data->shared->schema_count = orig_sm_data->shared->schema_count;
     sm_data->shared->ref_count = orig_sm_data->shared->ref_count;
@@ -1525,7 +1523,7 @@ schema_mount_compiled_print(const struct lysc_ext_instance *orig_ext, struct lys
     /* sm_data->shared->schemas */
     if (orig_sm_data->shared->schemas) {
         sm_data->shared->schemas = *mem;
-        *mem = (char *)*mem + CTXP_MEM_SIZE(sm_data->shared->schema_count * sizeof *sm_data->shared->schemas);
+        *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sm_data->shared->schema_count * sizeof *sm_data->shared->schemas);
 
         for (i = 0; i < sm_data->shared->schema_count; ++i) {
             /* ctx */
@@ -1538,12 +1536,12 @@ schema_mount_compiled_print(const struct lysc_ext_instance *orig_ext, struct lys
             /* mount_point */
             strcpy(*mem, orig_sm_data->shared->schemas[i].mount_point);
             sm_data->shared->schemas[i].mount_point = *mem;
-            *mem = (char *)*mem + CTXP_MEM_SIZE(strlen(sm_data->shared->schemas[i].mount_point) + 1);
+            *mem = (char *)*mem + LY_CTXP_MEM_SIZE(strlen(sm_data->shared->schemas[i].mount_point) + 1);
 
             /* content_id */
             strcpy(*mem, orig_sm_data->shared->schemas[i].content_id);
             sm_data->shared->schemas[i].content_id = *mem;
-            *mem = (char *)*mem + CTXP_MEM_SIZE(strlen(sm_data->shared->schemas[i].content_id) + 1);
+            *mem = (char *)*mem + LY_CTXP_MEM_SIZE(strlen(sm_data->shared->schemas[i].content_id) + 1);
         }
     }
 
diff --git a/src/plugins_exts/structure.c b/src/plugins_exts/structure.c
index b88c9a9a5..6309a2c30 100644
--- a/src/plugins_exts/structure.c
+++ b/src/plugins_exts/structure.c
@@ -329,9 +329,9 @@ structure_compiled_size(const struct lysc_ext_instance *ext, struct ly_ht *addr_
 
     struct_cdata = ext->compiled;
 
-    size += sizeof *struct_cdata;
+    size += LY_CTXP_MEM_SIZE(sizeof *struct_cdata);
     size += lyplg_ext_compiled_stmts_storage_size(ext->substmts, addr_ht);
-    size += sizeof *struct_cdata->top_cont;
+    size += LY_CTXP_MEM_SIZE(sizeof *struct_cdata->top_cont);
 
     return size;
 }
@@ -347,12 +347,12 @@ structure_compiled_print(const struct lysc_ext_instance *orig_ext, struct lysc_e
 
     /* ext structure */
     struct_cdata = ext->compiled = *mem;
-    *mem = (char *)*mem + sizeof *struct_cdata;
+    *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *struct_cdata);
     memset(struct_cdata, 0, sizeof *struct_cdata);
 
     /* top_cont */
     struct_cdata->top_cont = *mem;
-    *mem = (char *)*mem + sizeof *struct_cdata->top_cont;
+    *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *struct_cdata->top_cont);
     lyplg_ext_compiled_print_add_addr(addr_ht, orig_cdata->top_cont, struct_cdata->top_cont);
     memset(struct_cdata->top_cont, 0, sizeof *struct_cdata->top_cont);
 
diff --git a/src/printer_context.c b/src/printer_context.c
index 57d5f90e1..3fb50ac0a 100644
--- a/src/printer_context.c
+++ b/src/printer_context.c
@@ -46,10 +46,10 @@
         if (ORIG_ARRAY) { \
             LY_ARRAY_COUNT_TYPE count = LY_ARRAY_COUNT(ORIG_ARRAY); \
             memcpy(*MEM, &count, sizeof count); \
-            *MEM = (char *)*MEM + CTXP_MEM_SIZE(sizeof count); \
+            *MEM = (char *)*MEM + LY_CTXP_MEM_SIZE(sizeof count); \
             \
             ARRAY = *MEM; \
-            *MEM = (char *)*MEM + CTXP_MEM_SIZE(count * sizeof *ORIG_ARRAY); \
+            *MEM = (char *)*MEM + LY_CTXP_MEM_SIZE(count * sizeof *ORIG_ARRAY); \
         } else { \
             ARRAY = NULL; \
         }
@@ -67,24 +67,12 @@
 #define CTXP_OPTIONAL_STRUCT(FUNC, ORIG_PTR, PTR, ADDR_HT, PTR_SET, MEM) \
         if (ORIG_PTR) { \
             PTR = *MEM; \
-            *MEM = (char *)*MEM + CTXP_MEM_SIZE(sizeof *PTR); \
+            *MEM = (char *)*MEM + LY_CTXP_MEM_SIZE(sizeof *PTR); \
             FUNC(ORIG_PTR, PTR, ADDR_HT, PTR_SET, MEM); \
         } else { \
             PTR = NULL; \
         }
 
-/**
- * @brief Alignment of all the printed data, ensures all memory access is aligned to this number (B)
- */
-#define CTXP_MEM_ALIGN 8
-
-/**
- * @brief Adjust data size to an aligned size to make sure the following data is aligned.
- *
- * @param[in,out] SIZE Data size that is adjusted.
- */
-#define CTXP_MEM_SIZE(SIZE) ((SIZE) + ((~(SIZE) + 1) & (CTXP_MEM_ALIGN - 1)))
-
 static void
 ctxs_dict_strings(const struct ly_ht *ht, int *size)
 {
@@ -96,7 +84,7 @@ ctxs_dict_strings(const struct ly_ht *ht, int *size)
         dict_rec = (struct ly_dict_rec *)&rec->val;
 
         /* strings */
-        *size += CTXP_MEM_SIZE(strlen(dict_rec->value) + 1);
+        *size += LY_CTXP_MEM_SIZE(strlen(dict_rec->value) + 1);
     }
 }
 
@@ -117,7 +105,7 @@ ctxs_exts(const struct lysc_ext_instance *exts, struct ly_ht *ht, int *size)
 
         /* compiled, substmts storage */
         if (exts[u].def->plugin_ref && (ext_plg = LYSC_GET_EXT_PLG(exts[u].def->plugin_ref)) && ext_plg->compiled_size) {
-            *size += CTXP_MEM_SIZE(ext_plg->compiled_size(&exts[u], ht));
+            *size += LY_CTXP_MEM_SIZE(ext_plg->compiled_size(&exts[u], ht));
         }
     }
 }
@@ -131,7 +119,7 @@ ctxs_prefixes(const struct lysc_prefix *prefixes, int *size)
     LY_ARRAY_FOR(prefixes, u) {
         /* string not in the dictionary */
         if (prefixes[u].prefix) {
-            *size += CTXP_MEM_SIZE(strlen(prefixes[u].prefix) + 1);
+            *size += LY_CTXP_MEM_SIZE(strlen(prefixes[u].prefix) + 1);
         }
     }
 }
@@ -141,20 +129,20 @@ ctxs_expr(const struct lyxp_expr *exp, int *size)
 {
     uint32_t i, j;
 
-    *size += CTXP_MEM_SIZE(sizeof *exp);
+    *size += LY_CTXP_MEM_SIZE(sizeof *exp);
 
-    *size += CTXP_MEM_SIZE(strlen(exp->expr) + 1);
-    *size += CTXP_MEM_SIZE(exp->used * sizeof *exp->tokens);
-    *size += CTXP_MEM_SIZE(exp->used * sizeof *exp->tok_pos);
-    *size += CTXP_MEM_SIZE(exp->used * sizeof *exp->tok_len);
-    *size += CTXP_MEM_SIZE(exp->used * sizeof *exp->repeat);
+    *size += LY_CTXP_MEM_SIZE(strlen(exp->expr) + 1);
+    *size += LY_CTXP_MEM_SIZE(exp->used * sizeof *exp->tokens);
+    *size += LY_CTXP_MEM_SIZE(exp->used * sizeof *exp->tok_pos);
+    *size += LY_CTXP_MEM_SIZE(exp->used * sizeof *exp->tok_len);
+    *size += LY_CTXP_MEM_SIZE(exp->used * sizeof *exp->repeat);
     for (i = 0; i < exp->used; ++i) {
         if (!exp->repeat[i]) {
             continue;
         }
 
         for (j = 0; exp->repeat[i][j]; ++j) {}
-        *size += CTXP_MEM_SIZE((j + 1) * sizeof **exp->repeat);
+        *size += LY_CTXP_MEM_SIZE((j + 1) * sizeof **exp->repeat);
     }
 }
 
@@ -182,7 +170,7 @@ ctxs_when(const struct lysc_when *when, struct ly_ht *ht, int *size)
         return;
     }
 
-    *size += CTXP_MEM_SIZE(sizeof *when);
+    *size += LY_CTXP_MEM_SIZE(sizeof *when);
 
     ctxs_expr(when->cond, size);
     ctxs_prefixes(when->prefixes, size);
@@ -207,7 +195,7 @@ ctxs_range(const struct lysc_range *range, struct ly_ht *ht, int *size)
         return;
     }
 
-    *size += CTXP_MEM_SIZE(sizeof *range);
+    *size += LY_CTXP_MEM_SIZE(sizeof *range);
 
     *size += CTXS_SIZED_ARRAY(range->parts);
     ctxs_exts(range->exts, ht, size);
@@ -227,7 +215,7 @@ ctxs_patterns(const struct lysc_pattern **patterns, struct ly_ht *ht, int *size)
             continue;
         }
 
-        *size += CTXP_MEM_SIZE(sizeof *patterns[u]);
+        *size += LY_CTXP_MEM_SIZE(sizeof *patterns[u]);
         ctxs_exts(patterns[u]->exts, ht, size);
     }
 }
@@ -270,7 +258,7 @@ ctxs_type(const struct lysc_type *type, struct ly_ht *ht, int *size)
     switch (type->basetype) {
     case LY_TYPE_BINARY:
         type_bin = (const struct lysc_type_bin *)type;
-        *size += CTXP_MEM_SIZE(sizeof *type_bin);
+        *size += LY_CTXP_MEM_SIZE(sizeof *type_bin);
 
         ctxs_range(type_bin->length, ht, size);
         break;
@@ -283,13 +271,13 @@ ctxs_type(const struct lysc_type *type, struct ly_ht *ht, int *size)
     case LY_TYPE_INT32:
     case LY_TYPE_INT64:
         type_num = (const struct lysc_type_num *)type;
-        *size += CTXP_MEM_SIZE(sizeof *type_num);
+        *size += LY_CTXP_MEM_SIZE(sizeof *type_num);
 
         ctxs_range(type_num->range, ht, size);
         break;
     case LY_TYPE_STRING:
         type_str = (const struct lysc_type_str *)type;
-        *size += CTXP_MEM_SIZE(sizeof *type_str);
+        *size += LY_CTXP_MEM_SIZE(sizeof *type_str);
 
         ctxs_range(type_str->length, ht, size);
         ctxs_patterns((const struct lysc_pattern **)type_str->patterns, ht, size);
@@ -297,33 +285,33 @@ ctxs_type(const struct lysc_type *type, struct ly_ht *ht, int *size)
     case LY_TYPE_BITS:
     case LY_TYPE_ENUM:
         type_enum_bits = (const struct lysc_type_enum *)type;
-        *size += CTXP_MEM_SIZE(sizeof *type_enum_bits);
+        *size += LY_CTXP_MEM_SIZE(sizeof *type_enum_bits);
 
         ctxs_enums(type_enum_bits->enums, ht, size);
         break;
     case LY_TYPE_BOOL:
     case LY_TYPE_EMPTY:
-        *size += CTXP_MEM_SIZE(sizeof *type);
+        *size += LY_CTXP_MEM_SIZE(sizeof *type);
         break;
     case LY_TYPE_DEC64:
         type_dec = (const struct lysc_type_dec *)type;
-        *size += CTXP_MEM_SIZE(sizeof *type_dec);
+        *size += LY_CTXP_MEM_SIZE(sizeof *type_dec);
 
         ctxs_range(type_dec->range, ht, size);
         break;
     case LY_TYPE_IDENT:
         type_identref = (const struct lysc_type_identityref *)type;
-        *size += CTXP_MEM_SIZE(sizeof *type_identref);
+        *size += LY_CTXP_MEM_SIZE(sizeof *type_identref);
 
         *size += CTXS_SIZED_ARRAY(type_identref->bases);
         break;
     case LY_TYPE_INST:
         type_instid = (const struct lysc_type_instanceid *)type;
-        *size += CTXP_MEM_SIZE(sizeof *type_instid);
+        *size += LY_CTXP_MEM_SIZE(sizeof *type_instid);
         break;
     case LY_TYPE_LEAFREF:
         type_lref = (const struct lysc_type_leafref *)type;
-        *size += CTXP_MEM_SIZE(sizeof *type_lref);
+        *size += LY_CTXP_MEM_SIZE(sizeof *type_lref);
 
         ctxs_expr(type_lref->path, size);
         ctxs_prefixes(type_lref->prefixes, size);
@@ -331,7 +319,7 @@ ctxs_type(const struct lysc_type *type, struct ly_ht *ht, int *size)
         break;
     case LY_TYPE_UNION:
         type_union = (const struct lysc_type_union *)type;
-        *size += CTXP_MEM_SIZE(sizeof *type_union);
+        *size += LY_CTXP_MEM_SIZE(sizeof *type_union);
 
         *size += CTXS_SIZED_ARRAY(type_union->types);
         LY_ARRAY_FOR(type_union->types, u) {
@@ -366,7 +354,7 @@ ctxs_node(const struct lysc_node *node, struct ly_ht *ht, int *size)
     switch (node->nodetype) {
     case LYS_CONTAINER:
         cont = (const struct lysc_node_container *)node;
-        *size += CTXP_MEM_SIZE(sizeof *cont);
+        *size += LY_CTXP_MEM_SIZE(sizeof *cont);
 
         LY_LIST_FOR(cont->child, child) {
             ctxs_node(child, ht, size);
@@ -382,7 +370,7 @@ ctxs_node(const struct lysc_node *node, struct ly_ht *ht, int *size)
         break;
     case LYS_CHOICE:
         choic = (const struct lysc_node_choice *)node;
-        *size += CTXP_MEM_SIZE(sizeof *choic);
+        *size += LY_CTXP_MEM_SIZE(sizeof *choic);
 
         LY_LIST_FOR((const struct lysc_node *)choic->cases, child) {
             ctxs_node(child, ht, size);
@@ -391,7 +379,7 @@ ctxs_node(const struct lysc_node *node, struct ly_ht *ht, int *size)
         break;
     case LYS_LEAF:
         leaf = (const struct lysc_node_leaf *)node;
-        *size += CTXP_MEM_SIZE(sizeof *leaf);
+        *size += LY_CTXP_MEM_SIZE(sizeof *leaf);
 
         ctxs_musts(leaf->musts, ht, size);
         ctxs_whens((const struct lysc_when **)leaf->when, ht, size);
@@ -400,7 +388,7 @@ ctxs_node(const struct lysc_node *node, struct ly_ht *ht, int *size)
         break;
     case LYS_LEAFLIST:
         llist = (const struct lysc_node_leaflist *)node;
-        *size += CTXP_MEM_SIZE(sizeof *llist);
+        *size += LY_CTXP_MEM_SIZE(sizeof *llist);
 
         ctxs_musts(llist->musts, ht, size);
         ctxs_whens((const struct lysc_when **)llist->when, ht, size);
@@ -412,7 +400,7 @@ ctxs_node(const struct lysc_node *node, struct ly_ht *ht, int *size)
         break;
     case LYS_LIST:
         list = (const struct lysc_node_list *)node;
-        *size += CTXP_MEM_SIZE(sizeof *list);
+        *size += LY_CTXP_MEM_SIZE(sizeof *list);
 
         LY_LIST_FOR(list->child, child) {
             ctxs_node(child, ht, size);
@@ -427,21 +415,21 @@ ctxs_node(const struct lysc_node *node, struct ly_ht *ht, int *size)
         }
         *size += CTXS_SIZED_ARRAY(list->uniques);
         LY_ARRAY_FOR(list->uniques, u) {
-            *size += CTXP_MEM_SIZE(sizeof(LY_ARRAY_COUNT_TYPE)) +
-                    CTXP_MEM_SIZE(LY_ARRAY_COUNT(list->uniques[u]) * sizeof **list->uniques);
+            *size += LY_CTXP_MEM_SIZE(sizeof(LY_ARRAY_COUNT_TYPE)) +
+                    LY_CTXP_MEM_SIZE(LY_ARRAY_COUNT(list->uniques[u]) * sizeof **list->uniques);
         }
         break;
     case LYS_ANYXML:
     case LYS_ANYDATA:
         any = (const struct lysc_node_anydata *)node;
-        *size += CTXP_MEM_SIZE(sizeof *any);
+        *size += LY_CTXP_MEM_SIZE(sizeof *any);
 
         ctxs_musts(any->musts, ht, size);
         ctxs_whens((const struct lysc_when **)any->when, ht, size);
         break;
     case LYS_CASE:
         cas = (const struct lysc_node_case *)node;
-        *size += CTXP_MEM_SIZE(sizeof *cas);
+        *size += LY_CTXP_MEM_SIZE(sizeof *cas);
 
         LY_LIST_FOR(cas->child, child) {
             ctxs_node(child, ht, size);
@@ -451,7 +439,7 @@ ctxs_node(const struct lysc_node *node, struct ly_ht *ht, int *size)
     case LYS_RPC:
     case LYS_ACTION:
         act = (const struct lysc_node_action *)node;
-        *size += CTXP_MEM_SIZE(sizeof *act);
+        *size += LY_CTXP_MEM_SIZE(sizeof *act);
 
         ctxs_whens((const struct lysc_when **)act->when, ht, size);
         ctxs_node((struct lysc_node *)&act->input, ht, size);
@@ -469,7 +457,7 @@ ctxs_node(const struct lysc_node *node, struct ly_ht *ht, int *size)
         break;
     case LYS_NOTIF:
         notif = (const struct lysc_node_notif *)node;
-        *size += CTXP_MEM_SIZE(sizeof *notif);
+        *size += LY_CTXP_MEM_SIZE(sizeof *notif);
 
         LY_LIST_FOR(notif->child, child) {
             ctxs_node(child, ht, size);
@@ -493,7 +481,7 @@ ctxs_compiled(const struct lysc_module *compiled, struct ly_ht *ht, int *size)
     }
 
     /* compiled module */
-    *size += CTXP_MEM_SIZE(sizeof *compiled);
+    *size += LY_CTXP_MEM_SIZE(sizeof *compiled);
 
     *size += CTXS_SIZED_ARRAY(compiled->features);
     LY_LIST_FOR(compiled->data, node) {
@@ -539,7 +527,7 @@ static void
 ctxs_module(const struct lys_module *mod, struct ly_ht *ht, int *size)
 {
     /* module */
-    *size += CTXP_MEM_SIZE(sizeof *mod);
+    *size += LY_CTXP_MEM_SIZE(sizeof *mod);
 
     /* compiled module */
     ctxs_compiled(mod->compiled, ht, size);
@@ -561,13 +549,13 @@ ly_ctx_compiled_size_context(const struct ly_ctx *ctx, struct ly_ht *addr_ht, in
     const struct lys_module *mod;
 
     /* context */
-    *size += CTXP_MEM_SIZE(sizeof *ctx);
+    *size += LY_CTXP_MEM_SIZE(sizeof *ctx);
 
     /* all the strings in the dictionary */
     ctxs_dict_strings(ctx->dict.hash_tab, size);
 
     /* module set */
-    *size += CTXP_MEM_SIZE(ctx->modules.count * sizeof ctx->modules.objs);
+    *size += LY_CTXP_MEM_SIZE(ctx->modules.count * sizeof ctx->modules.objs);
     for (i = 0; i < ctx->modules.count; ++i) {
         mod = ctx->modules.objs[i];
 
@@ -720,7 +708,7 @@ ctxp_set(const struct ly_set *orig_set, struct ly_set *set, void **mem)
     set->size = orig_set->count;
     set->count = orig_set->count;
     set->objs = *mem;
-    *mem = (char *)*mem + CTXP_MEM_SIZE(set->count * sizeof set->objs);
+    *mem = (char *)*mem + LY_CTXP_MEM_SIZE(set->count * sizeof set->objs);
 }
 
 static void
@@ -741,7 +729,7 @@ ctxp_dict_strings(const struct ly_ht *orig_dict, struct ly_ht *addr_ht, void **m
         orig_str = dict_rec->value;
 
         str = *mem;
-        *mem = (char *)*mem + CTXP_MEM_SIZE(len);
+        *mem = (char *)*mem + LY_CTXP_MEM_SIZE(len);
 
         memcpy(str, orig_str, len);
 
@@ -801,30 +789,30 @@ ctxp_expr(const struct lyxp_expr *orig_exp, struct lyxp_expr *exp, void **mem)
     uint32_t i, len;
 
     exp->expr = *mem;
-    *mem = (char *)*mem + CTXP_MEM_SIZE(strlen(orig_exp->expr) + 1);
+    *mem = (char *)*mem + LY_CTXP_MEM_SIZE(strlen(orig_exp->expr) + 1);
     memcpy(exp->expr, orig_exp->expr, strlen(orig_exp->expr) + 1);
 
     exp->tokens = *mem;
-    *mem = (char *)*mem + CTXP_MEM_SIZE(orig_exp->used * sizeof *exp->tokens);
+    *mem = (char *)*mem + LY_CTXP_MEM_SIZE(orig_exp->used * sizeof *exp->tokens);
     memcpy(exp->tokens, orig_exp->tokens, orig_exp->used * sizeof *exp->tokens);
 
     exp->tok_pos = *mem;
-    *mem = (char *)*mem + CTXP_MEM_SIZE(orig_exp->used * sizeof *exp->tok_pos);
+    *mem = (char *)*mem + LY_CTXP_MEM_SIZE(orig_exp->used * sizeof *exp->tok_pos);
     memcpy(exp->tok_pos, orig_exp->tok_pos, orig_exp->used * sizeof *exp->tok_pos);
 
     exp->tok_len = *mem;
-    *mem = (char *)*mem + CTXP_MEM_SIZE(orig_exp->used * sizeof *exp->tok_len);
+    *mem = (char *)*mem + LY_CTXP_MEM_SIZE(orig_exp->used * sizeof *exp->tok_len);
     memcpy(exp->tok_len, orig_exp->tok_len, orig_exp->used * sizeof *exp->tok_len);
 
     exp->repeat = *mem;
-    *mem = (char *)*mem + CTXP_MEM_SIZE(orig_exp->used * sizeof *exp->repeat);
+    *mem = (char *)*mem + LY_CTXP_MEM_SIZE(orig_exp->used * sizeof *exp->repeat);
     for (i = 0; i < orig_exp->used; ++i) {
         if (orig_exp->repeat[i]) {
             for (len = 0; orig_exp->repeat[i][len]; ++len) {}
             ++len;
 
             exp->repeat[i] = *mem;
-            *mem = (char *)*mem + CTXP_MEM_SIZE(len * sizeof **exp->repeat);
+            *mem = (char *)*mem + LY_CTXP_MEM_SIZE(len * sizeof **exp->repeat);
             memcpy(exp->repeat[i], orig_exp->repeat[i], len * sizeof **exp->repeat);
         } else {
             exp->repeat[i] = NULL;
@@ -845,7 +833,7 @@ ctxp_prefix(const struct lysc_prefix *orig_prefix, struct lysc_prefix *prefix, s
         size = strlen(orig_prefix->prefix) + 1;
 
         prefix->prefix = *mem;
-        *mem = (char *)*mem + CTXP_MEM_SIZE(size);
+        *mem = (char *)*mem + LY_CTXP_MEM_SIZE(size);
         memcpy(prefix->prefix, orig_prefix->prefix, size);
     } else {
         prefix->prefix = NULL;
@@ -867,7 +855,7 @@ ctxp_must(const struct lysc_must *orig_must, struct lysc_must *must, struct ly_h
     }
 
     must->cond = *mem;
-    *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *must->cond);
+    *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *must->cond);
     ctxp_expr(orig_must->cond, must->cond, mem);
 
     CTXP_SIZED_ARRAY(orig_must->prefixes, must->prefixes, mem);
@@ -901,7 +889,7 @@ ctxp_when(const struct lysc_when *orig_when, struct lysc_when **when, struct ly_
     }
 
     w = *mem;
-    *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *w);
+    *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *w);
 
     if (orig_when->exts) {
         /* may be referenced in the parent */
@@ -909,7 +897,7 @@ ctxp_when(const struct lysc_when *orig_when, struct lysc_when **when, struct ly_
     }
 
     w->cond = *mem;
-    *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *w->cond);
+    *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *w->cond);
     ctxp_expr(orig_when->cond, w->cond, mem);
 
     w->context = ly_ctx_compiled_addr_ht_get(addr_ht, orig_when->context, 0);
@@ -980,7 +968,7 @@ ctxp_children(const struct lysc_node *orig_child, struct lysc_node **child, stru
         }
 
         ch = *mem;
-        *mem = (char *)*mem + CTXP_MEM_SIZE(node_size);
+        *mem = (char *)*mem + LY_CTXP_MEM_SIZE(node_size);
 
         /* referenced by pointers */
         ly_ctx_compiled_addr_ht_add(addr_ht, orig_child, ch);
@@ -1035,7 +1023,7 @@ ctxp_pattern(const struct lysc_pattern *orig_pattern, struct lysc_pattern **patt
     }
 
     p = *mem;
-    *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *p);
+    *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *p);
 
     if (orig_pattern->exts) {
         /* may be referenced in the parent */
@@ -1122,7 +1110,7 @@ ctxp_type(const struct lysc_type *orig_type, struct lysc_type **type, struct ly_
     case LY_TYPE_BINARY:
         orig_type_bin = (const struct lysc_type_bin *)orig_type;
         t = *mem;
-        *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *orig_type_bin);
+        *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *orig_type_bin);
         t_bin = (struct lysc_type_bin *)t;
 
         CTXP_OPTIONAL_STRUCT(ctxp_range, orig_type_bin->length, t_bin->length, addr_ht, ptr_set, mem);
@@ -1137,7 +1125,7 @@ ctxp_type(const struct lysc_type *orig_type, struct lysc_type **type, struct ly_
     case LY_TYPE_INT64:
         orig_type_num = (const struct lysc_type_num *)orig_type;
         t = *mem;
-        *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *orig_type_num);
+        *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *orig_type_num);
         t_num = (struct lysc_type_num *)t;
 
         CTXP_OPTIONAL_STRUCT(ctxp_range, orig_type_num->range, t_num->range, addr_ht, ptr_set, mem);
@@ -1145,7 +1133,7 @@ ctxp_type(const struct lysc_type *orig_type, struct lysc_type **type, struct ly_
     case LY_TYPE_STRING:
         orig_type_str = (const struct lysc_type_str *)orig_type;
         t = *mem;
-        *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *orig_type_str);
+        *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *orig_type_str);
         t_str = (struct lysc_type_str *)t;
 
         CTXP_OPTIONAL_STRUCT(ctxp_range, orig_type_str->length, t_str->length, addr_ht, ptr_set, mem);
@@ -1158,7 +1146,7 @@ ctxp_type(const struct lysc_type *orig_type, struct lysc_type **type, struct ly_
     case LY_TYPE_ENUM:
         orig_type_enum_bits = (const struct lysc_type_enum *)orig_type;
         t = *mem;
-        *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *orig_type_enum_bits);
+        *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *orig_type_enum_bits);
         t_enum_bits = (struct lysc_type_enum *)t;
 
         CTXP_SIZED_ARRAY(orig_type_enum_bits->enums, t_enum_bits->enums, mem);
@@ -1170,12 +1158,12 @@ ctxp_type(const struct lysc_type *orig_type, struct lysc_type **type, struct ly_
     case LY_TYPE_EMPTY:
         /* no additional members */
         t = *mem;
-        *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *orig_type);
+        *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *orig_type);
         break;
     case LY_TYPE_DEC64:
         orig_type_dec = (const struct lysc_type_dec *)orig_type;
         t = *mem;
-        *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *orig_type_dec);
+        *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *orig_type_dec);
         t_dec = (struct lysc_type_dec *)t;
 
         t_dec->fraction_digits = orig_type_dec->fraction_digits;
@@ -1184,7 +1172,7 @@ ctxp_type(const struct lysc_type *orig_type, struct lysc_type **type, struct ly_
     case LY_TYPE_IDENT:
         orig_type_identref = (const struct lysc_type_identityref *)orig_type;
         t = *mem;
-        *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *orig_type_identref);
+        *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *orig_type_identref);
         t_identref = (struct lysc_type_identityref *)t;
 
         CTXP_SIZED_ARRAY(orig_type_identref->bases, t_identref->bases, mem);
@@ -1197,7 +1185,7 @@ ctxp_type(const struct lysc_type *orig_type, struct lysc_type **type, struct ly_
     case LY_TYPE_INST:
         orig_type_instid = (const struct lysc_type_instanceid *)orig_type;
         t = *mem;
-        *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *orig_type_instid);
+        *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *orig_type_instid);
         t_instid = (struct lysc_type_instanceid *)t;
 
         t_instid->require_instance = orig_type_instid->require_instance;
@@ -1205,11 +1193,11 @@ ctxp_type(const struct lysc_type *orig_type, struct lysc_type **type, struct ly_
     case LY_TYPE_LEAFREF:
         orig_type_lref = (const struct lysc_type_leafref *)orig_type;
         t = *mem;
-        *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *orig_type_lref);
+        *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *orig_type_lref);
         t_lref = (struct lysc_type_leafref *)t;
 
         t_lref->path = *mem;
-        *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *t_lref->path);
+        *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *t_lref->path);
         ctxp_expr(orig_type_lref->path, t_lref->path, mem);
 
         CTXP_SIZED_ARRAY(orig_type_lref->prefixes, t_lref->prefixes, mem);
@@ -1223,7 +1211,7 @@ ctxp_type(const struct lysc_type *orig_type, struct lysc_type **type, struct ly_
     case LY_TYPE_UNION:
         orig_type_union = (const struct lysc_type_union *)orig_type;
         t = *mem;
-        *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *orig_type_union);
+        *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *orig_type_union);
         t_union = (struct lysc_type_union *)t;
 
         CTXP_SIZED_ARRAY(orig_type_union->types, t_union->types, mem);
@@ -1736,7 +1724,7 @@ ly_ctx_compiled_print_context(const struct ly_ctx *orig_ctx, struct ly_ctx *ctx,
     for (i = 0; i < ctx->modules.count; ++i) {
         /* allocate the shared module and store its new address in HT so it can be referenced */
         ctx->modules.objs[i] = *mem;
-        *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof(struct lys_module));
+        *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof(struct lys_module));
         ly_ctx_compiled_addr_ht_add(addr_ht, orig_ctx->modules.objs[i], ctx->modules.objs[i]);
     }
     for (i = 0; i < ctx->modules.count; ++i) {
@@ -1864,7 +1852,7 @@ ly_ctx_compiled_ext_stmts_storage_print(const struct lysc_ext_substmt *orig_subs
         case LY_STMT_LENGTH:
         case LY_STMT_RANGE:
             *substmts[u].storage_p = *mem;
-            *mem = (char *)*mem + CTXP_MEM_SIZE(sizeof(struct lysc_range));
+            *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof(struct lysc_range));
             ctxp_range(*orig_substmts[u].storage_p, *substmts[u].storage_p, addr_ht, ptr_set, mem);
             break;
         case LY_STMT_MAX_ELEMENTS:
-- 
2.43.0

