blob: 03154ede67cd0f0440940ee59e3cc974a5d760b7 [file] [log] [blame]
Radek Krejci5aeea3a2018-09-05 13:29:36 +02001/**
2 * @file hash_table.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskoae130f52023-04-20 14:25:16 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
5 * @brief libyang generic hash table implementation
Radek Krejci5aeea3a2018-09-05 13:29:36 +02006 *
Michal Vaskoae130f52023-04-20 14:25:16 +02007 * Copyright (c) 2015 - 2023 CESNET, z.s.p.o.
Radek Krejci5aeea3a2018-09-05 13:29:36 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#include "hash_table.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020017
Michal Vasko69730152020-10-09 16:30:07 +020018#include <assert.h>
19#include <pthread.h>
Radek Krejci5aeea3a2018-09-05 13:29:36 +020020#include <stdint.h>
21#include <stdlib.h>
Michal Vasko69730152020-10-09 16:30:07 +020022#include <string.h>
Radek Krejci5aeea3a2018-09-05 13:29:36 +020023
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020025#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "dict.h"
Radek Krejci857189e2020-09-01 13:26:36 +020027#include "log.h"
Radek Krejci5aeea3a2018-09-05 13:29:36 +020028
Michal Vaskoae130f52023-04-20 14:25:16 +020029LIBYANG_API_DEF uint32_t
30lyht_hash_multi(uint32_t hash, const char *key_part, size_t len)
Radek Krejci5aeea3a2018-09-05 13:29:36 +020031{
32 uint32_t i;
33
aPiecek4f07c3e2021-06-11 10:53:07 +020034 if (key_part && len) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +020035 for (i = 0; i < len; ++i) {
36 hash += key_part[i];
37 hash += (hash << 10);
38 hash ^= (hash >> 6);
39 }
40 } else {
41 hash += (hash << 3);
42 hash ^= (hash >> 11);
43 hash += (hash << 15);
44 }
45
46 return hash;
47}
48
Michal Vaskoae130f52023-04-20 14:25:16 +020049LIBYANG_API_DEF uint32_t
50lyht_hash(const char *key, size_t len)
Radek Krejcif2dc4c52018-11-08 09:04:13 +010051{
52 uint32_t hash;
53
Michal Vaskoae130f52023-04-20 14:25:16 +020054 hash = lyht_hash_multi(0, key, len);
55 return lyht_hash_multi(hash, NULL, len);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020056}
57
Olivier Matz75c00192023-09-21 14:35:12 +020058static LY_ERR
59lyht_init_hlists_and_records(struct ly_ht *ht)
Radek Krejci5aeea3a2018-09-05 13:29:36 +020060{
Olivier Matz75c00192023-09-21 14:35:12 +020061 struct ly_ht_rec *rec;
62 uint32_t i;
63
64 ht->recs = calloc(ht->size, ht->rec_size);
65 LY_CHECK_ERR_RET(!ht->recs, LOGMEM(NULL), LY_EMEM);
66 for (i = 0; i < ht->size; i++) {
67 rec = lyht_get_rec(ht->recs, ht->rec_size, i);
68 if (i != ht->size)
69 rec->next = i + 1;
70 else
71 rec->next = LYHT_NO_RECORD;
72 }
73
74 ht->hlists = malloc(sizeof(ht->hlists[0]) * ht->size);
75 LY_CHECK_ERR_RET(!ht->hlists, free(ht->recs); LOGMEM(NULL), LY_EMEM);
Olivier Matz6a669c12023-09-28 12:07:12 +020076 for (i = 0; i < ht->size; i++) {
77 ht->hlists[i].first = LYHT_NO_RECORD;
78 ht->hlists[i].last = LYHT_NO_RECORD;
79 }
Olivier Matz75c00192023-09-21 14:35:12 +020080 ht->first_free_rec = 0;
81
82 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020083}
84
Michal Vaskoae130f52023-04-20 14:25:16 +020085LIBYANG_API_DEF struct ly_ht *
Michal Vasko62524a92021-02-26 10:08:50 +010086lyht_new(uint32_t size, uint16_t val_size, lyht_value_equal_cb val_equal, void *cb_data, uint16_t resize)
Radek Krejci5aeea3a2018-09-05 13:29:36 +020087{
Michal Vasko8efac242023-03-30 08:24:56 +020088 struct ly_ht *ht;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020089
90 /* check that 2^x == size (power of 2) */
91 assert(size && !(size & (size - 1)));
92 assert(val_equal && val_size);
93 assert(resize == 0 || resize == 1);
94
95 if (size < LYHT_MIN_SIZE) {
96 size = LYHT_MIN_SIZE;
97 }
98
99 ht = malloc(sizeof *ht);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200100 LY_CHECK_ERR_RET(!ht, LOGMEM(NULL), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200101
102 ht->used = 0;
103 ht->size = size;
104 ht->val_equal = val_equal;
105 ht->cb_data = cb_data;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200106 ht->resize = resize;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200107
Olivier Matz75c00192023-09-21 14:35:12 +0200108 ht->rec_size = SIZEOF_LY_HT_REC + val_size;
109 if (lyht_init_hlists_and_records(ht) != LY_SUCCESS) {
110 free(ht);
111 return NULL;
112 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200113
114 return ht;
115}
116
Michal Vaskoae130f52023-04-20 14:25:16 +0200117LIBYANG_API_DEF lyht_value_equal_cb
Michal Vasko8efac242023-03-30 08:24:56 +0200118lyht_set_cb(struct ly_ht *ht, lyht_value_equal_cb new_val_equal)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200119{
Michal Vasko62524a92021-02-26 10:08:50 +0100120 lyht_value_equal_cb prev;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200121
122 prev = ht->val_equal;
123 ht->val_equal = new_val_equal;
124 return prev;
125}
126
Michal Vaskoae130f52023-04-20 14:25:16 +0200127LIBYANG_API_DEF void *
Michal Vasko8efac242023-03-30 08:24:56 +0200128lyht_set_cb_data(struct ly_ht *ht, void *new_cb_data)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200129{
130 void *prev;
131
132 prev = ht->cb_data;
133 ht->cb_data = new_cb_data;
134 return prev;
135}
136
Michal Vaskoae130f52023-04-20 14:25:16 +0200137LIBYANG_API_DEF struct ly_ht *
Michal Vasko8efac242023-03-30 08:24:56 +0200138lyht_dup(const struct ly_ht *orig)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200139{
Michal Vasko8efac242023-03-30 08:24:56 +0200140 struct ly_ht *ht;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200141
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200142 LY_CHECK_ARG_RET(NULL, orig, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200143
Olivier Matz75c00192023-09-21 14:35:12 +0200144 ht = lyht_new(orig->size, orig->rec_size - SIZEOF_LY_HT_REC, orig->val_equal, orig->cb_data, orig->resize ? 1 : 0);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200145 if (!ht) {
146 return NULL;
147 }
148
Olivier Matz75c00192023-09-21 14:35:12 +0200149 memcpy(ht->hlists, orig->hlists, sizeof(ht->hlists[0]) * orig->size);
150 memcpy(ht->recs, orig->recs, (size_t)orig->size * orig->rec_size);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200151 ht->used = orig->used;
152 return ht;
153}
154
Michal Vaskoae130f52023-04-20 14:25:16 +0200155LIBYANG_API_DEF void
Michal Vasko8efac242023-03-30 08:24:56 +0200156lyht_free(struct ly_ht *ht, void (*val_free)(void *val_p))
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200157{
Michal Vasko8efac242023-03-30 08:24:56 +0200158 struct ly_ht_rec *rec;
Olivier Matz75c00192023-09-21 14:35:12 +0200159 uint32_t hlist_idx;
160 uint32_t rec_idx;
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100161
162 if (!ht) {
163 return;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200164 }
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100165
166 if (val_free) {
Olivier Matz75c00192023-09-21 14:35:12 +0200167 LYHT_ITER_ALL_RECS(ht, hlist_idx, rec_idx, rec)
168 val_free(&rec->val);
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100169 }
Olivier Matz75c00192023-09-21 14:35:12 +0200170 free(ht->hlists);
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100171 free(ht->recs);
172 free(ht);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200173}
174
Michal Vaskodc95d9c2021-04-12 15:11:48 +0200175/**
176 * @brief Resize a hash table.
177 *
178 * @param[in] ht Hash table to resize.
179 * @param[in] operation Operation to perform. 1 to enlarge, -1 to shrink, 0 to only rehash all records.
180 * @return LY_ERR value.
181 */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200182static LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200183lyht_resize(struct ly_ht *ht, int operation)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200184{
Michal Vasko8efac242023-03-30 08:24:56 +0200185 struct ly_ht_rec *rec;
Olivier Matz6a669c12023-09-28 12:07:12 +0200186 struct ly_ht_hlist *old_hlists;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200187 unsigned char *old_recs;
Olivier Matz75c00192023-09-21 14:35:12 +0200188 uint32_t old_first_free_rec;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200189 uint32_t i, old_size;
Olivier Matz75c00192023-09-21 14:35:12 +0200190 uint32_t rec_idx;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200191
Olivier Matz75c00192023-09-21 14:35:12 +0200192 old_hlists = ht->hlists;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200193 old_recs = ht->recs;
194 old_size = ht->size;
Olivier Matz75c00192023-09-21 14:35:12 +0200195 old_first_free_rec = ht->first_free_rec;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200196
Michal Vaskodc95d9c2021-04-12 15:11:48 +0200197 if (operation > 0) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200198 /* double the size */
199 ht->size <<= 1;
Michal Vaskodc95d9c2021-04-12 15:11:48 +0200200 } else if (operation < 0) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200201 /* half the size */
202 ht->size >>= 1;
203 }
204
Olivier Matz75c00192023-09-21 14:35:12 +0200205 if (lyht_init_hlists_and_records(ht) != LY_SUCCESS) {
206 ht->hlists = old_hlists;
207 ht->recs = old_recs;
208 ht->size = old_size;
209 ht->first_free_rec = old_first_free_rec;
210 return LY_EMEM;
211 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200212
Olivier Matz75c00192023-09-21 14:35:12 +0200213 /* reset used, it will increase again */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200214 ht->used = 0;
215
216 /* add all the old records into the new records array */
Olivier Matz75c00192023-09-21 14:35:12 +0200217 for (i = 0; i < old_size; i++) {
Olivier Matz6a669c12023-09-28 12:07:12 +0200218 for (rec_idx = old_hlists[i].first, rec = lyht_get_rec(old_recs, ht->rec_size, rec_idx);
Olivier Matz75c00192023-09-21 14:35:12 +0200219 rec_idx != LYHT_NO_RECORD;
220 rec_idx = rec->next, rec = lyht_get_rec(old_recs, ht->rec_size, rec_idx)) {
221 LY_ERR ret;
222
223 ret = lyht_insert(ht, rec->val, rec->hash, NULL);
Michal Vasko26bbb272022-08-02 14:54:33 +0200224
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200225 assert(!ret);
226 (void)ret;
227 }
228 }
229
230 /* final touches */
231 free(old_recs);
Olivier Matz75c00192023-09-21 14:35:12 +0200232 free(old_hlists);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200233 return LY_SUCCESS;
234}
235
Michal Vaskoda859032020-07-14 12:20:14 +0200236/**
Michal Vaskoda859032020-07-14 12:20:14 +0200237 * @brief Search for a record with specific value and hash.
238 *
239 * @param[in] ht Hash table to search in.
240 * @param[in] val_p Pointer to the value to find.
241 * @param[in] hash Hash to find.
Radek Krejci857189e2020-09-01 13:26:36 +0200242 * @param[in] mod Whether the operation modifies the hash table (insert or remove) or not (find).
Michal Vaskoda859032020-07-14 12:20:14 +0200243 * @param[out] crec_p Optional found first record.
244 * @param[out] col Optional collision number of @p rec_p, 0 for no collision.
245 * @param[out] rec_p Found exact matching record, may be a collision of @p crec_p.
246 * @return LY_ENOTFOUND if no record found,
247 * @return LY_SUCCESS if record was found.
248 */
249static LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200250lyht_find_rec(struct ly_ht *ht, void *val_p, uint32_t hash, ly_bool mod, struct ly_ht_rec **crec_p, uint32_t *col,
251 struct ly_ht_rec **rec_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200252{
Olivier Matz75c00192023-09-21 14:35:12 +0200253 uint32_t hlist_idx = hash & (ht->size - 1);
254 struct ly_ht_rec *rec;
255 uint32_t rec_idx;
Michal Vaskoda859032020-07-14 12:20:14 +0200256
257 if (crec_p) {
258 *crec_p = NULL;
259 }
260 if (col) {
261 *col = 0;
262 }
263 *rec_p = NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200264
Olivier Matz75c00192023-09-21 14:35:12 +0200265 LYHT_ITER_HLIST_RECS(ht, hlist_idx, rec_idx, rec) {
Michal Vaskoda859032020-07-14 12:20:14 +0200266 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, mod, ht->cb_data)) {
267 if (crec_p) {
Olivier Matz75c00192023-09-21 14:35:12 +0200268 *crec_p = rec;
Michal Vaskoda859032020-07-14 12:20:14 +0200269 }
270 *rec_p = rec;
271 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200272 }
Olivier Matz75c00192023-09-21 14:35:12 +0200273
274 if (col) {
275 *col = *col + 1;
276 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200277 }
278
279 /* not found even in collisions */
Michal Vaskoda859032020-07-14 12:20:14 +0200280 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200281}
282
Michal Vaskoae130f52023-04-20 14:25:16 +0200283LIBYANG_API_DEF LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200284lyht_find(struct ly_ht *ht, void *val_p, uint32_t hash, void **match_p)
Michal Vaskoda859032020-07-14 12:20:14 +0200285{
Michal Vasko8efac242023-03-30 08:24:56 +0200286 struct ly_ht_rec *rec;
Michal Vaskoda859032020-07-14 12:20:14 +0200287
288 lyht_find_rec(ht, val_p, hash, 0, NULL, NULL, &rec);
289
290 if (rec && match_p) {
291 *match_p = rec->val;
292 }
293 return rec ? LY_SUCCESS : LY_ENOTFOUND;
294}
295
Michal Vaskoae130f52023-04-20 14:25:16 +0200296LIBYANG_API_DEF LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200297lyht_find_next_with_collision_cb(struct ly_ht *ht, void *val_p, uint32_t hash,
Michal Vasko6374de22022-09-05 15:48:48 +0200298 lyht_value_equal_cb collision_val_equal, void **match_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200299{
Michal Vasko8efac242023-03-30 08:24:56 +0200300 struct ly_ht_rec *rec, *crec;
Olivier Matz75c00192023-09-21 14:35:12 +0200301 uint32_t rec_idx;
302 uint32_t i;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200303
Michal Vasko6374de22022-09-05 15:48:48 +0200304 /* find the record of the previously found value */
Michal Vaskoda859032020-07-14 12:20:14 +0200305 if (lyht_find_rec(ht, val_p, hash, 1, &crec, &i, &rec)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200306 /* not found, cannot happen */
Michal Vaskoda859032020-07-14 12:20:14 +0200307 LOGINT_RET(NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200308 }
309
Olivier Matz75c00192023-09-21 14:35:12 +0200310 for (rec_idx = rec->next, rec = lyht_get_rec(ht->recs, ht->rec_size, rec_idx);
311 rec_idx != LYHT_NO_RECORD;
312 rec_idx = rec->next, rec = lyht_get_rec(ht->recs, ht->rec_size, rec_idx)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200313
Michal Vasko6374de22022-09-05 15:48:48 +0200314 if (rec->hash != hash) {
315 continue;
316 }
317
318 if (collision_val_equal) {
319 if (collision_val_equal(val_p, &rec->val, 0, ht->cb_data)) {
320 /* even the value matches */
321 if (match_p) {
322 *match_p = rec->val;
323 }
324 return LY_SUCCESS;
325 }
326 } else if (ht->val_equal(val_p, &rec->val, 0, ht->cb_data)) {
Michal Vaskoda859032020-07-14 12:20:14 +0200327 /* even the value matches */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200328 if (match_p) {
329 *match_p = rec->val;
330 }
Michal Vaskoda859032020-07-14 12:20:14 +0200331 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200332 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200333 }
334
335 /* the last equal value was already returned */
Michal Vaskoda859032020-07-14 12:20:14 +0200336 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200337}
338
Michal Vaskoae130f52023-04-20 14:25:16 +0200339LIBYANG_API_DEF LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200340lyht_find_next(struct ly_ht *ht, void *val_p, uint32_t hash, void **match_p)
Michal Vasko6374de22022-09-05 15:48:48 +0200341{
342 return lyht_find_next_with_collision_cb(ht, val_p, hash, NULL, match_p);
343}
344
Olivier Matz75c00192023-09-21 14:35:12 +0200345static LY_ERR
346__lyht_insert_with_resize_cb(struct ly_ht *ht, void *val_p, uint32_t hash, lyht_value_equal_cb resize_val_equal,
Michal Vasko62524a92021-02-26 10:08:50 +0100347 void **match_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200348{
Olivier Matz75c00192023-09-21 14:35:12 +0200349 uint32_t hlist_idx = hash & (ht->size - 1);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200350 LY_ERR r, ret = LY_SUCCESS;
Olivier Matz6a669c12023-09-28 12:07:12 +0200351 struct ly_ht_rec *rec, *prev_rec;
Michal Vasko62524a92021-02-26 10:08:50 +0100352 lyht_value_equal_cb old_val_equal = NULL;
Olivier Matz75c00192023-09-21 14:35:12 +0200353 uint32_t rec_idx;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200354
Olivier Matz75c00192023-09-21 14:35:12 +0200355 if (lyht_find_rec(ht, val_p, hash, 1, NULL, NULL, &rec) == LY_SUCCESS) {
356 if (rec && match_p) {
357 *match_p = rec->val;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200358 }
Olivier Matz75c00192023-09-21 14:35:12 +0200359 return LY_EEXIST;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200360 }
361
Olivier Matz75c00192023-09-21 14:35:12 +0200362 rec_idx = ht->first_free_rec;
363 assert(rec_idx < ht->size);
364 rec = lyht_get_rec(ht->recs, ht->rec_size, rec_idx);
365 ht->first_free_rec = rec->next;
Olivier Matz6a669c12023-09-28 12:07:12 +0200366
367 if (ht->hlists[hlist_idx].first == LYHT_NO_RECORD) {
368 ht->hlists[hlist_idx].first = rec_idx;
369 } else {
370 prev_rec = lyht_get_rec(ht->recs, ht->rec_size, ht->hlists[hlist_idx].last);
371 prev_rec->next = rec_idx;
372 }
373 rec->next = LYHT_NO_RECORD;
374 ht->hlists[hlist_idx].last = rec_idx;
Olivier Matz75c00192023-09-21 14:35:12 +0200375
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200376 rec->hash = hash;
Olivier Matz75c00192023-09-21 14:35:12 +0200377 memcpy(&rec->val, val_p, ht->rec_size - SIZEOF_LY_HT_REC);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200378 if (match_p) {
379 *match_p = (void *)&rec->val;
380 }
381
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200382 /* check size & enlarge if needed */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200383 ++ht->used;
384 if (ht->resize) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100385 r = (ht->used * LYHT_HUNDRED_PERCENTAGE) / ht->size;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200386 if ((ht->resize == 1) && (r >= LYHT_FIRST_SHRINK_PERCENTAGE)) {
387 /* enable shrinking */
388 ht->resize = 2;
389 }
390 if ((ht->resize == 2) && (r >= LYHT_ENLARGE_PERCENTAGE)) {
391 if (resize_val_equal) {
392 old_val_equal = lyht_set_cb(ht, resize_val_equal);
393 }
394
395 /* enlarge */
396 ret = lyht_resize(ht, 1);
397 /* if hash_table was resized, we need to find new matching value */
Michal Vasko69730152020-10-09 16:30:07 +0200398 if ((ret == LY_SUCCESS) && match_p) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200399 lyht_find(ht, val_p, hash, match_p);
400 }
401
402 if (resize_val_equal) {
403 lyht_set_cb(ht, old_val_equal);
404 }
405 }
406 }
407 return ret;
408}
409
Michal Vaskoae130f52023-04-20 14:25:16 +0200410LIBYANG_API_DEF LY_ERR
Olivier Matz75c00192023-09-21 14:35:12 +0200411lyht_insert_with_resize_cb(struct ly_ht *ht, void *val_p, uint32_t hash, lyht_value_equal_cb resize_val_equal,
412 void **match_p)
413{
414 return __lyht_insert_with_resize_cb(ht, val_p, hash, resize_val_equal, match_p);
415}
416
417LIBYANG_API_DEF LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200418lyht_insert(struct ly_ht *ht, void *val_p, uint32_t hash, void **match_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200419{
Olivier Matz75c00192023-09-21 14:35:12 +0200420 return __lyht_insert_with_resize_cb(ht, val_p, hash, NULL, match_p);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200421}
422
Michal Vaskoae130f52023-04-20 14:25:16 +0200423LIBYANG_API_DEF LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200424lyht_remove_with_resize_cb(struct ly_ht *ht, void *val_p, uint32_t hash, lyht_value_equal_cb resize_val_equal)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200425{
Olivier Matz75c00192023-09-21 14:35:12 +0200426 struct ly_ht_rec *found_rec, *prev_rec, *rec;
427 uint32_t hlist_idx = hash & (ht->size - 1);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200428 LY_ERR r, ret = LY_SUCCESS;
stewegd8e2fc92023-05-31 09:52:56 +0200429 lyht_value_equal_cb old_val_equal = NULL;
Olivier Matz75c00192023-09-21 14:35:12 +0200430 uint32_t prev_rec_idx;
431 uint32_t rec_idx;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200432
Olivier Matz75c00192023-09-21 14:35:12 +0200433 LY_CHECK_ERR_RET(lyht_find_rec(ht, val_p, hash, 1, NULL, NULL, &found_rec),
434 LOGARG(NULL, hash), LY_ENOTFOUND); /* hash not found */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200435
Olivier Matz75c00192023-09-21 14:35:12 +0200436 prev_rec_idx = LYHT_NO_RECORD;
437 LYHT_ITER_HLIST_RECS(ht, hlist_idx, rec_idx, rec) {
438 if (rec == found_rec)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200439 break;
Olivier Matz75c00192023-09-21 14:35:12 +0200440 prev_rec_idx = rec_idx;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200441 }
442
Olivier Matz75c00192023-09-21 14:35:12 +0200443 if (prev_rec_idx == LYHT_NO_RECORD) {
Olivier Matz6a669c12023-09-28 12:07:12 +0200444 ht->hlists[hlist_idx].first = rec->next;
445 if (rec->next == LYHT_NO_RECORD)
446 ht->hlists[hlist_idx].last = LYHT_NO_RECORD;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200447 } else {
Olivier Matz75c00192023-09-21 14:35:12 +0200448 prev_rec = lyht_get_rec(ht->recs, ht->rec_size, prev_rec_idx);
449 prev_rec->next = rec->next;
Olivier Matz6a669c12023-09-28 12:07:12 +0200450 if (rec->next == LYHT_NO_RECORD)
451 ht->hlists[hlist_idx].last = prev_rec_idx;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200452 }
453
Olivier Matz75c00192023-09-21 14:35:12 +0200454 rec->next = ht->first_free_rec;
455 ht->first_free_rec = rec_idx;
456
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200457 /* check size & shrink if needed */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200458 --ht->used;
459 if (ht->resize == 2) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100460 r = (ht->used * LYHT_HUNDRED_PERCENTAGE) / ht->size;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200461 if ((r < LYHT_SHRINK_PERCENTAGE) && (ht->size > LYHT_MIN_SIZE)) {
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200462 if (resize_val_equal) {
463 old_val_equal = lyht_set_cb(ht, resize_val_equal);
464 }
465
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200466 /* shrink */
Michal Vaskodc95d9c2021-04-12 15:11:48 +0200467 ret = lyht_resize(ht, -1);
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200468
469 if (resize_val_equal) {
470 lyht_set_cb(ht, old_val_equal);
471 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200472 }
473 }
474
475 return ret;
476}
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200477
Michal Vaskoae130f52023-04-20 14:25:16 +0200478LIBYANG_API_DEF LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200479lyht_remove(struct ly_ht *ht, void *val_p, uint32_t hash)
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200480{
481 return lyht_remove_with_resize_cb(ht, val_p, hash, NULL);
482}
Michal Vasko626196f2022-08-05 12:49:52 +0200483
Michal Vaskoae130f52023-04-20 14:25:16 +0200484LIBYANG_API_DEF uint32_t
Michal Vasko626196f2022-08-05 12:49:52 +0200485lyht_get_fixed_size(uint32_t item_count)
486{
Olivier Matz75c00192023-09-21 14:35:12 +0200487 if (item_count == 0)
488 return 1;
Michal Vasko626196f2022-08-05 12:49:52 +0200489
Olivier Matz75c00192023-09-21 14:35:12 +0200490 /* return next power of 2 (greater or equal) */
491 item_count--;
492 item_count |= item_count >> 1;
493 item_count |= item_count >> 2;
494 item_count |= item_count >> 4;
495 item_count |= item_count >> 8;
496 item_count |= item_count >> 16;
Michal Vasko626196f2022-08-05 12:49:52 +0200497
Olivier Matz75c00192023-09-21 14:35:12 +0200498 return item_count + 1;
Michal Vasko626196f2022-08-05 12:49:52 +0200499}