blob: 24c7ac69be12d4fcfa00d4f1532e91d213dc9297 [file] [log] [blame]
Michal Vaskoae130f52023-04-20 14:25:16 +02001/**
2 * @file dict.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @author Michal Vasko <mvasko@cesnet.cz>
5 * @brief libyang dictionary for storing strings
6 *
7 * Copyright (c) 2015 - 2023 CESNET, z.s.p.o.
8 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
16#include "dict.h"
17
18#include <assert.h>
19#include <pthread.h>
20#include <stdint.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include "common.h"
25#include "compat.h"
26#include "log.h"
27
28/* starting size of the dictionary */
29#define LYDICT_MIN_SIZE 1024
30
31/**
32 * @brief Comparison callback for dictionary's hash table
33 *
34 * Implementation of ::lyht_value_equal_cb.
35 */
36static ly_bool
37lydict_val_eq(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *cb_data)
38{
Michal Vaskode17be12023-09-29 14:09:53 +020039 const char *str1, *str2;
40 size_t *len1;
41
Michal Vaskoae130f52023-04-20 14:25:16 +020042 LY_CHECK_ARG_RET(NULL, val1_p, val2_p, cb_data, 0);
43
Michal Vaskode17be12023-09-29 14:09:53 +020044 str1 = ((struct ly_dict_rec *)val1_p)->value;
45 str2 = ((struct ly_dict_rec *)val2_p)->value;
46 len1 = cb_data;
Michal Vaskoae130f52023-04-20 14:25:16 +020047
48 LY_CHECK_ERR_RET(!str1, LOGARG(NULL, val1_p), 0);
49 LY_CHECK_ERR_RET(!str2, LOGARG(NULL, val2_p), 0);
50
Michal Vaskode17be12023-09-29 14:09:53 +020051 if (!strncmp(str1, str2, *len1) && !str2[*len1]) {
Michal Vaskoae130f52023-04-20 14:25:16 +020052 return 1;
53 }
54
55 return 0;
56}
57
58void
59lydict_init(struct ly_dict *dict)
60{
61 LY_CHECK_ARG_RET(NULL, dict, );
62
63 dict->hash_tab = lyht_new(LYDICT_MIN_SIZE, sizeof(struct ly_dict_rec), lydict_val_eq, NULL, 1);
64 LY_CHECK_ERR_RET(!dict->hash_tab, LOGINT(NULL), );
65 pthread_mutex_init(&dict->lock, NULL);
66}
67
68void
69lydict_clean(struct ly_dict *dict)
70{
71 struct ly_dict_rec *dict_rec = NULL;
72 struct ly_ht_rec *rec = NULL;
73
74 LY_CHECK_ARG_RET(NULL, dict, );
75
76 for (uint32_t i = 0; i < dict->hash_tab->size; i++) {
77 /* get ith record */
78 rec = (struct ly_ht_rec *)&dict->hash_tab->recs[i * dict->hash_tab->rec_size];
79 if (rec->hits == 1) {
80 /*
81 * this should not happen, all records inserted into
82 * dictionary are supposed to be removed using lydict_remove()
83 * before calling lydict_clean()
84 */
85 dict_rec = (struct ly_dict_rec *)rec->val;
86 LOGWRN(NULL, "String \"%s\" not freed from the dictionary, refcount %d", dict_rec->value, dict_rec->refcount);
87 /* if record wasn't removed before free string allocated for that record */
88#ifdef NDEBUG
89 free(dict_rec->value);
90#endif
91 }
92 }
93
94 /* free table and destroy mutex */
95 lyht_free(dict->hash_tab, NULL);
96 pthread_mutex_destroy(&dict->lock);
97}
98
99static ly_bool
100lydict_resize_val_eq(void *val1_p, void *val2_p, ly_bool mod, void *cb_data)
101{
102 LY_CHECK_ARG_RET(NULL, val1_p, val2_p, 0);
103
104 const char *str1 = ((struct ly_dict_rec *)val1_p)->value;
105 const char *str2 = ((struct ly_dict_rec *)val2_p)->value;
106
107 LY_CHECK_ERR_RET(!str1, LOGARG(NULL, val1_p), 0);
108 LY_CHECK_ERR_RET(!str2, LOGARG(NULL, val2_p), 0);
109
110 if (mod) {
111 /* used when inserting new values */
112 if (strcmp(str1, str2) == 0) {
113 return 1;
114 }
115 } else {
116 /* used when finding the original value again in the resized table */
117 return lydict_val_eq(val1_p, val2_p, mod, cb_data);
118 }
119
120 return 0;
121}
122
123LIBYANG_API_DEF LY_ERR
124lydict_remove(const struct ly_ctx *ctx, const char *value)
125{
126 LY_ERR ret = LY_SUCCESS;
127 size_t len;
128 uint32_t hash;
129 struct ly_dict_rec rec, *match = NULL;
130 char *val_p;
131
132 if (!ctx || !value) {
133 return LY_SUCCESS;
134 }
135
136 LOGDBG(LY_LDGDICT, "removing \"%s\"", value);
137
138 len = strlen(value);
139 hash = lyht_hash(value, len);
140
141 /* create record for lyht_find call */
142 rec.value = (char *)value;
143 rec.refcount = 0;
144
145 pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
146 /* set len as data for compare callback */
147 lyht_set_cb_data(ctx->dict.hash_tab, (void *)&len);
148 /* check if value is already inserted */
149 ret = lyht_find(ctx->dict.hash_tab, &rec, hash, (void **)&match);
150
151 if (ret == LY_SUCCESS) {
152 LY_CHECK_ERR_GOTO(!match, LOGINT(ctx), finish);
153
154 /* if value is already in dictionary, decrement reference counter */
155 match->refcount--;
156 if (match->refcount == 0) {
157 /*
158 * remove record
159 * save pointer to stored string before lyht_remove to
160 * free it after it is removed from hash table
161 */
162 val_p = match->value;
163 ret = lyht_remove_with_resize_cb(ctx->dict.hash_tab, &rec, hash, lydict_resize_val_eq);
164 free(val_p);
165 LY_CHECK_ERR_GOTO(ret, LOGINT(ctx), finish);
166 }
167 } else if (ret == LY_ENOTFOUND) {
168 LOGERR(ctx, LY_ENOTFOUND, "Value \"%s\" was not found in the dictionary.", value);
169 } else {
170 LOGINT(ctx);
171 }
172
173finish:
174 pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
175 return ret;
176}
177
178LY_ERR
179dict_insert(const struct ly_ctx *ctx, char *value, size_t len, ly_bool zerocopy, const char **str_p)
180{
181 LY_ERR ret = LY_SUCCESS;
182 struct ly_dict_rec *match = NULL, rec;
183 uint32_t hash;
184
185 LOGDBG(LY_LDGDICT, "inserting \"%.*s\"", (int)len, value);
186
187 hash = lyht_hash(value, len);
188 /* set len as data for compare callback */
189 lyht_set_cb_data(ctx->dict.hash_tab, (void *)&len);
190 /* create record for lyht_insert */
191 rec.value = value;
192 rec.refcount = 1;
193
194 ret = lyht_insert_with_resize_cb(ctx->dict.hash_tab, (void *)&rec, hash, lydict_resize_val_eq, (void **)&match);
195 if (ret == LY_EEXIST) {
196 match->refcount++;
197 if (zerocopy) {
198 free(value);
199 }
200 ret = LY_SUCCESS;
201 } else if (ret == LY_SUCCESS) {
202 if (!zerocopy) {
203 /*
204 * allocate string for new record
205 * record is already inserted in hash table
206 */
207 match->value = malloc(sizeof *match->value * (len + 1));
208 LY_CHECK_ERR_RET(!match->value, LOGMEM(ctx), LY_EMEM);
209 if (len) {
210 memcpy(match->value, value, len);
211 }
212 match->value[len] = '\0';
213 }
214 } else {
215 /* lyht_insert returned error */
216 if (zerocopy) {
217 free(value);
218 }
219 return ret;
220 }
221
222 if (str_p) {
223 *str_p = match->value;
224 }
225
226 return ret;
227}
228
229LIBYANG_API_DEF LY_ERR
230lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len, const char **str_p)
231{
232 LY_ERR result;
233
234 LY_CHECK_ARG_RET(ctx, ctx, str_p, LY_EINVAL);
235
236 if (!value) {
237 *str_p = NULL;
238 return LY_SUCCESS;
239 }
240
241 if (!len) {
242 len = strlen(value);
243 }
244
245 pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
246 result = dict_insert(ctx, (char *)value, len, 0, str_p);
247 pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
248
249 return result;
250}
251
252LIBYANG_API_DEF LY_ERR
253lydict_insert_zc(const struct ly_ctx *ctx, char *value, const char **str_p)
254{
255 LY_ERR result;
256
257 LY_CHECK_ARG_RET(ctx, ctx, str_p, LY_EINVAL);
258
259 if (!value) {
260 *str_p = NULL;
261 return LY_SUCCESS;
262 }
263
264 pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
265 result = dict_insert(ctx, value, strlen(value), 1, str_p);
266 pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
267
268 return result;
269}