[f85546d] | 1 | /*
|
---|
| 2 | * Copyright (c) 2019 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[fe656783] | 29 | /** @addtogroup hbench
|
---|
[f85546d] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <adt/hash_table.h>
|
---|
| 37 | #include <stdlib.h>
|
---|
| 38 | #include <stdio.h>
|
---|
| 39 | #include <str.h>
|
---|
[d926f42] | 40 | #include "hbench.h"
|
---|
[f85546d] | 41 |
|
---|
| 42 | typedef struct {
|
---|
| 43 | ht_link_t link;
|
---|
| 44 |
|
---|
| 45 | char *key;
|
---|
| 46 | char *value;
|
---|
| 47 | } param_t;
|
---|
| 48 |
|
---|
| 49 | static size_t param_hash(const ht_link_t *item)
|
---|
| 50 | {
|
---|
| 51 | param_t *param = hash_table_get_inst(item, param_t, link);
|
---|
| 52 | return str_size(param->key);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | static size_t param_key_hash(void *key)
|
---|
| 56 | {
|
---|
| 57 | char *key_str = key;
|
---|
| 58 | return str_size(key_str);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | static bool param_key_equal(void *key, const ht_link_t *item)
|
---|
| 62 | {
|
---|
| 63 | param_t *param = hash_table_get_inst(item, param_t, link);
|
---|
| 64 | char *key_str = key;
|
---|
| 65 |
|
---|
| 66 | return str_cmp(param->key, key_str) == 0;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | static bool param_equal(const ht_link_t *link_a, const ht_link_t *link_b)
|
---|
| 70 | {
|
---|
| 71 | param_t *a = hash_table_get_inst(link_a, param_t, link);
|
---|
| 72 | param_t *b = hash_table_get_inst(link_b, param_t, link);
|
---|
| 73 |
|
---|
| 74 | return str_cmp(a->key, b->key) == 0;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | static void param_remove(ht_link_t *item)
|
---|
| 78 | {
|
---|
| 79 | param_t *param = hash_table_get_inst(item, param_t, link);
|
---|
| 80 | free(param->key);
|
---|
| 81 | free(param->value);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | static hash_table_ops_t param_hash_table_ops = {
|
---|
| 85 | .hash = param_hash,
|
---|
| 86 | .key_hash = param_key_hash,
|
---|
| 87 | .key_equal = param_key_equal,
|
---|
| 88 | .equal = param_equal,
|
---|
| 89 | .remove_callback = param_remove
|
---|
| 90 | };
|
---|
| 91 |
|
---|
| 92 | /** Table of extra parameters (of param_t). */
|
---|
| 93 | static hash_table_t param_hash_table;
|
---|
| 94 |
|
---|
| 95 | extern errno_t bench_param_init(void)
|
---|
| 96 | {
|
---|
| 97 | bool ok = hash_table_create(¶m_hash_table, 0, 0, ¶m_hash_table_ops);
|
---|
| 98 | if (!ok) {
|
---|
| 99 | return ENOMEM;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | return EOK;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | extern void bench_param_cleanup(void)
|
---|
| 106 | {
|
---|
| 107 | hash_table_destroy(¶m_hash_table);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | errno_t bench_param_set(const char *key, const char *value)
|
---|
| 111 | {
|
---|
| 112 | param_t *param = malloc(sizeof(param_t));
|
---|
| 113 | if (param == NULL) {
|
---|
| 114 | return ENOMEM;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | param->key = str_dup(key);
|
---|
| 118 | param->value = str_dup(value);
|
---|
| 119 |
|
---|
| 120 | if ((param->key == NULL) || (param->value == NULL)) {
|
---|
| 121 | free(param->key);
|
---|
| 122 | free(param->value);
|
---|
| 123 | free(param);
|
---|
| 124 |
|
---|
| 125 | return ENOMEM;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | hash_table_insert(¶m_hash_table, ¶m->link);
|
---|
| 129 |
|
---|
| 130 | return EOK;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | const char *bench_param_get(const char *key, const char *default_value)
|
---|
| 134 | {
|
---|
| 135 | ht_link_t *item = hash_table_find(¶m_hash_table, (char *) key);
|
---|
| 136 |
|
---|
| 137 | if (item == NULL) {
|
---|
| 138 | return default_value;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | param_t *param = hash_table_get_inst(item, param_t, link);
|
---|
| 142 | return param->value;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /** @}
|
---|
| 146 | */
|
---|