blob: 68eebedb4ef33f3d5109cf716a50d58fde6d3317 [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
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "common.h"
24#include "dict.h"
Radek Krejci5aeea3a2018-09-05 13:29:36 +020025
26static int
27lydict_val_eq(void *val1_p, void *val2_p, int UNUSED(mod), void *cb_data)
28{
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020029 LY_CHECK_ARG_RET(NULL, val1_p, val2_p, cb_data, 0);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020030
31 const char *str1 = ((struct dict_rec *)val1_p)->value;
32 const char *str2 = ((struct dict_rec *)val2_p)->value;
33
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020034 LY_CHECK_ERR_RET(!str1, LOGARG(NULL, val1_p), 0);
35 LY_CHECK_ERR_RET(!str2, LOGARG(NULL, val2_p), 0);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020036
37 if (strncmp(str1, str2, *(size_t *)cb_data) == 0) {
38 return 1;
39 }
40
41 return 0;
42}
43
44void
45lydict_init(struct dict_table *dict)
46{
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020047 LY_CHECK_ARG_RET(NULL, dict,);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020048
49 dict->hash_tab = lyht_new(1024, sizeof(struct dict_rec), lydict_val_eq, NULL, 1);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020050 LY_CHECK_ERR_RET(!dict->hash_tab, LOGINT(NULL), );
Radek Krejci5aeea3a2018-09-05 13:29:36 +020051 pthread_mutex_init(&dict->lock, NULL);
52}
53
54void
55lydict_clean(struct dict_table *dict)
56{
57 unsigned int i;
58 struct dict_rec *dict_rec = NULL;
59 struct ht_rec *rec = NULL;
60
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020061 LY_CHECK_ARG_RET(NULL, dict,);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020062
63 for (i = 0; i < dict->hash_tab->size; i++) {
64 /* 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 */
72 dict_rec = (struct dict_rec *)rec->val;
73 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{
130 size_t len;
131 int ret;
132 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
153 if (ret == 0) {
154 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 *
Michal Vasko52927e22020-03-16 17:26:14 +0100176dict_insert(const struct ly_ctx *ctx, char *value, size_t len, int zerocopy)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200177{
178 struct dict_rec *match = NULL, rec;
179 int ret = 0;
180 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 *
258lyht_new(uint32_t size, uint16_t val_size, values_equal_cb val_equal, void *cb_data, int resize)
259{
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;
278 ht->resize = (uint16_t)resize;
279
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
335lyht_resize(struct hash_table *ht, int enlarge)
336{
337 struct ht_rec *rec;
338 unsigned char *old_recs;
339 uint32_t i, old_size;
340 int ret;
341
342 old_recs = ht->recs;
343 old_size = ht->size;
344
345 if (enlarge) {
346 /* double the size */
347 ht->size <<= 1;
348 } else {
349 /* half the size */
350 ht->size >>= 1;
351 }
352
353 ht->recs = calloc(ht->size, ht->rec_size);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200354 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 +0200355
356 /* reset used, it will increase again */
357 ht->used = 0;
358
359 /* add all the old records into the new records array */
360 for (i = 0; i < old_size; ++i) {
361 rec = lyht_get_rec(old_recs, ht->rec_size, i);
362 if (rec->hits > 0) {
363 ret = lyht_insert(ht, rec->val, rec->hash, NULL);
364 assert(!ret);
365 (void)ret;
366 }
367 }
368
369 /* final touches */
370 free(old_recs);
371 return LY_SUCCESS;
372}
373
374/* return: 0 - hash found, returned its record,
375 * 1 - hash not found, returned the record where it would be inserted */
376static int
377lyht_find_first(struct hash_table *ht, uint32_t hash, struct ht_rec **rec_p)
378{
379 struct ht_rec *rec;
380 uint32_t i, idx;
381
382 if (rec_p) {
383 *rec_p = NULL;
384 }
385
386 idx = i = hash & (ht->size - 1);
387 rec = lyht_get_rec(ht->recs, ht->rec_size, idx);
388
389 /* skip through overflow and deleted records */
390 while ((rec->hits != 0) && ((rec->hits == -1) || ((rec->hash & (ht->size - 1)) != idx))) {
391 if ((rec->hits == -1) && rec_p && !(*rec_p)) {
392 /* remember this record for return */
393 *rec_p = rec;
394 }
395 i = (i + 1) % ht->size;
396 if (i == idx) {
397 /* we went through all the records (very unlikely, but possible when many records are invalid),
398 * just return not found */
399 assert(!rec_p || *rec_p);
400 return 1;
401 }
402 rec = lyht_get_rec(ht->recs, ht->rec_size, i);
403 }
404 if (rec->hits == 0) {
405 /* we could not find the value */
406 if (rec_p && !*rec_p) {
407 *rec_p = rec;
408 }
409 return 1;
410 }
411
412 /* we have found a record with equal (shortened) hash */
413 if (rec_p) {
414 *rec_p = rec;
415 }
416 return 0;
417}
418
419/**
420 * @brief Search for the next collision.
421 *
422 * @param[in] ht Hash table to search in.
423 * @param[in,out] last Last returned collision record.
424 * @param[in] first First collision record (hits > 1).
425 * @return 0 when hash collision found, \p last points to this next collision,
426 * 1 when hash collision not found, \p last points to the record where it would be inserted.
427 */
428static int
429lyht_find_collision(struct hash_table *ht, struct ht_rec **last, struct ht_rec *first)
430{
431 struct ht_rec *empty = NULL;
432 uint32_t i, idx;
433
434 assert(last && *last);
435
436 idx = (*last)->hash & (ht->size - 1);
437 i = (((unsigned char *)*last) - ht->recs) / ht->rec_size;
438
439 do {
440 i = (i + 1) % ht->size;
441 *last = lyht_get_rec(ht->recs, ht->rec_size, i);
442 if (*last == first) {
443 /* we went through all the records (very unlikely, but possible when many records are invalid),
444 * just return an invalid record */
445 assert(empty);
446 *last = empty;
447 return 1;
448 }
449
450 if (((*last)->hits == -1) && !empty) {
451 empty = *last;
452 }
453 } while (((*last)->hits != 0) && (((*last)->hits == -1) || (((*last)->hash & (ht->size - 1)) != idx)));
454
455 if ((*last)->hits > 0) {
456 /* we found a collision */
457 assert((*last)->hits == 1);
458 return 0;
459 }
460
461 /* no next collision found, return the record where it would be inserted */
462 if (empty) {
463 *last = empty;
464 } /* else (*last)->hits == 0, it is already correct */
465 return 1;
466}
467
468int
469lyht_find(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
470{
471 struct ht_rec *rec, *crec;
472 uint32_t i, c;
473 int r;
474
475 if (lyht_find_first(ht, hash, &rec)) {
476 /* not found */
477 return 1;
478 }
479 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 0, ht->cb_data)) {
480 /* even the value matches */
481 if (match_p) {
482 *match_p = rec->val;
483 }
484 return 0;
485 }
486
487 /* some collisions, we need to go through them, too */
488 crec = rec;
489 c = rec->hits;
490 for (i = 1; i < c; ++i) {
491 r = lyht_find_collision(ht, &rec, crec);
492 assert(!r);
493 (void)r;
494
495 /* compare values */
496 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 0, ht->cb_data)) {
497 if (match_p) {
498 *match_p = rec->val;
499 }
500 return 0;
501 }
502 }
503
504 /* not found even in collisions */
505 return 1;
506}
507
508int
509lyht_find_next(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
510{
511 struct ht_rec *rec, *crec;
512 uint32_t i, c;
513 int r, found = 0;
514
515 if (lyht_find_first(ht, hash, &rec)) {
516 /* not found, cannot happen */
517 assert(0);
518 }
519
520 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
521 /* previously returned value */
522 found = 1;
523 }
524
525 if (rec->hits == 1) {
526 /* there are no more similar values */
527 assert(rec->hash == hash);
528 assert(found);
529 return 1;
530 }
531
532 /* go through collisions and find next one after the previous one */
533 crec = rec;
534 c = rec->hits;
535 for (i = 1; i < c; ++i) {
536 r = lyht_find_collision(ht, &rec, crec);
537 assert(!r);
538 (void)r;
539
540 if (rec->hash != hash) {
541 /* a normal collision, we are not interested in those */
542 continue;
543 }
544
545 if (found) {
546 /* next value with equal hash, found our value */
547 if (match_p) {
548 *match_p = rec->val;
549 }
550 return 0;
551 }
552
553 if (!ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
554 /* already returned value, skip */
555 continue;
556 }
557
558 /* this one was returned previously, continue looking */
559 found = 1;
560 }
561
562 /* the last equal value was already returned */
563 assert(found);
564 return 1;
565}
566
567LY_ERR
568lyht_insert_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash,
569 values_equal_cb resize_val_equal, void **match_p)
570{
571 struct ht_rec *rec, *crec = NULL;
572 int32_t i;
573 int r, ret;
574 values_equal_cb old_val_equal;
575
576 if (!lyht_find_first(ht, hash, &rec)) {
577 /* we found matching shortened hash */
578 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
579 /* even the value matches */
580 if (match_p) {
581 *match_p = (void *)&rec->val;
582 }
583 return LY_EEXIST;
584 }
585
586 /* some collisions, we need to go through them, too */
587 crec = rec;
588 for (i = 1; i < crec->hits; ++i) {
589 r = lyht_find_collision(ht, &rec, crec);
590 assert(!r);
591
592 /* compare values */
593 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
594 if (match_p) {
595 *match_p = (void *)&rec->val;
596 }
597 return LY_EEXIST;
598 }
599 }
600
601 /* value not found, get the record where it will be inserted */
602 r = lyht_find_collision(ht, &rec, crec);
603 assert(r);
604 }
605
606 /* insert it into the returned record */
607 assert(rec->hits < 1);
608 rec->hash = hash;
609 rec->hits = 1;
610 memcpy(&rec->val, val_p, ht->rec_size - (sizeof(struct ht_rec) - 1));
611 if (match_p) {
612 *match_p = (void *)&rec->val;
613 }
614
615 if (crec) {
616 /* there was a collision, increase hits */
617 if (crec->hits == INT32_MAX) {
618 LOGINT(NULL);
619 }
620 ++crec->hits;
621 }
622
623 /* check size & enlarge if needed */
624 ret = LY_SUCCESS;
625 ++ht->used;
626 if (ht->resize) {
627 r = (ht->used * 100) / ht->size;
628 if ((ht->resize == 1) && (r >= LYHT_FIRST_SHRINK_PERCENTAGE)) {
629 /* enable shrinking */
630 ht->resize = 2;
631 }
632 if ((ht->resize == 2) && (r >= LYHT_ENLARGE_PERCENTAGE)) {
633 if (resize_val_equal) {
634 old_val_equal = lyht_set_cb(ht, resize_val_equal);
635 }
636
637 /* enlarge */
638 ret = lyht_resize(ht, 1);
639 /* if hash_table was resized, we need to find new matching value */
640 if (ret == LY_SUCCESS && match_p) {
641 lyht_find(ht, val_p, hash, match_p);
642 }
643
644 if (resize_val_equal) {
645 lyht_set_cb(ht, old_val_equal);
646 }
647 }
648 }
649 return ret;
650}
651
Radek Krejci0ae092d2018-09-20 16:43:19 +0200652LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200653lyht_insert(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
654{
655 return lyht_insert_with_resize_cb(ht, val_p, hash, NULL, match_p);
656}
657
658LY_ERR
659lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash)
660{
661 struct ht_rec *rec, *crec;
662 int32_t i;
663 int first_matched = 0, r, ret;
664
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200665 LY_CHECK_ERR_RET(lyht_find_first(ht, hash, &rec), LOGARG(NULL, hash), LY_EINVAL); /* hash not found */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200666
667 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
668 /* even the value matches */
669 first_matched = 1;
670 }
671
672 /* we always need to go through collisions */
673 crec = rec;
674 for (i = 1; i < crec->hits; ++i) {
675 r = lyht_find_collision(ht, &rec, crec);
676 assert(!r);
677
678 /* compare values */
679 if (!first_matched && (rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
680 break;
681 }
682 }
683
684 if (i < crec->hits) {
685 /* one of collisions matched, reduce collision count, remove the record */
686 assert(!first_matched);
687 --crec->hits;
688 rec->hits = -1;
689 } else if (first_matched) {
690 /* the first record matches */
691 if (crec != rec) {
692 /* ... so put the last collision in its place */
693 rec->hits = crec->hits - 1;
694 memcpy(crec, rec, ht->rec_size);
695 }
696 rec->hits = -1;
697 } else {
698 /* value not found even in collisions */
699 LOGINT(NULL);
700 return LY_EINT;
701 }
702
703 /* check size & shrink if needed */
704 ret = LY_SUCCESS;
705 --ht->used;
706 if (ht->resize == 2) {
707 r = (ht->used * 100) / ht->size;
708 if ((r < LYHT_SHRINK_PERCENTAGE) && (ht->size > LYHT_MIN_SIZE)) {
709 /* shrink */
710 ret = lyht_resize(ht, 0);
711 }
712 }
713
714 return ret;
715}