dict NEW efficient dict_dup finction
diff --git a/src/dict.c b/src/dict.c
index a880696..ba574c8 100644
--- a/src/dict.c
+++ b/src/dict.c
@@ -264,3 +264,49 @@
return result;
}
+
+static LY_ERR
+dict_dup(const struct ly_ctx *ctx, char *value, const char **str_p)
+{
+ LY_ERR ret = LY_SUCCESS;
+ struct ly_dict_rec *match = NULL, rec;
+ uint32_t hash;
+
+ /* set new callback to only compare memory addresses */
+ lyht_value_equal_cb prev = lyht_set_cb(ctx->dict.hash_tab, lydict_resize_val_eq);
+
+ LOGDBG(LY_LDGDICT, "duplicating %s", value);
+ hash = lyht_hash(value, strlen(value));
+ rec.value = value;
+
+ ret = lyht_find(ctx->dict.hash_tab, (void *)&rec, hash, (void **)&match);
+ if (ret == LY_SUCCESS) {
+ /* record found, increase refcount */
+ match->refcount++;
+ *str_p = match->value;
+ }
+
+ /* restore callback */
+ lyht_set_cb(ctx->dict.hash_tab, prev);
+
+ return ret;
+}
+
+LIBYANG_API_DEF LY_ERR
+lydict_dup(const struct ly_ctx *ctx, const char *value, const char **str_p)
+{
+ LY_ERR result;
+
+ LY_CHECK_ARG_RET(ctx, ctx, str_p, LY_EINVAL);
+
+ if (!value) {
+ *str_p = NULL;
+ return LY_SUCCESS;
+ }
+
+ pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
+ result = dict_dup(ctx, (char *)value, str_p);
+ pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
+
+ return result;
+}