blob: 5f1af016e2ffb556c6dc85788c1a532013648097 [file] [log] [blame]
Radek Krejci5aeea3a2018-09-05 13:29:36 +02001/**
2 * @file hash_table.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief libyang dictionary for storing strings and generic hash table
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#include "hash_table.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020016
Radek Krejci5aeea3a2018-09-05 13:29:36 +020017#include <string.h>
18#include <stdint.h>
19#include <stdlib.h>
20#include <pthread.h>
21#include <assert.h>
22
Michal Vaskoc5a22832020-08-20 13:21:33 +020023#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
25#include "dict.h"
Radek Krejci857189e2020-09-01 13:26:36 +020026#include "log.h"
Radek Krejci5aeea3a2018-09-05 13:29:36 +020027
Radek Krejci857189e2020-09-01 13:26:36 +020028/**
29 * @brief Comparison callback for dictionary's hash table
30 *
31 * Implementation of ::values_equal_cb.
32 */
33static ly_bool
34lydict_val_eq(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *cb_data)
Radek Krejci5aeea3a2018-09-05 13:29:36 +020035{
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020036 LY_CHECK_ARG_RET(NULL, val1_p, val2_p, cb_data, 0);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020037
38 const char *str1 = ((struct dict_rec *)val1_p)->value;
39 const char *str2 = ((struct dict_rec *)val2_p)->value;
40
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020041 LY_CHECK_ERR_RET(!str1, LOGARG(NULL, val1_p), 0);
42 LY_CHECK_ERR_RET(!str2, LOGARG(NULL, val2_p), 0);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020043
44 if (strncmp(str1, str2, *(size_t *)cb_data) == 0) {
45 return 1;
46 }
47
48 return 0;
49}
50
51void
52lydict_init(struct dict_table *dict)
53{
Michal Vaskod989ba02020-08-24 10:59:24 +020054 LY_CHECK_ARG_RET(NULL, dict, );
Radek Krejci5aeea3a2018-09-05 13:29:36 +020055
56 dict->hash_tab = lyht_new(1024, sizeof(struct dict_rec), lydict_val_eq, NULL, 1);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020057 LY_CHECK_ERR_RET(!dict->hash_tab, LOGINT(NULL), );
Radek Krejci5aeea3a2018-09-05 13:29:36 +020058 pthread_mutex_init(&dict->lock, NULL);
59}
60
61void
62lydict_clean(struct dict_table *dict)
63{
Michal Vasko44f3d2c2020-08-24 09:49:38 +020064 struct dict_rec *dict_rec = NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020065 struct ht_rec *rec = NULL;
66
Michal Vaskod989ba02020-08-24 10:59:24 +020067 LY_CHECK_ARG_RET(NULL, dict, );
Radek Krejci5aeea3a2018-09-05 13:29:36 +020068
Radek Krejci1deb5be2020-08-26 16:43:36 +020069 for (uint32_t i = 0; i < dict->hash_tab->size; i++) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +020070 /* get ith record */
71 rec = (struct ht_rec *)&dict->hash_tab->recs[i * dict->hash_tab->rec_size];
72 if (rec->hits == 1) {
73 /*
74 * this should not happen, all records inserted into
75 * dictionary are supposed to be removed using lydict_remove()
76 * before calling lydict_clean()
77 */
Michal Vasko44f3d2c2020-08-24 09:49:38 +020078 dict_rec = (struct dict_rec *)rec->val;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020079 LOGWRN(NULL, "String \"%s\" not freed from the dictionary, refcount %d", dict_rec->value, dict_rec->refcount);
80 /* if record wasn't removed before free string allocated for that record */
81#ifdef NDEBUG
82 free(dict_rec->value);
83#endif
84 }
85 }
86
87 /* free table and destroy mutex */
88 lyht_free(dict->hash_tab);
89 pthread_mutex_destroy(&dict->lock);
90}
91
92/*
Radek Krejci5aeea3a2018-09-05 13:29:36 +020093 * Usage:
94 * - init hash to 0
95 * - repeatedly call dict_hash_multi(), provide hash from the last call
96 * - call dict_hash_multi() with key_part = NULL to finish the hash
97 */
98uint32_t
99dict_hash_multi(uint32_t hash, const char *key_part, size_t len)
100{
101 uint32_t i;
102
103 if (key_part) {
104 for (i = 0; i < len; ++i) {
105 hash += key_part[i];
106 hash += (hash << 10);
107 hash ^= (hash >> 6);
108 }
109 } else {
110 hash += (hash << 3);
111 hash ^= (hash >> 11);
112 hash += (hash << 15);
113 }
114
115 return hash;
116}
117
Radek Krejcif2dc4c52018-11-08 09:04:13 +0100118/*
119 * Bob Jenkin's one-at-a-time hash
120 * http://www.burtleburtle.net/bob/hash/doobs.html
121 *
122 * Spooky hash is faster, but it works only for little endian architectures.
123 */
124uint32_t
125dict_hash(const char *key, size_t len)
126{
127 uint32_t hash;
128
129 hash = dict_hash_multi(0, key, len);
130 return dict_hash_multi(hash, NULL, len);
131}
132
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200133API void
Michal Vasko52927e22020-03-16 17:26:14 +0100134lydict_remove(const struct ly_ctx *ctx, const char *value)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200135{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200136 LY_ERR ret;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200137 size_t len;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200138 uint32_t hash;
139 struct dict_rec rec, *match = NULL;
140 char *val_p;
141
Michal Vasko0f6b3e22018-09-07 12:18:12 +0200142 if (!value) {
Michal Vasko0b3a4172018-09-07 12:20:18 +0200143 return;
Michal Vasko0f6b3e22018-09-07 12:18:12 +0200144 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200145
146 len = strlen(value);
147 hash = dict_hash(value, len);
148
149 /* create record for lyht_find call */
150 rec.value = (char *)value;
151 rec.refcount = 0;
152
Michal Vasko52927e22020-03-16 17:26:14 +0100153 pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200154 /* set len as data for compare callback */
155 lyht_set_cb_data(ctx->dict.hash_tab, (void *)&len);
156 /* check if value is already inserted */
157 ret = lyht_find(ctx->dict.hash_tab, &rec, hash, (void **)&match);
158
Radek Krejci1deb5be2020-08-26 16:43:36 +0200159 if (ret == LY_SUCCESS) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200160 LY_CHECK_ERR_GOTO(!match, LOGINT(ctx), finish);
161
162 /* if value is already in dictionary, decrement reference counter */
163 match->refcount--;
164 if (match->refcount == 0) {
165 /*
166 * remove record
167 * save pointer to stored string before lyht_remove to
168 * free it after it is removed from hash table
169 */
170 val_p = match->value;
171 ret = lyht_remove(ctx->dict.hash_tab, &rec, hash);
172 free(val_p);
173 LY_CHECK_ERR_GOTO(ret, LOGINT(ctx), finish);
174 }
175 }
176
177finish:
Michal Vasko52927e22020-03-16 17:26:14 +0100178 pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200179}
180
Radek Krejci011e4aa2020-09-04 15:22:31 +0200181LY_ERR
182dict_insert(const struct ly_ctx *ctx, char *value, size_t len, ly_bool zerocopy, const char **str_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200183{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200184 LY_ERR ret = LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200185 struct dict_rec *match = NULL, rec;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200186 uint32_t hash;
187
188 hash = dict_hash(value, len);
189 /* set len as data for compare callback */
190 lyht_set_cb_data(ctx->dict.hash_tab, (void *)&len);
191 /* create record for lyht_insert */
192 rec.value = value;
193 rec.refcount = 1;
194
195 LOGDBG(LY_LDGDICT, "inserting \"%s\"", rec.value);
196 ret = lyht_insert(ctx->dict.hash_tab, (void *)&rec, hash, (void **)&match);
Michal Vaskofcc96b92018-09-12 11:12:01 +0200197 if (ret == LY_EEXIST) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200198 match->refcount++;
199 if (zerocopy) {
200 free(value);
201 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200202 ret = LY_SUCCESS;
Michal Vaskofcc96b92018-09-12 11:12:01 +0200203 } else if (ret == LY_SUCCESS) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200204 if (!zerocopy) {
205 /*
206 * allocate string for new record
207 * record is already inserted in hash table
208 */
209 match->value = malloc(sizeof *match->value * (len + 1));
Radek Krejci011e4aa2020-09-04 15:22:31 +0200210 LY_CHECK_ERR_RET(!match->value, LOGMEM(ctx), LY_EMEM);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200211 memcpy(match->value, value, len);
212 match->value[len] = '\0';
213 }
214 } else {
215 /* lyht_insert returned error */
David Sedlák234a91f2019-08-15 13:16:43 +0200216 if (zerocopy) {
217 free(value);
218 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200219 return ret;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200220 }
221
Radek Krejci011e4aa2020-09-04 15:22:31 +0200222 if (str_p) {
223 *str_p = match->value;
224 }
225
226 return ret;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200227}
228
Radek Krejci011e4aa2020-09-04 15:22:31 +0200229API LY_ERR
230lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len, const char **str_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200231{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200232 LY_ERR result;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200233
Radek Krejci011e4aa2020-09-04 15:22:31 +0200234 LY_CHECK_ARG_RET(ctx, ctx, value, LY_EINVAL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200235
Radek Krejci0ae092d2018-09-20 16:43:19 +0200236 if (!len) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200237 len = strlen(value);
238 }
239
Michal Vasko52927e22020-03-16 17:26:14 +0100240 pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200241 result = dict_insert(ctx, (char *)value, len, 0, str_p);
Michal Vasko52927e22020-03-16 17:26:14 +0100242 pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200243
244 return result;
245}
246
Radek Krejci011e4aa2020-09-04 15:22:31 +0200247API LY_ERR
248lydict_insert_zc(const struct ly_ctx *ctx, char *value, const char **str_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200249{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200250 LY_ERR result;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200251
Radek Krejci011e4aa2020-09-04 15:22:31 +0200252 LY_CHECK_ARG_RET(ctx, ctx, value, LY_EINVAL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200253
Michal Vasko52927e22020-03-16 17:26:14 +0100254 pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200255 result = dict_insert(ctx, value, strlen(value), 1, str_p);
Michal Vasko52927e22020-03-16 17:26:14 +0100256 pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200257
258 return result;
259}
260
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200261struct ht_rec *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200262lyht_get_rec(unsigned char *recs, uint16_t rec_size, uint32_t idx)
263{
264 return (struct ht_rec *)&recs[idx * rec_size];
265}
266
267struct hash_table *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200268lyht_new(uint32_t size, uint16_t val_size, values_equal_cb val_equal, void *cb_data, uint16_t resize)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200269{
270 struct hash_table *ht;
271
272 /* check that 2^x == size (power of 2) */
273 assert(size && !(size & (size - 1)));
274 assert(val_equal && val_size);
275 assert(resize == 0 || resize == 1);
276
277 if (size < LYHT_MIN_SIZE) {
278 size = LYHT_MIN_SIZE;
279 }
280
281 ht = malloc(sizeof *ht);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200282 LY_CHECK_ERR_RET(!ht, LOGMEM(NULL), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200283
284 ht->used = 0;
285 ht->size = size;
286 ht->val_equal = val_equal;
287 ht->cb_data = cb_data;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200288 ht->resize = resize;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200289
290 ht->rec_size = (sizeof(struct ht_rec) - 1) + val_size;
291 /* allocate the records correctly */
292 ht->recs = calloc(size, ht->rec_size);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200293 LY_CHECK_ERR_RET(!ht->recs, free(ht); LOGMEM(NULL), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200294
295 return ht;
296}
297
298values_equal_cb
299lyht_set_cb(struct hash_table *ht, values_equal_cb new_val_equal)
300{
301 values_equal_cb prev;
302
303 prev = ht->val_equal;
304 ht->val_equal = new_val_equal;
305 return prev;
306}
307
308void *
309lyht_set_cb_data(struct hash_table *ht, void *new_cb_data)
310{
311 void *prev;
312
313 prev = ht->cb_data;
314 ht->cb_data = new_cb_data;
315 return prev;
316}
317
318struct hash_table *
319lyht_dup(const struct hash_table *orig)
320{
321 struct hash_table *ht;
322
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200323 LY_CHECK_ARG_RET(NULL, orig, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200324
325 ht = lyht_new(orig->size, orig->rec_size - (sizeof(struct ht_rec) - 1), orig->val_equal, orig->cb_data, orig->resize ? 1 : 0);
326 if (!ht) {
327 return NULL;
328 }
329
330 memcpy(ht->recs, orig->recs, orig->used * orig->rec_size);
331 ht->used = orig->used;
332 return ht;
333}
334
335void
336lyht_free(struct hash_table *ht)
337{
338 if (ht) {
339 free(ht->recs);
340 free(ht);
341 }
342}
343
344static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200345lyht_resize(struct hash_table *ht, ly_bool enlarge)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200346{
347 struct ht_rec *rec;
348 unsigned char *old_recs;
349 uint32_t i, old_size;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200350
351 old_recs = ht->recs;
352 old_size = ht->size;
353
354 if (enlarge) {
355 /* double the size */
356 ht->size <<= 1;
357 } else {
358 /* half the size */
359 ht->size >>= 1;
360 }
361
362 ht->recs = calloc(ht->size, ht->rec_size);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200363 LY_CHECK_ERR_RET(!ht->recs, LOGMEM(NULL); ht->recs = old_recs; ht->size = old_size, LY_EMEM);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200364
365 /* reset used, it will increase again */
366 ht->used = 0;
367
368 /* add all the old records into the new records array */
369 for (i = 0; i < old_size; ++i) {
370 rec = lyht_get_rec(old_recs, ht->rec_size, i);
371 if (rec->hits > 0) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200372 LY_ERR ret = lyht_insert(ht, rec->val, rec->hash, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200373 assert(!ret);
374 (void)ret;
375 }
376 }
377
378 /* final touches */
379 free(old_recs);
380 return LY_SUCCESS;
381}
382
Michal Vaskoda859032020-07-14 12:20:14 +0200383/**
384 * @brief Search for the first match.
385 *
386 * @param[in] ht Hash table to search in.
387 * @param[in] hash Hash to find.
388 * @param[out] rec_p Optional found record.
389 * @return LY_SUCCESS hash found, returned its record,
390 * @return LY_ENOTFOUND hash not found, returned the record where it would be inserted.
391 */
392static LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200393lyht_find_first(struct hash_table *ht, uint32_t hash, struct ht_rec **rec_p)
394{
395 struct ht_rec *rec;
396 uint32_t i, idx;
397
398 if (rec_p) {
399 *rec_p = NULL;
400 }
401
402 idx = i = hash & (ht->size - 1);
403 rec = lyht_get_rec(ht->recs, ht->rec_size, idx);
404
405 /* skip through overflow and deleted records */
406 while ((rec->hits != 0) && ((rec->hits == -1) || ((rec->hash & (ht->size - 1)) != idx))) {
407 if ((rec->hits == -1) && rec_p && !(*rec_p)) {
408 /* remember this record for return */
409 *rec_p = rec;
410 }
411 i = (i + 1) % ht->size;
412 if (i == idx) {
413 /* we went through all the records (very unlikely, but possible when many records are invalid),
414 * just return not found */
415 assert(!rec_p || *rec_p);
Michal Vaskoda859032020-07-14 12:20:14 +0200416 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200417 }
418 rec = lyht_get_rec(ht->recs, ht->rec_size, i);
419 }
420 if (rec->hits == 0) {
421 /* we could not find the value */
422 if (rec_p && !*rec_p) {
423 *rec_p = rec;
424 }
Michal Vaskoda859032020-07-14 12:20:14 +0200425 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200426 }
427
428 /* we have found a record with equal (shortened) hash */
429 if (rec_p) {
430 *rec_p = rec;
431 }
Michal Vaskoda859032020-07-14 12:20:14 +0200432 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200433}
434
435/**
436 * @brief Search for the next collision.
437 *
438 * @param[in] ht Hash table to search in.
439 * @param[in,out] last Last returned collision record.
440 * @param[in] first First collision record (hits > 1).
Michal Vaskoda859032020-07-14 12:20:14 +0200441 * @return LY_SUCCESS when hash collision found, \p last points to this next collision,
442 * @return LY_ENOTFOUND when hash collision not found, \p last points to the record where it would be inserted.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200443 */
Michal Vaskoda859032020-07-14 12:20:14 +0200444static LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200445lyht_find_collision(struct hash_table *ht, struct ht_rec **last, struct ht_rec *first)
446{
447 struct ht_rec *empty = NULL;
448 uint32_t i, idx;
449
450 assert(last && *last);
451
452 idx = (*last)->hash & (ht->size - 1);
453 i = (((unsigned char *)*last) - ht->recs) / ht->rec_size;
454
455 do {
456 i = (i + 1) % ht->size;
457 *last = lyht_get_rec(ht->recs, ht->rec_size, i);
458 if (*last == first) {
459 /* we went through all the records (very unlikely, but possible when many records are invalid),
460 * just return an invalid record */
461 assert(empty);
462 *last = empty;
Michal Vaskoda859032020-07-14 12:20:14 +0200463 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200464 }
465
466 if (((*last)->hits == -1) && !empty) {
467 empty = *last;
468 }
469 } while (((*last)->hits != 0) && (((*last)->hits == -1) || (((*last)->hash & (ht->size - 1)) != idx)));
470
471 if ((*last)->hits > 0) {
472 /* we found a collision */
473 assert((*last)->hits == 1);
Michal Vaskoda859032020-07-14 12:20:14 +0200474 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200475 }
476
477 /* no next collision found, return the record where it would be inserted */
478 if (empty) {
479 *last = empty;
480 } /* else (*last)->hits == 0, it is already correct */
Michal Vaskoda859032020-07-14 12:20:14 +0200481 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200482}
483
Michal Vaskoda859032020-07-14 12:20:14 +0200484/**
485 * @brief Search for a record with specific value and hash.
486 *
487 * @param[in] ht Hash table to search in.
488 * @param[in] val_p Pointer to the value to find.
489 * @param[in] hash Hash to find.
Radek Krejci857189e2020-09-01 13:26:36 +0200490 * @param[in] mod Whether the operation modifies the hash table (insert or remove) or not (find).
Michal Vaskoda859032020-07-14 12:20:14 +0200491 * @param[out] crec_p Optional found first record.
492 * @param[out] col Optional collision number of @p rec_p, 0 for no collision.
493 * @param[out] rec_p Found exact matching record, may be a collision of @p crec_p.
494 * @return LY_ENOTFOUND if no record found,
495 * @return LY_SUCCESS if record was found.
496 */
497static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200498lyht_find_rec(struct hash_table *ht, void *val_p, uint32_t hash, ly_bool mod, struct ht_rec **crec_p, uint32_t *col,
Radek Krejci0f969882020-08-21 16:56:47 +0200499 struct ht_rec **rec_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200500{
501 struct ht_rec *rec, *crec;
502 uint32_t i, c;
Michal Vaskoda859032020-07-14 12:20:14 +0200503 LY_ERR r;
504
505 if (crec_p) {
506 *crec_p = NULL;
507 }
508 if (col) {
509 *col = 0;
510 }
511 *rec_p = NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200512
513 if (lyht_find_first(ht, hash, &rec)) {
514 /* not found */
Michal Vaskoda859032020-07-14 12:20:14 +0200515 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200516 }
Michal Vaskoda859032020-07-14 12:20:14 +0200517 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, mod, ht->cb_data)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200518 /* even the value matches */
Michal Vaskoda859032020-07-14 12:20:14 +0200519 if (crec_p) {
520 *crec_p = rec;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200521 }
Michal Vaskoda859032020-07-14 12:20:14 +0200522 if (col) {
523 *col = 0;
524 }
525 *rec_p = rec;
526 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200527 }
528
529 /* some collisions, we need to go through them, too */
530 crec = rec;
Michal Vaskoda859032020-07-14 12:20:14 +0200531 c = crec->hits;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200532 for (i = 1; i < c; ++i) {
533 r = lyht_find_collision(ht, &rec, crec);
534 assert(!r);
535 (void)r;
536
537 /* compare values */
Michal Vaskoda859032020-07-14 12:20:14 +0200538 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, mod, ht->cb_data)) {
539 if (crec_p) {
540 *crec_p = crec;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200541 }
Michal Vaskoda859032020-07-14 12:20:14 +0200542 if (col) {
543 *col = i;
544 }
545 *rec_p = rec;
546 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200547 }
548 }
549
550 /* not found even in collisions */
Michal Vaskoda859032020-07-14 12:20:14 +0200551 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200552}
553
Michal Vaskoda859032020-07-14 12:20:14 +0200554LY_ERR
555lyht_find(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
556{
557 struct ht_rec *rec;
558
559 lyht_find_rec(ht, val_p, hash, 0, NULL, NULL, &rec);
560
561 if (rec && match_p) {
562 *match_p = rec->val;
563 }
564 return rec ? LY_SUCCESS : LY_ENOTFOUND;
565}
566
567LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200568lyht_find_next(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
569{
570 struct ht_rec *rec, *crec;
571 uint32_t i, c;
Michal Vaskoda859032020-07-14 12:20:14 +0200572 LY_ERR r;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200573
Michal Vaskoda859032020-07-14 12:20:14 +0200574 /* found the record of the previously found value */
575 if (lyht_find_rec(ht, val_p, hash, 1, &crec, &i, &rec)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200576 /* not found, cannot happen */
Michal Vaskoda859032020-07-14 12:20:14 +0200577 LOGINT_RET(NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200578 }
579
580 /* go through collisions and find next one after the previous one */
Michal Vaskoda859032020-07-14 12:20:14 +0200581 c = crec->hits;
582 for (++i; i < c; ++i) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200583 r = lyht_find_collision(ht, &rec, crec);
584 assert(!r);
585 (void)r;
586
Michal Vaskoda859032020-07-14 12:20:14 +0200587 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 0, ht->cb_data)) {
588 /* even the value matches */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200589 if (match_p) {
590 *match_p = rec->val;
591 }
Michal Vaskoda859032020-07-14 12:20:14 +0200592 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200593 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200594 }
595
596 /* the last equal value was already returned */
Michal Vaskoda859032020-07-14 12:20:14 +0200597 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200598}
599
600LY_ERR
601lyht_insert_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash,
Radek Krejci0f969882020-08-21 16:56:47 +0200602 values_equal_cb resize_val_equal, void **match_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200603{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200604 LY_ERR r, ret = LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200605 struct ht_rec *rec, *crec = NULL;
606 int32_t i;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200607 values_equal_cb old_val_equal;
608
609 if (!lyht_find_first(ht, hash, &rec)) {
610 /* we found matching shortened hash */
611 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
612 /* even the value matches */
613 if (match_p) {
614 *match_p = (void *)&rec->val;
615 }
616 return LY_EEXIST;
617 }
618
619 /* some collisions, we need to go through them, too */
620 crec = rec;
621 for (i = 1; i < crec->hits; ++i) {
622 r = lyht_find_collision(ht, &rec, crec);
623 assert(!r);
624
625 /* compare values */
626 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
627 if (match_p) {
628 *match_p = (void *)&rec->val;
629 }
630 return LY_EEXIST;
631 }
632 }
633
634 /* value not found, get the record where it will be inserted */
635 r = lyht_find_collision(ht, &rec, crec);
636 assert(r);
637 }
638
639 /* insert it into the returned record */
640 assert(rec->hits < 1);
641 rec->hash = hash;
642 rec->hits = 1;
643 memcpy(&rec->val, val_p, ht->rec_size - (sizeof(struct ht_rec) - 1));
644 if (match_p) {
645 *match_p = (void *)&rec->val;
646 }
647
648 if (crec) {
649 /* there was a collision, increase hits */
650 if (crec->hits == INT32_MAX) {
651 LOGINT(NULL);
652 }
653 ++crec->hits;
654 }
655
656 /* check size & enlarge if needed */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200657 ++ht->used;
658 if (ht->resize) {
659 r = (ht->used * 100) / ht->size;
660 if ((ht->resize == 1) && (r >= LYHT_FIRST_SHRINK_PERCENTAGE)) {
661 /* enable shrinking */
662 ht->resize = 2;
663 }
664 if ((ht->resize == 2) && (r >= LYHT_ENLARGE_PERCENTAGE)) {
665 if (resize_val_equal) {
666 old_val_equal = lyht_set_cb(ht, resize_val_equal);
667 }
668
669 /* enlarge */
670 ret = lyht_resize(ht, 1);
671 /* if hash_table was resized, we need to find new matching value */
672 if (ret == LY_SUCCESS && match_p) {
673 lyht_find(ht, val_p, hash, match_p);
674 }
675
676 if (resize_val_equal) {
677 lyht_set_cb(ht, old_val_equal);
678 }
679 }
680 }
681 return ret;
682}
683
Radek Krejci0ae092d2018-09-20 16:43:19 +0200684LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200685lyht_insert(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
686{
687 return lyht_insert_with_resize_cb(ht, val_p, hash, NULL, match_p);
688}
689
690LY_ERR
691lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash)
692{
693 struct ht_rec *rec, *crec;
694 int32_t i;
Radek Krejci857189e2020-09-01 13:26:36 +0200695 ly_bool first_matched = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200696 LY_ERR r, ret = LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200697
Michal Vasko4a4c7ed2020-07-17 09:30:12 +0200698 LY_CHECK_ERR_RET(lyht_find_first(ht, hash, &rec), LOGARG(NULL, hash), LY_ENOTFOUND); /* hash not found */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200699
700 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
701 /* even the value matches */
702 first_matched = 1;
703 }
704
705 /* we always need to go through collisions */
706 crec = rec;
707 for (i = 1; i < crec->hits; ++i) {
708 r = lyht_find_collision(ht, &rec, crec);
709 assert(!r);
710
711 /* compare values */
712 if (!first_matched && (rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
713 break;
714 }
715 }
716
717 if (i < crec->hits) {
718 /* one of collisions matched, reduce collision count, remove the record */
719 assert(!first_matched);
720 --crec->hits;
721 rec->hits = -1;
722 } else if (first_matched) {
723 /* the first record matches */
724 if (crec != rec) {
725 /* ... so put the last collision in its place */
726 rec->hits = crec->hits - 1;
727 memcpy(crec, rec, ht->rec_size);
728 }
729 rec->hits = -1;
730 } else {
731 /* value not found even in collisions */
Michal Vasko4a4c7ed2020-07-17 09:30:12 +0200732 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200733 }
734
735 /* check size & shrink if needed */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200736 --ht->used;
737 if (ht->resize == 2) {
738 r = (ht->used * 100) / ht->size;
739 if ((r < LYHT_SHRINK_PERCENTAGE) && (ht->size > LYHT_MIN_SIZE)) {
740 /* shrink */
741 ret = lyht_resize(ht, 0);
742 }
743 }
744
745 return ret;
746}