context CHANGE clb for copying data nodes priv pointer

Fixes #478
diff --git a/src/context.c b/src/context.c
index e3e4f07..c7b7b2c 100644
--- a/src/context.c
+++ b/src/context.c
@@ -738,6 +738,16 @@
     return ctx->data_clb;
 }
 
+#ifdef LY_ENABLED_LYD_PRIV
+
+API void
+ly_ctx_set_priv_dup_clb(struct ly_ctx *ctx, void *(*priv_dup_clb)(const void *priv))
+{
+    ctx->priv_dup_clb = priv_dup_clb;
+}
+
+#endif
+
 const struct lys_module *
 ly_ctx_load_sub_module(struct ly_ctx *ctx, struct lys_module *module, const char *name, const char *revision,
                        int implement, struct unres_schema *unres)
diff --git a/src/context.h b/src/context.h
index 37af833..42dc569 100644
--- a/src/context.h
+++ b/src/context.h
@@ -44,6 +44,9 @@
     void *imp_clb_data;
     ly_module_data_clb data_clb;
     void *data_clb_data;
+#ifdef LY_ENABLED_LYD_PRIV
+    void *(*priv_dup_clb)(const void *priv);
+#endif
     pthread_key_t errlist_key;
     uint8_t internal_module_count;
 };
diff --git a/src/libyang.h.in b/src/libyang.h.in
index 016cb7b..805d055 100644
--- a/src/libyang.h.in
+++ b/src/libyang.h.in
@@ -1437,6 +1437,12 @@
  */
 ly_module_data_clb ly_ctx_get_module_data_clb(const struct ly_ctx *ctx, void **user_data);
 
+#ifdef LY_ENABLED_LYD_PRIV
+
+void ly_ctx_set_priv_dup_clb(struct ly_ctx *ctx, void *(*priv_dup_clb)(const void *priv));
+
+#endif
+
 /**
  * @brief Get pointer to the schema tree of the module of the specified namespace
  *
diff --git a/src/tree_data.c b/src/tree_data.c
index 1caf8d0..a45aaa8 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -5237,10 +5237,11 @@
     } else {
         /* the context is the same so also the pointer into the schema will be the same */
         new->schema = orig->schema;
+        ctx = orig->schema->module->ctx;
     }
     new->attr = NULL;
     LY_TREE_FOR(orig->attr, attr) {
-        lyd_dup_attr(ctx ? ctx : orig->schema->module->ctx, new, attr);
+        lyd_dup_attr(ctx, new, attr);
     }
     new->next = NULL;
     new->prev = new;
@@ -5255,6 +5256,12 @@
     }
 #endif
 
+#ifdef LY_ENABLED_LYD_PRIV
+    if (ctx->priv_dup_clb) {
+        new->priv = ctx->priv_dup_clb(orig->priv);
+    }
+#endif
+
     if (parent && lyd_insert(parent, new)) {
         return EXIT_FAILURE;
     }