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