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