[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 |
|
---|
[0db0df2] | 36 | #include <adt/hash.h>
|
---|
[f85546d] | 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 |
|
---|
[0db0df2] | 45 | size_t hash;
|
---|
[f85546d] | 46 | char *key;
|
---|
| 47 | char *value;
|
---|
| 48 | } param_t;
|
---|
| 49 |
|
---|
| 50 | static size_t param_hash(const ht_link_t *item)
|
---|
| 51 | {
|
---|
| 52 | param_t *param = hash_table_get_inst(item, param_t, link);
|
---|
[0db0df2] | 53 | return param->hash;
|
---|
[f85546d] | 54 | }
|
---|
| 55 |
|
---|
[5e801dc] | 56 | static size_t param_key_hash(const void *key)
|
---|
[f85546d] | 57 | {
|
---|
[0db0df2] | 58 | return hash_string(key);
|
---|
[f85546d] | 59 | }
|
---|
| 60 |
|
---|
[0db0df2] | 61 | static bool param_key_equal(const void *key, size_t hash, const ht_link_t *item)
|
---|
[f85546d] | 62 | {
|
---|
| 63 | param_t *param = hash_table_get_inst(item, param_t, link);
|
---|
| 64 |
|
---|
[0db0df2] | 65 | if (param->hash != hash)
|
---|
| 66 | return false;
|
---|
| 67 |
|
---|
| 68 | const char *key_str = key;
|
---|
[f85546d] | 69 | return str_cmp(param->key, key_str) == 0;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | static bool param_equal(const ht_link_t *link_a, const ht_link_t *link_b)
|
---|
| 73 | {
|
---|
| 74 | param_t *a = hash_table_get_inst(link_a, param_t, link);
|
---|
| 75 | param_t *b = hash_table_get_inst(link_b, param_t, link);
|
---|
| 76 |
|
---|
[0db0df2] | 77 | return a->hash == b->hash && str_cmp(a->key, b->key) == 0;
|
---|
[f85546d] | 78 | }
|
---|
| 79 |
|
---|
| 80 | static void param_remove(ht_link_t *item)
|
---|
| 81 | {
|
---|
| 82 | param_t *param = hash_table_get_inst(item, param_t, link);
|
---|
| 83 | free(param->key);
|
---|
| 84 | free(param->value);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[61eb2ce2] | 87 | static const hash_table_ops_t param_hash_table_ops = {
|
---|
[f85546d] | 88 | .hash = param_hash,
|
---|
| 89 | .key_hash = param_key_hash,
|
---|
| 90 | .key_equal = param_key_equal,
|
---|
| 91 | .equal = param_equal,
|
---|
| 92 | .remove_callback = param_remove
|
---|
| 93 | };
|
---|
| 94 |
|
---|
[8d2289c] | 95 | errno_t bench_env_init(bench_env_t *env)
|
---|
[f85546d] | 96 | {
|
---|
[d17cf8c] | 97 | bool ok = hash_table_create(&env->parameters, 0, 0, ¶m_hash_table_ops);
|
---|
[f85546d] | 98 | if (!ok) {
|
---|
| 99 | return ENOMEM;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[871cff9a] | 102 | env->run_count = DEFAULT_RUN_COUNT;
|
---|
[68655bc2] | 103 | env->minimal_run_duration_nanos = MSEC2NSEC(DEFAULT_MIN_RUN_DURATION_MSEC);
|
---|
[871cff9a] | 104 |
|
---|
[f85546d] | 105 | return EOK;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[8d2289c] | 108 | void bench_env_cleanup(bench_env_t *env)
|
---|
[f85546d] | 109 | {
|
---|
[d17cf8c] | 110 | hash_table_destroy(&env->parameters);
|
---|
[f85546d] | 111 | }
|
---|
| 112 |
|
---|
[d17cf8c] | 113 | errno_t bench_env_param_set(bench_env_t *env, const char *key, const char *value)
|
---|
[f85546d] | 114 | {
|
---|
| 115 | param_t *param = malloc(sizeof(param_t));
|
---|
| 116 | if (param == NULL) {
|
---|
| 117 | return ENOMEM;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | param->key = str_dup(key);
|
---|
| 121 | param->value = str_dup(value);
|
---|
[0db0df2] | 122 | param->hash = hash_string(key);
|
---|
[f85546d] | 123 |
|
---|
| 124 | if ((param->key == NULL) || (param->value == NULL)) {
|
---|
| 125 | free(param->key);
|
---|
| 126 | free(param->value);
|
---|
| 127 | free(param);
|
---|
| 128 |
|
---|
| 129 | return ENOMEM;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[d17cf8c] | 132 | hash_table_insert(&env->parameters, ¶m->link);
|
---|
[f85546d] | 133 |
|
---|
| 134 | return EOK;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[d17cf8c] | 137 | const char *bench_env_param_get(bench_env_t *env, const char *key, const char *default_value)
|
---|
[f85546d] | 138 | {
|
---|
[0db0df2] | 139 | ht_link_t *item = hash_table_find(&env->parameters, key);
|
---|
[f85546d] | 140 |
|
---|
| 141 | if (item == NULL) {
|
---|
| 142 | return default_value;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | param_t *param = hash_table_get_inst(item, param_t, link);
|
---|
| 146 | return param->value;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /** @}
|
---|
| 150 | */
|
---|