blob: 86df07a4cef121dacf3059528329e136ecd24e7c [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
Olivier Matz6ad93d92023-09-28 12:13:36 +0200183lyht_resize(struct ly_ht *ht, int operation, int check)
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
Olivier Matz6ad93d92023-09-28 12:13:36 +0200223 if (check)
224 ret = lyht_insert(ht, rec->val, rec->hash, NULL);
225 else
226 ret = lyht_insert_no_check(ht, rec->val, rec->hash, NULL);
Michal Vasko26bbb272022-08-02 14:54:33 +0200227
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200228 assert(!ret);
229 (void)ret;
230 }
231 }
232
233 /* final touches */
234 free(old_recs);
Olivier Matz75c00192023-09-21 14:35:12 +0200235 free(old_hlists);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200236 return LY_SUCCESS;
237}
238
Michal Vaskoda859032020-07-14 12:20:14 +0200239/**
Michal Vaskoda859032020-07-14 12:20:14 +0200240 * @brief Search for a record with specific value and hash.
241 *
242 * @param[in] ht Hash table to search in.
243 * @param[in] val_p Pointer to the value to find.
244 * @param[in] hash Hash to find.
Radek Krejci857189e2020-09-01 13:26:36 +0200245 * @param[in] mod Whether the operation modifies the hash table (insert or remove) or not (find).
Michal Vaskoda859032020-07-14 12:20:14 +0200246 * @param[out] crec_p Optional found first record.
247 * @param[out] col Optional collision number of @p rec_p, 0 for no collision.
248 * @param[out] rec_p Found exact matching record, may be a collision of @p crec_p.
249 * @return LY_ENOTFOUND if no record found,
250 * @return LY_SUCCESS if record was found.
251 */
252static LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200253lyht_find_rec(struct ly_ht *ht, void *val_p, uint32_t hash, ly_bool mod, struct ly_ht_rec **crec_p, uint32_t *col,
254 struct ly_ht_rec **rec_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200255{
Olivier Matz75c00192023-09-21 14:35:12 +0200256 uint32_t hlist_idx = hash & (ht->size - 1);
257 struct ly_ht_rec *rec;
258 uint32_t rec_idx;
Michal Vaskoda859032020-07-14 12:20:14 +0200259
260 if (crec_p) {
261 *crec_p = NULL;
262 }
263 if (col) {
264 *col = 0;
265 }
266 *rec_p = NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200267
Olivier Matz75c00192023-09-21 14:35:12 +0200268 LYHT_ITER_HLIST_RECS(ht, hlist_idx, rec_idx, rec) {
Michal Vaskoda859032020-07-14 12:20:14 +0200269 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, mod, ht->cb_data)) {
270 if (crec_p) {
Olivier Matz75c00192023-09-21 14:35:12 +0200271 *crec_p = rec;
Michal Vaskoda859032020-07-14 12:20:14 +0200272 }
273 *rec_p = rec;
274 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200275 }
Olivier Matz75c00192023-09-21 14:35:12 +0200276
277 if (col) {
278 *col = *col + 1;
279 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200280 }
281
282 /* not found even in collisions */
Michal Vaskoda859032020-07-14 12:20:14 +0200283 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200284}
285
Michal Vaskoae130f52023-04-20 14:25:16 +0200286LIBYANG_API_DEF LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200287lyht_find(struct ly_ht *ht, void *val_p, uint32_t hash, void **match_p)
Michal Vaskoda859032020-07-14 12:20:14 +0200288{
Michal Vasko8efac242023-03-30 08:24:56 +0200289 struct ly_ht_rec *rec;
Michal Vaskoda859032020-07-14 12:20:14 +0200290
291 lyht_find_rec(ht, val_p, hash, 0, NULL, NULL, &rec);
292
293 if (rec && match_p) {
294 *match_p = rec->val;
295 }
296 return rec ? LY_SUCCESS : LY_ENOTFOUND;
297}
298
Michal Vaskoae130f52023-04-20 14:25:16 +0200299LIBYANG_API_DEF LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200300lyht_find_next_with_collision_cb(struct ly_ht *ht, void *val_p, uint32_t hash,
Michal Vasko6374de22022-09-05 15:48:48 +0200301 lyht_value_equal_cb collision_val_equal, void **match_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200302{
Michal Vasko8efac242023-03-30 08:24:56 +0200303 struct ly_ht_rec *rec, *crec;
Olivier Matz75c00192023-09-21 14:35:12 +0200304 uint32_t rec_idx;
305 uint32_t i;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200306
Michal Vasko6374de22022-09-05 15:48:48 +0200307 /* find the record of the previously found value */
Michal Vaskoda859032020-07-14 12:20:14 +0200308 if (lyht_find_rec(ht, val_p, hash, 1, &crec, &i, &rec)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200309 /* not found, cannot happen */
Michal Vaskoda859032020-07-14 12:20:14 +0200310 LOGINT_RET(NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200311 }
312
Olivier Matz75c00192023-09-21 14:35:12 +0200313 for (rec_idx = rec->next, rec = lyht_get_rec(ht->recs, ht->rec_size, rec_idx);
314 rec_idx != LYHT_NO_RECORD;
315 rec_idx = rec->next, rec = lyht_get_rec(ht->recs, ht->rec_size, rec_idx)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200316
Michal Vasko6374de22022-09-05 15:48:48 +0200317 if (rec->hash != hash) {
318 continue;
319 }
320
321 if (collision_val_equal) {
322 if (collision_val_equal(val_p, &rec->val, 0, ht->cb_data)) {
323 /* even the value matches */
324 if (match_p) {
325 *match_p = rec->val;
326 }
327 return LY_SUCCESS;
328 }
329 } else if (ht->val_equal(val_p, &rec->val, 0, ht->cb_data)) {
Michal Vaskoda859032020-07-14 12:20:14 +0200330 /* even the value matches */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200331 if (match_p) {
332 *match_p = rec->val;
333 }
Michal Vaskoda859032020-07-14 12:20:14 +0200334 return LY_SUCCESS;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200335 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200336 }
337
338 /* the last equal value was already returned */
Michal Vaskoda859032020-07-14 12:20:14 +0200339 return LY_ENOTFOUND;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200340}
341
Michal Vaskoae130f52023-04-20 14:25:16 +0200342LIBYANG_API_DEF LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200343lyht_find_next(struct ly_ht *ht, void *val_p, uint32_t hash, void **match_p)
Michal Vasko6374de22022-09-05 15:48:48 +0200344{
345 return lyht_find_next_with_collision_cb(ht, val_p, hash, NULL, match_p);
346}
347
Olivier Matz75c00192023-09-21 14:35:12 +0200348static LY_ERR
349__lyht_insert_with_resize_cb(struct ly_ht *ht, void *val_p, uint32_t hash, lyht_value_equal_cb resize_val_equal,
Olivier Matz6ad93d92023-09-28 12:13:36 +0200350 void **match_p, int check)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200351{
Olivier Matz75c00192023-09-21 14:35:12 +0200352 uint32_t hlist_idx = hash & (ht->size - 1);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200353 LY_ERR r, ret = LY_SUCCESS;
Olivier Matz6a669c12023-09-28 12:07:12 +0200354 struct ly_ht_rec *rec, *prev_rec;
Michal Vasko62524a92021-02-26 10:08:50 +0100355 lyht_value_equal_cb old_val_equal = NULL;
Olivier Matz75c00192023-09-21 14:35:12 +0200356 uint32_t rec_idx;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200357
Olivier Matz6ad93d92023-09-28 12:13:36 +0200358 if (check) {
359 if (lyht_find_rec(ht, val_p, hash, 1, NULL, NULL, &rec) == LY_SUCCESS) {
360 if (rec && match_p) {
361 *match_p = rec->val;
362 }
363 return LY_EEXIST;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200364 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200365 }
366
Olivier Matz75c00192023-09-21 14:35:12 +0200367 rec_idx = ht->first_free_rec;
368 assert(rec_idx < ht->size);
369 rec = lyht_get_rec(ht->recs, ht->rec_size, rec_idx);
370 ht->first_free_rec = rec->next;
Olivier Matz6a669c12023-09-28 12:07:12 +0200371
372 if (ht->hlists[hlist_idx].first == LYHT_NO_RECORD) {
373 ht->hlists[hlist_idx].first = rec_idx;
374 } else {
375 prev_rec = lyht_get_rec(ht->recs, ht->rec_size, ht->hlists[hlist_idx].last);
376 prev_rec->next = rec_idx;
377 }
378 rec->next = LYHT_NO_RECORD;
379 ht->hlists[hlist_idx].last = rec_idx;
Olivier Matz75c00192023-09-21 14:35:12 +0200380
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200381 rec->hash = hash;
Olivier Matz75c00192023-09-21 14:35:12 +0200382 memcpy(&rec->val, val_p, ht->rec_size - SIZEOF_LY_HT_REC);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200383 if (match_p) {
384 *match_p = (void *)&rec->val;
385 }
386
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200387 /* check size & enlarge if needed */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200388 ++ht->used;
389 if (ht->resize) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100390 r = (ht->used * LYHT_HUNDRED_PERCENTAGE) / ht->size;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200391 if ((ht->resize == 1) && (r >= LYHT_FIRST_SHRINK_PERCENTAGE)) {
392 /* enable shrinking */
393 ht->resize = 2;
394 }
395 if ((ht->resize == 2) && (r >= LYHT_ENLARGE_PERCENTAGE)) {
396 if (resize_val_equal) {
397 old_val_equal = lyht_set_cb(ht, resize_val_equal);
398 }
399
400 /* enlarge */
Olivier Matz6ad93d92023-09-28 12:13:36 +0200401 ret = lyht_resize(ht, 1, check);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200402 /* if hash_table was resized, we need to find new matching value */
Michal Vasko69730152020-10-09 16:30:07 +0200403 if ((ret == LY_SUCCESS) && match_p) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200404 lyht_find(ht, val_p, hash, match_p);
405 }
406
407 if (resize_val_equal) {
408 lyht_set_cb(ht, old_val_equal);
409 }
410 }
411 }
412 return ret;
413}
414
Michal Vaskoae130f52023-04-20 14:25:16 +0200415LIBYANG_API_DEF LY_ERR
Olivier Matz75c00192023-09-21 14:35:12 +0200416lyht_insert_with_resize_cb(struct ly_ht *ht, void *val_p, uint32_t hash, lyht_value_equal_cb resize_val_equal,
417 void **match_p)
418{
Olivier Matz6ad93d92023-09-28 12:13:36 +0200419 return __lyht_insert_with_resize_cb(ht, val_p, hash, resize_val_equal, match_p, 1);
Olivier Matz75c00192023-09-21 14:35:12 +0200420}
421
422LIBYANG_API_DEF LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200423lyht_insert(struct ly_ht *ht, void *val_p, uint32_t hash, void **match_p)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200424{
Olivier Matz6ad93d92023-09-28 12:13:36 +0200425 return __lyht_insert_with_resize_cb(ht, val_p, hash, NULL, match_p, 1);
426}
427
428LIBYANG_API_DEF LY_ERR
429lyht_insert_no_check(struct ly_ht *ht, void *val_p, uint32_t hash, void **match_p)
430{
431 return __lyht_insert_with_resize_cb(ht, val_p, hash, NULL, match_p, 0);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200432}
433
Michal Vaskoae130f52023-04-20 14:25:16 +0200434LIBYANG_API_DEF LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200435lyht_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 +0200436{
Olivier Matz75c00192023-09-21 14:35:12 +0200437 struct ly_ht_rec *found_rec, *prev_rec, *rec;
438 uint32_t hlist_idx = hash & (ht->size - 1);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200439 LY_ERR r, ret = LY_SUCCESS;
stewegd8e2fc92023-05-31 09:52:56 +0200440 lyht_value_equal_cb old_val_equal = NULL;
Olivier Matz75c00192023-09-21 14:35:12 +0200441 uint32_t prev_rec_idx;
442 uint32_t rec_idx;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200443
Olivier Matz75c00192023-09-21 14:35:12 +0200444 LY_CHECK_ERR_RET(lyht_find_rec(ht, val_p, hash, 1, NULL, NULL, &found_rec),
445 LOGARG(NULL, hash), LY_ENOTFOUND); /* hash not found */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200446
Olivier Matz75c00192023-09-21 14:35:12 +0200447 prev_rec_idx = LYHT_NO_RECORD;
448 LYHT_ITER_HLIST_RECS(ht, hlist_idx, rec_idx, rec) {
449 if (rec == found_rec)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200450 break;
Olivier Matz75c00192023-09-21 14:35:12 +0200451 prev_rec_idx = rec_idx;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200452 }
453
Olivier Matz75c00192023-09-21 14:35:12 +0200454 if (prev_rec_idx == LYHT_NO_RECORD) {
Olivier Matz6a669c12023-09-28 12:07:12 +0200455 ht->hlists[hlist_idx].first = rec->next;
456 if (rec->next == LYHT_NO_RECORD)
457 ht->hlists[hlist_idx].last = LYHT_NO_RECORD;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200458 } else {
Olivier Matz75c00192023-09-21 14:35:12 +0200459 prev_rec = lyht_get_rec(ht->recs, ht->rec_size, prev_rec_idx);
460 prev_rec->next = rec->next;
Olivier Matz6a669c12023-09-28 12:07:12 +0200461 if (rec->next == LYHT_NO_RECORD)
462 ht->hlists[hlist_idx].last = prev_rec_idx;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200463 }
464
Olivier Matz75c00192023-09-21 14:35:12 +0200465 rec->next = ht->first_free_rec;
466 ht->first_free_rec = rec_idx;
467
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200468 /* check size & shrink if needed */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200469 --ht->used;
470 if (ht->resize == 2) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100471 r = (ht->used * LYHT_HUNDRED_PERCENTAGE) / ht->size;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200472 if ((r < LYHT_SHRINK_PERCENTAGE) && (ht->size > LYHT_MIN_SIZE)) {
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200473 if (resize_val_equal) {
474 old_val_equal = lyht_set_cb(ht, resize_val_equal);
475 }
476
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200477 /* shrink */
Olivier Matz6ad93d92023-09-28 12:13:36 +0200478 ret = lyht_resize(ht, -1, 1);
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200479
480 if (resize_val_equal) {
481 lyht_set_cb(ht, old_val_equal);
482 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200483 }
484 }
485
486 return ret;
487}
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200488
Michal Vaskoae130f52023-04-20 14:25:16 +0200489LIBYANG_API_DEF LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200490lyht_remove(struct ly_ht *ht, void *val_p, uint32_t hash)
Michal Vasko5bcc33b2020-10-06 15:33:44 +0200491{
492 return lyht_remove_with_resize_cb(ht, val_p, hash, NULL);
493}
Michal Vasko626196f2022-08-05 12:49:52 +0200494
Michal Vaskoae130f52023-04-20 14:25:16 +0200495LIBYANG_API_DEF uint32_t
Michal Vasko626196f2022-08-05 12:49:52 +0200496lyht_get_fixed_size(uint32_t item_count)
497{
Olivier Matz75c00192023-09-21 14:35:12 +0200498 if (item_count == 0)
499 return 1;
Michal Vasko626196f2022-08-05 12:49:52 +0200500
Olivier Matz75c00192023-09-21 14:35:12 +0200501 /* return next power of 2 (greater or equal) */
502 item_count--;
503 item_count |= item_count >> 1;
504 item_count |= item_count >> 2;
505 item_count |= item_count >> 4;
506 item_count |= item_count >> 8;
507 item_count |= item_count >> 16;
Michal Vasko626196f2022-08-05 12:49:52 +0200508
Olivier Matz75c00192023-09-21 14:35:12 +0200509 return item_count + 1;
Michal Vasko626196f2022-08-05 12:49:52 +0200510}