source: mainline/uspace/app/hbench/env.c@ 2d81880

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2d81880 was d17cf8c, checked in by Vojtech Horky <vojtech.horky@…>, 7 years ago

hbench: remove global state

Move benchmark parameters into a benchmark environment structure that is
passed to each benchmark.

  • Property mode set to 100644
File size: 3.6 KB
Line 
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
29/** @addtogroup hbench
30 * @{
31 */
32/**
33 * @file
34 */
35
36#include <stdlib.h>
37#include <stdio.h>
38#include <str.h>
39#include "hbench.h"
40
41typedef struct {
42 ht_link_t link;
43
44 char *key;
45 char *value;
46} param_t;
47
48static 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
54static size_t param_key_hash(void *key)
55{
56 char *key_str = key;
57 return str_size(key_str);
58}
59
60static 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
68static 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
76static 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
83static 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
91extern errno_t bench_env_init(bench_env_t *env)
92{
93 bool ok = hash_table_create(&env->parameters, 0, 0, &param_hash_table_ops);
94 if (!ok) {
95 return ENOMEM;
96 }
97
98 return EOK;
99}
100
101extern void bench_env_cleanup(bench_env_t *env)
102{
103 hash_table_destroy(&env->parameters);
104}
105
106errno_t bench_env_param_set(bench_env_t *env, const char *key, const char *value)
107{
108 param_t *param = malloc(sizeof(param_t));
109 if (param == NULL) {
110 return ENOMEM;
111 }
112
113 param->key = str_dup(key);
114 param->value = str_dup(value);
115
116 if ((param->key == NULL) || (param->value == NULL)) {
117 free(param->key);
118 free(param->value);
119 free(param);
120
121 return ENOMEM;
122 }
123
124 hash_table_insert(&env->parameters, &param->link);
125
126 return EOK;
127}
128
129const char *bench_env_param_get(bench_env_t *env, const char *key, const char *default_value)
130{
131 ht_link_t *item = hash_table_find(&env->parameters, (char *) key);
132
133 if (item == NULL) {
134 return default_value;
135 }
136
137 param_t *param = hash_table_get_inst(item, param_t, link);
138 return param->value;
139}
140
141/** @}
142 */
Note: See TracBrowser for help on using the repository browser.