1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
15 #include <ecoli_init.h>
16 #include <ecoli_malloc.h>
17 #include <ecoli_log.h>
18 #include <ecoli_test.h>
19 #include <ecoli_murmurhash.h>
20 #include <ecoli_dict.h>
24 EC_LOG_TYPE_REGISTER(dict);
26 static uint32_t ec_dict_seed;
32 ec_dict_elt_free_t free;
33 unsigned int refcount;
36 struct ec_dict_elt_ref {
37 TAILQ_ENTRY(ec_dict_elt_ref) next;
38 TAILQ_ENTRY(ec_dict_elt_ref) hnext;
39 struct ec_dict_elt *elt;
42 TAILQ_HEAD(ec_dict_elt_ref_list, ec_dict_elt_ref);
47 struct ec_dict_elt_ref_list list;
48 struct ec_dict_elt_ref_list *table;
51 struct ec_dict *ec_dict(void)
55 dict = ec_calloc(1, sizeof(*dict));
58 TAILQ_INIT(&dict->list);
63 static struct ec_dict_elt_ref *
64 ec_dict_lookup(const struct ec_dict *dict, const char *key)
66 struct ec_dict_elt_ref *ref;
67 uint32_t h, mask = dict->table_size - 1;
69 if (dict == NULL || key == NULL) {
73 if (dict->table_size == 0) {
78 h = ec_murmurhash3(key, strlen(key), ec_dict_seed);
79 TAILQ_FOREACH(ref, &dict->table[h & mask], hnext) {
80 if (strcmp(ref->elt->key, key) == 0)
88 static void ec_dict_elt_ref_free(struct ec_dict_elt_ref *ref)
90 struct ec_dict_elt *elt;
96 if (elt != NULL && --elt->refcount == 0) {
98 if (elt->free != NULL)
105 bool ec_dict_has_key(const struct ec_dict *dict, const char *key)
107 return !!ec_dict_lookup(dict, key);
110 void *ec_dict_get(const struct ec_dict *dict, const char *key)
112 struct ec_dict_elt_ref *ref;
114 ref = ec_dict_lookup(dict, key);
118 return ref->elt->val;
121 int ec_dict_del(struct ec_dict *dict, const char *key)
123 struct ec_dict_elt_ref *ref;
126 ref = ec_dict_lookup(dict, key);
130 /* we could resize table here */
132 TAILQ_REMOVE(&dict->list, ref, next);
133 idx = ref->elt->hash & (dict->table_size - 1);
134 TAILQ_REMOVE(&dict->table[idx], ref, hnext);
135 ec_dict_elt_ref_free(ref);
141 static int ec_dict_table_resize(struct ec_dict *dict, size_t new_size)
143 struct ec_dict_elt_ref_list *new_table;
144 struct ec_dict_elt_ref *ref;
147 if (new_size == 0 || (new_size & (new_size - 1))) {
152 new_table = ec_calloc(new_size, sizeof(*dict->table));
153 if (new_table == NULL)
155 for (i = 0; i < new_size; i++)
156 TAILQ_INIT(&new_table[i]);
158 TAILQ_FOREACH(ref, &dict->list, next) {
159 i = ref->elt->hash & (dict->table_size - 1);
160 TAILQ_REMOVE(&dict->table[i], ref, hnext);
161 i = ref->elt->hash & (new_size - 1);
162 TAILQ_INSERT_TAIL(&new_table[i], ref, hnext);
165 ec_free(dict->table);
166 dict->table = new_table;
167 dict->table_size = new_size;
173 __ec_dict_set(struct ec_dict *dict, struct ec_dict_elt_ref *ref)
179 /* remove previous entry if any */
180 ec_dict_del(dict, ref->elt->key);
182 if (dict->len >= dict->table_size) {
183 if (dict->table_size != 0)
184 new_size = dict->table_size << FACTOR;
186 new_size = 1 << FACTOR;
187 ret = ec_dict_table_resize(dict, new_size);
192 mask = dict->table_size - 1;
193 TAILQ_INSERT_TAIL(&dict->table[ref->elt->hash & mask], ref, hnext);
194 TAILQ_INSERT_TAIL(&dict->list, ref, next);
200 int ec_dict_set(struct ec_dict *dict, const char *key, void *val,
201 ec_dict_elt_free_t free_cb)
203 struct ec_dict_elt *elt = NULL;
204 struct ec_dict_elt_ref *ref = NULL;
207 if (dict == NULL || key == NULL) {
212 ref = ec_calloc(1, sizeof(*ref));
216 elt = ec_calloc(1, sizeof(*elt));
225 elt->key = ec_strdup(key);
226 if (elt->key == NULL)
228 h = ec_murmurhash3(key, strlen(key), ec_dict_seed);
231 if (__ec_dict_set(dict, ref) < 0)
237 if (free_cb != NULL && val != NULL)
239 ec_dict_elt_ref_free(ref);
243 void ec_dict_free(struct ec_dict *dict)
245 struct ec_dict_elt_ref *ref;
251 while (!TAILQ_EMPTY(&dict->list)) {
252 ref = TAILQ_FIRST(&dict->list);
253 TAILQ_REMOVE(&dict->list, ref, next);
254 idx = ref->elt->hash & (dict->table_size - 1);
255 TAILQ_REMOVE(&dict->table[idx], ref, hnext);
256 ec_dict_elt_ref_free(ref);
258 ec_free(dict->table);
262 size_t ec_dict_len(const struct ec_dict *dict)
267 struct ec_dict_elt_ref *
268 ec_dict_iter(const struct ec_dict *dict)
273 return TAILQ_FIRST(&dict->list);
276 struct ec_dict_elt_ref *
277 ec_dict_iter_next(struct ec_dict_elt_ref *iter)
282 return TAILQ_NEXT(iter, next);
286 ec_dict_iter_get_key(const struct ec_dict_elt_ref *iter)
291 return iter->elt->key;
295 ec_dict_iter_get_val(const struct ec_dict_elt_ref *iter)
300 return iter->elt->val;
303 void ec_dict_dump(FILE *out, const struct ec_dict *dict)
305 struct ec_dict_elt_ref *iter;
308 fprintf(out, "empty dict\n");
312 fprintf(out, "dict:\n");
313 for (iter = ec_dict_iter(dict);
315 iter = ec_dict_iter_next(iter)) {
316 fprintf(out, " %s: %p\n",
317 ec_dict_iter_get_key(iter),
318 ec_dict_iter_get_val(iter));
322 struct ec_dict *ec_dict_dup(const struct ec_dict *dict)
324 struct ec_dict *dup = NULL;
325 struct ec_dict_elt_ref *ref, *dup_ref = NULL;
331 TAILQ_FOREACH(ref, &dict->list, next) {
332 dup_ref = ec_calloc(1, sizeof(*ref));
335 dup_ref->elt = ref->elt;
336 ref->elt->refcount++;
338 if (__ec_dict_set(dup, dup_ref) < 0)
345 ec_dict_elt_ref_free(dup_ref);
350 static int ec_dict_init_func(void)
355 return 0;//XXX for test reproduceability
357 fd = open("/dev/urandom", 0);
359 fprintf(stderr, "failed to open /dev/urandom\n");
362 ret = read(fd, &ec_dict_seed, sizeof(ec_dict_seed));
363 if (ret != sizeof(ec_dict_seed)) {
364 fprintf(stderr, "failed to read /dev/urandom\n");
371 static struct ec_init ec_dict_init = {
372 .init = ec_dict_init_func,
376 EC_INIT_REGISTER(ec_dict_init);
378 /* LCOV_EXCL_START */
379 static int ec_dict_testcase(void)
381 struct ec_dict *dict, *dup;
382 struct ec_dict_elt_ref *iter;
385 int ret, testres = 0;
392 EC_LOG(EC_LOG_ERR, "cannot create dict\n");
397 for (iter = ec_dict_iter(dict);
399 iter = ec_dict_iter_next(iter)) {
402 testres |= EC_TEST_CHECK(count == 0, "invalid count in iterator");
404 testres |= EC_TEST_CHECK(ec_dict_len(dict) == 0, "bad dict len");
405 ret = ec_dict_set(dict, "key1", "val1", NULL);
406 testres |= EC_TEST_CHECK(ret == 0, "cannot set key");
407 ret = ec_dict_set(dict, "key2", ec_strdup("val2"), ec_free_func);
408 testres |= EC_TEST_CHECK(ret == 0, "cannot set key");
409 testres |= EC_TEST_CHECK(ec_dict_len(dict) == 2, "bad dict len");
411 val = ec_dict_get(dict, "key1");
412 testres |= EC_TEST_CHECK(val != NULL && !strcmp(val, "val1"),
413 "invalid dict value");
414 val = ec_dict_get(dict, "key2");
415 testres |= EC_TEST_CHECK(val != NULL && !strcmp(val, "val2"),
416 "invalid dict value");
417 val = ec_dict_get(dict, "key3");
418 testres |= EC_TEST_CHECK(val == NULL, "key3 should be NULL");
420 ret = ec_dict_set(dict, "key1", "another_val1", NULL);
421 testres |= EC_TEST_CHECK(ret == 0, "cannot set key");
422 ret = ec_dict_set(dict, "key2", ec_strdup("another_val2"),
424 testres |= EC_TEST_CHECK(ret == 0, "cannot set key");
425 testres |= EC_TEST_CHECK(ec_dict_len(dict) == 2,
428 val = ec_dict_get(dict, "key1");
429 testres |= EC_TEST_CHECK(val != NULL && !strcmp(val, "another_val1"),
430 "invalid dict value");
431 val = ec_dict_get(dict, "key2");
432 testres |= EC_TEST_CHECK(val != NULL && !strcmp(val, "another_val2"),
433 "invalid dict value");
434 testres |= EC_TEST_CHECK(ec_dict_has_key(dict, "key1"),
435 "key1 should be in dict");
437 f = open_memstream(&buf, &buflen);
440 ec_dict_dump(f, NULL);
446 f = open_memstream(&buf, &buflen);
449 ec_dict_dump(f, dict);
455 ret = ec_dict_del(dict, "key1");
456 testres |= EC_TEST_CHECK(ret == 0, "cannot del key1");
457 testres |= EC_TEST_CHECK(ec_dict_len(dict) == 1,
459 ret = ec_dict_del(dict, "key2");
460 testres |= EC_TEST_CHECK(ret == 0, "cannot del key2");
461 testres |= EC_TEST_CHECK(ec_dict_len(dict) == 0,
464 for (i = 0; i < 100; i++) {
466 snprintf(key, sizeof(key), "k%zd", i);
467 ret = ec_dict_set(dict, key, "val", NULL);
468 testres |= EC_TEST_CHECK(ret == 0, "cannot set key");
470 dup = ec_dict_dup(dict);
471 testres |= EC_TEST_CHECK(dup != NULL, "cannot duplicate dict");
473 for (i = 0; i < 100; i++) {
475 snprintf(key, sizeof(key), "k%zd", i);
476 val = ec_dict_get(dup, key);
477 testres |= EC_TEST_CHECK(
478 val != NULL && !strcmp(val, "val"),
479 "invalid dict value");
486 for (iter = ec_dict_iter(dict);
488 iter = ec_dict_iter_next(iter)) {
491 testres |= EC_TEST_CHECK(count == 100, "invalid count in iterator");
494 ret = ec_dict_set(dict, NULL, "val1", NULL);
495 testres |= EC_TEST_CHECK(ret == -1, "should not be able to set key");
496 val = ec_dict_get(dict, NULL);
497 testres |= EC_TEST_CHECK(val == NULL, "get(NULL) should no success");
512 static struct ec_test ec_dict_test = {
514 .test = ec_dict_testcase,
517 EC_TEST_REGISTER(ec_dict_test);