blob: 0564eb96cae5c7c55a488405e8eee9e26f69530b [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
Michal Vaskoe180ed02021-02-05 16:31:20 +0100159API LY_ERR
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{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100162 LY_ERR ret = LY_SUCCESS;
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 Vaskoe180ed02021-02-05 16:31:20 +0100169 return LY_SUCCESS;
Michal Vasko0f6b3e22018-09-07 12:18:12 +0200170 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200171
Michal Vaskoe180ed02021-02-05 16:31:20 +0100172 LOGDBG(LY_LDGDICT, "removing \"%s\"", value);
173
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200174 len = strlen(value);
175 hash = dict_hash(value, len);
176
177 /* create record for lyht_find call */
178 rec.value = (char *)value;
179 rec.refcount = 0;
180
Michal Vasko52927e22020-03-16 17:26:14 +0100181 pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200182 /* set len as data for compare callback */
183 lyht_set_cb_data(ctx->dict.hash_tab, (void *)&len);
184 /* check if value is already inserted */
185 ret = lyht_find(ctx->dict.hash_tab, &rec, hash, (void **)&match);
186
Radek Krejci1deb5be2020-08-26 16:43:36 +0200187 if (ret == LY_SUCCESS) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200188 LY_CHECK_ERR_GOTO(!match, LOGINT(ctx), finish);
189
190 /* if value is already in dictionary, decrement reference counter */
191 match->refcount--;
192 if (match->refcount == 0) {
193 /*
194 * remove record
195 * save pointer to stored string before lyht_remove to
196 * free it after it is removed from hash table
197 */
198 val_p = match->value;
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200199 ret = lyht_remove_with_resize_cb(ctx->dict.hash_tab, &rec, hash, lydict_resize_val_eq);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200200 free(val_p);
201 LY_CHECK_ERR_GOTO(ret, LOGINT(ctx), finish);
202 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100203 } else if (ret == LY_ENOTFOUND) {
204 LOGERR(ctx, LY_ENOTFOUND, "Value \"%s\" was not found in the dictionary.", value);
205 } else {
206 LOGINT(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200207 }
208
209finish:
Michal Vasko52927e22020-03-16 17:26:14 +0100210 pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100211 return ret;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200212}
213
Radek Krejci011e4aa2020-09-04 15:22:31 +0200214LY_ERR
215dict_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 +0200216{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200217 LY_ERR ret = LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200218 struct dict_rec *match = NULL, rec;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200219 uint32_t hash;
220
Michal Vaskoe180ed02021-02-05 16:31:20 +0100221 LOGDBG(LY_LDGDICT, "inserting \"%.*s\"", len, value);
222
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200223 hash = dict_hash(value, len);
224 /* set len as data for compare callback */
225 lyht_set_cb_data(ctx->dict.hash_tab, (void *)&len);
226 /* create record for lyht_insert */
227 rec.value = value;
228 rec.refcount = 1;
229
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200230 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 +0200231 if (ret == LY_EEXIST) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200232 match->refcount++;
233 if (zerocopy) {
234 free(value);
235 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200236 ret = LY_SUCCESS;
Michal Vaskofcc96b92018-09-12 11:12:01 +0200237 } else if (ret == LY_SUCCESS) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200238 if (!zerocopy) {
239 /*
240 * allocate string for new record
241 * record is already inserted in hash table
242 */
243 match->value = malloc(sizeof *match->value * (len + 1));
Radek Krejci011e4aa2020-09-04 15:22:31 +0200244 LY_CHECK_ERR_RET(!match->value, LOGMEM(ctx), LY_EMEM);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200245 memcpy(match->value, value, len);
246 match->value[len] = '\0';
247 }
248 } else {
249 /* lyht_insert returned error */
David Sedlák234a91f2019-08-15 13:16:43 +0200250 if (zerocopy) {
251 free(value);
252 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200253 return ret;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200254 }
255
Radek Krejci011e4aa2020-09-04 15:22:31 +0200256 if (str_p) {
257 *str_p = match->value;
258 }
259
260 return ret;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200261}
262
Radek Krejci011e4aa2020-09-04 15:22:31 +0200263API LY_ERR
264lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len, const char **str_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200265{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200266 LY_ERR result;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200267
Radek Krejci011e4aa2020-09-04 15:22:31 +0200268 LY_CHECK_ARG_RET(ctx, ctx, value, LY_EINVAL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200269
Radek Krejci0ae092d2018-09-20 16:43:19 +0200270 if (!len) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200271 len = strlen(value);
272 }
273
Michal Vasko52927e22020-03-16 17:26:14 +0100274 pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200275 result = dict_insert(ctx, (char *)value, len, 0, str_p);
Michal Vasko52927e22020-03-16 17:26:14 +0100276 pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200277
278 return result;
279}
280
Radek Krejci011e4aa2020-09-04 15:22:31 +0200281API LY_ERR
282lydict_insert_zc(const struct ly_ctx *ctx, char *value, const char **str_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200283{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200284 LY_ERR result;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200285
Radek Krejci011e4aa2020-09-04 15:22:31 +0200286 LY_CHECK_ARG_RET(ctx, ctx, value, LY_EINVAL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200287
Michal Vasko52927e22020-03-16 17:26:14 +0100288 pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200289 result = dict_insert(ctx, value, strlen(value), 1, str_p);
Michal Vasko52927e22020-03-16 17:26:14 +0100290 pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200291
292 return result;
293}
294
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200295struct ht_rec *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200296lyht_get_rec(unsigned char *recs, uint16_t rec_size, uint32_t idx)
297{
298 return (struct ht_rec *)&recs[idx * rec_size];
299}
300
301struct hash_table *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200302lyht_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 +0200303{
304 struct hash_table *ht;
305
306 /* check that 2^x == size (power of 2) */
307 assert(size && !(size & (size - 1)));
308 assert(val_equal && val_size);
309 assert(resize == 0 || resize == 1);
310
311 if (size < LYHT_MIN_SIZE) {
312 size = LYHT_MIN_SIZE;
313 }
314
315 ht = malloc(sizeof *ht);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200316 LY_CHECK_ERR_RET(!ht, LOGMEM(NULL), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200317
318 ht->used = 0;
319 ht->size = size;
320 ht->val_equal = val_equal;
321 ht->cb_data = cb_data;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200322 ht->resize = resize;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200323
324 ht->rec_size = (sizeof(struct ht_rec) - 1) + val_size;
325 /* allocate the records correctly */
326 ht->recs = calloc(size, ht->rec_size);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200327 LY_CHECK_ERR_RET(!ht->recs, free(ht); LOGMEM(NULL), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200328
329 return ht;
330}
331
332values_equal_cb
333lyht_set_cb(struct hash_table *ht, values_equal_cb new_val_equal)
334{
335 values_equal_cb prev;
336
337 prev = ht->val_equal;
338 ht->val_equal = new_val_equal;
339 return prev;
340}
341
342void *
343lyht_set_cb_data(struct hash_table *ht, void *new_cb_data)
344{
345 void *prev;
346
347 prev = ht->cb_data;
348 ht->cb_data = new_cb_data;
349 return prev;
350}
351
352struct hash_table *
353lyht_dup(const struct hash_table *orig)
354{
355 struct hash_table *ht;
356
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200357 LY_CHECK_ARG_RET(NULL, orig, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200358
359 ht = lyht_new(orig->size, orig->rec_size - (sizeof(struct ht_rec) - 1), orig->val_equal, orig->cb_data, orig->resize ? 1 : 0);
360 if (!ht) {
361 return NULL;
362 }
363
364 memcpy(ht->recs, orig->recs, orig->used * orig->rec_size);
365 ht->used = orig->used;
366 return ht;
367}
368
369void
370lyht_free(struct hash_table *ht)
371{
372 if (ht) {
373 free(ht->recs);
374 free(ht);
375 }
376}
377
378static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200379lyht_resize(struct hash_table *ht, ly_bool enlarge)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200380{
381 struct ht_rec *rec;
382 unsigned char *old_recs;
383 uint32_t i, old_size;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200384
385 old_recs = ht->recs;
386 old_size = ht->size;
387
388 if (enlarge) {
389 /* double the size */
390 ht->size <<= 1;
391 } else {
392 /* half the size */
393 ht->size >>= 1;
394 }
395
396 ht->recs = calloc(ht->size, ht->rec_size);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200397 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 +0200398
399 /* reset used, it will increase again */
400 ht->used = 0;
401
402 /* add all the old records into the new records array */
403 for (i = 0; i < old_size; ++i) {
404 rec = lyht_get_rec(old_recs, ht->rec_size, i);
405 if (rec->hits > 0) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200406 LY_ERR ret = lyht_insert(ht, rec->val, rec->hash, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200407 assert(!ret);
408 (void)ret;
409 }
410 }
411
412 /* final touches */
413 free(old_recs);
414 return LY_SUCCESS;
415}
416
Michal Vaskoda859032020-07-14 12:20:14 +0200417/**
418 * @brief Search for the first match.
419 *
420 * @param[in] ht Hash table to search in.
421 * @param[in] hash Hash to find.
422 * @param[out] rec_p Optional found record.
423 * @return LY_SUCCESS hash found, returned its record,
424 * @return LY_ENOTFOUND hash not found, returned the record where it would be inserted.
425 */
426static LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200427lyht_find_first(struct hash_table *ht, uint32_t hash, struct ht_rec **rec_p)
428{
429 struct ht_rec *rec;
430 uint32_t i, idx;
431
432 if (rec_p) {
433 *rec_p = NULL;
434 }
435
436 idx = i = hash & (ht->size - 1);
437 rec = lyht_get_rec(ht->recs, ht->rec_size, idx);
438
439 /* skip through overflow and deleted records */
440 while ((rec->hits != 0) && ((rec->hits == -1) || ((rec->hash & (ht->size - 1)) != idx))) {
441 if ((rec->hits == -1) && rec_p && !(*rec_p)) {
442 /* remember this record for return */
443 *rec_p = rec;
444 }
445 i = (i + 1) % ht->size;
446 if (i == idx) {
447 /* we went through all the records (very unlikely, but possible when many records are invalid),
448 * just return not found */
449 assert(!rec_p || *rec_p);
Michal Vaskoda859032020-07-14 12:20:14 +0200450 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200451 }
452 rec = lyht_get_rec(ht->recs, ht->rec_size, i);
453 }
454 if (rec->hits == 0) {
455 /* we could not find the value */
456 if (rec_p && !*rec_p) {
457 *rec_p = rec;
458 }
Michal Vaskoda859032020-07-14 12:20:14 +0200459 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200460 }
461
462 /* we have found a record with equal (shortened) hash */
463 if (rec_p) {
464 *rec_p = rec;
465 }
Michal Vaskoda859032020-07-14 12:20:14 +0200466 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200467}
468
469/**
470 * @brief Search for the next collision.
471 *
472 * @param[in] ht Hash table to search in.
473 * @param[in,out] last Last returned collision record.
474 * @param[in] first First collision record (hits > 1).
Michal Vaskoda859032020-07-14 12:20:14 +0200475 * @return LY_SUCCESS when hash collision found, \p last points to this next collision,
476 * @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 +0200477 */
Michal Vaskoda859032020-07-14 12:20:14 +0200478static LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200479lyht_find_collision(struct hash_table *ht, struct ht_rec **last, struct ht_rec *first)
480{
481 struct ht_rec *empty = NULL;
482 uint32_t i, idx;
483
484 assert(last && *last);
485
486 idx = (*last)->hash & (ht->size - 1);
487 i = (((unsigned char *)*last) - ht->recs) / ht->rec_size;
488
489 do {
490 i = (i + 1) % ht->size;
491 *last = lyht_get_rec(ht->recs, ht->rec_size, i);
492 if (*last == first) {
493 /* we went through all the records (very unlikely, but possible when many records are invalid),
494 * just return an invalid record */
495 assert(empty);
496 *last = empty;
Michal Vaskoda859032020-07-14 12:20:14 +0200497 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200498 }
499
500 if (((*last)->hits == -1) && !empty) {
501 empty = *last;
502 }
503 } while (((*last)->hits != 0) && (((*last)->hits == -1) || (((*last)->hash & (ht->size - 1)) != idx)));
504
505 if ((*last)->hits > 0) {
506 /* we found a collision */
507 assert((*last)->hits == 1);
Michal Vaskoda859032020-07-14 12:20:14 +0200508 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200509 }
510
511 /* no next collision found, return the record where it would be inserted */
512 if (empty) {
513 *last = empty;
514 } /* else (*last)->hits == 0, it is already correct */
Michal Vaskoda859032020-07-14 12:20:14 +0200515 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200516}
517
Michal Vaskoda859032020-07-14 12:20:14 +0200518/**
519 * @brief Search for a record with specific value and hash.
520 *
521 * @param[in] ht Hash table to search in.
522 * @param[in] val_p Pointer to the value to find.
523 * @param[in] hash Hash to find.
Radek Krejci857189e2020-09-01 13:26:36 +0200524 * @param[in] mod Whether the operation modifies the hash table (insert or remove) or not (find).
Michal Vaskoda859032020-07-14 12:20:14 +0200525 * @param[out] crec_p Optional found first record.
526 * @param[out] col Optional collision number of @p rec_p, 0 for no collision.
527 * @param[out] rec_p Found exact matching record, may be a collision of @p crec_p.
528 * @return LY_ENOTFOUND if no record found,
529 * @return LY_SUCCESS if record was found.
530 */
531static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200532lyht_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 +0200533 struct ht_rec **rec_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200534{
535 struct ht_rec *rec, *crec;
536 uint32_t i, c;
Michal Vaskoda859032020-07-14 12:20:14 +0200537 LY_ERR r;
538
539 if (crec_p) {
540 *crec_p = NULL;
541 }
542 if (col) {
543 *col = 0;
544 }
545 *rec_p = NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200546
547 if (lyht_find_first(ht, hash, &rec)) {
548 /* not found */
Michal Vaskoda859032020-07-14 12:20:14 +0200549 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200550 }
Michal Vaskoda859032020-07-14 12:20:14 +0200551 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, mod, ht->cb_data)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200552 /* even the value matches */
Michal Vaskoda859032020-07-14 12:20:14 +0200553 if (crec_p) {
554 *crec_p = rec;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200555 }
Michal Vaskoda859032020-07-14 12:20:14 +0200556 if (col) {
557 *col = 0;
558 }
559 *rec_p = rec;
560 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200561 }
562
563 /* some collisions, we need to go through them, too */
564 crec = rec;
Michal Vaskoda859032020-07-14 12:20:14 +0200565 c = crec->hits;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200566 for (i = 1; i < c; ++i) {
567 r = lyht_find_collision(ht, &rec, crec);
568 assert(!r);
569 (void)r;
570
571 /* compare values */
Michal Vaskoda859032020-07-14 12:20:14 +0200572 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, mod, ht->cb_data)) {
573 if (crec_p) {
574 *crec_p = crec;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200575 }
Michal Vaskoda859032020-07-14 12:20:14 +0200576 if (col) {
577 *col = i;
578 }
579 *rec_p = rec;
580 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200581 }
582 }
583
584 /* not found even in collisions */
Michal Vaskoda859032020-07-14 12:20:14 +0200585 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200586}
587
Michal Vaskoda859032020-07-14 12:20:14 +0200588LY_ERR
589lyht_find(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
590{
591 struct ht_rec *rec;
592
593 lyht_find_rec(ht, val_p, hash, 0, NULL, NULL, &rec);
594
595 if (rec && match_p) {
596 *match_p = rec->val;
597 }
598 return rec ? LY_SUCCESS : LY_ENOTFOUND;
599}
600
601LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200602lyht_find_next(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
603{
604 struct ht_rec *rec, *crec;
605 uint32_t i, c;
Michal Vaskoda859032020-07-14 12:20:14 +0200606 LY_ERR r;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200607
Michal Vaskoda859032020-07-14 12:20:14 +0200608 /* found the record of the previously found value */
609 if (lyht_find_rec(ht, val_p, hash, 1, &crec, &i, &rec)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200610 /* not found, cannot happen */
Michal Vaskoda859032020-07-14 12:20:14 +0200611 LOGINT_RET(NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200612 }
613
614 /* go through collisions and find next one after the previous one */
Michal Vaskoda859032020-07-14 12:20:14 +0200615 c = crec->hits;
616 for (++i; i < c; ++i) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200617 r = lyht_find_collision(ht, &rec, crec);
618 assert(!r);
619 (void)r;
620
Michal Vaskoda859032020-07-14 12:20:14 +0200621 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 0, ht->cb_data)) {
622 /* even the value matches */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200623 if (match_p) {
624 *match_p = rec->val;
625 }
Michal Vaskoda859032020-07-14 12:20:14 +0200626 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200627 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200628 }
629
630 /* the last equal value was already returned */
Michal Vaskoda859032020-07-14 12:20:14 +0200631 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200632}
633
634LY_ERR
635lyht_insert_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash,
Radek Krejci0f969882020-08-21 16:56:47 +0200636 values_equal_cb resize_val_equal, void **match_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200637{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200638 LY_ERR r, ret = LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200639 struct ht_rec *rec, *crec = NULL;
640 int32_t i;
Radek Krejci540ce982020-11-26 12:01:30 +0100641 values_equal_cb old_val_equal = NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200642
643 if (!lyht_find_first(ht, hash, &rec)) {
644 /* we found matching shortened hash */
645 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
646 /* even the value matches */
647 if (match_p) {
648 *match_p = (void *)&rec->val;
649 }
650 return LY_EEXIST;
651 }
652
653 /* some collisions, we need to go through them, too */
654 crec = rec;
655 for (i = 1; i < crec->hits; ++i) {
656 r = lyht_find_collision(ht, &rec, crec);
657 assert(!r);
658
659 /* compare values */
660 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
661 if (match_p) {
662 *match_p = (void *)&rec->val;
663 }
664 return LY_EEXIST;
665 }
666 }
667
668 /* value not found, get the record where it will be inserted */
669 r = lyht_find_collision(ht, &rec, crec);
670 assert(r);
671 }
672
673 /* insert it into the returned record */
674 assert(rec->hits < 1);
675 rec->hash = hash;
676 rec->hits = 1;
677 memcpy(&rec->val, val_p, ht->rec_size - (sizeof(struct ht_rec) - 1));
678 if (match_p) {
679 *match_p = (void *)&rec->val;
680 }
681
682 if (crec) {
683 /* there was a collision, increase hits */
684 if (crec->hits == INT32_MAX) {
685 LOGINT(NULL);
686 }
687 ++crec->hits;
688 }
689
690 /* check size & enlarge if needed */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200691 ++ht->used;
692 if (ht->resize) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100693 r = (ht->used * LYHT_HUNDRED_PERCENTAGE) / ht->size;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200694 if ((ht->resize == 1) && (r >= LYHT_FIRST_SHRINK_PERCENTAGE)) {
695 /* enable shrinking */
696 ht->resize = 2;
697 }
698 if ((ht->resize == 2) && (r >= LYHT_ENLARGE_PERCENTAGE)) {
699 if (resize_val_equal) {
700 old_val_equal = lyht_set_cb(ht, resize_val_equal);
701 }
702
703 /* enlarge */
704 ret = lyht_resize(ht, 1);
705 /* if hash_table was resized, we need to find new matching value */
Michal Vasko69730152020-10-09 16:30:07 +0200706 if ((ret == LY_SUCCESS) && match_p) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200707 lyht_find(ht, val_p, hash, match_p);
708 }
709
710 if (resize_val_equal) {
711 lyht_set_cb(ht, old_val_equal);
712 }
713 }
714 }
715 return ret;
716}
717
Radek Krejci0ae092d2018-09-20 16:43:19 +0200718LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200719lyht_insert(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
720{
721 return lyht_insert_with_resize_cb(ht, val_p, hash, NULL, match_p);
722}
723
724LY_ERR
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200725lyht_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 +0200726{
727 struct ht_rec *rec, *crec;
728 int32_t i;
Radek Krejci857189e2020-09-01 13:26:36 +0200729 ly_bool first_matched = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200730 LY_ERR r, ret = LY_SUCCESS;
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200731 values_equal_cb old_val_equal;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200732
Michal Vasko4a4c7ed2020-07-17 09:30:12 +0200733 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 +0200734
735 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
736 /* even the value matches */
737 first_matched = 1;
738 }
739
740 /* we always need to go through collisions */
741 crec = rec;
742 for (i = 1; i < crec->hits; ++i) {
743 r = lyht_find_collision(ht, &rec, crec);
744 assert(!r);
745
746 /* compare values */
747 if (!first_matched && (rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
748 break;
749 }
750 }
751
752 if (i < crec->hits) {
753 /* one of collisions matched, reduce collision count, remove the record */
754 assert(!first_matched);
755 --crec->hits;
756 rec->hits = -1;
757 } else if (first_matched) {
758 /* the first record matches */
759 if (crec != rec) {
760 /* ... so put the last collision in its place */
761 rec->hits = crec->hits - 1;
762 memcpy(crec, rec, ht->rec_size);
763 }
764 rec->hits = -1;
765 } else {
766 /* value not found even in collisions */
Michal Vasko4a4c7ed2020-07-17 09:30:12 +0200767 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200768 }
769
770 /* check size & shrink if needed */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200771 --ht->used;
772 if (ht->resize == 2) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100773 r = (ht->used * LYHT_HUNDRED_PERCENTAGE) / ht->size;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200774 if ((r < LYHT_SHRINK_PERCENTAGE) && (ht->size > LYHT_MIN_SIZE)) {
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200775 if (resize_val_equal) {
776 old_val_equal = lyht_set_cb(ht, resize_val_equal);
777 }
778
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200779 /* shrink */
780 ret = lyht_resize(ht, 0);
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200781
782 if (resize_val_equal) {
783 lyht_set_cb(ht, old_val_equal);
784 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200785 }
786 }
787
788 return ret;
789}
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200790
791LY_ERR
792lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash)
793{
794 return lyht_remove_with_resize_cb(ht, val_p, hash, NULL);
795}