blob: 9d34ca9ba0bd23b275df088ae2fee71c3f4ac3e6 [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
181static char *
Radek Krejci857189e2020-09-01 13:26:36 +0200182dict_insert(const struct ly_ctx *ctx, char *value, size_t len, ly_bool zerocopy)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200183{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200184 LY_ERR ret = 0;
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 }
Michal Vaskofcc96b92018-09-12 11:12:01 +0200202 } else if (ret == LY_SUCCESS) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200203 if (!zerocopy) {
204 /*
205 * allocate string for new record
206 * record is already inserted in hash table
207 */
208 match->value = malloc(sizeof *match->value * (len + 1));
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200209 LY_CHECK_ERR_RET(!match->value, LOGMEM(ctx), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200210 memcpy(match->value, value, len);
211 match->value[len] = '\0';
212 }
213 } else {
214 /* lyht_insert returned error */
215 LOGINT(ctx);
David Sedlák234a91f2019-08-15 13:16:43 +0200216 if (zerocopy) {
217 free(value);
218 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200219 return NULL;
220 }
221
222 return match->value;
223}
224
225API const char *
Michal Vasko52927e22020-03-16 17:26:14 +0100226lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200227{
228 const char *result;
229
Radek Krejci0ae092d2018-09-20 16:43:19 +0200230 LY_CHECK_ARG_RET(ctx, ctx, value, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200231
Radek Krejci0ae092d2018-09-20 16:43:19 +0200232 if (!len) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200233 len = strlen(value);
234 }
235
Michal Vasko52927e22020-03-16 17:26:14 +0100236 pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200237 result = dict_insert(ctx, (char *)value, len, 0);
Michal Vasko52927e22020-03-16 17:26:14 +0100238 pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200239
240 return result;
241}
242
243API const char *
Michal Vasko52927e22020-03-16 17:26:14 +0100244lydict_insert_zc(const struct ly_ctx *ctx, char *value)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200245{
246 const char *result;
247
Radek Krejci0ae092d2018-09-20 16:43:19 +0200248 LY_CHECK_ARG_RET(ctx, ctx, value, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200249
Michal Vasko52927e22020-03-16 17:26:14 +0100250 pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200251 result = dict_insert(ctx, value, strlen(value), 1);
Michal Vasko52927e22020-03-16 17:26:14 +0100252 pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200253
254 return result;
255}
256
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200257struct ht_rec *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200258lyht_get_rec(unsigned char *recs, uint16_t rec_size, uint32_t idx)
259{
260 return (struct ht_rec *)&recs[idx * rec_size];
261}
262
263struct hash_table *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200264lyht_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 +0200265{
266 struct hash_table *ht;
267
268 /* check that 2^x == size (power of 2) */
269 assert(size && !(size & (size - 1)));
270 assert(val_equal && val_size);
271 assert(resize == 0 || resize == 1);
272
273 if (size < LYHT_MIN_SIZE) {
274 size = LYHT_MIN_SIZE;
275 }
276
277 ht = malloc(sizeof *ht);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200278 LY_CHECK_ERR_RET(!ht, LOGMEM(NULL), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200279
280 ht->used = 0;
281 ht->size = size;
282 ht->val_equal = val_equal;
283 ht->cb_data = cb_data;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200284 ht->resize = resize;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200285
286 ht->rec_size = (sizeof(struct ht_rec) - 1) + val_size;
287 /* allocate the records correctly */
288 ht->recs = calloc(size, ht->rec_size);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200289 LY_CHECK_ERR_RET(!ht->recs, free(ht); LOGMEM(NULL), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200290
291 return ht;
292}
293
294values_equal_cb
295lyht_set_cb(struct hash_table *ht, values_equal_cb new_val_equal)
296{
297 values_equal_cb prev;
298
299 prev = ht->val_equal;
300 ht->val_equal = new_val_equal;
301 return prev;
302}
303
304void *
305lyht_set_cb_data(struct hash_table *ht, void *new_cb_data)
306{
307 void *prev;
308
309 prev = ht->cb_data;
310 ht->cb_data = new_cb_data;
311 return prev;
312}
313
314struct hash_table *
315lyht_dup(const struct hash_table *orig)
316{
317 struct hash_table *ht;
318
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200319 LY_CHECK_ARG_RET(NULL, orig, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200320
321 ht = lyht_new(orig->size, orig->rec_size - (sizeof(struct ht_rec) - 1), orig->val_equal, orig->cb_data, orig->resize ? 1 : 0);
322 if (!ht) {
323 return NULL;
324 }
325
326 memcpy(ht->recs, orig->recs, orig->used * orig->rec_size);
327 ht->used = orig->used;
328 return ht;
329}
330
331void
332lyht_free(struct hash_table *ht)
333{
334 if (ht) {
335 free(ht->recs);
336 free(ht);
337 }
338}
339
340static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200341lyht_resize(struct hash_table *ht, ly_bool enlarge)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200342{
343 struct ht_rec *rec;
344 unsigned char *old_recs;
345 uint32_t i, old_size;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200346
347 old_recs = ht->recs;
348 old_size = ht->size;
349
350 if (enlarge) {
351 /* double the size */
352 ht->size <<= 1;
353 } else {
354 /* half the size */
355 ht->size >>= 1;
356 }
357
358 ht->recs = calloc(ht->size, ht->rec_size);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200359 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 +0200360
361 /* reset used, it will increase again */
362 ht->used = 0;
363
364 /* add all the old records into the new records array */
365 for (i = 0; i < old_size; ++i) {
366 rec = lyht_get_rec(old_recs, ht->rec_size, i);
367 if (rec->hits > 0) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200368 LY_ERR ret = lyht_insert(ht, rec->val, rec->hash, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200369 assert(!ret);
370 (void)ret;
371 }
372 }
373
374 /* final touches */
375 free(old_recs);
376 return LY_SUCCESS;
377}
378
Michal Vaskoda859032020-07-14 12:20:14 +0200379/**
380 * @brief Search for the first match.
381 *
382 * @param[in] ht Hash table to search in.
383 * @param[in] hash Hash to find.
384 * @param[out] rec_p Optional found record.
385 * @return LY_SUCCESS hash found, returned its record,
386 * @return LY_ENOTFOUND hash not found, returned the record where it would be inserted.
387 */
388static LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200389lyht_find_first(struct hash_table *ht, uint32_t hash, struct ht_rec **rec_p)
390{
391 struct ht_rec *rec;
392 uint32_t i, idx;
393
394 if (rec_p) {
395 *rec_p = NULL;
396 }
397
398 idx = i = hash & (ht->size - 1);
399 rec = lyht_get_rec(ht->recs, ht->rec_size, idx);
400
401 /* skip through overflow and deleted records */
402 while ((rec->hits != 0) && ((rec->hits == -1) || ((rec->hash & (ht->size - 1)) != idx))) {
403 if ((rec->hits == -1) && rec_p && !(*rec_p)) {
404 /* remember this record for return */
405 *rec_p = rec;
406 }
407 i = (i + 1) % ht->size;
408 if (i == idx) {
409 /* we went through all the records (very unlikely, but possible when many records are invalid),
410 * just return not found */
411 assert(!rec_p || *rec_p);
Michal Vaskoda859032020-07-14 12:20:14 +0200412 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200413 }
414 rec = lyht_get_rec(ht->recs, ht->rec_size, i);
415 }
416 if (rec->hits == 0) {
417 /* we could not find the value */
418 if (rec_p && !*rec_p) {
419 *rec_p = rec;
420 }
Michal Vaskoda859032020-07-14 12:20:14 +0200421 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200422 }
423
424 /* we have found a record with equal (shortened) hash */
425 if (rec_p) {
426 *rec_p = rec;
427 }
Michal Vaskoda859032020-07-14 12:20:14 +0200428 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200429}
430
431/**
432 * @brief Search for the next collision.
433 *
434 * @param[in] ht Hash table to search in.
435 * @param[in,out] last Last returned collision record.
436 * @param[in] first First collision record (hits > 1).
Michal Vaskoda859032020-07-14 12:20:14 +0200437 * @return LY_SUCCESS when hash collision found, \p last points to this next collision,
438 * @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 +0200439 */
Michal Vaskoda859032020-07-14 12:20:14 +0200440static LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200441lyht_find_collision(struct hash_table *ht, struct ht_rec **last, struct ht_rec *first)
442{
443 struct ht_rec *empty = NULL;
444 uint32_t i, idx;
445
446 assert(last && *last);
447
448 idx = (*last)->hash & (ht->size - 1);
449 i = (((unsigned char *)*last) - ht->recs) / ht->rec_size;
450
451 do {
452 i = (i + 1) % ht->size;
453 *last = lyht_get_rec(ht->recs, ht->rec_size, i);
454 if (*last == first) {
455 /* we went through all the records (very unlikely, but possible when many records are invalid),
456 * just return an invalid record */
457 assert(empty);
458 *last = empty;
Michal Vaskoda859032020-07-14 12:20:14 +0200459 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200460 }
461
462 if (((*last)->hits == -1) && !empty) {
463 empty = *last;
464 }
465 } while (((*last)->hits != 0) && (((*last)->hits == -1) || (((*last)->hash & (ht->size - 1)) != idx)));
466
467 if ((*last)->hits > 0) {
468 /* we found a collision */
469 assert((*last)->hits == 1);
Michal Vaskoda859032020-07-14 12:20:14 +0200470 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200471 }
472
473 /* no next collision found, return the record where it would be inserted */
474 if (empty) {
475 *last = empty;
476 } /* else (*last)->hits == 0, it is already correct */
Michal Vaskoda859032020-07-14 12:20:14 +0200477 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200478}
479
Michal Vaskoda859032020-07-14 12:20:14 +0200480/**
481 * @brief Search for a record with specific value and hash.
482 *
483 * @param[in] ht Hash table to search in.
484 * @param[in] val_p Pointer to the value to find.
485 * @param[in] hash Hash to find.
Radek Krejci857189e2020-09-01 13:26:36 +0200486 * @param[in] mod Whether the operation modifies the hash table (insert or remove) or not (find).
Michal Vaskoda859032020-07-14 12:20:14 +0200487 * @param[out] crec_p Optional found first record.
488 * @param[out] col Optional collision number of @p rec_p, 0 for no collision.
489 * @param[out] rec_p Found exact matching record, may be a collision of @p crec_p.
490 * @return LY_ENOTFOUND if no record found,
491 * @return LY_SUCCESS if record was found.
492 */
493static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200494lyht_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 +0200495 struct ht_rec **rec_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200496{
497 struct ht_rec *rec, *crec;
498 uint32_t i, c;
Michal Vaskoda859032020-07-14 12:20:14 +0200499 LY_ERR r;
500
501 if (crec_p) {
502 *crec_p = NULL;
503 }
504 if (col) {
505 *col = 0;
506 }
507 *rec_p = NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200508
509 if (lyht_find_first(ht, hash, &rec)) {
510 /* not found */
Michal Vaskoda859032020-07-14 12:20:14 +0200511 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200512 }
Michal Vaskoda859032020-07-14 12:20:14 +0200513 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, mod, ht->cb_data)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200514 /* even the value matches */
Michal Vaskoda859032020-07-14 12:20:14 +0200515 if (crec_p) {
516 *crec_p = rec;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200517 }
Michal Vaskoda859032020-07-14 12:20:14 +0200518 if (col) {
519 *col = 0;
520 }
521 *rec_p = rec;
522 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200523 }
524
525 /* some collisions, we need to go through them, too */
526 crec = rec;
Michal Vaskoda859032020-07-14 12:20:14 +0200527 c = crec->hits;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200528 for (i = 1; i < c; ++i) {
529 r = lyht_find_collision(ht, &rec, crec);
530 assert(!r);
531 (void)r;
532
533 /* compare values */
Michal Vaskoda859032020-07-14 12:20:14 +0200534 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, mod, ht->cb_data)) {
535 if (crec_p) {
536 *crec_p = crec;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200537 }
Michal Vaskoda859032020-07-14 12:20:14 +0200538 if (col) {
539 *col = i;
540 }
541 *rec_p = rec;
542 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200543 }
544 }
545
546 /* not found even in collisions */
Michal Vaskoda859032020-07-14 12:20:14 +0200547 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200548}
549
Michal Vaskoda859032020-07-14 12:20:14 +0200550LY_ERR
551lyht_find(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
552{
553 struct ht_rec *rec;
554
555 lyht_find_rec(ht, val_p, hash, 0, NULL, NULL, &rec);
556
557 if (rec && match_p) {
558 *match_p = rec->val;
559 }
560 return rec ? LY_SUCCESS : LY_ENOTFOUND;
561}
562
563LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200564lyht_find_next(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
565{
566 struct ht_rec *rec, *crec;
567 uint32_t i, c;
Michal Vaskoda859032020-07-14 12:20:14 +0200568 LY_ERR r;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200569
Michal Vaskoda859032020-07-14 12:20:14 +0200570 /* found the record of the previously found value */
571 if (lyht_find_rec(ht, val_p, hash, 1, &crec, &i, &rec)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200572 /* not found, cannot happen */
Michal Vaskoda859032020-07-14 12:20:14 +0200573 LOGINT_RET(NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200574 }
575
576 /* go through collisions and find next one after the previous one */
Michal Vaskoda859032020-07-14 12:20:14 +0200577 c = crec->hits;
578 for (++i; i < c; ++i) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200579 r = lyht_find_collision(ht, &rec, crec);
580 assert(!r);
581 (void)r;
582
Michal Vaskoda859032020-07-14 12:20:14 +0200583 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 0, ht->cb_data)) {
584 /* even the value matches */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200585 if (match_p) {
586 *match_p = rec->val;
587 }
Michal Vaskoda859032020-07-14 12:20:14 +0200588 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200589 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200590 }
591
592 /* the last equal value was already returned */
Michal Vaskoda859032020-07-14 12:20:14 +0200593 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200594}
595
596LY_ERR
597lyht_insert_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash,
Radek Krejci0f969882020-08-21 16:56:47 +0200598 values_equal_cb resize_val_equal, void **match_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200599{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200600 LY_ERR r, ret = LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200601 struct ht_rec *rec, *crec = NULL;
602 int32_t i;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200603 values_equal_cb old_val_equal;
604
605 if (!lyht_find_first(ht, hash, &rec)) {
606 /* we found matching shortened hash */
607 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
608 /* even the value matches */
609 if (match_p) {
610 *match_p = (void *)&rec->val;
611 }
612 return LY_EEXIST;
613 }
614
615 /* some collisions, we need to go through them, too */
616 crec = rec;
617 for (i = 1; i < crec->hits; ++i) {
618 r = lyht_find_collision(ht, &rec, crec);
619 assert(!r);
620
621 /* compare values */
622 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
623 if (match_p) {
624 *match_p = (void *)&rec->val;
625 }
626 return LY_EEXIST;
627 }
628 }
629
630 /* value not found, get the record where it will be inserted */
631 r = lyht_find_collision(ht, &rec, crec);
632 assert(r);
633 }
634
635 /* insert it into the returned record */
636 assert(rec->hits < 1);
637 rec->hash = hash;
638 rec->hits = 1;
639 memcpy(&rec->val, val_p, ht->rec_size - (sizeof(struct ht_rec) - 1));
640 if (match_p) {
641 *match_p = (void *)&rec->val;
642 }
643
644 if (crec) {
645 /* there was a collision, increase hits */
646 if (crec->hits == INT32_MAX) {
647 LOGINT(NULL);
648 }
649 ++crec->hits;
650 }
651
652 /* check size & enlarge if needed */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200653 ++ht->used;
654 if (ht->resize) {
655 r = (ht->used * 100) / ht->size;
656 if ((ht->resize == 1) && (r >= LYHT_FIRST_SHRINK_PERCENTAGE)) {
657 /* enable shrinking */
658 ht->resize = 2;
659 }
660 if ((ht->resize == 2) && (r >= LYHT_ENLARGE_PERCENTAGE)) {
661 if (resize_val_equal) {
662 old_val_equal = lyht_set_cb(ht, resize_val_equal);
663 }
664
665 /* enlarge */
666 ret = lyht_resize(ht, 1);
667 /* if hash_table was resized, we need to find new matching value */
668 if (ret == LY_SUCCESS && match_p) {
669 lyht_find(ht, val_p, hash, match_p);
670 }
671
672 if (resize_val_equal) {
673 lyht_set_cb(ht, old_val_equal);
674 }
675 }
676 }
677 return ret;
678}
679
Radek Krejci0ae092d2018-09-20 16:43:19 +0200680LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200681lyht_insert(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
682{
683 return lyht_insert_with_resize_cb(ht, val_p, hash, NULL, match_p);
684}
685
686LY_ERR
687lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash)
688{
689 struct ht_rec *rec, *crec;
690 int32_t i;
Radek Krejci857189e2020-09-01 13:26:36 +0200691 ly_bool first_matched = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200692 LY_ERR r, ret = LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200693
Michal Vasko4a4c7ed2020-07-17 09:30:12 +0200694 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 +0200695
696 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
697 /* even the value matches */
698 first_matched = 1;
699 }
700
701 /* we always need to go through collisions */
702 crec = rec;
703 for (i = 1; i < crec->hits; ++i) {
704 r = lyht_find_collision(ht, &rec, crec);
705 assert(!r);
706
707 /* compare values */
708 if (!first_matched && (rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
709 break;
710 }
711 }
712
713 if (i < crec->hits) {
714 /* one of collisions matched, reduce collision count, remove the record */
715 assert(!first_matched);
716 --crec->hits;
717 rec->hits = -1;
718 } else if (first_matched) {
719 /* the first record matches */
720 if (crec != rec) {
721 /* ... so put the last collision in its place */
722 rec->hits = crec->hits - 1;
723 memcpy(crec, rec, ht->rec_size);
724 }
725 rec->hits = -1;
726 } else {
727 /* value not found even in collisions */
Michal Vasko4a4c7ed2020-07-17 09:30:12 +0200728 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200729 }
730
731 /* check size & shrink if needed */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200732 --ht->used;
733 if (ht->resize == 2) {
734 r = (ht->used * 100) / ht->size;
735 if ((r < LYHT_SHRINK_PERCENTAGE) && (ht->size > LYHT_MIN_SIZE)) {
736 /* shrink */
737 ret = lyht_resize(ht, 0);
738 }
739 }
740
741 return ret;
742}