source: mainline/uspace/app/trace/proto.c@ 0ca7286

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0ca7286 was 0ca7286, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

Added resizing to user space (single-threaded) hash_table. Resizes in a way to mitigate effects of bad hash functions. Change of interface affected many files.

  • Property mode set to 100644
File size: 4.9 KB
Line 
1/*
2 * Copyright (c) 2008 Jiri Svoboda
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 trace
30 * @{
31 */
32/** @file
33 */
34
35#include <stdio.h>
36#include <stdlib.h>
37#include <adt/hash_table.h>
38
39#include "trace.h"
40#include "proto.h"
41
42
43/* Maps service number to protocol */
44static hash_table_t srv_proto;
45
46typedef struct {
47 unsigned srv;
48 proto_t *proto;
49 link_t link;
50} srv_proto_t;
51
52typedef struct {
53 sysarg_t method;
54 oper_t *oper;
55 link_t link;
56} method_oper_t;
57
58
59
60
61static size_t srv_proto_key_hash(unsigned long key[])
62{
63 return key[0];
64}
65
66static size_t srv_proto_hash(const link_t *item)
67{
68 srv_proto_t *sp = hash_table_get_instance(item, srv_proto_t, link);
69 unsigned long key = sp->srv;
70 return srv_proto_key_hash(&key);
71}
72
73static bool srv_proto_match(unsigned long key[], size_t keys, const link_t *item)
74{
75 srv_proto_t *sp = hash_table_get_instance(item, srv_proto_t, link);
76
77 return key[0] == sp->srv;
78}
79
80static hash_table_ops_t srv_proto_ops = {
81 .hash = srv_proto_hash,
82 .key_hash = srv_proto_key_hash,
83 .match = srv_proto_match,
84 .equal = 0,
85 .remove_callback = 0
86};
87
88
89static size_t method_oper_key_hash(unsigned long key[])
90{
91 return key[0];
92}
93
94static size_t method_oper_hash(const link_t *item)
95{
96 method_oper_t *mo = hash_table_get_instance(item, method_oper_t, link);
97 unsigned long key = mo->method;
98 return method_oper_key_hash(&key);
99}
100
101static bool method_oper_match(unsigned long key[], size_t keys,
102 const link_t *item)
103{
104 method_oper_t *mo = hash_table_get_instance(item, method_oper_t, link);
105
106 return key[0] == mo->method;
107}
108
109static hash_table_ops_t method_oper_ops = {
110 .hash = method_oper_hash,
111 .key_hash = method_oper_key_hash,
112 .match = method_oper_match,
113 .equal = 0,
114 .remove_callback = 0
115};
116
117
118void proto_init(void)
119{
120 hash_table_create(&srv_proto, 0, 1, &srv_proto_ops);
121}
122
123void proto_cleanup(void)
124{
125 hash_table_destroy(&srv_proto);
126}
127
128void proto_register(int srv, proto_t *proto)
129{
130 srv_proto_t *sp;
131
132 sp = malloc(sizeof(srv_proto_t));
133 sp->srv = srv;
134 sp->proto = proto;
135
136 hash_table_insert(&srv_proto, &sp->link);
137}
138
139proto_t *proto_get_by_srv(int srv)
140{
141 link_t *item;
142 srv_proto_t *sp;
143
144 unsigned long key = srv;
145 item = hash_table_find(&srv_proto, &key);
146 if (item == NULL) return NULL;
147
148 sp = hash_table_get_instance(item, srv_proto_t, link);
149 return sp->proto;
150}
151
152static void proto_struct_init(proto_t *proto, const char *name)
153{
154 proto->name = name;
155 hash_table_create(&proto->method_oper, 0, 1, &method_oper_ops);
156}
157
158proto_t *proto_new(const char *name)
159{
160 proto_t *p;
161
162 p = malloc(sizeof(proto_t));
163 proto_struct_init(p, name);
164
165 return p;
166}
167
168void proto_delete(proto_t *proto)
169{
170 free(proto);
171}
172
173void proto_add_oper(proto_t *proto, int method, oper_t *oper)
174{
175 method_oper_t *mo;
176
177 mo = malloc(sizeof(method_oper_t));
178 mo->method = method;
179 mo->oper = oper;
180
181 hash_table_insert(&proto->method_oper, &mo->link);
182}
183
184oper_t *proto_get_oper(proto_t *proto, int method)
185{
186 unsigned long key;
187 link_t *item;
188 method_oper_t *mo;
189
190 key = method;
191 item = hash_table_find(&proto->method_oper, &key);
192 if (item == NULL) return NULL;
193
194 mo = hash_table_get_instance(item, method_oper_t, link);
195 return mo->oper;
196}
197
198static void oper_struct_init(oper_t *oper, const char *name)
199{
200 oper->name = name;
201}
202
203oper_t *oper_new(const char *name, int argc, val_type_t *arg_types,
204 val_type_t rv_type, int respc, val_type_t *resp_types)
205{
206 oper_t *o;
207 int i;
208
209 o = malloc(sizeof(oper_t));
210 oper_struct_init(o, name);
211
212 o->argc = argc;
213 for (i = 0; i < argc; i++)
214 o->arg_type[i] = arg_types[i];
215
216 o->rv_type = rv_type;
217
218 o->respc = respc;
219 for (i = 0; i < respc; i++)
220 o->resp_type[i] = resp_types[i];
221
222 return o;
223}
224
225/** @}
226 */
Note: See TracBrowser for help on using the repository browser.