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