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 | 5a28e0e | 2018-08-07 11:29:06 +0200 | [diff] [blame] | 401 | int ret; |
Michal Vasko | 04210d7 | 2018-03-16 12:07:47 +0100 | [diff] [blame] | 402 | |
| 403 | old_recs = ht->recs; |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 404 | old_size = ht->size; |
Michal Vasko | 04210d7 | 2018-03-16 12:07:47 +0100 | [diff] [blame] | 405 | |
| 406 | if (enlarge) { |
| 407 | /* double the size */ |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 408 | ht->size <<= 1; |
Michal Vasko | 04210d7 | 2018-03-16 12:07:47 +0100 | [diff] [blame] | 409 | } else { |
| 410 | /* half the size */ |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 411 | ht->size >>= 1; |
Michal Vasko | 04210d7 | 2018-03-16 12:07:47 +0100 | [diff] [blame] | 412 | } |
| 413 | |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 414 | ht->recs = calloc(ht->size, ht->rec_size); |
| 415 | 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] | 416 | |
| 417 | /* reset used, it will increase again */ |
| 418 | ht->used = 0; |
| 419 | |
| 420 | /* add all the old records into the new records array */ |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 421 | for (i = 0; i < old_size; ++i) { |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 422 | rec = lyht_get_rec(old_recs, ht->rec_size, i); |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 423 | if (rec->hits > 0) { |
Michal Vasko | 5a28e0e | 2018-08-07 11:29:06 +0200 | [diff] [blame] | 424 | ret = lyht_insert(ht, rec->val, rec->hash); |
| 425 | assert(!ret); |
| 426 | (void)ret; |
Michal Vasko | 04210d7 | 2018-03-16 12:07:47 +0100 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
| 430 | /* final touches */ |
Michal Vasko | 04210d7 | 2018-03-16 12:07:47 +0100 | [diff] [blame] | 431 | free(old_recs); |
| 432 | return 0; |
| 433 | } |
| 434 | |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 435 | /* return: 0 - hash found, returned its record, |
| 436 | * 1 - hash not found, returned the record where it would be inserted */ |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 437 | static int |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 438 | 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] | 439 | { |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 440 | struct ht_rec *rec; |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 441 | uint32_t i, idx; |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 442 | |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 443 | if (rec_p) { |
| 444 | *rec_p = NULL; |
| 445 | } |
| 446 | |
| 447 | idx = i = hash & (ht->size - 1); |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 448 | rec = lyht_get_rec(ht->recs, ht->rec_size, idx); |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 449 | |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 450 | /* skip through overflow and deleted records */ |
| 451 | while ((rec->hits != 0) && ((rec->hits == -1) || ((rec->hash & (ht->size - 1)) != idx))) { |
| 452 | if ((rec->hits == -1) && rec_p && !(*rec_p)) { |
| 453 | /* remember this record for return */ |
| 454 | *rec_p = rec; |
| 455 | } |
| 456 | i = (i + 1) % ht->size; |
Michal Vasko | 7f6f0f9 | 2018-05-21 13:57:30 +0200 | [diff] [blame] | 457 | if (i == idx) { |
| 458 | /* we went through all the records (very unlikely, but possible when many records are invalid), |
| 459 | * just return not found */ |
| 460 | assert(!rec_p || *rec_p); |
| 461 | return 1; |
| 462 | } |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 463 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 464 | } |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 465 | if (rec->hits == 0) { |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 466 | /* we could not find the value */ |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 467 | if (rec_p && !*rec_p) { |
| 468 | *rec_p = rec; |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 469 | } |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 470 | return 1; |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 471 | } |
| 472 | |
Michal Vasko | 7854883 | 2018-06-04 12:07:52 +0200 | [diff] [blame] | 473 | /* we have found a record with equal (shortened) hash */ |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 474 | if (rec_p) { |
| 475 | *rec_p = rec; |
| 476 | } |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 477 | return 0; |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 478 | } |
| 479 | |
Michal Vasko | 1de7bd1 | 2018-05-21 14:27:02 +0200 | [diff] [blame] | 480 | /** |
| 481 | * @brief Search for the next collision. |
| 482 | * |
| 483 | * @param[in] ht Hash table to search in. |
| 484 | * @param[in,out] last Last returned collision record. |
| 485 | * @param[in] first First collision record (hits > 1). |
| 486 | * @return 0 when hash collision found, \p last points to this next collision, |
| 487 | * 1 when hash collision not found, \p last points to the record where it would be inserted. |
| 488 | */ |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 489 | static int |
Michal Vasko | 1de7bd1 | 2018-05-21 14:27:02 +0200 | [diff] [blame] | 490 | 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] | 491 | { |
| 492 | struct ht_rec *empty = NULL; |
Michal Vasko | 1de7bd1 | 2018-05-21 14:27:02 +0200 | [diff] [blame] | 493 | uint32_t i, idx; |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 494 | |
| 495 | assert(last && *last); |
| 496 | |
| 497 | idx = (*last)->hash & (ht->size - 1); |
Michal Vasko | 1de7bd1 | 2018-05-21 14:27:02 +0200 | [diff] [blame] | 498 | i = (((unsigned char *)*last) - ht->recs) / ht->rec_size; |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 499 | |
| 500 | do { |
| 501 | i = (i + 1) % ht->size; |
Michal Vasko | 1de7bd1 | 2018-05-21 14:27:02 +0200 | [diff] [blame] | 502 | *last = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 503 | if (*last == first) { |
Michal Vasko | 1d89f90 | 2018-05-21 13:01:32 +0200 | [diff] [blame] | 504 | /* we went through all the records (very unlikely, but possible when many records are invalid), |
| 505 | * just return an invalid record */ |
| 506 | assert(empty); |
| 507 | *last = empty; |
| 508 | return 1; |
| 509 | } |
Michal Vasko | 1de7bd1 | 2018-05-21 14:27:02 +0200 | [diff] [blame] | 510 | |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 511 | if (((*last)->hits == -1) && !empty) { |
| 512 | empty = *last; |
| 513 | } |
| 514 | } while (((*last)->hits != 0) && (((*last)->hits == -1) || (((*last)->hash & (ht->size - 1)) != idx))); |
| 515 | |
| 516 | if ((*last)->hits > 0) { |
| 517 | /* we found a collision */ |
| 518 | assert((*last)->hits == 1); |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | /* no next collision found, return the record where it would be inserted */ |
| 523 | if (empty) { |
| 524 | *last = empty; |
| 525 | } /* else (*last)->hits == 0, it is already correct */ |
| 526 | return 1; |
| 527 | } |
| 528 | |
| 529 | int |
Michal Vasko | fd5581a | 2018-04-25 13:20:20 +0200 | [diff] [blame] | 530 | 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] | 531 | { |
Michal Vasko | 1de7bd1 | 2018-05-21 14:27:02 +0200 | [diff] [blame] | 532 | struct ht_rec *rec, *crec; |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 533 | uint32_t i, c; |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 534 | int r; |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 535 | |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 536 | if (lyht_find_first(ht, hash, &rec)) { |
| 537 | /* not found */ |
| 538 | return 1; |
| 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, 0, ht->cb_data)) { |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 541 | /* even the value matches */ |
Michal Vasko | fd5581a | 2018-04-25 13:20:20 +0200 | [diff] [blame] | 542 | if (match_p) { |
| 543 | *match_p = rec->val; |
| 544 | } |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 545 | return 0; |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /* some collisions, we need to go through them, too */ |
Michal Vasko | 1de7bd1 | 2018-05-21 14:27:02 +0200 | [diff] [blame] | 549 | crec = rec; |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 550 | c = rec->hits; |
| 551 | for (i = 1; i < c; ++i) { |
Michal Vasko | 1de7bd1 | 2018-05-21 14:27:02 +0200 | [diff] [blame] | 552 | r = lyht_find_collision(ht, &rec, crec); |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 553 | assert(!r); |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 554 | (void)r; |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 555 | |
| 556 | /* compare values */ |
Michal Vasko | 7854883 | 2018-06-04 12:07:52 +0200 | [diff] [blame] | 557 | 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] | 558 | if (match_p) { |
| 559 | *match_p = rec->val; |
| 560 | } |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 561 | return 0; |
| 562 | } |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 563 | } |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 564 | |
| 565 | /* not found even in collisions */ |
| 566 | return 1; |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | int |
Michal Vasko | fd5581a | 2018-04-25 13:20:20 +0200 | [diff] [blame] | 570 | lyht_find_next(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p) |
| 571 | { |
Michal Vasko | 1de7bd1 | 2018-05-21 14:27:02 +0200 | [diff] [blame] | 572 | struct ht_rec *rec, *crec; |
Michal Vasko | fd5581a | 2018-04-25 13:20:20 +0200 | [diff] [blame] | 573 | uint32_t i, c; |
| 574 | int r, found = 0; |
| 575 | |
| 576 | if (lyht_find_first(ht, hash, &rec)) { |
| 577 | /* not found, cannot happen */ |
| 578 | assert(0); |
| 579 | } |
| 580 | |
Michal Vasko | 7854883 | 2018-06-04 12:07:52 +0200 | [diff] [blame] | 581 | 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] | 582 | /* previously returned value */ |
| 583 | found = 1; |
| 584 | } |
| 585 | |
| 586 | if (rec->hits == 1) { |
| 587 | /* there are no more similar values */ |
| 588 | assert(rec->hash == hash); |
| 589 | assert(found); |
| 590 | return 1; |
| 591 | } |
| 592 | |
| 593 | /* go through collisions and find next one after the previous one */ |
Michal Vasko | 1de7bd1 | 2018-05-21 14:27:02 +0200 | [diff] [blame] | 594 | crec = rec; |
Michal Vasko | fd5581a | 2018-04-25 13:20:20 +0200 | [diff] [blame] | 595 | c = rec->hits; |
| 596 | for (i = 1; i < c; ++i) { |
Michal Vasko | 1de7bd1 | 2018-05-21 14:27:02 +0200 | [diff] [blame] | 597 | r = lyht_find_collision(ht, &rec, crec); |
Michal Vasko | fd5581a | 2018-04-25 13:20:20 +0200 | [diff] [blame] | 598 | assert(!r); |
| 599 | (void)r; |
| 600 | |
| 601 | if (rec->hash != hash) { |
| 602 | /* a normal collision, we are not interested in those */ |
| 603 | continue; |
| 604 | } |
| 605 | |
| 606 | if (found) { |
| 607 | /* next value with equal hash, found our value */ |
| 608 | if (match_p) { |
| 609 | *match_p = rec->val; |
| 610 | } |
| 611 | return 0; |
| 612 | } |
| 613 | |
Michal Vasko | 6634294 | 2018-04-27 13:56:21 +0200 | [diff] [blame] | 614 | if (!ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) { |
Michal Vasko | fd5581a | 2018-04-25 13:20:20 +0200 | [diff] [blame] | 615 | /* already returned value, skip */ |
| 616 | continue; |
| 617 | } |
| 618 | |
| 619 | /* this one was returned previously, continue looking */ |
| 620 | found = 1; |
| 621 | } |
| 622 | |
| 623 | /* the last equal value was already returned */ |
| 624 | assert(found); |
| 625 | return 1; |
| 626 | } |
| 627 | |
| 628 | int |
Michal Vasko | a1db4be | 2018-08-07 11:30:43 +0200 | [diff] [blame^] | 629 | lyht_insert_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash, values_equal_cb resize_val_equal) |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 630 | { |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 631 | struct ht_rec *rec, *crec = NULL; |
| 632 | int32_t i; |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 633 | int r, ret; |
Michal Vasko | a1db4be | 2018-08-07 11:30:43 +0200 | [diff] [blame^] | 634 | values_equal_cb old_val_equal; |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 635 | |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 636 | if (!lyht_find_first(ht, hash, &rec)) { |
Michal Vasko | 7854883 | 2018-06-04 12:07:52 +0200 | [diff] [blame] | 637 | /* we found matching shortened hash */ |
| 638 | 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] | 639 | /* even the value matches */ |
| 640 | return 1; |
| 641 | } |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 642 | |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 643 | /* some collisions, we need to go through them, too */ |
| 644 | crec = rec; |
| 645 | for (i = 1; i < crec->hits; ++i) { |
Michal Vasko | 1de7bd1 | 2018-05-21 14:27:02 +0200 | [diff] [blame] | 646 | r = lyht_find_collision(ht, &rec, crec); |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 647 | assert(!r); |
| 648 | |
| 649 | /* compare values */ |
Michal Vasko | 7854883 | 2018-06-04 12:07:52 +0200 | [diff] [blame] | 650 | if ((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] | 651 | return 1; |
| 652 | } |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 653 | } |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 654 | |
| 655 | /* value not found, get the record where it will be inserted */ |
Michal Vasko | 1de7bd1 | 2018-05-21 14:27:02 +0200 | [diff] [blame] | 656 | r = lyht_find_collision(ht, &rec, crec); |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 657 | assert(r); |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 658 | } |
| 659 | |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 660 | /* insert it into the returned record */ |
| 661 | assert(rec->hits < 1); |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 662 | rec->hash = hash; |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 663 | rec->hits = 1; |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 664 | memcpy(&rec->val, val_p, ht->rec_size - (sizeof(struct ht_rec) - 1)); |
| 665 | |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 666 | if (crec) { |
| 667 | /* there was a collision, increase hits */ |
Michal Vasko | e330d71 | 2018-04-25 13:20:43 +0200 | [diff] [blame] | 668 | if (crec->hits == INT32_MAX) { |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 669 | LOGINT(NULL); |
| 670 | } |
| 671 | ++crec->hits; |
| 672 | } |
| 673 | |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 674 | /* check size & enlarge if needed */ |
| 675 | ret = 0; |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 676 | ++ht->used; |
Michal Vasko | 04210d7 | 2018-03-16 12:07:47 +0100 | [diff] [blame] | 677 | if (ht->resize) { |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 678 | r = (ht->used * 100) / ht->size; |
| 679 | if ((ht->resize == 1) && (r >= LYHT_FIRST_SHRINK_PERCENTAGE)) { |
Michal Vasko | 04210d7 | 2018-03-16 12:07:47 +0100 | [diff] [blame] | 680 | /* enable shrinking */ |
| 681 | ht->resize = 2; |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 682 | } |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 683 | if ((ht->resize == 2) && (r >= LYHT_ENLARGE_PERCENTAGE)) { |
Michal Vasko | a1db4be | 2018-08-07 11:30:43 +0200 | [diff] [blame^] | 684 | if (resize_val_equal) { |
| 685 | old_val_equal = lyht_set_cb(ht, resize_val_equal); |
| 686 | } |
| 687 | |
Michal Vasko | 04210d7 | 2018-03-16 12:07:47 +0100 | [diff] [blame] | 688 | /* enlarge */ |
| 689 | ret = lyht_resize(ht, 1); |
Michal Vasko | a1db4be | 2018-08-07 11:30:43 +0200 | [diff] [blame^] | 690 | |
| 691 | if (resize_val_equal) { |
| 692 | lyht_set_cb(ht, old_val_equal); |
| 693 | } |
Michal Vasko | 04210d7 | 2018-03-16 12:07:47 +0100 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | return ret; |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | int |
Michal Vasko | a1db4be | 2018-08-07 11:30:43 +0200 | [diff] [blame^] | 700 | lyht_insert(struct hash_table *ht, void *val_p, uint32_t hash) |
| 701 | { |
| 702 | return lyht_insert_with_resize_cb(ht, val_p, hash, NULL); |
| 703 | } |
| 704 | |
| 705 | int |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 706 | lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash) |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 707 | { |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 708 | struct ht_rec *rec, *crec; |
| 709 | int32_t i; |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 710 | int first_matched = 0, r, ret; |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 711 | |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 712 | if (lyht_find_first(ht, hash, &rec)) { |
| 713 | /* hash not found */ |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 714 | return 1; |
| 715 | } |
Michal Vasko | 7854883 | 2018-06-04 12:07:52 +0200 | [diff] [blame] | 716 | 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] | 717 | /* even the value matches */ |
| 718 | first_matched = 1; |
| 719 | } |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 720 | |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 721 | /* we always need to go through collisions */ |
| 722 | crec = rec; |
| 723 | for (i = 1; i < crec->hits; ++i) { |
Michal Vasko | 1de7bd1 | 2018-05-21 14:27:02 +0200 | [diff] [blame] | 724 | r = lyht_find_collision(ht, &rec, crec); |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 725 | assert(!r); |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 726 | |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 727 | /* compare values */ |
Michal Vasko | 7854883 | 2018-06-04 12:07:52 +0200 | [diff] [blame] | 728 | 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] | 729 | break; |
| 730 | } |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 731 | } |
| 732 | |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 733 | if (i < crec->hits) { |
| 734 | /* one of collisions matched, reduce collision count, remove the record */ |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 735 | assert(!first_matched); |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 736 | --crec->hits; |
| 737 | rec->hits = -1; |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 738 | } else if (first_matched) { |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 739 | /* the first record matches */ |
| 740 | if (crec != rec) { |
| 741 | /* ... so put the last collision in its place */ |
| 742 | rec->hits = crec->hits - 1; |
| 743 | memcpy(crec, rec, ht->rec_size); |
| 744 | } |
| 745 | rec->hits = -1; |
| 746 | } else { |
| 747 | /* value not found even in collisions */ |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 748 | assert(!first_matched); |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 749 | return 1; |
| 750 | } |
| 751 | |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 752 | /* check size & shrink if needed */ |
| 753 | ret = 0; |
| 754 | --ht->used; |
| 755 | if (ht->resize == 2) { |
Michal Vasko | 7d3ad5a | 2018-03-27 14:22:19 +0200 | [diff] [blame] | 756 | r = (ht->used * 100) / ht->size; |
| 757 | if ((r < LYHT_SHRINK_PERCENTAGE) && (ht->size > LYHT_MIN_SIZE)) { |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 758 | /* shrink */ |
| 759 | ret = lyht_resize(ht, 0); |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 760 | } |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 761 | } |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 762 | |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame] | 763 | return ret; |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 764 | } |