blob: a60c3dad5cacc59d67a8769e3466baa4e5e05520 [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
15#include <string.h>
16#include <stdint.h>
17#include <stdlib.h>
18#include <pthread.h>
19#include <assert.h>
20
21#include "common.h"
22#include "context.h"
23#include "hash_table.h"
24
25static int
26lydict_val_eq(void *val1_p, void *val2_p, int UNUSED(mod), void *cb_data)
27{
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020028 LY_CHECK_ARG_RET(NULL, val1_p, val2_p, cb_data, 0);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020029
30 const char *str1 = ((struct dict_rec *)val1_p)->value;
31 const char *str2 = ((struct dict_rec *)val2_p)->value;
32
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020033 LY_CHECK_ERR_RET(!str1, LOGARG(NULL, val1_p), 0);
34 LY_CHECK_ERR_RET(!str2, LOGARG(NULL, val2_p), 0);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020035
36 if (strncmp(str1, str2, *(size_t *)cb_data) == 0) {
37 return 1;
38 }
39
40 return 0;
41}
42
43void
44lydict_init(struct dict_table *dict)
45{
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020046 LY_CHECK_ARG_RET(NULL, dict,);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020047
48 dict->hash_tab = lyht_new(1024, sizeof(struct dict_rec), lydict_val_eq, NULL, 1);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020049 LY_CHECK_ERR_RET(!dict->hash_tab, LOGINT(NULL), );
Radek Krejci5aeea3a2018-09-05 13:29:36 +020050 pthread_mutex_init(&dict->lock, NULL);
51}
52
53void
54lydict_clean(struct dict_table *dict)
55{
56 unsigned int i;
57 struct dict_rec *dict_rec = NULL;
58 struct ht_rec *rec = NULL;
59
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020060 LY_CHECK_ARG_RET(NULL, dict,);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020061
62 for (i = 0; i < dict->hash_tab->size; i++) {
63 /* get ith record */
64 rec = (struct ht_rec *)&dict->hash_tab->recs[i * dict->hash_tab->rec_size];
65 if (rec->hits == 1) {
66 /*
67 * this should not happen, all records inserted into
68 * dictionary are supposed to be removed using lydict_remove()
69 * before calling lydict_clean()
70 */
71 dict_rec = (struct dict_rec *)rec->val;
72 LOGWRN(NULL, "String \"%s\" not freed from the dictionary, refcount %d", dict_rec->value, dict_rec->refcount);
73 /* if record wasn't removed before free string allocated for that record */
74#ifdef NDEBUG
75 free(dict_rec->value);
76#endif
77 }
78 }
79
80 /* free table and destroy mutex */
81 lyht_free(dict->hash_tab);
82 pthread_mutex_destroy(&dict->lock);
83}
84
85/*
86 * Bob Jenkin's one-at-a-time hash
87 * http://www.burtleburtle.net/bob/hash/doobs.html
88 *
89 * Spooky hash is faster, but it works only for little endian architectures.
90 */
91static uint32_t
92dict_hash(const char *key, size_t len)
93{
94 uint32_t hash, i;
95
96 for (hash = i = 0; i < len; ++i) {
97 hash += key[i];
98 hash += (hash << 10);
99 hash ^= (hash >> 6);
100 }
101 hash += (hash << 3);
102 hash ^= (hash >> 11);
103 hash += (hash << 15);
104 return hash;
105}
106
107/*
108 * Usage:
109 * - init hash to 0
110 * - repeatedly call dict_hash_multi(), provide hash from the last call
111 * - call dict_hash_multi() with key_part = NULL to finish the hash
112 */
113uint32_t
114dict_hash_multi(uint32_t hash, const char *key_part, size_t len)
115{
116 uint32_t i;
117
118 if (key_part) {
119 for (i = 0; i < len; ++i) {
120 hash += key_part[i];
121 hash += (hash << 10);
122 hash ^= (hash >> 6);
123 }
124 } else {
125 hash += (hash << 3);
126 hash ^= (hash >> 11);
127 hash += (hash << 15);
128 }
129
130 return hash;
131}
132
133API void
134lydict_remove(struct ly_ctx *ctx, const char *value)
135{
136 size_t len;
137 int ret;
138 uint32_t hash;
139 struct dict_rec rec, *match = NULL;
140 char *val_p;
141
Radek Krejci0ae092d2018-09-20 16:43:19 +0200142 LY_CHECK_ARG_RET(ctx, ctx, value,);
Michal Vasko0f6b3e22018-09-07 12:18:12 +0200143
144 if (!value) {
Michal Vasko0b3a4172018-09-07 12:20:18 +0200145 return;
Michal Vasko0f6b3e22018-09-07 12:18:12 +0200146 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200147
148 len = strlen(value);
149 hash = dict_hash(value, len);
150
151 /* create record for lyht_find call */
152 rec.value = (char *)value;
153 rec.refcount = 0;
154
155 pthread_mutex_lock(&ctx->dict.lock);
156 /* set len as data for compare callback */
157 lyht_set_cb_data(ctx->dict.hash_tab, (void *)&len);
158 /* check if value is already inserted */
159 ret = lyht_find(ctx->dict.hash_tab, &rec, hash, (void **)&match);
160
161 if (ret == 0) {
162 LY_CHECK_ERR_GOTO(!match, LOGINT(ctx), finish);
163
164 /* if value is already in dictionary, decrement reference counter */
165 match->refcount--;
166 if (match->refcount == 0) {
167 /*
168 * remove record
169 * save pointer to stored string before lyht_remove to
170 * free it after it is removed from hash table
171 */
172 val_p = match->value;
173 ret = lyht_remove(ctx->dict.hash_tab, &rec, hash);
174 free(val_p);
175 LY_CHECK_ERR_GOTO(ret, LOGINT(ctx), finish);
176 }
177 }
178
179finish:
180 pthread_mutex_unlock(&ctx->dict.lock);
181}
182
183static char *
184dict_insert(struct ly_ctx *ctx, char *value, size_t len, int zerocopy)
185{
186 struct dict_rec *match = NULL, rec;
187 int ret = 0;
188 uint32_t hash;
189
190 hash = dict_hash(value, len);
191 /* set len as data for compare callback */
192 lyht_set_cb_data(ctx->dict.hash_tab, (void *)&len);
193 /* create record for lyht_insert */
194 rec.value = value;
195 rec.refcount = 1;
196
197 LOGDBG(LY_LDGDICT, "inserting \"%s\"", rec.value);
198 ret = lyht_insert(ctx->dict.hash_tab, (void *)&rec, hash, (void **)&match);
Michal Vaskofcc96b92018-09-12 11:12:01 +0200199 if (ret == LY_EEXIST) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200200 match->refcount++;
201 if (zerocopy) {
202 free(value);
203 }
Michal Vaskofcc96b92018-09-12 11:12:01 +0200204 } else if (ret == LY_SUCCESS) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200205 if (!zerocopy) {
206 /*
207 * allocate string for new record
208 * record is already inserted in hash table
209 */
210 match->value = malloc(sizeof *match->value * (len + 1));
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200211 LY_CHECK_ERR_RET(!match->value, LOGMEM(ctx), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200212 memcpy(match->value, value, len);
213 match->value[len] = '\0';
214 }
215 } else {
216 /* lyht_insert returned error */
217 LOGINT(ctx);
218 return NULL;
219 }
220
221 return match->value;
222}
223
224API const char *
225lydict_insert(struct ly_ctx *ctx, const char *value, size_t len)
226{
227 const char *result;
228
Radek Krejci0ae092d2018-09-20 16:43:19 +0200229 LY_CHECK_ARG_RET(ctx, ctx, value, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200230
Radek Krejci0ae092d2018-09-20 16:43:19 +0200231 if (!len) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200232 len = strlen(value);
233 }
234
235 pthread_mutex_lock(&ctx->dict.lock);
236 result = dict_insert(ctx, (char *)value, len, 0);
237 pthread_mutex_unlock(&ctx->dict.lock);
238
239 return result;
240}
241
242API const char *
243lydict_insert_zc(struct ly_ctx *ctx, char *value)
244{
245 const char *result;
246
Radek Krejci0ae092d2018-09-20 16:43:19 +0200247 LY_CHECK_ARG_RET(ctx, ctx, value, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200248
249 pthread_mutex_lock(&ctx->dict.lock);
250 result = dict_insert(ctx, value, strlen(value), 1);
251 pthread_mutex_unlock(&ctx->dict.lock);
252
253 return result;
254}
255
256static struct ht_rec *
257lyht_get_rec(unsigned char *recs, uint16_t rec_size, uint32_t idx)
258{
259 return (struct ht_rec *)&recs[idx * rec_size];
260}
261
262struct hash_table *
263lyht_new(uint32_t size, uint16_t val_size, values_equal_cb val_equal, void *cb_data, int resize)
264{
265 struct hash_table *ht;
266
267 /* check that 2^x == size (power of 2) */
268 assert(size && !(size & (size - 1)));
269 assert(val_equal && val_size);
270 assert(resize == 0 || resize == 1);
271
272 if (size < LYHT_MIN_SIZE) {
273 size = LYHT_MIN_SIZE;
274 }
275
276 ht = malloc(sizeof *ht);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200277 LY_CHECK_ERR_RET(!ht, LOGMEM(NULL), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200278
279 ht->used = 0;
280 ht->size = size;
281 ht->val_equal = val_equal;
282 ht->cb_data = cb_data;
283 ht->resize = (uint16_t)resize;
284
285 ht->rec_size = (sizeof(struct ht_rec) - 1) + val_size;
286 /* allocate the records correctly */
287 ht->recs = calloc(size, ht->rec_size);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200288 LY_CHECK_ERR_RET(!ht->recs, free(ht); LOGMEM(NULL), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200289
290 return ht;
291}
292
293values_equal_cb
294lyht_set_cb(struct hash_table *ht, values_equal_cb new_val_equal)
295{
296 values_equal_cb prev;
297
298 prev = ht->val_equal;
299 ht->val_equal = new_val_equal;
300 return prev;
301}
302
303void *
304lyht_set_cb_data(struct hash_table *ht, void *new_cb_data)
305{
306 void *prev;
307
308 prev = ht->cb_data;
309 ht->cb_data = new_cb_data;
310 return prev;
311}
312
313struct hash_table *
314lyht_dup(const struct hash_table *orig)
315{
316 struct hash_table *ht;
317
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200318 LY_CHECK_ARG_RET(NULL, orig, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200319
320 ht = lyht_new(orig->size, orig->rec_size - (sizeof(struct ht_rec) - 1), orig->val_equal, orig->cb_data, orig->resize ? 1 : 0);
321 if (!ht) {
322 return NULL;
323 }
324
325 memcpy(ht->recs, orig->recs, orig->used * orig->rec_size);
326 ht->used = orig->used;
327 return ht;
328}
329
330void
331lyht_free(struct hash_table *ht)
332{
333 if (ht) {
334 free(ht->recs);
335 free(ht);
336 }
337}
338
339static LY_ERR
340lyht_resize(struct hash_table *ht, int enlarge)
341{
342 struct ht_rec *rec;
343 unsigned char *old_recs;
344 uint32_t i, old_size;
345 int ret;
346
347 old_recs = ht->recs;
348 old_size = ht->size;
349
350 if (enlarge) {
351 /* double the size */
352 ht->size <<= 1;
353 } else {
354 /* half the size */
355 ht->size >>= 1;
356 }
357
358 ht->recs = calloc(ht->size, ht->rec_size);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200359 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 +0200360
361 /* reset used, it will increase again */
362 ht->used = 0;
363
364 /* add all the old records into the new records array */
365 for (i = 0; i < old_size; ++i) {
366 rec = lyht_get_rec(old_recs, ht->rec_size, i);
367 if (rec->hits > 0) {
368 ret = lyht_insert(ht, rec->val, rec->hash, NULL);
369 assert(!ret);
370 (void)ret;
371 }
372 }
373
374 /* final touches */
375 free(old_recs);
376 return LY_SUCCESS;
377}
378
379/* return: 0 - hash found, returned its record,
380 * 1 - hash not found, returned the record where it would be inserted */
381static int
382lyht_find_first(struct hash_table *ht, uint32_t hash, struct ht_rec **rec_p)
383{
384 struct ht_rec *rec;
385 uint32_t i, idx;
386
387 if (rec_p) {
388 *rec_p = NULL;
389 }
390
391 idx = i = hash & (ht->size - 1);
392 rec = lyht_get_rec(ht->recs, ht->rec_size, idx);
393
394 /* skip through overflow and deleted records */
395 while ((rec->hits != 0) && ((rec->hits == -1) || ((rec->hash & (ht->size - 1)) != idx))) {
396 if ((rec->hits == -1) && rec_p && !(*rec_p)) {
397 /* remember this record for return */
398 *rec_p = rec;
399 }
400 i = (i + 1) % ht->size;
401 if (i == idx) {
402 /* we went through all the records (very unlikely, but possible when many records are invalid),
403 * just return not found */
404 assert(!rec_p || *rec_p);
405 return 1;
406 }
407 rec = lyht_get_rec(ht->recs, ht->rec_size, i);
408 }
409 if (rec->hits == 0) {
410 /* we could not find the value */
411 if (rec_p && !*rec_p) {
412 *rec_p = rec;
413 }
414 return 1;
415 }
416
417 /* we have found a record with equal (shortened) hash */
418 if (rec_p) {
419 *rec_p = rec;
420 }
421 return 0;
422}
423
424/**
425 * @brief Search for the next collision.
426 *
427 * @param[in] ht Hash table to search in.
428 * @param[in,out] last Last returned collision record.
429 * @param[in] first First collision record (hits > 1).
430 * @return 0 when hash collision found, \p last points to this next collision,
431 * 1 when hash collision not found, \p last points to the record where it would be inserted.
432 */
433static int
434lyht_find_collision(struct hash_table *ht, struct ht_rec **last, struct ht_rec *first)
435{
436 struct ht_rec *empty = NULL;
437 uint32_t i, idx;
438
439 assert(last && *last);
440
441 idx = (*last)->hash & (ht->size - 1);
442 i = (((unsigned char *)*last) - ht->recs) / ht->rec_size;
443
444 do {
445 i = (i + 1) % ht->size;
446 *last = lyht_get_rec(ht->recs, ht->rec_size, i);
447 if (*last == first) {
448 /* we went through all the records (very unlikely, but possible when many records are invalid),
449 * just return an invalid record */
450 assert(empty);
451 *last = empty;
452 return 1;
453 }
454
455 if (((*last)->hits == -1) && !empty) {
456 empty = *last;
457 }
458 } while (((*last)->hits != 0) && (((*last)->hits == -1) || (((*last)->hash & (ht->size - 1)) != idx)));
459
460 if ((*last)->hits > 0) {
461 /* we found a collision */
462 assert((*last)->hits == 1);
463 return 0;
464 }
465
466 /* no next collision found, return the record where it would be inserted */
467 if (empty) {
468 *last = empty;
469 } /* else (*last)->hits == 0, it is already correct */
470 return 1;
471}
472
473int
474lyht_find(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
475{
476 struct ht_rec *rec, *crec;
477 uint32_t i, c;
478 int r;
479
480 if (lyht_find_first(ht, hash, &rec)) {
481 /* not found */
482 return 1;
483 }
484 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 0, ht->cb_data)) {
485 /* even the value matches */
486 if (match_p) {
487 *match_p = rec->val;
488 }
489 return 0;
490 }
491
492 /* some collisions, we need to go through them, too */
493 crec = rec;
494 c = rec->hits;
495 for (i = 1; i < c; ++i) {
496 r = lyht_find_collision(ht, &rec, crec);
497 assert(!r);
498 (void)r;
499
500 /* compare values */
501 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 0, ht->cb_data)) {
502 if (match_p) {
503 *match_p = rec->val;
504 }
505 return 0;
506 }
507 }
508
509 /* not found even in collisions */
510 return 1;
511}
512
513int
514lyht_find_next(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
515{
516 struct ht_rec *rec, *crec;
517 uint32_t i, c;
518 int r, found = 0;
519
520 if (lyht_find_first(ht, hash, &rec)) {
521 /* not found, cannot happen */
522 assert(0);
523 }
524
525 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
526 /* previously returned value */
527 found = 1;
528 }
529
530 if (rec->hits == 1) {
531 /* there are no more similar values */
532 assert(rec->hash == hash);
533 assert(found);
534 return 1;
535 }
536
537 /* go through collisions and find next one after the previous one */
538 crec = rec;
539 c = rec->hits;
540 for (i = 1; i < c; ++i) {
541 r = lyht_find_collision(ht, &rec, crec);
542 assert(!r);
543 (void)r;
544
545 if (rec->hash != hash) {
546 /* a normal collision, we are not interested in those */
547 continue;
548 }
549
550 if (found) {
551 /* next value with equal hash, found our value */
552 if (match_p) {
553 *match_p = rec->val;
554 }
555 return 0;
556 }
557
558 if (!ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
559 /* already returned value, skip */
560 continue;
561 }
562
563 /* this one was returned previously, continue looking */
564 found = 1;
565 }
566
567 /* the last equal value was already returned */
568 assert(found);
569 return 1;
570}
571
572LY_ERR
573lyht_insert_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash,
574 values_equal_cb resize_val_equal, void **match_p)
575{
576 struct ht_rec *rec, *crec = NULL;
577 int32_t i;
578 int r, ret;
579 values_equal_cb old_val_equal;
580
581 if (!lyht_find_first(ht, hash, &rec)) {
582 /* we found matching shortened hash */
583 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
584 /* even the value matches */
585 if (match_p) {
586 *match_p = (void *)&rec->val;
587 }
588 return LY_EEXIST;
589 }
590
591 /* some collisions, we need to go through them, too */
592 crec = rec;
593 for (i = 1; i < crec->hits; ++i) {
594 r = lyht_find_collision(ht, &rec, crec);
595 assert(!r);
596
597 /* compare values */
598 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
599 if (match_p) {
600 *match_p = (void *)&rec->val;
601 }
602 return LY_EEXIST;
603 }
604 }
605
606 /* value not found, get the record where it will be inserted */
607 r = lyht_find_collision(ht, &rec, crec);
608 assert(r);
609 }
610
611 /* insert it into the returned record */
612 assert(rec->hits < 1);
613 rec->hash = hash;
614 rec->hits = 1;
615 memcpy(&rec->val, val_p, ht->rec_size - (sizeof(struct ht_rec) - 1));
616 if (match_p) {
617 *match_p = (void *)&rec->val;
618 }
619
620 if (crec) {
621 /* there was a collision, increase hits */
622 if (crec->hits == INT32_MAX) {
623 LOGINT(NULL);
624 }
625 ++crec->hits;
626 }
627
628 /* check size & enlarge if needed */
629 ret = LY_SUCCESS;
630 ++ht->used;
631 if (ht->resize) {
632 r = (ht->used * 100) / ht->size;
633 if ((ht->resize == 1) && (r >= LYHT_FIRST_SHRINK_PERCENTAGE)) {
634 /* enable shrinking */
635 ht->resize = 2;
636 }
637 if ((ht->resize == 2) && (r >= LYHT_ENLARGE_PERCENTAGE)) {
638 if (resize_val_equal) {
639 old_val_equal = lyht_set_cb(ht, resize_val_equal);
640 }
641
642 /* enlarge */
643 ret = lyht_resize(ht, 1);
644 /* if hash_table was resized, we need to find new matching value */
645 if (ret == LY_SUCCESS && match_p) {
646 lyht_find(ht, val_p, hash, match_p);
647 }
648
649 if (resize_val_equal) {
650 lyht_set_cb(ht, old_val_equal);
651 }
652 }
653 }
654 return ret;
655}
656
Radek Krejci0ae092d2018-09-20 16:43:19 +0200657LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200658lyht_insert(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
659{
660 return lyht_insert_with_resize_cb(ht, val_p, hash, NULL, match_p);
661}
662
663LY_ERR
664lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash)
665{
666 struct ht_rec *rec, *crec;
667 int32_t i;
668 int first_matched = 0, r, ret;
669
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200670 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 +0200671
672 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
673 /* even the value matches */
674 first_matched = 1;
675 }
676
677 /* we always need to go through collisions */
678 crec = rec;
679 for (i = 1; i < crec->hits; ++i) {
680 r = lyht_find_collision(ht, &rec, crec);
681 assert(!r);
682
683 /* compare values */
684 if (!first_matched && (rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
685 break;
686 }
687 }
688
689 if (i < crec->hits) {
690 /* one of collisions matched, reduce collision count, remove the record */
691 assert(!first_matched);
692 --crec->hits;
693 rec->hits = -1;
694 } else if (first_matched) {
695 /* the first record matches */
696 if (crec != rec) {
697 /* ... so put the last collision in its place */
698 rec->hits = crec->hits - 1;
699 memcpy(crec, rec, ht->rec_size);
700 }
701 rec->hits = -1;
702 } else {
703 /* value not found even in collisions */
704 LOGINT(NULL);
705 return LY_EINT;
706 }
707
708 /* check size & shrink if needed */
709 ret = LY_SUCCESS;
710 --ht->used;
711 if (ht->resize == 2) {
712 r = (ht->used * 100) / ht->size;
713 if ((r < LYHT_SHRINK_PERCENTAGE) && (ht->size > LYHT_MIN_SIZE)) {
714 /* shrink */
715 ret = lyht_resize(ht, 0);
716 }
717 }
718
719 return ret;
720}