/src/libyang/src/hash_table.c
Line | Count | Source |
1 | | /** |
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 | | |
15 | | #include "hash_table.h" |
16 | | |
17 | | #include <assert.h> |
18 | | #include <pthread.h> |
19 | | #include <stdint.h> |
20 | | #include <stdlib.h> |
21 | | #include <string.h> |
22 | | |
23 | | #include "common.h" |
24 | | #include "compat.h" |
25 | | #include "dict.h" |
26 | | #include "log.h" |
27 | | |
28 | 0 | #define LYDICT_MIN_SIZE 1024 |
29 | | |
30 | | /** |
31 | | * @brief Comparison callback for dictionary's hash table |
32 | | * |
33 | | * Implementation of ::lyht_value_equal_cb. |
34 | | */ |
35 | | static ly_bool |
36 | | lydict_val_eq(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *cb_data) |
37 | 0 | { |
38 | 0 | LY_CHECK_ARG_RET(NULL, val1_p, val2_p, cb_data, 0); |
39 | |
|
40 | 0 | const char *str1 = ((struct dict_rec *)val1_p)->value; |
41 | 0 | const char *str2 = ((struct dict_rec *)val2_p)->value; |
42 | |
|
43 | 0 | LY_CHECK_ERR_RET(!str1, LOGARG(NULL, val1_p), 0); |
44 | 0 | LY_CHECK_ERR_RET(!str2, LOGARG(NULL, val2_p), 0); |
45 | |
|
46 | 0 | if (strncmp(str1, str2, *(size_t *)cb_data) == 0) { |
47 | 0 | return 1; |
48 | 0 | } |
49 | | |
50 | 0 | return 0; |
51 | 0 | } |
52 | | |
53 | | void |
54 | | lydict_init(struct dict_table *dict) |
55 | 0 | { |
56 | 0 | LY_CHECK_ARG_RET(NULL, dict, ); |
57 | |
|
58 | 0 | dict->hash_tab = lyht_new(LYDICT_MIN_SIZE, sizeof(struct dict_rec), lydict_val_eq, NULL, 1); |
59 | 0 | LY_CHECK_ERR_RET(!dict->hash_tab, LOGINT(NULL), ); |
60 | 0 | pthread_mutex_init(&dict->lock, NULL); |
61 | 0 | } |
62 | | |
63 | | void |
64 | | lydict_clean(struct dict_table *dict) |
65 | 0 | { |
66 | 0 | struct dict_rec *dict_rec = NULL; |
67 | 0 | struct ht_rec *rec = NULL; |
68 | |
|
69 | 0 | LY_CHECK_ARG_RET(NULL, dict, ); |
70 | |
|
71 | 0 | for (uint32_t i = 0; i < dict->hash_tab->size; i++) { |
72 | | /* get ith record */ |
73 | 0 | rec = (struct ht_rec *)&dict->hash_tab->recs[i * dict->hash_tab->rec_size]; |
74 | 0 | 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 | | */ |
80 | 0 | dict_rec = (struct dict_rec *)rec->val; |
81 | 0 | 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 | 0 | #ifdef NDEBUG |
84 | 0 | free(dict_rec->value); |
85 | 0 | #endif |
86 | 0 | } |
87 | 0 | } |
88 | | |
89 | | /* free table and destroy mutex */ |
90 | 0 | lyht_free(dict->hash_tab); |
91 | 0 | pthread_mutex_destroy(&dict->lock); |
92 | 0 | } |
93 | | |
94 | | /* |
95 | | * 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 | | */ |
100 | | uint32_t |
101 | | dict_hash_multi(uint32_t hash, const char *key_part, size_t len) |
102 | 0 | { |
103 | 0 | uint32_t i; |
104 | |
|
105 | 0 | if (key_part) { |
106 | 0 | for (i = 0; i < len; ++i) { |
107 | 0 | hash += key_part[i]; |
108 | 0 | hash += (hash << 10); |
109 | 0 | hash ^= (hash >> 6); |
110 | 0 | } |
111 | 0 | } else { |
112 | 0 | hash += (hash << 3); |
113 | 0 | hash ^= (hash >> 11); |
114 | 0 | hash += (hash << 15); |
115 | 0 | } |
116 | |
|
117 | 0 | return hash; |
118 | 0 | } |
119 | | |
120 | | /* |
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 | | */ |
126 | | uint32_t |
127 | | dict_hash(const char *key, size_t len) |
128 | 0 | { |
129 | 0 | uint32_t hash; |
130 | |
|
131 | 0 | hash = dict_hash_multi(0, key, len); |
132 | 0 | return dict_hash_multi(hash, NULL, len); |
133 | 0 | } |
134 | | |
135 | | static ly_bool |
136 | | lydict_resize_val_eq(void *val1_p, void *val2_p, ly_bool mod, void *cb_data) |
137 | 0 | { |
138 | 0 | LY_CHECK_ARG_RET(NULL, val1_p, val2_p, 0); |
139 | |
|
140 | 0 | const char *str1 = ((struct dict_rec *)val1_p)->value; |
141 | 0 | const char *str2 = ((struct dict_rec *)val2_p)->value; |
142 | |
|
143 | 0 | LY_CHECK_ERR_RET(!str1, LOGARG(NULL, val1_p), 0); |
144 | 0 | LY_CHECK_ERR_RET(!str2, LOGARG(NULL, val2_p), 0); |
145 | |
|
146 | 0 | if (mod) { |
147 | | /* used when inserting new values */ |
148 | 0 | if (strcmp(str1, str2) == 0) { |
149 | 0 | return 1; |
150 | 0 | } |
151 | 0 | } else { |
152 | | /* used when finding the original value again in the resized table */ |
153 | 0 | return lydict_val_eq(val1_p, val2_p, mod, cb_data); |
154 | 0 | } |
155 | | |
156 | 0 | return 0; |
157 | 0 | } |
158 | | |
159 | | API LY_ERR |
160 | | lydict_remove(const struct ly_ctx *ctx, const char *value) |
161 | 0 | { |
162 | 0 | LY_ERR ret = LY_SUCCESS; |
163 | 0 | size_t len; |
164 | 0 | uint32_t hash; |
165 | 0 | struct dict_rec rec, *match = NULL; |
166 | 0 | char *val_p; |
167 | |
|
168 | 0 | if (!ctx || !value) { |
169 | 0 | return LY_SUCCESS; |
170 | 0 | } |
171 | | |
172 | 0 | LOGDBG(LY_LDGDICT, "removing \"%s\"", value); |
173 | |
|
174 | 0 | len = strlen(value); |
175 | 0 | hash = dict_hash(value, len); |
176 | | |
177 | | /* create record for lyht_find call */ |
178 | 0 | rec.value = (char *)value; |
179 | 0 | rec.refcount = 0; |
180 | |
|
181 | 0 | pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock); |
182 | | /* set len as data for compare callback */ |
183 | 0 | lyht_set_cb_data(ctx->dict.hash_tab, (void *)&len); |
184 | | /* check if value is already inserted */ |
185 | 0 | ret = lyht_find(ctx->dict.hash_tab, &rec, hash, (void **)&match); |
186 | |
|
187 | 0 | if (ret == LY_SUCCESS) { |
188 | 0 | LY_CHECK_ERR_GOTO(!match, LOGINT(ctx), finish); |
189 | | |
190 | | /* if value is already in dictionary, decrement reference counter */ |
191 | 0 | match->refcount--; |
192 | 0 | 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 | 0 | val_p = match->value; |
199 | 0 | ret = lyht_remove_with_resize_cb(ctx->dict.hash_tab, &rec, hash, lydict_resize_val_eq); |
200 | 0 | free(val_p); |
201 | 0 | LY_CHECK_ERR_GOTO(ret, LOGINT(ctx), finish); |
202 | 0 | } |
203 | 0 | } else if (ret == LY_ENOTFOUND) { |
204 | 0 | LOGERR(ctx, LY_ENOTFOUND, "Value \"%s\" was not found in the dictionary.", value); |
205 | 0 | } else { |
206 | 0 | LOGINT(ctx); |
207 | 0 | } |
208 | | |
209 | 0 | finish: |
210 | 0 | pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock); |
211 | 0 | return ret; |
212 | 0 | } |
213 | | |
214 | | LY_ERR |
215 | | dict_insert(const struct ly_ctx *ctx, char *value, size_t len, ly_bool zerocopy, const char **str_p) |
216 | 0 | { |
217 | 0 | LY_ERR ret = LY_SUCCESS; |
218 | 0 | struct dict_rec *match = NULL, rec; |
219 | 0 | uint32_t hash; |
220 | |
|
221 | 0 | LOGDBG(LY_LDGDICT, "inserting \"%.*s\"", (int)len, value); |
222 | |
|
223 | 0 | hash = dict_hash(value, len); |
224 | | /* set len as data for compare callback */ |
225 | 0 | lyht_set_cb_data(ctx->dict.hash_tab, (void *)&len); |
226 | | /* create record for lyht_insert */ |
227 | 0 | rec.value = value; |
228 | 0 | rec.refcount = 1; |
229 | |
|
230 | 0 | ret = lyht_insert_with_resize_cb(ctx->dict.hash_tab, (void *)&rec, hash, lydict_resize_val_eq, (void **)&match); |
231 | 0 | if (ret == LY_EEXIST) { |
232 | 0 | match->refcount++; |
233 | 0 | if (zerocopy) { |
234 | 0 | free(value); |
235 | 0 | } |
236 | 0 | ret = LY_SUCCESS; |
237 | 0 | } else if (ret == LY_SUCCESS) { |
238 | 0 | if (!zerocopy) { |
239 | | /* |
240 | | * allocate string for new record |
241 | | * record is already inserted in hash table |
242 | | */ |
243 | 0 | match->value = malloc(sizeof *match->value * (len + 1)); |
244 | 0 | LY_CHECK_ERR_RET(!match->value, LOGMEM(ctx), LY_EMEM); |
245 | 0 | memcpy(match->value, value, len); |
246 | 0 | match->value[len] = '\0'; |
247 | 0 | } |
248 | 0 | } else { |
249 | | /* lyht_insert returned error */ |
250 | 0 | if (zerocopy) { |
251 | 0 | free(value); |
252 | 0 | } |
253 | 0 | return ret; |
254 | 0 | } |
255 | | |
256 | 0 | if (str_p) { |
257 | 0 | *str_p = match->value; |
258 | 0 | } |
259 | |
|
260 | 0 | return ret; |
261 | 0 | } |
262 | | |
263 | | API LY_ERR |
264 | | lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len, const char **str_p) |
265 | 0 | { |
266 | 0 | LY_ERR result; |
267 | |
|
268 | 0 | LY_CHECK_ARG_RET(ctx, ctx, str_p, LY_EINVAL); |
269 | |
|
270 | 0 | if (!value) { |
271 | 0 | *str_p = NULL; |
272 | 0 | return LY_SUCCESS; |
273 | 0 | } |
274 | | |
275 | 0 | if (!len) { |
276 | 0 | len = strlen(value); |
277 | 0 | } |
278 | |
|
279 | 0 | pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock); |
280 | 0 | result = dict_insert(ctx, (char *)value, len, 0, str_p); |
281 | 0 | pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock); |
282 | |
|
283 | 0 | return result; |
284 | 0 | } |
285 | | |
286 | | API LY_ERR |
287 | | lydict_insert_zc(const struct ly_ctx *ctx, char *value, const char **str_p) |
288 | 0 | { |
289 | 0 | LY_ERR result; |
290 | |
|
291 | 0 | LY_CHECK_ARG_RET(ctx, ctx, str_p, LY_EINVAL); |
292 | |
|
293 | 0 | if (!value) { |
294 | 0 | *str_p = NULL; |
295 | 0 | return LY_SUCCESS; |
296 | 0 | } |
297 | | |
298 | 0 | pthread_mutex_lock((pthread_mutex_t *)&ctx->dict.lock); |
299 | 0 | result = dict_insert(ctx, value, strlen(value), 1, str_p); |
300 | 0 | pthread_mutex_unlock((pthread_mutex_t *)&ctx->dict.lock); |
301 | |
|
302 | 0 | return result; |
303 | 0 | } |
304 | | |
305 | | struct ht_rec * |
306 | | lyht_get_rec(unsigned char *recs, uint16_t rec_size, uint32_t idx) |
307 | 0 | { |
308 | 0 | return (struct ht_rec *)&recs[idx * rec_size]; |
309 | 0 | } |
310 | | |
311 | | struct hash_table * |
312 | | lyht_new(uint32_t size, uint16_t val_size, lyht_value_equal_cb val_equal, void *cb_data, uint16_t resize) |
313 | 0 | { |
314 | 0 | struct hash_table *ht; |
315 | | |
316 | | /* check that 2^x == size (power of 2) */ |
317 | 0 | assert(size && !(size & (size - 1))); |
318 | 0 | assert(val_equal && val_size); |
319 | 0 | assert(resize == 0 || resize == 1); |
320 | |
|
321 | 0 | if (size < LYHT_MIN_SIZE) { |
322 | 0 | size = LYHT_MIN_SIZE; |
323 | 0 | } |
324 | |
|
325 | 0 | ht = malloc(sizeof *ht); |
326 | 0 | LY_CHECK_ERR_RET(!ht, LOGMEM(NULL), NULL); |
327 | |
|
328 | 0 | ht->used = 0; |
329 | 0 | ht->size = size; |
330 | 0 | ht->invalid = 0; |
331 | 0 | ht->val_equal = val_equal; |
332 | 0 | ht->cb_data = cb_data; |
333 | 0 | ht->resize = resize; |
334 | |
|
335 | 0 | ht->rec_size = (sizeof(struct ht_rec) - 1) + val_size; |
336 | | /* allocate the records correctly */ |
337 | 0 | ht->recs = calloc(size, ht->rec_size); |
338 | 0 | LY_CHECK_ERR_RET(!ht->recs, free(ht); LOGMEM(NULL), NULL); |
339 | |
|
340 | 0 | return ht; |
341 | 0 | } |
342 | | |
343 | | lyht_value_equal_cb |
344 | | lyht_set_cb(struct hash_table *ht, lyht_value_equal_cb new_val_equal) |
345 | 0 | { |
346 | 0 | lyht_value_equal_cb prev; |
347 | |
|
348 | 0 | prev = ht->val_equal; |
349 | 0 | ht->val_equal = new_val_equal; |
350 | 0 | return prev; |
351 | 0 | } |
352 | | |
353 | | void * |
354 | | lyht_set_cb_data(struct hash_table *ht, void *new_cb_data) |
355 | 0 | { |
356 | 0 | void *prev; |
357 | |
|
358 | 0 | prev = ht->cb_data; |
359 | 0 | ht->cb_data = new_cb_data; |
360 | 0 | return prev; |
361 | 0 | } |
362 | | |
363 | | struct hash_table * |
364 | | lyht_dup(const struct hash_table *orig) |
365 | 0 | { |
366 | 0 | struct hash_table *ht; |
367 | |
|
368 | 0 | LY_CHECK_ARG_RET(NULL, orig, NULL); |
369 | |
|
370 | 0 | ht = lyht_new(orig->size, orig->rec_size - (sizeof(struct ht_rec) - 1), orig->val_equal, orig->cb_data, orig->resize ? 1 : 0); |
371 | 0 | if (!ht) { |
372 | 0 | return NULL; |
373 | 0 | } |
374 | | |
375 | 0 | memcpy(ht->recs, orig->recs, orig->used * orig->rec_size); |
376 | 0 | ht->used = orig->used; |
377 | 0 | ht->invalid = orig->invalid; |
378 | 0 | return ht; |
379 | 0 | } |
380 | | |
381 | | void |
382 | | lyht_free(struct hash_table *ht) |
383 | 0 | { |
384 | 0 | if (ht) { |
385 | 0 | free(ht->recs); |
386 | 0 | free(ht); |
387 | 0 | } |
388 | 0 | } |
389 | | |
390 | | /** |
391 | | * @brief Resize a hash table. |
392 | | * |
393 | | * @param[in] ht Hash table to resize. |
394 | | * @param[in] operation Operation to perform. 1 to enlarge, -1 to shrink, 0 to only rehash all records. |
395 | | * @return LY_ERR value. |
396 | | */ |
397 | | static LY_ERR |
398 | | lyht_resize(struct hash_table *ht, int operation) |
399 | 0 | { |
400 | 0 | struct ht_rec *rec; |
401 | 0 | unsigned char *old_recs; |
402 | 0 | uint32_t i, old_size; |
403 | |
|
404 | 0 | old_recs = ht->recs; |
405 | 0 | old_size = ht->size; |
406 | |
|
407 | 0 | if (operation > 0) { |
408 | | /* double the size */ |
409 | 0 | ht->size <<= 1; |
410 | 0 | } else if (operation < 0) { |
411 | | /* half the size */ |
412 | 0 | ht->size >>= 1; |
413 | 0 | } |
414 | |
|
415 | 0 | ht->recs = calloc(ht->size, ht->rec_size); |
416 | 0 | LY_CHECK_ERR_RET(!ht->recs, LOGMEM(NULL); ht->recs = old_recs; ht->size = old_size, LY_EMEM); |
417 | | |
418 | | /* reset used and invalid, it will increase again */ |
419 | 0 | ht->used = 0; |
420 | 0 | ht->invalid = 0; |
421 | | |
422 | | /* add all the old records into the new records array */ |
423 | 0 | for (i = 0; i < old_size; ++i) { |
424 | 0 | rec = lyht_get_rec(old_recs, ht->rec_size, i); |
425 | 0 | if (rec->hits > 0) { |
426 | 0 | LY_ERR ret = lyht_insert(ht, rec->val, rec->hash, NULL); |
427 | 0 | assert(!ret); |
428 | 0 | (void)ret; |
429 | 0 | } |
430 | 0 | } |
431 | | |
432 | | /* final touches */ |
433 | 0 | free(old_recs); |
434 | 0 | return LY_SUCCESS; |
435 | 0 | } |
436 | | |
437 | | /** |
438 | | * @brief Search for the first match. |
439 | | * |
440 | | * @param[in] ht Hash table to search in. |
441 | | * @param[in] hash Hash to find. |
442 | | * @param[out] rec_p Optional found record. |
443 | | * @return LY_SUCCESS hash found, returned its record, |
444 | | * @return LY_ENOTFOUND hash not found, returned the record where it would be inserted. |
445 | | */ |
446 | | static LY_ERR |
447 | | lyht_find_first(struct hash_table *ht, uint32_t hash, struct ht_rec **rec_p) |
448 | 0 | { |
449 | 0 | struct ht_rec *rec; |
450 | 0 | uint32_t i, idx; |
451 | |
|
452 | 0 | if (rec_p) { |
453 | 0 | *rec_p = NULL; |
454 | 0 | } |
455 | |
|
456 | 0 | idx = i = hash & (ht->size - 1); |
457 | 0 | rec = lyht_get_rec(ht->recs, ht->rec_size, idx); |
458 | | |
459 | | /* skip through overflow and deleted records */ |
460 | 0 | while ((rec->hits != 0) && ((rec->hits == -1) || ((rec->hash & (ht->size - 1)) != idx))) { |
461 | 0 | if ((rec->hits == -1) && rec_p && !(*rec_p)) { |
462 | | /* remember this record for return */ |
463 | 0 | *rec_p = rec; |
464 | 0 | } |
465 | 0 | i = (i + 1) % ht->size; |
466 | 0 | if (i == idx) { |
467 | | /* we went through all the records (very unlikely, but possible when many records are invalid), |
468 | | * just return not found */ |
469 | 0 | assert(!rec_p || *rec_p); |
470 | 0 | return LY_ENOTFOUND; |
471 | 0 | } |
472 | 0 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
473 | 0 | } |
474 | 0 | if (rec->hits == 0) { |
475 | | /* we could not find the value */ |
476 | 0 | if (rec_p && !*rec_p) { |
477 | 0 | *rec_p = rec; |
478 | 0 | } |
479 | 0 | return LY_ENOTFOUND; |
480 | 0 | } |
481 | | |
482 | | /* we have found a record with equal (shortened) hash */ |
483 | 0 | if (rec_p) { |
484 | 0 | *rec_p = rec; |
485 | 0 | } |
486 | 0 | return LY_SUCCESS; |
487 | 0 | } |
488 | | |
489 | | /** |
490 | | * @brief Search for the next collision. |
491 | | * |
492 | | * @param[in] ht Hash table to search in. |
493 | | * @param[in,out] last Last returned collision record. |
494 | | * @param[in] first First collision record (hits > 1). |
495 | | * @return LY_SUCCESS when hash collision found, \p last points to this next collision, |
496 | | * @return LY_ENOTFOUND when hash collision not found, \p last points to the record where it would be inserted. |
497 | | */ |
498 | | static LY_ERR |
499 | | lyht_find_collision(struct hash_table *ht, struct ht_rec **last, struct ht_rec *first) |
500 | 0 | { |
501 | 0 | struct ht_rec *empty = NULL; |
502 | 0 | uint32_t i, idx; |
503 | |
|
504 | 0 | assert(last && *last); |
505 | |
|
506 | 0 | idx = (*last)->hash & (ht->size - 1); |
507 | 0 | i = (((unsigned char *)*last) - ht->recs) / ht->rec_size; |
508 | |
|
509 | 0 | do { |
510 | 0 | i = (i + 1) % ht->size; |
511 | 0 | *last = lyht_get_rec(ht->recs, ht->rec_size, i); |
512 | 0 | if (*last == first) { |
513 | | /* we went through all the records (very unlikely, but possible when many records are invalid), |
514 | | * just return an invalid record */ |
515 | 0 | assert(empty); |
516 | 0 | *last = empty; |
517 | 0 | return LY_ENOTFOUND; |
518 | 0 | } |
519 | | |
520 | 0 | if (((*last)->hits == -1) && !empty) { |
521 | 0 | empty = *last; |
522 | 0 | } |
523 | 0 | } while (((*last)->hits != 0) && (((*last)->hits == -1) || (((*last)->hash & (ht->size - 1)) != idx))); |
524 | | |
525 | 0 | if ((*last)->hits > 0) { |
526 | | /* we found a collision */ |
527 | 0 | assert((*last)->hits == 1); |
528 | 0 | return LY_SUCCESS; |
529 | 0 | } |
530 | | |
531 | | /* no next collision found, return the record where it would be inserted */ |
532 | 0 | if (empty) { |
533 | 0 | *last = empty; |
534 | 0 | } /* else (*last)->hits == 0, it is already correct */ |
535 | 0 | return LY_ENOTFOUND; |
536 | 0 | } |
537 | | |
538 | | /** |
539 | | * @brief Search for a record with specific value and hash. |
540 | | * |
541 | | * @param[in] ht Hash table to search in. |
542 | | * @param[in] val_p Pointer to the value to find. |
543 | | * @param[in] hash Hash to find. |
544 | | * @param[in] mod Whether the operation modifies the hash table (insert or remove) or not (find). |
545 | | * @param[out] crec_p Optional found first record. |
546 | | * @param[out] col Optional collision number of @p rec_p, 0 for no collision. |
547 | | * @param[out] rec_p Found exact matching record, may be a collision of @p crec_p. |
548 | | * @return LY_ENOTFOUND if no record found, |
549 | | * @return LY_SUCCESS if record was found. |
550 | | */ |
551 | | static LY_ERR |
552 | | lyht_find_rec(struct hash_table *ht, void *val_p, uint32_t hash, ly_bool mod, struct ht_rec **crec_p, uint32_t *col, |
553 | | struct ht_rec **rec_p) |
554 | 0 | { |
555 | 0 | struct ht_rec *rec, *crec; |
556 | 0 | uint32_t i, c; |
557 | 0 | LY_ERR r; |
558 | |
|
559 | 0 | if (crec_p) { |
560 | 0 | *crec_p = NULL; |
561 | 0 | } |
562 | 0 | if (col) { |
563 | 0 | *col = 0; |
564 | 0 | } |
565 | 0 | *rec_p = NULL; |
566 | |
|
567 | 0 | if (lyht_find_first(ht, hash, &rec)) { |
568 | | /* not found */ |
569 | 0 | return LY_ENOTFOUND; |
570 | 0 | } |
571 | 0 | if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, mod, ht->cb_data)) { |
572 | | /* even the value matches */ |
573 | 0 | if (crec_p) { |
574 | 0 | *crec_p = rec; |
575 | 0 | } |
576 | 0 | if (col) { |
577 | 0 | *col = 0; |
578 | 0 | } |
579 | 0 | *rec_p = rec; |
580 | 0 | return LY_SUCCESS; |
581 | 0 | } |
582 | | |
583 | | /* some collisions, we need to go through them, too */ |
584 | 0 | crec = rec; |
585 | 0 | c = crec->hits; |
586 | 0 | for (i = 1; i < c; ++i) { |
587 | 0 | r = lyht_find_collision(ht, &rec, crec); |
588 | 0 | assert(!r); |
589 | 0 | (void)r; |
590 | | |
591 | | /* compare values */ |
592 | 0 | if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, mod, ht->cb_data)) { |
593 | 0 | if (crec_p) { |
594 | 0 | *crec_p = crec; |
595 | 0 | } |
596 | 0 | if (col) { |
597 | 0 | *col = i; |
598 | 0 | } |
599 | 0 | *rec_p = rec; |
600 | 0 | return LY_SUCCESS; |
601 | 0 | } |
602 | 0 | } |
603 | | |
604 | | /* not found even in collisions */ |
605 | 0 | return LY_ENOTFOUND; |
606 | 0 | } |
607 | | |
608 | | LY_ERR |
609 | | lyht_find(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p) |
610 | 0 | { |
611 | 0 | struct ht_rec *rec; |
612 | |
|
613 | 0 | lyht_find_rec(ht, val_p, hash, 0, NULL, NULL, &rec); |
614 | |
|
615 | 0 | if (rec && match_p) { |
616 | 0 | *match_p = rec->val; |
617 | 0 | } |
618 | 0 | return rec ? LY_SUCCESS : LY_ENOTFOUND; |
619 | 0 | } |
620 | | |
621 | | LY_ERR |
622 | | lyht_find_next(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p) |
623 | 0 | { |
624 | 0 | struct ht_rec *rec, *crec; |
625 | 0 | uint32_t i, c; |
626 | 0 | LY_ERR r; |
627 | | |
628 | | /* found the record of the previously found value */ |
629 | 0 | if (lyht_find_rec(ht, val_p, hash, 1, &crec, &i, &rec)) { |
630 | | /* not found, cannot happen */ |
631 | 0 | LOGINT_RET(NULL); |
632 | 0 | } |
633 | | |
634 | | /* go through collisions and find next one after the previous one */ |
635 | 0 | c = crec->hits; |
636 | 0 | for (++i; i < c; ++i) { |
637 | 0 | r = lyht_find_collision(ht, &rec, crec); |
638 | 0 | assert(!r); |
639 | 0 | (void)r; |
640 | |
|
641 | 0 | if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 0, ht->cb_data)) { |
642 | | /* even the value matches */ |
643 | 0 | if (match_p) { |
644 | 0 | *match_p = rec->val; |
645 | 0 | } |
646 | 0 | return LY_SUCCESS; |
647 | 0 | } |
648 | 0 | } |
649 | | |
650 | | /* the last equal value was already returned */ |
651 | 0 | return LY_ENOTFOUND; |
652 | 0 | } |
653 | | |
654 | | LY_ERR |
655 | | lyht_insert_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash, lyht_value_equal_cb resize_val_equal, |
656 | | void **match_p) |
657 | 0 | { |
658 | 0 | LY_ERR r, ret = LY_SUCCESS; |
659 | 0 | struct ht_rec *rec, *crec = NULL; |
660 | 0 | int32_t i; |
661 | 0 | lyht_value_equal_cb old_val_equal = NULL; |
662 | |
|
663 | 0 | if (!lyht_find_first(ht, hash, &rec)) { |
664 | | /* we found matching shortened hash */ |
665 | 0 | if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) { |
666 | | /* even the value matches */ |
667 | 0 | if (match_p) { |
668 | 0 | *match_p = (void *)&rec->val; |
669 | 0 | } |
670 | 0 | return LY_EEXIST; |
671 | 0 | } |
672 | | |
673 | | /* some collisions, we need to go through them, too */ |
674 | 0 | crec = rec; |
675 | 0 | for (i = 1; i < crec->hits; ++i) { |
676 | 0 | r = lyht_find_collision(ht, &rec, crec); |
677 | 0 | assert(!r); |
678 | | |
679 | | /* compare values */ |
680 | 0 | if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) { |
681 | 0 | if (match_p) { |
682 | 0 | *match_p = (void *)&rec->val; |
683 | 0 | } |
684 | 0 | return LY_EEXIST; |
685 | 0 | } |
686 | 0 | } |
687 | | |
688 | | /* value not found, get the record where it will be inserted */ |
689 | 0 | r = lyht_find_collision(ht, &rec, crec); |
690 | 0 | assert(r); |
691 | 0 | } |
692 | | |
693 | | /* insert it into the returned record */ |
694 | 0 | assert(rec->hits < 1); |
695 | 0 | if (rec->hits < 0) { |
696 | 0 | --ht->invalid; |
697 | 0 | } |
698 | 0 | rec->hash = hash; |
699 | 0 | rec->hits = 1; |
700 | 0 | memcpy(&rec->val, val_p, ht->rec_size - (sizeof(struct ht_rec) - 1)); |
701 | 0 | if (match_p) { |
702 | 0 | *match_p = (void *)&rec->val; |
703 | 0 | } |
704 | |
|
705 | 0 | if (crec) { |
706 | | /* there was a collision, increase hits */ |
707 | 0 | if (crec->hits == INT32_MAX) { |
708 | 0 | LOGINT(NULL); |
709 | 0 | } |
710 | 0 | ++crec->hits; |
711 | 0 | } |
712 | | |
713 | | /* check size & enlarge if needed */ |
714 | 0 | ++ht->used; |
715 | 0 | if (ht->resize) { |
716 | 0 | r = (ht->used * LYHT_HUNDRED_PERCENTAGE) / ht->size; |
717 | 0 | if ((ht->resize == 1) && (r >= LYHT_FIRST_SHRINK_PERCENTAGE)) { |
718 | | /* enable shrinking */ |
719 | 0 | ht->resize = 2; |
720 | 0 | } |
721 | 0 | if ((ht->resize == 2) && (r >= LYHT_ENLARGE_PERCENTAGE)) { |
722 | 0 | if (resize_val_equal) { |
723 | 0 | old_val_equal = lyht_set_cb(ht, resize_val_equal); |
724 | 0 | } |
725 | | |
726 | | /* enlarge */ |
727 | 0 | ret = lyht_resize(ht, 1); |
728 | | /* if hash_table was resized, we need to find new matching value */ |
729 | 0 | if ((ret == LY_SUCCESS) && match_p) { |
730 | 0 | lyht_find(ht, val_p, hash, match_p); |
731 | 0 | } |
732 | |
|
733 | 0 | if (resize_val_equal) { |
734 | 0 | lyht_set_cb(ht, old_val_equal); |
735 | 0 | } |
736 | 0 | } |
737 | 0 | } |
738 | 0 | return ret; |
739 | 0 | } |
740 | | |
741 | | LY_ERR |
742 | | lyht_insert(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p) |
743 | 0 | { |
744 | 0 | return lyht_insert_with_resize_cb(ht, val_p, hash, NULL, match_p); |
745 | 0 | } |
746 | | |
747 | | LY_ERR |
748 | | lyht_remove_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash, lyht_value_equal_cb resize_val_equal) |
749 | 0 | { |
750 | 0 | struct ht_rec *rec, *crec; |
751 | 0 | int32_t i; |
752 | 0 | ly_bool first_matched = 0; |
753 | 0 | LY_ERR r, ret = LY_SUCCESS; |
754 | 0 | lyht_value_equal_cb old_val_equal; |
755 | |
|
756 | 0 | LY_CHECK_ERR_RET(lyht_find_first(ht, hash, &rec), LOGARG(NULL, hash), LY_ENOTFOUND); /* hash not found */ |
757 | |
|
758 | 0 | if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) { |
759 | | /* even the value matches */ |
760 | 0 | first_matched = 1; |
761 | 0 | } |
762 | | |
763 | | /* we always need to go through collisions */ |
764 | 0 | crec = rec; |
765 | 0 | for (i = 1; i < crec->hits; ++i) { |
766 | 0 | r = lyht_find_collision(ht, &rec, crec); |
767 | 0 | assert(!r); |
768 | | |
769 | | /* compare values */ |
770 | 0 | if (!first_matched && (rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) { |
771 | 0 | break; |
772 | 0 | } |
773 | 0 | } |
774 | |
|
775 | 0 | if (i < crec->hits) { |
776 | | /* one of collisions matched, reduce collision count, remove the record */ |
777 | 0 | assert(!first_matched); |
778 | 0 | --crec->hits; |
779 | 0 | rec->hits = -1; |
780 | 0 | } else if (first_matched) { |
781 | | /* the first record matches */ |
782 | 0 | if (crec != rec) { |
783 | | /* ... so put the last collision in its place */ |
784 | 0 | rec->hits = crec->hits - 1; |
785 | 0 | memcpy(crec, rec, ht->rec_size); |
786 | 0 | } |
787 | 0 | rec->hits = -1; |
788 | 0 | } else { |
789 | | /* value not found even in collisions */ |
790 | 0 | return LY_ENOTFOUND; |
791 | 0 | } |
792 | | |
793 | | /* check size & shrink if needed */ |
794 | 0 | --ht->used; |
795 | 0 | ++ht->invalid; |
796 | 0 | if (ht->resize == 2) { |
797 | 0 | r = (ht->used * LYHT_HUNDRED_PERCENTAGE) / ht->size; |
798 | 0 | if ((r < LYHT_SHRINK_PERCENTAGE) && (ht->size > LYHT_MIN_SIZE)) { |
799 | 0 | if (resize_val_equal) { |
800 | 0 | old_val_equal = lyht_set_cb(ht, resize_val_equal); |
801 | 0 | } |
802 | | |
803 | | /* shrink */ |
804 | 0 | ret = lyht_resize(ht, -1); |
805 | |
|
806 | 0 | if (resize_val_equal) { |
807 | 0 | lyht_set_cb(ht, old_val_equal); |
808 | 0 | } |
809 | 0 | } |
810 | 0 | } |
811 | | |
812 | | /* rehash all records if needed */ |
813 | 0 | r = ((ht->size - ht->used - ht->invalid) * 100) / ht->size; |
814 | 0 | if (r < LYHT_REHASH_PERCENTAGE) { |
815 | 0 | if (resize_val_equal) { |
816 | 0 | old_val_equal = lyht_set_cb(ht, resize_val_equal); |
817 | 0 | } |
818 | | |
819 | | /* rehash */ |
820 | 0 | ret = lyht_resize(ht, 0); |
821 | |
|
822 | 0 | if (resize_val_equal) { |
823 | 0 | lyht_set_cb(ht, old_val_equal); |
824 | 0 | } |
825 | 0 | } |
826 | |
|
827 | 0 | return ret; |
828 | 0 | } |
829 | | |
830 | | LY_ERR |
831 | | lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash) |
832 | 0 | { |
833 | | return lyht_remove_with_resize_cb(ht, val_p, hash, NULL); |
834 | 0 | } |