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