blob: 6f169d290f69bc41c05af99f70edb7ed09cd72da [file] [log] [blame]
Radek Krejciee627582015-04-20 17:39:59 +02001/**
Michal Vasko6c810702018-03-14 16:23:21 +01002 * @file hash_table.c
Radek Krejciee627582015-04-20 17:39:59 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko6c810702018-03-14 16:23:21 +01004 * @brief libyang dictionary for storing strings and generic hash table
Radek Krejciee627582015-04-20 17:39:59 +02005 *
Michal Vasko6c810702018-03-14 16:23:21 +01006 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
Radek Krejciee627582015-04-20 17:39:59 +02007 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * 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 Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejciee627582015-04-20 17:39:59 +020013 */
14
15#include <string.h>
16#include <stdint.h>
17#include <stdlib.h>
Radek Krejcifebcbfb2016-02-04 14:58:42 +010018#include <pthread.h>
Michal Vasko6c810702018-03-14 16:23:21 +010019#include <assert.h>
Radek Krejciee627582015-04-20 17:39:59 +020020
Radek Krejciee627582015-04-20 17:39:59 +020021#include "common.h"
Radek Krejcida04f4a2015-05-21 12:54:09 +020022#include "context.h"
Michal Vasko6c810702018-03-14 16:23:21 +010023#include "hash_table.h"
Radek Krejciee627582015-04-20 17:39:59 +020024
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020025void
26lydict_init(struct dict_table *dict)
Radek Krejcida04f4a2015-05-21 12:54:09 +020027{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020028 if (!dict) {
Michal Vasko53b7da02018-02-13 15:28:42 +010029 LOGARG;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020030 return;
31 }
Radek Krejciee627582015-04-20 17:39:59 +020032
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020033 dict->hash_mask = DICT_SIZE - 1;
Radek Krejcifebcbfb2016-02-04 14:58:42 +010034 pthread_mutex_init(&dict->lock, NULL);
Radek Krejcida04f4a2015-05-21 12:54:09 +020035}
36
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020037void
38lydict_clean(struct dict_table *dict)
Radek Krejcida04f4a2015-05-21 12:54:09 +020039{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020040 int i;
41 struct dict_rec *chain, *rec;
Radek Krejcida04f4a2015-05-21 12:54:09 +020042
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020043 if (!dict) {
Michal Vasko53b7da02018-02-13 15:28:42 +010044 LOGARG;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020045 return;
46 }
Radek Krejcida04f4a2015-05-21 12:54:09 +020047
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020048 for (i = 0; i < DICT_SIZE; i++) {
49 rec = &dict->recs[i];
50 chain = rec->next;
Radek Krejcida04f4a2015-05-21 12:54:09 +020051
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020052 free(rec->value);
53 while (chain) {
54 rec = chain;
55 chain = rec->next;
Radek Krejcida04f4a2015-05-21 12:54:09 +020056
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020057 free(rec->value);
58 free(rec);
59 }
60 }
Radek Krejcifebcbfb2016-02-04 14:58:42 +010061
62 pthread_mutex_destroy(&dict->lock);
Radek Krejcida04f4a2015-05-21 12:54:09 +020063}
Radek Krejciee627582015-04-20 17:39:59 +020064
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 Krejci6e4ffbb2015-06-16 10:34:41 +020071static uint32_t
72dict_hash(const char *key, size_t len)
Radek Krejciee627582015-04-20 17:39:59 +020073{
74 uint32_t hash, i;
75
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020076 for (hash = i = 0; i < len; ++i) {
Radek Krejciee627582015-04-20 17:39:59 +020077 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 Krejci63b79c82016-08-10 10:09:33 +020087/*
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 */
93uint32_t
94dict_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 Krejcifcf37bd2015-10-22 10:27:53 +0200113API void
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200114lydict_remove(struct ly_ctx *ctx, const char *value)
Radek Krejciee627582015-04-20 17:39:59 +0200115{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200116 size_t len;
117 uint32_t index;
118 struct dict_rec *record, *prev = NULL;
Radek Krejciee627582015-04-20 17:39:59 +0200119
Michal Vasko5c328e72016-10-13 12:17:37 +0200120 if (!value || !ctx) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200121 return;
122 }
Radek Krejciee627582015-04-20 17:39:59 +0200123
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200124 len = strlen(value);
Radek Krejciee627582015-04-20 17:39:59 +0200125
Radek Krejcifebcbfb2016-02-04 14:58:42 +0100126 pthread_mutex_lock(&ctx->dict.lock);
127
Michal Vasko5c328e72016-10-13 12:17:37 +0200128 if (!ctx->dict.used) {
129 pthread_mutex_unlock(&ctx->dict.lock);
130 return;
131 }
132
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200133 index = dict_hash(value, len) & ctx->dict.hash_mask;
134 record = &ctx->dict.recs[index];
Radek Krejciee627582015-04-20 17:39:59 +0200135
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200136 while (record && record->value != value) {
137 prev = record;
138 record = record->next;
139 }
Radek Krejciee627582015-04-20 17:39:59 +0200140
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200141 if (!record) {
142 /* record not found */
Radek Krejcifebcbfb2016-02-04 14:58:42 +0100143 pthread_mutex_unlock(&ctx->dict.lock);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200144 return;
145 }
Radek Krejciee627582015-04-20 17:39:59 +0200146
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200147 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 Krejci6b0a8882016-02-16 17:37:21 +0100169 ctx->dict.used--;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200170 }
Radek Krejcifebcbfb2016-02-04 14:58:42 +0100171
172 pthread_mutex_unlock(&ctx->dict.lock);
Radek Krejciee627582015-04-20 17:39:59 +0200173}
174
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200175static char *
176dict_insert(struct ly_ctx *ctx, char *value, size_t len, int zerocopy)
Radek Krejciee627582015-04-20 17:39:59 +0200177{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200178 uint32_t index;
Radek Krejci592e00d2016-05-19 12:38:08 +0200179 int match = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200180 struct dict_rec *record, *new;
Radek Krejciee627582015-04-20 17:39:59 +0200181
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200182 index = dict_hash(value, len) & ctx->dict.hash_mask;
183 record = &ctx->dict.recs[index];
Radek Krejciee627582015-04-20 17:39:59 +0200184
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200185 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 Vasko53b7da02018-02-13 15:28:42 +0100191 LY_CHECK_ERR_RETURN(!record->value, LOGMEM(ctx), NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200192 memcpy(record->value, value, len);
193 record->value[len] = '\0';
194 }
195 record->refcount = 1;
Radek Krejci592e00d2016-05-19 12:38:08 +0200196 if (len > DICT_REC_MAXLEN) {
197 record->len = 0;
198 } else {
199 record->len = len;
200 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200201 record->next = NULL;
Radek Krejciee627582015-04-20 17:39:59 +0200202
Radek Krejci6b0a8882016-02-16 17:37:21 +0100203 ctx->dict.used++;
204
Michal Vasko3e3228d2017-02-24 14:55:32 +0100205 LOGDBG(LY_LDGDICT, "inserting \"%s\"", record->value);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200206 return record->value;
207 }
Radek Krejciee627582015-04-20 17:39:59 +0200208
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200209 /* collision, search if the value is already in dict */
210 while (record) {
Radek Krejci592e00d2016-05-19 12:38:08 +0200211 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 Krejci6e4ffbb2015-06-16 10:34:41 +0200225 /* record found */
Radek Krejci592e00d2016-05-19 12:38:08 +0200226 if (record->refcount == DICT_REC_MAXCOUNT) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100227 LOGWRN(ctx, "Refcount overflow detected, duplicating dictionary record");
Radek Krejci592e00d2016-05-19 12:38:08 +0200228 break;
229 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200230 record->refcount++;
Radek Krejci4ea08382015-04-21 09:41:40 +0200231
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200232 if (zerocopy) {
233 free(value);
234 }
Radek Krejci4ea08382015-04-21 09:41:40 +0200235
Michal Vasko3e3228d2017-02-24 14:55:32 +0100236 LOGDBG(LY_LDGDICT, "inserting (refcount) \"%s\"", record->value);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200237 return record->value;
238 }
Radek Krejciee627582015-04-20 17:39:59 +0200239
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200240 if (!record->next) {
241 /* not present, add as a new record in chain */
242 break;
243 }
Radek Krejciee627582015-04-20 17:39:59 +0200244
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200245 record = record->next;
246 }
Radek Krejciee627582015-04-20 17:39:59 +0200247
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200248 /* create new record and add it behind the last record */
249 new = malloc(sizeof *record);
Michal Vasko53b7da02018-02-13 15:28:42 +0100250 LY_CHECK_ERR_RETURN(!new, LOGMEM(ctx), NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200251 if (zerocopy) {
252 new->value = value;
253 } else {
254 new->value = malloc((len + 1) * sizeof *record->value);
Michal Vasko53b7da02018-02-13 15:28:42 +0100255 LY_CHECK_ERR_RETURN(!new->value, LOGMEM(ctx); free(new), NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200256 memcpy(new->value, value, len);
257 new->value[len] = '\0';
258 }
259 new->refcount = 1;
Radek Krejci592e00d2016-05-19 12:38:08 +0200260 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 Krejci6e4ffbb2015-06-16 10:34:41 +0200266 record->next = new;
Radek Krejciee627582015-04-20 17:39:59 +0200267
Radek Krejci6b0a8882016-02-16 17:37:21 +0100268 ctx->dict.used++;
269
Michal Vasko3e3228d2017-02-24 14:55:32 +0100270 LOGDBG(LY_LDGDICT, "inserting \"%s\" with collision ", new->value);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200271 return new->value;
Radek Krejciee627582015-04-20 17:39:59 +0200272}
Radek Krejci4ea08382015-04-21 09:41:40 +0200273
Radek Krejcifcf37bd2015-10-22 10:27:53 +0200274API const char *
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200275lydict_insert(struct ly_ctx *ctx, const char *value, size_t len)
Radek Krejci4ea08382015-04-21 09:41:40 +0200276{
Radek Krejcifebcbfb2016-02-04 14:58:42 +0100277 const char *result;
278
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200279 if (value && !len) {
280 len = strlen(value);
281 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +0200282
Radek Krejcia5269642015-07-20 19:04:11 +0200283 if (!value) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200284 return NULL;
285 }
Radek Krejcifebcbfb2016-02-04 14:58:42 +0100286
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 Krejci4ea08382015-04-21 09:41:40 +0200292}
293
Radek Krejcifcf37bd2015-10-22 10:27:53 +0200294API const char *
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200295lydict_insert_zc(struct ly_ctx *ctx, char *value)
Radek Krejci4ea08382015-04-21 09:41:40 +0200296{
Radek Krejcifebcbfb2016-02-04 14:58:42 +0100297 const char *result;
298
Radek Krejcia5269642015-07-20 19:04:11 +0200299 if (!value) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200300 return NULL;
301 }
Radek Krejcifebcbfb2016-02-04 14:58:42 +0100302
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 Krejci4ea08382015-04-21 09:41:40 +0200308}
Michal Vasko6c810702018-03-14 16:23:21 +0100309
Michal Vasko419fce02018-03-21 11:55:09 +0100310static struct ht_rec *
311lyht_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 Vasko6c810702018-03-14 16:23:21 +0100316struct hash_table *
Michal Vasko419fce02018-03-21 11:55:09 +0100317lyht_new(uint32_t size, uint16_t val_size, values_equal_cb val_equal, void *cb_data, int resize)
Michal Vasko6c810702018-03-14 16:23:21 +0100318{
319 struct hash_table *ht;
320
321 /* check that 2^x == size (power of 2) */
Michal Vaskod0f84c32018-03-16 12:13:15 +0100322 assert(size && !(size & (size - 1)));
Michal Vasko419fce02018-03-21 11:55:09 +0100323 assert(val_equal && val_size);
Michal Vasko04210d72018-03-16 12:07:47 +0100324 assert(resize == 0 || resize == 1);
Michal Vasko6c810702018-03-14 16:23:21 +0100325
Michal Vaskod0f84c32018-03-16 12:13:15 +0100326 if (size < LYHT_MIN_SIZE) {
327 size = LYHT_MIN_SIZE;
328 }
329
Michal Vasko6c810702018-03-14 16:23:21 +0100330 ht = malloc(sizeof *ht);
331 LY_CHECK_ERR_RETURN(!ht, LOGMEM(NULL), NULL);
332
Michal Vasko6c810702018-03-14 16:23:21 +0100333 ht->used = 0;
334 ht->size = size;
335 ht->val_equal = val_equal;
336 ht->cb_data = cb_data;
Michal Vasko419fce02018-03-21 11:55:09 +0100337 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 Vasko6c810702018-03-14 16:23:21 +0100344 return ht;
345}
346
Michal Vasko8ac94532018-05-18 09:38:13 +0200347values_equal_cb
348lyht_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
357void *
358lyht_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 Vasko8690d122018-04-26 14:28:58 +0200367struct hash_table *
368lyht_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 Vasko6c810702018-03-14 16:23:21 +0100386void
387lyht_free(struct hash_table *ht)
388{
389 if (ht) {
390 free(ht->recs);
391 free(ht);
392 }
393}
394
Michal Vasko04210d72018-03-16 12:07:47 +0100395static int
396lyht_resize(struct hash_table *ht, int enlarge)
397{
Michal Vasko419fce02018-03-21 11:55:09 +0100398 struct ht_rec *rec;
399 unsigned char *old_recs;
Michal Vasko24affa02018-04-03 09:06:06 +0200400 uint32_t i, old_size;
Michal Vasko04210d72018-03-16 12:07:47 +0100401
402 old_recs = ht->recs;
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200403 old_size = ht->size;
Michal Vasko04210d72018-03-16 12:07:47 +0100404
405 if (enlarge) {
406 /* double the size */
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200407 ht->size <<= 1;
Michal Vasko04210d72018-03-16 12:07:47 +0100408 } else {
409 /* half the size */
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200410 ht->size >>= 1;
Michal Vasko04210d72018-03-16 12:07:47 +0100411 }
412
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200413 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 Vasko04210d72018-03-16 12:07:47 +0100415
416 /* reset used, it will increase again */
417 ht->used = 0;
418
419 /* add all the old records into the new records array */
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200420 for (i = 0; i < old_size; ++i) {
Michal Vasko419fce02018-03-21 11:55:09 +0100421 rec = lyht_get_rec(old_recs, ht->rec_size, i);
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200422 if (rec->hits > 0) {
Michal Vasko24affa02018-04-03 09:06:06 +0200423 lyht_insert(ht, rec->val, rec->hash);
Michal Vasko04210d72018-03-16 12:07:47 +0100424 }
425 }
426
427 /* final touches */
Michal Vasko04210d72018-03-16 12:07:47 +0100428 free(old_recs);
429 return 0;
430}
431
Michal Vasko24affa02018-04-03 09:06:06 +0200432/* return: 0 - hash found, returned its record,
433 * 1 - hash not found, returned the record where it would be inserted */
Michal Vasko419fce02018-03-21 11:55:09 +0100434static int
Michal Vasko24affa02018-04-03 09:06:06 +0200435lyht_find_first(struct hash_table *ht, uint32_t hash, struct ht_rec **rec_p)
Michal Vasko6c810702018-03-14 16:23:21 +0100436{
Michal Vasko419fce02018-03-21 11:55:09 +0100437 struct ht_rec *rec;
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200438 uint32_t i, idx;
Michal Vasko6c810702018-03-14 16:23:21 +0100439
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200440 if (rec_p) {
441 *rec_p = NULL;
442 }
443
444 idx = i = hash & (ht->size - 1);
Michal Vasko419fce02018-03-21 11:55:09 +0100445 rec = lyht_get_rec(ht->recs, ht->rec_size, idx);
Michal Vasko6c810702018-03-14 16:23:21 +0100446
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200447 /* 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 Vasko419fce02018-03-21 11:55:09 +0100455 }
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200456 if (rec->hits == 0) {
Michal Vasko419fce02018-03-21 11:55:09 +0100457 /* we could not find the value */
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200458 if (rec_p && !*rec_p) {
459 *rec_p = rec;
Michal Vasko6c810702018-03-14 16:23:21 +0100460 }
Michal Vasko419fce02018-03-21 11:55:09 +0100461 return 1;
Michal Vasko6c810702018-03-14 16:23:21 +0100462 }
463
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200464 /* we have found a record with equal hash */
465 if (rec_p) {
466 *rec_p = rec;
467 }
Michal Vasko24affa02018-04-03 09:06:06 +0200468 return 0;
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200469}
470
Michal Vasko24affa02018-04-03 09:06:06 +0200471/* return: 0 - hash collision found, returned its record,
472 * 1 - hash collision not found, returned the record where it would be inserted */
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200473static int
474lyht_find_collision(struct hash_table *ht, struct ht_rec **last)
475{
476 struct ht_rec *empty = NULL;
Michal Vasko1d89f902018-05-21 13:01:32 +0200477 uint32_t start_i, i, idx;
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200478
479 assert(last && *last);
480
481 idx = (*last)->hash & (ht->size - 1);
Michal Vasko1d89f902018-05-21 13:01:32 +0200482 start_i = i = (((unsigned char *)*last) - ht->recs) / ht->rec_size;
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200483
484 do {
485 i = (i + 1) % ht->size;
Michal Vasko1d89f902018-05-21 13:01:32 +0200486 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 Vasko7d3ad5a2018-03-27 14:22:19 +0200493 *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
512int
Michal Vaskofd5581a2018-04-25 13:20:20 +0200513lyht_find(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200514{
515 struct ht_rec *rec;
516 uint32_t i, c;
Michal Vasko24affa02018-04-03 09:06:06 +0200517 int r;
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200518
Michal Vasko24affa02018-04-03 09:06:06 +0200519 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 Vaskofd5581a2018-04-25 13:20:20 +0200525 if (match_p) {
526 *match_p = rec->val;
527 }
Michal Vasko24affa02018-04-03 09:06:06 +0200528 return 0;
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200529 }
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 Vasko24affa02018-04-03 09:06:06 +0200536 (void)r;
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200537
538 /* compare values */
Michal Vasko24affa02018-04-03 09:06:06 +0200539 if (ht->val_equal(val_p, &rec->val, 0, ht->cb_data)) {
Michal Vaskofd5581a2018-04-25 13:20:20 +0200540 if (match_p) {
541 *match_p = rec->val;
542 }
Michal Vasko419fce02018-03-21 11:55:09 +0100543 return 0;
544 }
Michal Vasko419fce02018-03-21 11:55:09 +0100545 }
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200546
547 /* not found even in collisions */
548 return 1;
Michal Vasko419fce02018-03-21 11:55:09 +0100549}
550
551int
Michal Vaskofd5581a2018-04-25 13:20:20 +0200552lyht_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 Vasko66342942018-04-27 13:56:21 +0200563 if (ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
Michal Vaskofd5581a2018-04-25 13:20:20 +0200564 /* 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 Vasko66342942018-04-27 13:56:21 +0200595 if (!ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
Michal Vaskofd5581a2018-04-25 13:20:20 +0200596 /* 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
609int
Michal Vasko419fce02018-03-21 11:55:09 +0100610lyht_insert(struct hash_table *ht, void *val_p, uint32_t hash)
611{
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200612 struct ht_rec *rec, *crec = NULL;
613 int32_t i;
Michal Vasko24affa02018-04-03 09:06:06 +0200614 int r, ret;
Michal Vasko419fce02018-03-21 11:55:09 +0100615
Michal Vasko24affa02018-04-03 09:06:06 +0200616 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 Vasko419fce02018-03-21 11:55:09 +0100622
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200623 /* 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 Vasko24affa02018-04-03 09:06:06 +0200630 if (ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200631 return 1;
632 }
Michal Vasko419fce02018-03-21 11:55:09 +0100633 }
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200634
635 /* value not found, get the record where it will be inserted */
636 r = lyht_find_collision(ht, &rec);
637 assert(r);
Michal Vasko419fce02018-03-21 11:55:09 +0100638 }
639
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200640 /* insert it into the returned record */
641 assert(rec->hits < 1);
Michal Vasko419fce02018-03-21 11:55:09 +0100642 rec->hash = hash;
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200643 rec->hits = 1;
Michal Vasko419fce02018-03-21 11:55:09 +0100644 memcpy(&rec->val, val_p, ht->rec_size - (sizeof(struct ht_rec) - 1));
645
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200646 if (crec) {
647 /* there was a collision, increase hits */
Michal Vaskoe330d712018-04-25 13:20:43 +0200648 if (crec->hits == INT32_MAX) {
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200649 LOGINT(NULL);
650 }
651 ++crec->hits;
652 }
653
Michal Vasko419fce02018-03-21 11:55:09 +0100654 /* check size & enlarge if needed */
655 ret = 0;
Michal Vasko6c810702018-03-14 16:23:21 +0100656 ++ht->used;
Michal Vasko04210d72018-03-16 12:07:47 +0100657 if (ht->resize) {
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200658 r = (ht->used * 100) / ht->size;
659 if ((ht->resize == 1) && (r >= LYHT_FIRST_SHRINK_PERCENTAGE)) {
Michal Vasko04210d72018-03-16 12:07:47 +0100660 /* enable shrinking */
661 ht->resize = 2;
Michal Vasko419fce02018-03-21 11:55:09 +0100662 }
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200663 if ((ht->resize == 2) && (r >= LYHT_ENLARGE_PERCENTAGE)) {
Michal Vasko04210d72018-03-16 12:07:47 +0100664 /* enlarge */
665 ret = lyht_resize(ht, 1);
666 }
667 }
668 return ret;
Michal Vasko6c810702018-03-14 16:23:21 +0100669}
670
671int
Michal Vasko419fce02018-03-21 11:55:09 +0100672lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash)
Michal Vasko6c810702018-03-14 16:23:21 +0100673{
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200674 struct ht_rec *rec, *crec;
675 int32_t i;
Michal Vasko24affa02018-04-03 09:06:06 +0200676 int first_matched = 0, r, ret;
Michal Vasko6c810702018-03-14 16:23:21 +0100677
Michal Vasko24affa02018-04-03 09:06:06 +0200678 if (lyht_find_first(ht, hash, &rec)) {
679 /* hash not found */
Michal Vasko6c810702018-03-14 16:23:21 +0100680 return 1;
681 }
Michal Vasko24affa02018-04-03 09:06:06 +0200682 if (ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
683 /* even the value matches */
684 first_matched = 1;
685 }
Michal Vasko6c810702018-03-14 16:23:21 +0100686
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200687 /* 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 Vasko419fce02018-03-21 11:55:09 +0100692
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200693 /* compare values */
Michal Vasko24affa02018-04-03 09:06:06 +0200694 if (!first_matched && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200695 break;
696 }
Michal Vasko419fce02018-03-21 11:55:09 +0100697 }
698
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200699 if (i < crec->hits) {
700 /* one of collisions matched, reduce collision count, remove the record */
Michal Vasko24affa02018-04-03 09:06:06 +0200701 assert(!first_matched);
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200702 --crec->hits;
703 rec->hits = -1;
Michal Vasko24affa02018-04-03 09:06:06 +0200704 } else if (first_matched) {
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200705 /* 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 Vasko24affa02018-04-03 09:06:06 +0200714 assert(!first_matched);
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200715 return 1;
716 }
717
Michal Vasko419fce02018-03-21 11:55:09 +0100718 /* check size & shrink if needed */
719 ret = 0;
720 --ht->used;
721 if (ht->resize == 2) {
Michal Vasko7d3ad5a2018-03-27 14:22:19 +0200722 r = (ht->used * 100) / ht->size;
723 if ((r < LYHT_SHRINK_PERCENTAGE) && (ht->size > LYHT_MIN_SIZE)) {
Michal Vasko419fce02018-03-21 11:55:09 +0100724 /* shrink */
725 ret = lyht_resize(ht, 0);
Michal Vasko6c810702018-03-14 16:23:21 +0100726 }
Michal Vasko419fce02018-03-21 11:55:09 +0100727 }
Michal Vasko6c810702018-03-14 16:23:21 +0100728
Michal Vasko419fce02018-03-21 11:55:09 +0100729 return ret;
Michal Vasko6c810702018-03-14 16:23:21 +0100730}