blob: bc6ec8b2657c2463377a909b963aef9c137aec57 [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 Krejci5aeea3a2018-09-05 13:29:36 +020026
Radek Krejci1deb5be2020-08-26 16:43:36 +020027static uint8_t
28lydict_val_eq(void *val1_p, void *val2_p, uint8_t UNUSED(mod), void *cb_data)
Radek Krejci5aeea3a2018-09-05 13:29:36 +020029{
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020030 LY_CHECK_ARG_RET(NULL, val1_p, val2_p, cb_data, 0);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020031
32 const char *str1 = ((struct dict_rec *)val1_p)->value;
33 const char *str2 = ((struct dict_rec *)val2_p)->value;
34
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020035 LY_CHECK_ERR_RET(!str1, LOGARG(NULL, val1_p), 0);
36 LY_CHECK_ERR_RET(!str2, LOGARG(NULL, val2_p), 0);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020037
38 if (strncmp(str1, str2, *(size_t *)cb_data) == 0) {
39 return 1;
40 }
41
42 return 0;
43}
44
45void
46lydict_init(struct dict_table *dict)
47{
Michal Vaskod989ba02020-08-24 10:59:24 +020048 LY_CHECK_ARG_RET(NULL, dict, );
Radek Krejci5aeea3a2018-09-05 13:29:36 +020049
50 dict->hash_tab = lyht_new(1024, sizeof(struct dict_rec), lydict_val_eq, NULL, 1);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020051 LY_CHECK_ERR_RET(!dict->hash_tab, LOGINT(NULL), );
Radek Krejci5aeea3a2018-09-05 13:29:36 +020052 pthread_mutex_init(&dict->lock, NULL);
53}
54
55void
56lydict_clean(struct dict_table *dict)
57{
Michal Vasko44f3d2c2020-08-24 09:49:38 +020058 struct dict_rec *dict_rec = NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020059 struct ht_rec *rec = NULL;
60
Michal Vaskod989ba02020-08-24 10:59:24 +020061 LY_CHECK_ARG_RET(NULL, dict, );
Radek Krejci5aeea3a2018-09-05 13:29:36 +020062
Radek Krejci1deb5be2020-08-26 16:43:36 +020063 for (uint32_t i = 0; i < dict->hash_tab->size; i++) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +020064 /* get ith record */
65 rec = (struct ht_rec *)&dict->hash_tab->recs[i * dict->hash_tab->rec_size];
66 if (rec->hits == 1) {
67 /*
68 * this should not happen, all records inserted into
69 * dictionary are supposed to be removed using lydict_remove()
70 * before calling lydict_clean()
71 */
Michal Vasko44f3d2c2020-08-24 09:49:38 +020072 dict_rec = (struct dict_rec *)rec->val;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020073 LOGWRN(NULL, "String \"%s\" not freed from the dictionary, refcount %d", dict_rec->value, dict_rec->refcount);
74 /* if record wasn't removed before free string allocated for that record */
75#ifdef NDEBUG
76 free(dict_rec->value);
77#endif
78 }
79 }
80
81 /* free table and destroy mutex */
82 lyht_free(dict->hash_tab);
83 pthread_mutex_destroy(&dict->lock);
84}
85
86/*
Radek Krejci5aeea3a2018-09-05 13:29:36 +020087 * Usage:
88 * - init hash to 0
89 * - repeatedly call dict_hash_multi(), provide hash from the last call
90 * - call dict_hash_multi() with key_part = NULL to finish the hash
91 */
92uint32_t
93dict_hash_multi(uint32_t hash, const char *key_part, size_t len)
94{
95 uint32_t i;
96
97 if (key_part) {
98 for (i = 0; i < len; ++i) {
99 hash += key_part[i];
100 hash += (hash << 10);
101 hash ^= (hash >> 6);
102 }
103 } else {
104 hash += (hash << 3);
105 hash ^= (hash >> 11);
106 hash += (hash << 15);
107 }
108
109 return hash;
110}
111
Radek Krejcif2dc4c52018-11-08 09:04:13 +0100112/*
113 * Bob Jenkin's one-at-a-time hash
114 * http://www.burtleburtle.net/bob/hash/doobs.html
115 *
116 * Spooky hash is faster, but it works only for little endian architectures.
117 */
118uint32_t
119dict_hash(const char *key, size_t len)
120{
121 uint32_t hash;
122
123 hash = dict_hash_multi(0, key, len);
124 return dict_hash_multi(hash, NULL, len);
125}
126
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200127API void
Michal Vasko52927e22020-03-16 17:26:14 +0100128lydict_remove(const struct ly_ctx *ctx, const char *value)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200129{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200130 LY_ERR ret;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200131 size_t len;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200132 uint32_t hash;
133 struct dict_rec rec, *match = NULL;
134 char *val_p;
135
Michal Vasko0f6b3e22018-09-07 12:18:12 +0200136 if (!value) {
Michal Vasko0b3a4172018-09-07 12:20:18 +0200137 return;
Michal Vasko0f6b3e22018-09-07 12:18:12 +0200138 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200139
140 len = strlen(value);
141 hash = dict_hash(value, len);
142
143 /* create record for lyht_find call */
144 rec.value = (char *)value;
145 rec.refcount = 0;
146
Michal Vasko52927e22020-03-16 17:26:14 +0100147 pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200148 /* set len as data for compare callback */
149 lyht_set_cb_data(ctx->dict.hash_tab, (void *)&len);
150 /* check if value is already inserted */
151 ret = lyht_find(ctx->dict.hash_tab, &rec, hash, (void **)&match);
152
Radek Krejci1deb5be2020-08-26 16:43:36 +0200153 if (ret == LY_SUCCESS) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200154 LY_CHECK_ERR_GOTO(!match, LOGINT(ctx), finish);
155
156 /* if value is already in dictionary, decrement reference counter */
157 match->refcount--;
158 if (match->refcount == 0) {
159 /*
160 * remove record
161 * save pointer to stored string before lyht_remove to
162 * free it after it is removed from hash table
163 */
164 val_p = match->value;
165 ret = lyht_remove(ctx->dict.hash_tab, &rec, hash);
166 free(val_p);
167 LY_CHECK_ERR_GOTO(ret, LOGINT(ctx), finish);
168 }
169 }
170
171finish:
Michal Vasko52927e22020-03-16 17:26:14 +0100172 pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200173}
174
175static char *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200176dict_insert(const struct ly_ctx *ctx, char *value, size_t len, uint8_t zerocopy)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200177{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200178 LY_ERR ret = 0;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200179 struct dict_rec *match = NULL, rec;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200180 uint32_t hash;
181
182 hash = dict_hash(value, len);
183 /* set len as data for compare callback */
184 lyht_set_cb_data(ctx->dict.hash_tab, (void *)&len);
185 /* create record for lyht_insert */
186 rec.value = value;
187 rec.refcount = 1;
188
189 LOGDBG(LY_LDGDICT, "inserting \"%s\"", rec.value);
190 ret = lyht_insert(ctx->dict.hash_tab, (void *)&rec, hash, (void **)&match);
Michal Vaskofcc96b92018-09-12 11:12:01 +0200191 if (ret == LY_EEXIST) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200192 match->refcount++;
193 if (zerocopy) {
194 free(value);
195 }
Michal Vaskofcc96b92018-09-12 11:12:01 +0200196 } else if (ret == LY_SUCCESS) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200197 if (!zerocopy) {
198 /*
199 * allocate string for new record
200 * record is already inserted in hash table
201 */
202 match->value = malloc(sizeof *match->value * (len + 1));
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200203 LY_CHECK_ERR_RET(!match->value, LOGMEM(ctx), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200204 memcpy(match->value, value, len);
205 match->value[len] = '\0';
206 }
207 } else {
208 /* lyht_insert returned error */
209 LOGINT(ctx);
David Sedlák234a91f2019-08-15 13:16:43 +0200210 if (zerocopy) {
211 free(value);
212 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200213 return NULL;
214 }
215
216 return match->value;
217}
218
219API const char *
Michal Vasko52927e22020-03-16 17:26:14 +0100220lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200221{
222 const char *result;
223
Radek Krejci0ae092d2018-09-20 16:43:19 +0200224 LY_CHECK_ARG_RET(ctx, ctx, value, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200225
Radek Krejci0ae092d2018-09-20 16:43:19 +0200226 if (!len) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200227 len = strlen(value);
228 }
229
Michal Vasko52927e22020-03-16 17:26:14 +0100230 pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200231 result = dict_insert(ctx, (char *)value, len, 0);
Michal Vasko52927e22020-03-16 17:26:14 +0100232 pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200233
234 return result;
235}
236
237API const char *
Michal Vasko52927e22020-03-16 17:26:14 +0100238lydict_insert_zc(const struct ly_ctx *ctx, char *value)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200239{
240 const char *result;
241
Radek Krejci0ae092d2018-09-20 16:43:19 +0200242 LY_CHECK_ARG_RET(ctx, ctx, value, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200243
Michal Vasko52927e22020-03-16 17:26:14 +0100244 pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200245 result = dict_insert(ctx, value, strlen(value), 1);
Michal Vasko52927e22020-03-16 17:26:14 +0100246 pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200247
248 return result;
249}
250
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200251struct ht_rec *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200252lyht_get_rec(unsigned char *recs, uint16_t rec_size, uint32_t idx)
253{
254 return (struct ht_rec *)&recs[idx * rec_size];
255}
256
257struct hash_table *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200258lyht_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 +0200259{
260 struct hash_table *ht;
261
262 /* check that 2^x == size (power of 2) */
263 assert(size && !(size & (size - 1)));
264 assert(val_equal && val_size);
265 assert(resize == 0 || resize == 1);
266
267 if (size < LYHT_MIN_SIZE) {
268 size = LYHT_MIN_SIZE;
269 }
270
271 ht = malloc(sizeof *ht);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200272 LY_CHECK_ERR_RET(!ht, LOGMEM(NULL), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200273
274 ht->used = 0;
275 ht->size = size;
276 ht->val_equal = val_equal;
277 ht->cb_data = cb_data;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200278 ht->resize = resize;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200279
280 ht->rec_size = (sizeof(struct ht_rec) - 1) + val_size;
281 /* allocate the records correctly */
282 ht->recs = calloc(size, ht->rec_size);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200283 LY_CHECK_ERR_RET(!ht->recs, free(ht); LOGMEM(NULL), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200284
285 return ht;
286}
287
288values_equal_cb
289lyht_set_cb(struct hash_table *ht, values_equal_cb new_val_equal)
290{
291 values_equal_cb prev;
292
293 prev = ht->val_equal;
294 ht->val_equal = new_val_equal;
295 return prev;
296}
297
298void *
299lyht_set_cb_data(struct hash_table *ht, void *new_cb_data)
300{
301 void *prev;
302
303 prev = ht->cb_data;
304 ht->cb_data = new_cb_data;
305 return prev;
306}
307
308struct hash_table *
309lyht_dup(const struct hash_table *orig)
310{
311 struct hash_table *ht;
312
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200313 LY_CHECK_ARG_RET(NULL, orig, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200314
315 ht = lyht_new(orig->size, orig->rec_size - (sizeof(struct ht_rec) - 1), orig->val_equal, orig->cb_data, orig->resize ? 1 : 0);
316 if (!ht) {
317 return NULL;
318 }
319
320 memcpy(ht->recs, orig->recs, orig->used * orig->rec_size);
321 ht->used = orig->used;
322 return ht;
323}
324
325void
326lyht_free(struct hash_table *ht)
327{
328 if (ht) {
329 free(ht->recs);
330 free(ht);
331 }
332}
333
334static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200335lyht_resize(struct hash_table *ht, uint8_t enlarge)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200336{
337 struct ht_rec *rec;
338 unsigned char *old_recs;
339 uint32_t i, old_size;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200340
341 old_recs = ht->recs;
342 old_size = ht->size;
343
344 if (enlarge) {
345 /* double the size */
346 ht->size <<= 1;
347 } else {
348 /* half the size */
349 ht->size >>= 1;
350 }
351
352 ht->recs = calloc(ht->size, ht->rec_size);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200353 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 +0200354
355 /* reset used, it will increase again */
356 ht->used = 0;
357
358 /* add all the old records into the new records array */
359 for (i = 0; i < old_size; ++i) {
360 rec = lyht_get_rec(old_recs, ht->rec_size, i);
361 if (rec->hits > 0) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200362 LY_ERR ret = lyht_insert(ht, rec->val, rec->hash, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200363 assert(!ret);
364 (void)ret;
365 }
366 }
367
368 /* final touches */
369 free(old_recs);
370 return LY_SUCCESS;
371}
372
Michal Vaskoda859032020-07-14 12:20:14 +0200373/**
374 * @brief Search for the first match.
375 *
376 * @param[in] ht Hash table to search in.
377 * @param[in] hash Hash to find.
378 * @param[out] rec_p Optional found record.
379 * @return LY_SUCCESS hash found, returned its record,
380 * @return LY_ENOTFOUND hash not found, returned the record where it would be inserted.
381 */
382static LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200383lyht_find_first(struct hash_table *ht, uint32_t hash, struct ht_rec **rec_p)
384{
385 struct ht_rec *rec;
386 uint32_t i, idx;
387
388 if (rec_p) {
389 *rec_p = NULL;
390 }
391
392 idx = i = hash & (ht->size - 1);
393 rec = lyht_get_rec(ht->recs, ht->rec_size, idx);
394
395 /* skip through overflow and deleted records */
396 while ((rec->hits != 0) && ((rec->hits == -1) || ((rec->hash & (ht->size - 1)) != idx))) {
397 if ((rec->hits == -1) && rec_p && !(*rec_p)) {
398 /* remember this record for return */
399 *rec_p = rec;
400 }
401 i = (i + 1) % ht->size;
402 if (i == idx) {
403 /* we went through all the records (very unlikely, but possible when many records are invalid),
404 * just return not found */
405 assert(!rec_p || *rec_p);
Michal Vaskoda859032020-07-14 12:20:14 +0200406 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200407 }
408 rec = lyht_get_rec(ht->recs, ht->rec_size, i);
409 }
410 if (rec->hits == 0) {
411 /* we could not find the value */
412 if (rec_p && !*rec_p) {
413 *rec_p = rec;
414 }
Michal Vaskoda859032020-07-14 12:20:14 +0200415 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200416 }
417
418 /* we have found a record with equal (shortened) hash */
419 if (rec_p) {
420 *rec_p = rec;
421 }
Michal Vaskoda859032020-07-14 12:20:14 +0200422 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200423}
424
425/**
426 * @brief Search for the next collision.
427 *
428 * @param[in] ht Hash table to search in.
429 * @param[in,out] last Last returned collision record.
430 * @param[in] first First collision record (hits > 1).
Michal Vaskoda859032020-07-14 12:20:14 +0200431 * @return LY_SUCCESS when hash collision found, \p last points to this next collision,
432 * @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 +0200433 */
Michal Vaskoda859032020-07-14 12:20:14 +0200434static LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200435lyht_find_collision(struct hash_table *ht, struct ht_rec **last, struct ht_rec *first)
436{
437 struct ht_rec *empty = NULL;
438 uint32_t i, idx;
439
440 assert(last && *last);
441
442 idx = (*last)->hash & (ht->size - 1);
443 i = (((unsigned char *)*last) - ht->recs) / ht->rec_size;
444
445 do {
446 i = (i + 1) % ht->size;
447 *last = lyht_get_rec(ht->recs, ht->rec_size, i);
448 if (*last == first) {
449 /* we went through all the records (very unlikely, but possible when many records are invalid),
450 * just return an invalid record */
451 assert(empty);
452 *last = empty;
Michal Vaskoda859032020-07-14 12:20:14 +0200453 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200454 }
455
456 if (((*last)->hits == -1) && !empty) {
457 empty = *last;
458 }
459 } while (((*last)->hits != 0) && (((*last)->hits == -1) || (((*last)->hash & (ht->size - 1)) != idx)));
460
461 if ((*last)->hits > 0) {
462 /* we found a collision */
463 assert((*last)->hits == 1);
Michal Vaskoda859032020-07-14 12:20:14 +0200464 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200465 }
466
467 /* no next collision found, return the record where it would be inserted */
468 if (empty) {
469 *last = empty;
470 } /* else (*last)->hits == 0, it is already correct */
Michal Vaskoda859032020-07-14 12:20:14 +0200471 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200472}
473
Michal Vaskoda859032020-07-14 12:20:14 +0200474/**
475 * @brief Search for a record with specific value and hash.
476 *
477 * @param[in] ht Hash table to search in.
478 * @param[in] val_p Pointer to the value to find.
479 * @param[in] hash Hash to find.
480 * @param[in] mod Operation kind for the val_equal callback.
481 * @param[out] crec_p Optional found first record.
482 * @param[out] col Optional collision number of @p rec_p, 0 for no collision.
483 * @param[out] rec_p Found exact matching record, may be a collision of @p crec_p.
484 * @return LY_ENOTFOUND if no record found,
485 * @return LY_SUCCESS if record was found.
486 */
487static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200488lyht_find_rec(struct hash_table *ht, void *val_p, uint32_t hash, uint8_t mod, struct ht_rec **crec_p, uint32_t *col,
Radek Krejci0f969882020-08-21 16:56:47 +0200489 struct ht_rec **rec_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200490{
491 struct ht_rec *rec, *crec;
492 uint32_t i, c;
Michal Vaskoda859032020-07-14 12:20:14 +0200493 LY_ERR r;
494
495 if (crec_p) {
496 *crec_p = NULL;
497 }
498 if (col) {
499 *col = 0;
500 }
501 *rec_p = NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200502
503 if (lyht_find_first(ht, hash, &rec)) {
504 /* not found */
Michal Vaskoda859032020-07-14 12:20:14 +0200505 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200506 }
Michal Vaskoda859032020-07-14 12:20:14 +0200507 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, mod, ht->cb_data)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200508 /* even the value matches */
Michal Vaskoda859032020-07-14 12:20:14 +0200509 if (crec_p) {
510 *crec_p = rec;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200511 }
Michal Vaskoda859032020-07-14 12:20:14 +0200512 if (col) {
513 *col = 0;
514 }
515 *rec_p = rec;
516 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200517 }
518
519 /* some collisions, we need to go through them, too */
520 crec = rec;
Michal Vaskoda859032020-07-14 12:20:14 +0200521 c = crec->hits;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200522 for (i = 1; i < c; ++i) {
523 r = lyht_find_collision(ht, &rec, crec);
524 assert(!r);
525 (void)r;
526
527 /* compare values */
Michal Vaskoda859032020-07-14 12:20:14 +0200528 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, mod, ht->cb_data)) {
529 if (crec_p) {
530 *crec_p = crec;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200531 }
Michal Vaskoda859032020-07-14 12:20:14 +0200532 if (col) {
533 *col = i;
534 }
535 *rec_p = rec;
536 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200537 }
538 }
539
540 /* not found even in collisions */
Michal Vaskoda859032020-07-14 12:20:14 +0200541 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200542}
543
Michal Vaskoda859032020-07-14 12:20:14 +0200544LY_ERR
545lyht_find(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
546{
547 struct ht_rec *rec;
548
549 lyht_find_rec(ht, val_p, hash, 0, NULL, NULL, &rec);
550
551 if (rec && match_p) {
552 *match_p = rec->val;
553 }
554 return rec ? LY_SUCCESS : LY_ENOTFOUND;
555}
556
557LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200558lyht_find_next(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
559{
560 struct ht_rec *rec, *crec;
561 uint32_t i, c;
Michal Vaskoda859032020-07-14 12:20:14 +0200562 LY_ERR r;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200563
Michal Vaskoda859032020-07-14 12:20:14 +0200564 /* found the record of the previously found value */
565 if (lyht_find_rec(ht, val_p, hash, 1, &crec, &i, &rec)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200566 /* not found, cannot happen */
Michal Vaskoda859032020-07-14 12:20:14 +0200567 LOGINT_RET(NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200568 }
569
570 /* go through collisions and find next one after the previous one */
Michal Vaskoda859032020-07-14 12:20:14 +0200571 c = crec->hits;
572 for (++i; i < c; ++i) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200573 r = lyht_find_collision(ht, &rec, crec);
574 assert(!r);
575 (void)r;
576
Michal Vaskoda859032020-07-14 12:20:14 +0200577 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 0, ht->cb_data)) {
578 /* even the value matches */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200579 if (match_p) {
580 *match_p = rec->val;
581 }
Michal Vaskoda859032020-07-14 12:20:14 +0200582 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200583 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200584 }
585
586 /* the last equal value was already returned */
Michal Vaskoda859032020-07-14 12:20:14 +0200587 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200588}
589
590LY_ERR
591lyht_insert_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash,
Radek Krejci0f969882020-08-21 16:56:47 +0200592 values_equal_cb resize_val_equal, void **match_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200593{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200594 LY_ERR r, ret = LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200595 struct ht_rec *rec, *crec = NULL;
596 int32_t i;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200597 values_equal_cb old_val_equal;
598
599 if (!lyht_find_first(ht, hash, &rec)) {
600 /* we found matching shortened hash */
601 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
602 /* even the value matches */
603 if (match_p) {
604 *match_p = (void *)&rec->val;
605 }
606 return LY_EEXIST;
607 }
608
609 /* some collisions, we need to go through them, too */
610 crec = rec;
611 for (i = 1; i < crec->hits; ++i) {
612 r = lyht_find_collision(ht, &rec, crec);
613 assert(!r);
614
615 /* compare values */
616 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
617 if (match_p) {
618 *match_p = (void *)&rec->val;
619 }
620 return LY_EEXIST;
621 }
622 }
623
624 /* value not found, get the record where it will be inserted */
625 r = lyht_find_collision(ht, &rec, crec);
626 assert(r);
627 }
628
629 /* insert it into the returned record */
630 assert(rec->hits < 1);
631 rec->hash = hash;
632 rec->hits = 1;
633 memcpy(&rec->val, val_p, ht->rec_size - (sizeof(struct ht_rec) - 1));
634 if (match_p) {
635 *match_p = (void *)&rec->val;
636 }
637
638 if (crec) {
639 /* there was a collision, increase hits */
640 if (crec->hits == INT32_MAX) {
641 LOGINT(NULL);
642 }
643 ++crec->hits;
644 }
645
646 /* check size & enlarge if needed */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200647 ++ht->used;
648 if (ht->resize) {
649 r = (ht->used * 100) / ht->size;
650 if ((ht->resize == 1) && (r >= LYHT_FIRST_SHRINK_PERCENTAGE)) {
651 /* enable shrinking */
652 ht->resize = 2;
653 }
654 if ((ht->resize == 2) && (r >= LYHT_ENLARGE_PERCENTAGE)) {
655 if (resize_val_equal) {
656 old_val_equal = lyht_set_cb(ht, resize_val_equal);
657 }
658
659 /* enlarge */
660 ret = lyht_resize(ht, 1);
661 /* if hash_table was resized, we need to find new matching value */
662 if (ret == LY_SUCCESS && match_p) {
663 lyht_find(ht, val_p, hash, match_p);
664 }
665
666 if (resize_val_equal) {
667 lyht_set_cb(ht, old_val_equal);
668 }
669 }
670 }
671 return ret;
672}
673
Radek Krejci0ae092d2018-09-20 16:43:19 +0200674LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200675lyht_insert(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
676{
677 return lyht_insert_with_resize_cb(ht, val_p, hash, NULL, match_p);
678}
679
680LY_ERR
681lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash)
682{
683 struct ht_rec *rec, *crec;
684 int32_t i;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200685 uint8_t first_matched = 0;
686 LY_ERR r, ret = LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200687
Michal Vasko4a4c7ed2020-07-17 09:30:12 +0200688 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 +0200689
690 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
691 /* even the value matches */
692 first_matched = 1;
693 }
694
695 /* we always need to go through collisions */
696 crec = rec;
697 for (i = 1; i < crec->hits; ++i) {
698 r = lyht_find_collision(ht, &rec, crec);
699 assert(!r);
700
701 /* compare values */
702 if (!first_matched && (rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
703 break;
704 }
705 }
706
707 if (i < crec->hits) {
708 /* one of collisions matched, reduce collision count, remove the record */
709 assert(!first_matched);
710 --crec->hits;
711 rec->hits = -1;
712 } else if (first_matched) {
713 /* the first record matches */
714 if (crec != rec) {
715 /* ... so put the last collision in its place */
716 rec->hits = crec->hits - 1;
717 memcpy(crec, rec, ht->rec_size);
718 }
719 rec->hits = -1;
720 } else {
721 /* value not found even in collisions */
Michal Vasko4a4c7ed2020-07-17 09:30:12 +0200722 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200723 }
724
725 /* check size & shrink if needed */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200726 --ht->used;
727 if (ht->resize == 2) {
728 r = (ht->used * 100) / ht->size;
729 if ((r < LYHT_SHRINK_PERCENTAGE) && (ht->size > LYHT_MIN_SIZE)) {
730 /* shrink */
731 ret = lyht_resize(ht, 0);
732 }
733 }
734
735 return ret;
736}