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