blob: e181a117462da32c96b689ccf1b8f508a98a6683 [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
Michal Vasko69730152020-10-09 16:30:07 +020017#include <assert.h>
18#include <pthread.h>
Radek Krejci5aeea3a2018-09-05 13:29:36 +020019#include <stdint.h>
20#include <stdlib.h>
Michal Vasko69730152020-10-09 16:30:07 +020021#include <string.h>
Radek Krejci5aeea3a2018-09-05 13:29:36 +020022
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020024#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "dict.h"
Radek Krejci857189e2020-09-01 13:26:36 +020026#include "log.h"
Radek Krejci5aeea3a2018-09-05 13:29:36 +020027
Radek Krejcif13b87b2020-12-01 22:02:17 +010028#define LYDICT_MIN_SIZE 1024
29
Radek Krejci857189e2020-09-01 13:26:36 +020030/**
31 * @brief Comparison callback for dictionary's hash table
32 *
33 * Implementation of ::values_equal_cb.
34 */
35static ly_bool
36lydict_val_eq(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *cb_data)
Radek Krejci5aeea3a2018-09-05 13:29:36 +020037{
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020038 LY_CHECK_ARG_RET(NULL, val1_p, val2_p, cb_data, 0);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020039
40 const char *str1 = ((struct dict_rec *)val1_p)->value;
41 const char *str2 = ((struct dict_rec *)val2_p)->value;
42
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020043 LY_CHECK_ERR_RET(!str1, LOGARG(NULL, val1_p), 0);
44 LY_CHECK_ERR_RET(!str2, LOGARG(NULL, val2_p), 0);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020045
46 if (strncmp(str1, str2, *(size_t *)cb_data) == 0) {
47 return 1;
48 }
49
50 return 0;
51}
52
53void
54lydict_init(struct dict_table *dict)
55{
Michal Vaskod989ba02020-08-24 10:59:24 +020056 LY_CHECK_ARG_RET(NULL, dict, );
Radek Krejci5aeea3a2018-09-05 13:29:36 +020057
Radek Krejcif13b87b2020-12-01 22:02:17 +010058 dict->hash_tab = lyht_new(LYDICT_MIN_SIZE, sizeof(struct dict_rec), lydict_val_eq, NULL, 1);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020059 LY_CHECK_ERR_RET(!dict->hash_tab, LOGINT(NULL), );
Radek Krejci5aeea3a2018-09-05 13:29:36 +020060 pthread_mutex_init(&dict->lock, NULL);
61}
62
63void
64lydict_clean(struct dict_table *dict)
65{
Michal Vasko44f3d2c2020-08-24 09:49:38 +020066 struct dict_rec *dict_rec = NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020067 struct ht_rec *rec = NULL;
68
Michal Vaskod989ba02020-08-24 10:59:24 +020069 LY_CHECK_ARG_RET(NULL, dict, );
Radek Krejci5aeea3a2018-09-05 13:29:36 +020070
Radek Krejci1deb5be2020-08-26 16:43:36 +020071 for (uint32_t i = 0; i < dict->hash_tab->size; i++) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +020072 /* get ith record */
73 rec = (struct ht_rec *)&dict->hash_tab->recs[i * dict->hash_tab->rec_size];
74 if (rec->hits == 1) {
75 /*
76 * this should not happen, all records inserted into
77 * dictionary are supposed to be removed using lydict_remove()
78 * before calling lydict_clean()
79 */
Michal Vasko44f3d2c2020-08-24 09:49:38 +020080 dict_rec = (struct dict_rec *)rec->val;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020081 LOGWRN(NULL, "String \"%s\" not freed from the dictionary, refcount %d", dict_rec->value, dict_rec->refcount);
82 /* if record wasn't removed before free string allocated for that record */
83#ifdef NDEBUG
84 free(dict_rec->value);
85#endif
86 }
87 }
88
89 /* free table and destroy mutex */
90 lyht_free(dict->hash_tab);
91 pthread_mutex_destroy(&dict->lock);
92}
93
94/*
Radek Krejci5aeea3a2018-09-05 13:29:36 +020095 * Usage:
96 * - init hash to 0
97 * - repeatedly call dict_hash_multi(), provide hash from the last call
98 * - call dict_hash_multi() with key_part = NULL to finish the hash
99 */
100uint32_t
101dict_hash_multi(uint32_t hash, const char *key_part, size_t len)
102{
103 uint32_t i;
104
105 if (key_part) {
106 for (i = 0; i < len; ++i) {
107 hash += key_part[i];
108 hash += (hash << 10);
109 hash ^= (hash >> 6);
110 }
111 } else {
112 hash += (hash << 3);
113 hash ^= (hash >> 11);
114 hash += (hash << 15);
115 }
116
117 return hash;
118}
119
Radek Krejcif2dc4c52018-11-08 09:04:13 +0100120/*
121 * Bob Jenkin's one-at-a-time hash
122 * http://www.burtleburtle.net/bob/hash/doobs.html
123 *
124 * Spooky hash is faster, but it works only for little endian architectures.
125 */
126uint32_t
127dict_hash(const char *key, size_t len)
128{
129 uint32_t hash;
130
131 hash = dict_hash_multi(0, key, len);
132 return dict_hash_multi(hash, NULL, len);
133}
134
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200135static ly_bool
136lydict_resize_val_eq(void *val1_p, void *val2_p, ly_bool mod, void *cb_data)
137{
138 LY_CHECK_ARG_RET(NULL, val1_p, val2_p, 0);
139
140 const char *str1 = ((struct dict_rec *)val1_p)->value;
141 const char *str2 = ((struct dict_rec *)val2_p)->value;
142
143 LY_CHECK_ERR_RET(!str1, LOGARG(NULL, val1_p), 0);
144 LY_CHECK_ERR_RET(!str2, LOGARG(NULL, val2_p), 0);
145
146 if (mod) {
147 /* used when inserting new values */
148 if (strcmp(str1, str2) == 0) {
149 return 1;
150 }
151 } else {
152 /* used when finding the original value again in the resized table */
153 return lydict_val_eq(val1_p, val2_p, mod, cb_data);
154 }
155
156 return 0;
157}
158
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200159API void
Michal Vasko52927e22020-03-16 17:26:14 +0100160lydict_remove(const struct ly_ctx *ctx, const char *value)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200161{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200162 LY_ERR ret;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200163 size_t len;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200164 uint32_t hash;
165 struct dict_rec rec, *match = NULL;
166 char *val_p;
167
Michal Vasko0f6b3e22018-09-07 12:18:12 +0200168 if (!value) {
Michal Vasko0b3a4172018-09-07 12:20:18 +0200169 return;
Michal Vasko0f6b3e22018-09-07 12:18:12 +0200170 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200171
172 len = strlen(value);
173 hash = dict_hash(value, len);
174
175 /* create record for lyht_find call */
176 rec.value = (char *)value;
177 rec.refcount = 0;
178
Michal Vasko52927e22020-03-16 17:26:14 +0100179 pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200180 /* set len as data for compare callback */
181 lyht_set_cb_data(ctx->dict.hash_tab, (void *)&len);
182 /* check if value is already inserted */
183 ret = lyht_find(ctx->dict.hash_tab, &rec, hash, (void **)&match);
184
Radek Krejci1deb5be2020-08-26 16:43:36 +0200185 if (ret == LY_SUCCESS) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200186 LY_CHECK_ERR_GOTO(!match, LOGINT(ctx), finish);
187
188 /* if value is already in dictionary, decrement reference counter */
189 match->refcount--;
190 if (match->refcount == 0) {
191 /*
192 * remove record
193 * save pointer to stored string before lyht_remove to
194 * free it after it is removed from hash table
195 */
196 val_p = match->value;
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200197 ret = lyht_remove_with_resize_cb(ctx->dict.hash_tab, &rec, hash, lydict_resize_val_eq);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200198 free(val_p);
199 LY_CHECK_ERR_GOTO(ret, LOGINT(ctx), finish);
200 }
201 }
202
203finish:
Michal Vasko52927e22020-03-16 17:26:14 +0100204 pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200205}
206
Radek Krejci011e4aa2020-09-04 15:22:31 +0200207LY_ERR
208dict_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 +0200209{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200210 LY_ERR ret = LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200211 struct dict_rec *match = NULL, rec;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200212 uint32_t hash;
213
214 hash = dict_hash(value, len);
215 /* set len as data for compare callback */
216 lyht_set_cb_data(ctx->dict.hash_tab, (void *)&len);
217 /* create record for lyht_insert */
218 rec.value = value;
219 rec.refcount = 1;
220
221 LOGDBG(LY_LDGDICT, "inserting \"%s\"", rec.value);
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200222 ret = lyht_insert_with_resize_cb(ctx->dict.hash_tab, (void *)&rec, hash, lydict_resize_val_eq, (void **)&match);
Michal Vaskofcc96b92018-09-12 11:12:01 +0200223 if (ret == LY_EEXIST) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200224 match->refcount++;
225 if (zerocopy) {
226 free(value);
227 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200228 ret = LY_SUCCESS;
Michal Vaskofcc96b92018-09-12 11:12:01 +0200229 } else if (ret == LY_SUCCESS) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200230 if (!zerocopy) {
231 /*
232 * allocate string for new record
233 * record is already inserted in hash table
234 */
235 match->value = malloc(sizeof *match->value * (len + 1));
Radek Krejci011e4aa2020-09-04 15:22:31 +0200236 LY_CHECK_ERR_RET(!match->value, LOGMEM(ctx), LY_EMEM);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200237 memcpy(match->value, value, len);
238 match->value[len] = '\0';
239 }
240 } else {
241 /* lyht_insert returned error */
David Sedlák234a91f2019-08-15 13:16:43 +0200242 if (zerocopy) {
243 free(value);
244 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200245 return ret;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200246 }
247
Radek Krejci011e4aa2020-09-04 15:22:31 +0200248 if (str_p) {
249 *str_p = match->value;
250 }
251
252 return ret;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200253}
254
Radek Krejci011e4aa2020-09-04 15:22:31 +0200255API LY_ERR
256lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len, const char **str_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200257{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200258 LY_ERR result;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200259
Radek Krejci011e4aa2020-09-04 15:22:31 +0200260 LY_CHECK_ARG_RET(ctx, ctx, value, LY_EINVAL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200261
Radek Krejci0ae092d2018-09-20 16:43:19 +0200262 if (!len) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200263 len = strlen(value);
264 }
265
Michal Vasko52927e22020-03-16 17:26:14 +0100266 pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200267 result = dict_insert(ctx, (char *)value, len, 0, str_p);
Michal Vasko52927e22020-03-16 17:26:14 +0100268 pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200269
270 return result;
271}
272
Radek Krejci011e4aa2020-09-04 15:22:31 +0200273API LY_ERR
274lydict_insert_zc(const struct ly_ctx *ctx, char *value, const char **str_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200275{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200276 LY_ERR result;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200277
Radek Krejci011e4aa2020-09-04 15:22:31 +0200278 LY_CHECK_ARG_RET(ctx, ctx, value, LY_EINVAL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200279
Michal Vasko52927e22020-03-16 17:26:14 +0100280 pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200281 result = dict_insert(ctx, value, strlen(value), 1, str_p);
Michal Vasko52927e22020-03-16 17:26:14 +0100282 pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200283
284 return result;
285}
286
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200287struct ht_rec *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200288lyht_get_rec(unsigned char *recs, uint16_t rec_size, uint32_t idx)
289{
290 return (struct ht_rec *)&recs[idx * rec_size];
291}
292
293struct hash_table *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200294lyht_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 +0200295{
296 struct hash_table *ht;
297
298 /* check that 2^x == size (power of 2) */
299 assert(size && !(size & (size - 1)));
300 assert(val_equal && val_size);
301 assert(resize == 0 || resize == 1);
302
303 if (size < LYHT_MIN_SIZE) {
304 size = LYHT_MIN_SIZE;
305 }
306
307 ht = malloc(sizeof *ht);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200308 LY_CHECK_ERR_RET(!ht, LOGMEM(NULL), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200309
310 ht->used = 0;
311 ht->size = size;
312 ht->val_equal = val_equal;
313 ht->cb_data = cb_data;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200314 ht->resize = resize;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200315
316 ht->rec_size = (sizeof(struct ht_rec) - 1) + val_size;
317 /* allocate the records correctly */
318 ht->recs = calloc(size, ht->rec_size);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200319 LY_CHECK_ERR_RET(!ht->recs, free(ht); LOGMEM(NULL), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200320
321 return ht;
322}
323
324values_equal_cb
325lyht_set_cb(struct hash_table *ht, values_equal_cb new_val_equal)
326{
327 values_equal_cb prev;
328
329 prev = ht->val_equal;
330 ht->val_equal = new_val_equal;
331 return prev;
332}
333
334void *
335lyht_set_cb_data(struct hash_table *ht, void *new_cb_data)
336{
337 void *prev;
338
339 prev = ht->cb_data;
340 ht->cb_data = new_cb_data;
341 return prev;
342}
343
344struct hash_table *
345lyht_dup(const struct hash_table *orig)
346{
347 struct hash_table *ht;
348
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200349 LY_CHECK_ARG_RET(NULL, orig, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200350
351 ht = lyht_new(orig->size, orig->rec_size - (sizeof(struct ht_rec) - 1), orig->val_equal, orig->cb_data, orig->resize ? 1 : 0);
352 if (!ht) {
353 return NULL;
354 }
355
356 memcpy(ht->recs, orig->recs, orig->used * orig->rec_size);
357 ht->used = orig->used;
358 return ht;
359}
360
361void
362lyht_free(struct hash_table *ht)
363{
364 if (ht) {
365 free(ht->recs);
366 free(ht);
367 }
368}
369
370static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200371lyht_resize(struct hash_table *ht, ly_bool enlarge)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200372{
373 struct ht_rec *rec;
374 unsigned char *old_recs;
375 uint32_t i, old_size;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200376
377 old_recs = ht->recs;
378 old_size = ht->size;
379
380 if (enlarge) {
381 /* double the size */
382 ht->size <<= 1;
383 } else {
384 /* half the size */
385 ht->size >>= 1;
386 }
387
388 ht->recs = calloc(ht->size, ht->rec_size);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200389 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 +0200390
391 /* reset used, it will increase again */
392 ht->used = 0;
393
394 /* add all the old records into the new records array */
395 for (i = 0; i < old_size; ++i) {
396 rec = lyht_get_rec(old_recs, ht->rec_size, i);
397 if (rec->hits > 0) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200398 LY_ERR ret = lyht_insert(ht, rec->val, rec->hash, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200399 assert(!ret);
400 (void)ret;
401 }
402 }
403
404 /* final touches */
405 free(old_recs);
406 return LY_SUCCESS;
407}
408
Michal Vaskoda859032020-07-14 12:20:14 +0200409/**
410 * @brief Search for the first match.
411 *
412 * @param[in] ht Hash table to search in.
413 * @param[in] hash Hash to find.
414 * @param[out] rec_p Optional found record.
415 * @return LY_SUCCESS hash found, returned its record,
416 * @return LY_ENOTFOUND hash not found, returned the record where it would be inserted.
417 */
418static LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200419lyht_find_first(struct hash_table *ht, uint32_t hash, struct ht_rec **rec_p)
420{
421 struct ht_rec *rec;
422 uint32_t i, idx;
423
424 if (rec_p) {
425 *rec_p = NULL;
426 }
427
428 idx = i = hash & (ht->size - 1);
429 rec = lyht_get_rec(ht->recs, ht->rec_size, idx);
430
431 /* skip through overflow and deleted records */
432 while ((rec->hits != 0) && ((rec->hits == -1) || ((rec->hash & (ht->size - 1)) != idx))) {
433 if ((rec->hits == -1) && rec_p && !(*rec_p)) {
434 /* remember this record for return */
435 *rec_p = rec;
436 }
437 i = (i + 1) % ht->size;
438 if (i == idx) {
439 /* we went through all the records (very unlikely, but possible when many records are invalid),
440 * just return not found */
441 assert(!rec_p || *rec_p);
Michal Vaskoda859032020-07-14 12:20:14 +0200442 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200443 }
444 rec = lyht_get_rec(ht->recs, ht->rec_size, i);
445 }
446 if (rec->hits == 0) {
447 /* we could not find the value */
448 if (rec_p && !*rec_p) {
449 *rec_p = rec;
450 }
Michal Vaskoda859032020-07-14 12:20:14 +0200451 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200452 }
453
454 /* we have found a record with equal (shortened) hash */
455 if (rec_p) {
456 *rec_p = rec;
457 }
Michal Vaskoda859032020-07-14 12:20:14 +0200458 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200459}
460
461/**
462 * @brief Search for the next collision.
463 *
464 * @param[in] ht Hash table to search in.
465 * @param[in,out] last Last returned collision record.
466 * @param[in] first First collision record (hits > 1).
Michal Vaskoda859032020-07-14 12:20:14 +0200467 * @return LY_SUCCESS when hash collision found, \p last points to this next collision,
468 * @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 +0200469 */
Michal Vaskoda859032020-07-14 12:20:14 +0200470static LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200471lyht_find_collision(struct hash_table *ht, struct ht_rec **last, struct ht_rec *first)
472{
473 struct ht_rec *empty = NULL;
474 uint32_t i, idx;
475
476 assert(last && *last);
477
478 idx = (*last)->hash & (ht->size - 1);
479 i = (((unsigned char *)*last) - ht->recs) / ht->rec_size;
480
481 do {
482 i = (i + 1) % ht->size;
483 *last = lyht_get_rec(ht->recs, ht->rec_size, i);
484 if (*last == first) {
485 /* we went through all the records (very unlikely, but possible when many records are invalid),
486 * just return an invalid record */
487 assert(empty);
488 *last = empty;
Michal Vaskoda859032020-07-14 12:20:14 +0200489 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200490 }
491
492 if (((*last)->hits == -1) && !empty) {
493 empty = *last;
494 }
495 } while (((*last)->hits != 0) && (((*last)->hits == -1) || (((*last)->hash & (ht->size - 1)) != idx)));
496
497 if ((*last)->hits > 0) {
498 /* we found a collision */
499 assert((*last)->hits == 1);
Michal Vaskoda859032020-07-14 12:20:14 +0200500 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200501 }
502
503 /* no next collision found, return the record where it would be inserted */
504 if (empty) {
505 *last = empty;
506 } /* else (*last)->hits == 0, it is already correct */
Michal Vaskoda859032020-07-14 12:20:14 +0200507 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200508}
509
Michal Vaskoda859032020-07-14 12:20:14 +0200510/**
511 * @brief Search for a record with specific value and hash.
512 *
513 * @param[in] ht Hash table to search in.
514 * @param[in] val_p Pointer to the value to find.
515 * @param[in] hash Hash to find.
Radek Krejci857189e2020-09-01 13:26:36 +0200516 * @param[in] mod Whether the operation modifies the hash table (insert or remove) or not (find).
Michal Vaskoda859032020-07-14 12:20:14 +0200517 * @param[out] crec_p Optional found first record.
518 * @param[out] col Optional collision number of @p rec_p, 0 for no collision.
519 * @param[out] rec_p Found exact matching record, may be a collision of @p crec_p.
520 * @return LY_ENOTFOUND if no record found,
521 * @return LY_SUCCESS if record was found.
522 */
523static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200524lyht_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 +0200525 struct ht_rec **rec_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200526{
527 struct ht_rec *rec, *crec;
528 uint32_t i, c;
Michal Vaskoda859032020-07-14 12:20:14 +0200529 LY_ERR r;
530
531 if (crec_p) {
532 *crec_p = NULL;
533 }
534 if (col) {
535 *col = 0;
536 }
537 *rec_p = NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200538
539 if (lyht_find_first(ht, hash, &rec)) {
540 /* not found */
Michal Vaskoda859032020-07-14 12:20:14 +0200541 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200542 }
Michal Vaskoda859032020-07-14 12:20:14 +0200543 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, mod, ht->cb_data)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200544 /* even the value matches */
Michal Vaskoda859032020-07-14 12:20:14 +0200545 if (crec_p) {
546 *crec_p = rec;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200547 }
Michal Vaskoda859032020-07-14 12:20:14 +0200548 if (col) {
549 *col = 0;
550 }
551 *rec_p = rec;
552 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200553 }
554
555 /* some collisions, we need to go through them, too */
556 crec = rec;
Michal Vaskoda859032020-07-14 12:20:14 +0200557 c = crec->hits;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200558 for (i = 1; i < c; ++i) {
559 r = lyht_find_collision(ht, &rec, crec);
560 assert(!r);
561 (void)r;
562
563 /* compare values */
Michal Vaskoda859032020-07-14 12:20:14 +0200564 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, mod, ht->cb_data)) {
565 if (crec_p) {
566 *crec_p = crec;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200567 }
Michal Vaskoda859032020-07-14 12:20:14 +0200568 if (col) {
569 *col = i;
570 }
571 *rec_p = rec;
572 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200573 }
574 }
575
576 /* not found even in collisions */
Michal Vaskoda859032020-07-14 12:20:14 +0200577 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200578}
579
Michal Vaskoda859032020-07-14 12:20:14 +0200580LY_ERR
581lyht_find(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
582{
583 struct ht_rec *rec;
584
585 lyht_find_rec(ht, val_p, hash, 0, NULL, NULL, &rec);
586
587 if (rec && match_p) {
588 *match_p = rec->val;
589 }
590 return rec ? LY_SUCCESS : LY_ENOTFOUND;
591}
592
593LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200594lyht_find_next(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
595{
596 struct ht_rec *rec, *crec;
597 uint32_t i, c;
Michal Vaskoda859032020-07-14 12:20:14 +0200598 LY_ERR r;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200599
Michal Vaskoda859032020-07-14 12:20:14 +0200600 /* found the record of the previously found value */
601 if (lyht_find_rec(ht, val_p, hash, 1, &crec, &i, &rec)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200602 /* not found, cannot happen */
Michal Vaskoda859032020-07-14 12:20:14 +0200603 LOGINT_RET(NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200604 }
605
606 /* go through collisions and find next one after the previous one */
Michal Vaskoda859032020-07-14 12:20:14 +0200607 c = crec->hits;
608 for (++i; i < c; ++i) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200609 r = lyht_find_collision(ht, &rec, crec);
610 assert(!r);
611 (void)r;
612
Michal Vaskoda859032020-07-14 12:20:14 +0200613 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 0, ht->cb_data)) {
614 /* even the value matches */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200615 if (match_p) {
616 *match_p = rec->val;
617 }
Michal Vaskoda859032020-07-14 12:20:14 +0200618 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200619 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200620 }
621
622 /* the last equal value was already returned */
Michal Vaskoda859032020-07-14 12:20:14 +0200623 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200624}
625
626LY_ERR
627lyht_insert_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash,
Radek Krejci0f969882020-08-21 16:56:47 +0200628 values_equal_cb resize_val_equal, void **match_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200629{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200630 LY_ERR r, ret = LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200631 struct ht_rec *rec, *crec = NULL;
632 int32_t i;
Radek Krejci540ce982020-11-26 12:01:30 +0100633 values_equal_cb old_val_equal = NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200634
635 if (!lyht_find_first(ht, hash, &rec)) {
636 /* we found matching shortened hash */
637 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
638 /* even the value matches */
639 if (match_p) {
640 *match_p = (void *)&rec->val;
641 }
642 return LY_EEXIST;
643 }
644
645 /* some collisions, we need to go through them, too */
646 crec = rec;
647 for (i = 1; i < crec->hits; ++i) {
648 r = lyht_find_collision(ht, &rec, crec);
649 assert(!r);
650
651 /* compare values */
652 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
653 if (match_p) {
654 *match_p = (void *)&rec->val;
655 }
656 return LY_EEXIST;
657 }
658 }
659
660 /* value not found, get the record where it will be inserted */
661 r = lyht_find_collision(ht, &rec, crec);
662 assert(r);
663 }
664
665 /* insert it into the returned record */
666 assert(rec->hits < 1);
667 rec->hash = hash;
668 rec->hits = 1;
669 memcpy(&rec->val, val_p, ht->rec_size - (sizeof(struct ht_rec) - 1));
670 if (match_p) {
671 *match_p = (void *)&rec->val;
672 }
673
674 if (crec) {
675 /* there was a collision, increase hits */
676 if (crec->hits == INT32_MAX) {
677 LOGINT(NULL);
678 }
679 ++crec->hits;
680 }
681
682 /* check size & enlarge if needed */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200683 ++ht->used;
684 if (ht->resize) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100685 r = (ht->used * LYHT_HUNDRED_PERCENTAGE) / ht->size;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200686 if ((ht->resize == 1) && (r >= LYHT_FIRST_SHRINK_PERCENTAGE)) {
687 /* enable shrinking */
688 ht->resize = 2;
689 }
690 if ((ht->resize == 2) && (r >= LYHT_ENLARGE_PERCENTAGE)) {
691 if (resize_val_equal) {
692 old_val_equal = lyht_set_cb(ht, resize_val_equal);
693 }
694
695 /* enlarge */
696 ret = lyht_resize(ht, 1);
697 /* if hash_table was resized, we need to find new matching value */
Michal Vasko69730152020-10-09 16:30:07 +0200698 if ((ret == LY_SUCCESS) && match_p) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200699 lyht_find(ht, val_p, hash, match_p);
700 }
701
702 if (resize_val_equal) {
703 lyht_set_cb(ht, old_val_equal);
704 }
705 }
706 }
707 return ret;
708}
709
Radek Krejci0ae092d2018-09-20 16:43:19 +0200710LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200711lyht_insert(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
712{
713 return lyht_insert_with_resize_cb(ht, val_p, hash, NULL, match_p);
714}
715
716LY_ERR
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200717lyht_remove_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash, values_equal_cb resize_val_equal)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200718{
719 struct ht_rec *rec, *crec;
720 int32_t i;
Radek Krejci857189e2020-09-01 13:26:36 +0200721 ly_bool first_matched = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200722 LY_ERR r, ret = LY_SUCCESS;
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200723 values_equal_cb old_val_equal;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200724
Michal Vasko4a4c7ed2020-07-17 09:30:12 +0200725 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 +0200726
727 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
728 /* even the value matches */
729 first_matched = 1;
730 }
731
732 /* we always need to go through collisions */
733 crec = rec;
734 for (i = 1; i < crec->hits; ++i) {
735 r = lyht_find_collision(ht, &rec, crec);
736 assert(!r);
737
738 /* compare values */
739 if (!first_matched && (rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
740 break;
741 }
742 }
743
744 if (i < crec->hits) {
745 /* one of collisions matched, reduce collision count, remove the record */
746 assert(!first_matched);
747 --crec->hits;
748 rec->hits = -1;
749 } else if (first_matched) {
750 /* the first record matches */
751 if (crec != rec) {
752 /* ... so put the last collision in its place */
753 rec->hits = crec->hits - 1;
754 memcpy(crec, rec, ht->rec_size);
755 }
756 rec->hits = -1;
757 } else {
758 /* value not found even in collisions */
Michal Vasko4a4c7ed2020-07-17 09:30:12 +0200759 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200760 }
761
762 /* check size & shrink if needed */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200763 --ht->used;
764 if (ht->resize == 2) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100765 r = (ht->used * LYHT_HUNDRED_PERCENTAGE) / ht->size;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200766 if ((r < LYHT_SHRINK_PERCENTAGE) && (ht->size > LYHT_MIN_SIZE)) {
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200767 if (resize_val_equal) {
768 old_val_equal = lyht_set_cb(ht, resize_val_equal);
769 }
770
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200771 /* shrink */
772 ret = lyht_resize(ht, 0);
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200773
774 if (resize_val_equal) {
775 lyht_set_cb(ht, old_val_equal);
776 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200777 }
778 }
779
780 return ret;
781}
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200782
783LY_ERR
784lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash)
785{
786 return lyht_remove_with_resize_cb(ht, val_p, hash, NULL);
787}