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