Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file dict.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief libyang dictionary for storing strings |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 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> |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 19 | |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 20 | #include "common.h" |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 21 | #include "context.h" |
Radek Krejci | 41912fe | 2015-10-22 10:22:12 +0200 | [diff] [blame] | 22 | #include "dict_private.h" |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 23 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 24 | void |
| 25 | lydict_init(struct dict_table *dict) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 26 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 27 | if (!dict) { |
| 28 | ly_errno = LY_EINVAL; |
| 29 | return; |
| 30 | } |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 31 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 32 | dict->hash_mask = DICT_SIZE - 1; |
Radek Krejci | febcbfb | 2016-02-04 14:58:42 +0100 | [diff] [blame] | 33 | pthread_mutex_init(&dict->lock, NULL); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 34 | } |
| 35 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 36 | void |
| 37 | lydict_clean(struct dict_table *dict) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 38 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 39 | int i; |
| 40 | struct dict_rec *chain, *rec; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 41 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 42 | if (!dict) { |
| 43 | ly_errno = LY_EINVAL; |
| 44 | return; |
| 45 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 46 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 47 | for (i = 0; i < DICT_SIZE; i++) { |
| 48 | rec = &dict->recs[i]; |
| 49 | chain = rec->next; |
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 | free(rec->value); |
| 52 | while (chain) { |
| 53 | rec = chain; |
| 54 | chain = rec->next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 55 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 56 | free(rec->value); |
| 57 | free(rec); |
| 58 | } |
| 59 | } |
Radek Krejci | febcbfb | 2016-02-04 14:58:42 +0100 | [diff] [blame] | 60 | |
| 61 | pthread_mutex_destroy(&dict->lock); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 62 | } |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | * Bob Jenkin's one-at-a-time hash |
| 66 | * http://www.burtleburtle.net/bob/hash/doobs.html |
| 67 | * |
| 68 | * Spooky hash is faster, but it works only for little endian architectures. |
| 69 | */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 70 | static uint32_t |
| 71 | dict_hash(const char *key, size_t len) |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 72 | { |
| 73 | uint32_t hash, i; |
| 74 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 75 | for (hash = i = 0; i < len; ++i) { |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 76 | hash += key[i]; |
| 77 | hash += (hash << 10); |
| 78 | hash ^= (hash >> 6); |
| 79 | } |
| 80 | hash += (hash << 3); |
| 81 | hash ^= (hash >> 11); |
| 82 | hash += (hash << 15); |
| 83 | return hash; |
| 84 | } |
| 85 | |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame^] | 86 | /* |
| 87 | * Usage: |
| 88 | * - init hash to 0 |
| 89 | * - repeatedly call dict_hash_multi(), provide hash from the last call |
| 90 | * - call dict_hash_multi() with key_part = NULL to finish the hash |
| 91 | */ |
| 92 | uint32_t |
| 93 | dict_hash_multi(uint32_t hash, const char *key_part, size_t len) |
| 94 | { |
| 95 | uint32_t i; |
| 96 | |
| 97 | if (key_part) { |
| 98 | for (i = 0; i < len; ++i) { |
| 99 | hash += key_part[i]; |
| 100 | hash += (hash << 10); |
| 101 | hash ^= (hash >> 6); |
| 102 | } |
| 103 | } else { |
| 104 | hash += (hash << 3); |
| 105 | hash ^= (hash >> 11); |
| 106 | hash += (hash << 15); |
| 107 | } |
| 108 | |
| 109 | return hash; |
| 110 | } |
| 111 | |
Radek Krejci | fcf37bd | 2015-10-22 10:27:53 +0200 | [diff] [blame] | 112 | API void |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 113 | lydict_remove(struct ly_ctx *ctx, const char *value) |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 114 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 115 | size_t len; |
| 116 | uint32_t index; |
| 117 | struct dict_rec *record, *prev = NULL; |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 118 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 119 | if (!value || !ctx || !ctx->dict.used) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 120 | return; |
| 121 | } |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 122 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 123 | len = strlen(value); |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 124 | |
Radek Krejci | febcbfb | 2016-02-04 14:58:42 +0100 | [diff] [blame] | 125 | pthread_mutex_lock(&ctx->dict.lock); |
| 126 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 127 | index = dict_hash(value, len) & ctx->dict.hash_mask; |
| 128 | record = &ctx->dict.recs[index]; |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 129 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 130 | while (record && record->value != value) { |
| 131 | prev = record; |
| 132 | record = record->next; |
| 133 | } |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 134 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 135 | if (!record) { |
| 136 | /* record not found */ |
Radek Krejci | febcbfb | 2016-02-04 14:58:42 +0100 | [diff] [blame] | 137 | pthread_mutex_unlock(&ctx->dict.lock); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 138 | return; |
| 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 | record->refcount--; |
| 142 | if (!record->refcount) { |
| 143 | free(record->value); |
| 144 | if (record->next) { |
| 145 | if (prev) { |
| 146 | /* change in dynamically allocated chain */ |
| 147 | prev->next = record->next; |
| 148 | free(record); |
| 149 | } else { |
| 150 | /* move dynamically allocated record into the static array */ |
| 151 | prev = record->next; /* temporary storage */ |
| 152 | memcpy(record, record->next, sizeof *record); |
| 153 | free(prev); |
| 154 | } |
| 155 | } else if (prev) { |
| 156 | /* removing last record from the dynamically allocated chain */ |
| 157 | prev->next = NULL; |
| 158 | free(record); |
| 159 | } else { |
| 160 | /* clean the static record content */ |
| 161 | memset(record, 0, sizeof *record); |
| 162 | } |
Radek Krejci | 6b0a888 | 2016-02-16 17:37:21 +0100 | [diff] [blame] | 163 | ctx->dict.used--; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 164 | } |
Radek Krejci | febcbfb | 2016-02-04 14:58:42 +0100 | [diff] [blame] | 165 | |
| 166 | pthread_mutex_unlock(&ctx->dict.lock); |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 169 | static char * |
| 170 | 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] | 171 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 172 | uint32_t index; |
Radek Krejci | 592e00d | 2016-05-19 12:38:08 +0200 | [diff] [blame] | 173 | int match = 0; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 174 | struct dict_rec *record, *new; |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 175 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 176 | index = dict_hash(value, len) & ctx->dict.hash_mask; |
| 177 | record = &ctx->dict.recs[index]; |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 178 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 179 | if (!record->value) { |
| 180 | /* first record with this hash */ |
| 181 | if (zerocopy) { |
| 182 | record->value = value; |
| 183 | } else { |
| 184 | record->value = malloc((len + 1) * sizeof *record->value); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 185 | if (!record->value) { |
| 186 | LOGMEM; |
| 187 | return NULL; |
| 188 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 189 | memcpy(record->value, value, len); |
| 190 | record->value[len] = '\0'; |
| 191 | } |
| 192 | record->refcount = 1; |
Radek Krejci | 592e00d | 2016-05-19 12:38:08 +0200 | [diff] [blame] | 193 | if (len > DICT_REC_MAXLEN) { |
| 194 | record->len = 0; |
| 195 | } else { |
| 196 | record->len = len; |
| 197 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 198 | record->next = NULL; |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 199 | |
Radek Krejci | 6b0a888 | 2016-02-16 17:37:21 +0100 | [diff] [blame] | 200 | ctx->dict.used++; |
| 201 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 202 | LOGDBG("DICT: inserting \"%s\"", record->value); |
| 203 | return record->value; |
| 204 | } |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 205 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 206 | /* collision, search if the value is already in dict */ |
| 207 | while (record) { |
Radek Krejci | 592e00d | 2016-05-19 12:38:08 +0200 | [diff] [blame] | 208 | if (record->len) { |
| 209 | /* for strings shorter than DICT_REC_MAXLEN we are able to speed up |
| 210 | * recognition of varying strings according to their lengths, and |
| 211 | * for strings with the same length it is safe to use faster memcmp() |
| 212 | * instead of strncmp() */ |
| 213 | if ((record->len == len) && !memcmp(value, record->value, len)) { |
| 214 | match = 1; |
| 215 | } |
| 216 | } else { |
| 217 | if (!strncmp(value, record->value, len) && record->value[len] == '\0') { |
| 218 | match = 1; |
| 219 | } |
| 220 | } |
| 221 | if (match) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 222 | /* record found */ |
Radek Krejci | 592e00d | 2016-05-19 12:38:08 +0200 | [diff] [blame] | 223 | if (record->refcount == DICT_REC_MAXCOUNT) { |
| 224 | LOGWRN("DICT: refcount overflow detected, duplicating record"); |
| 225 | break; |
| 226 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 227 | record->refcount++; |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 228 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 229 | if (zerocopy) { |
| 230 | free(value); |
| 231 | } |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 232 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 233 | LOGDBG("DICT: inserting (refcount) \"%s\"", record->value); |
| 234 | return record->value; |
| 235 | } |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 236 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 237 | if (!record->next) { |
| 238 | /* not present, add as a new record in chain */ |
| 239 | break; |
| 240 | } |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 241 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 242 | record = record->next; |
| 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 | /* create new record and add it behind the last record */ |
| 246 | new = malloc(sizeof *record); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 247 | if (!new) { |
| 248 | LOGMEM; |
| 249 | return NULL; |
| 250 | } |
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 | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 255 | if (!new->value) { |
| 256 | LOGMEM; |
| 257 | free(new); |
| 258 | return NULL; |
| 259 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 260 | memcpy(new->value, value, len); |
| 261 | new->value[len] = '\0'; |
| 262 | } |
| 263 | new->refcount = 1; |
Radek Krejci | 592e00d | 2016-05-19 12:38:08 +0200 | [diff] [blame] | 264 | if (len > DICT_REC_MAXLEN) { |
| 265 | new->len = 0; |
| 266 | } else { |
| 267 | new->len = len; |
| 268 | } |
| 269 | 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] | 270 | record->next = new; |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 271 | |
Radek Krejci | 6b0a888 | 2016-02-16 17:37:21 +0100 | [diff] [blame] | 272 | ctx->dict.used++; |
| 273 | |
Michal Vasko | 29e7bce | 2015-09-11 16:00:15 +0200 | [diff] [blame] | 274 | LOGDBG("DICT: inserting \"%s\" with collision ", new->value); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 275 | return new->value; |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 276 | } |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 277 | |
Radek Krejci | fcf37bd | 2015-10-22 10:27:53 +0200 | [diff] [blame] | 278 | API const char * |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 279 | lydict_insert(struct ly_ctx *ctx, const char *value, size_t len) |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 280 | { |
Radek Krejci | febcbfb | 2016-02-04 14:58:42 +0100 | [diff] [blame] | 281 | const char *result; |
| 282 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 283 | if (value && !len) { |
| 284 | len = strlen(value); |
| 285 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 286 | |
Radek Krejci | a526964 | 2015-07-20 19:04:11 +0200 | [diff] [blame] | 287 | if (!value) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 288 | return NULL; |
| 289 | } |
Radek Krejci | febcbfb | 2016-02-04 14:58:42 +0100 | [diff] [blame] | 290 | |
| 291 | pthread_mutex_lock(&ctx->dict.lock); |
| 292 | result = dict_insert(ctx, (char *)value, len, 0); |
| 293 | pthread_mutex_unlock(&ctx->dict.lock); |
| 294 | |
| 295 | return result; |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Radek Krejci | fcf37bd | 2015-10-22 10:27:53 +0200 | [diff] [blame] | 298 | API const char * |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 299 | lydict_insert_zc(struct ly_ctx *ctx, char *value) |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 300 | { |
Radek Krejci | febcbfb | 2016-02-04 14:58:42 +0100 | [diff] [blame] | 301 | const char *result; |
| 302 | |
Radek Krejci | a526964 | 2015-07-20 19:04:11 +0200 | [diff] [blame] | 303 | if (!value) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 304 | return NULL; |
| 305 | } |
Radek Krejci | febcbfb | 2016-02-04 14:58:42 +0100 | [diff] [blame] | 306 | |
| 307 | pthread_mutex_lock(&ctx->dict.lock); |
| 308 | result = dict_insert(ctx, value, strlen(value), 1); |
| 309 | pthread_mutex_unlock(&ctx->dict.lock); |
| 310 | |
| 311 | return result; |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 312 | } |