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