source: mainline/uspace/app/hbench/env.c@ b433f68

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b433f68 was 5e801dc, checked in by GitHub <noreply@…>, 6 years ago

Indicate and enforce constness of hash table key in certain functions (#158)

The assumption here is that modifying key in the hash/equal functions in something completely unexpected, and not something you would ever want to do intentionally, so it makes sense to disallow it entirely to get that extra level of checking.

  • Property mode set to 100644
File size: 3.7 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(const void *key)
55{
56 const char *key_str = key;
57 return str_size(key_str);
58}
59
60static bool param_key_equal(const void *key, const ht_link_t *item)
61{
62 param_t *param = hash_table_get_inst(item, param_t, link);
63 const 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
91errno_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 env->run_count = DEFAULT_RUN_COUNT;
99 env->minimal_run_duration_nanos = MSEC2NSEC(DEFAULT_MIN_RUN_DURATION_SEC);
100
101 return EOK;
102}
103
104void bench_env_cleanup(bench_env_t *env)
105{
106 hash_table_destroy(&env->parameters);
107}
108
109errno_t bench_env_param_set(bench_env_t *env, const char *key, const char *value)
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
127 hash_table_insert(&env->parameters, &param->link);
128
129 return EOK;
130}
131
132const char *bench_env_param_get(bench_env_t *env, const char *key, const char *default_value)
133{
134 ht_link_t *item = hash_table_find(&env->parameters, (char *) key);
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 */
Note: See TracBrowser for help on using the repository browser.