source: mainline/uspace/app/trace/proto.c@ 8fefd8b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8fefd8b was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • 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/* Maps service number to protocol */
43static hash_table_t srv_proto;
44
45typedef struct {
46 int srv;
47 proto_t *proto;
48 ht_link_t link;
49} srv_proto_t;
50
51typedef struct {
52 int method;
53 oper_t *oper;
54 ht_link_t link;
55} method_oper_t;
56
57/* Hash table operations. */
58
59static size_t srv_proto_key_hash(void *key)
60{
61 return *(int *)key;
62}
63
64static size_t srv_proto_hash(const ht_link_t *item)
65{
66 srv_proto_t *sp = hash_table_get_inst(item, srv_proto_t, link);
67 return sp->srv;
68}
69
70static bool srv_proto_key_equal(void *key, const ht_link_t *item)
71{
72 srv_proto_t *sp = hash_table_get_inst(item, srv_proto_t, link);
73 return sp->srv == *(int *)key;
74}
75
76static hash_table_ops_t srv_proto_ops = {
77 .hash = srv_proto_hash,
78 .key_hash = srv_proto_key_hash,
79 .key_equal = srv_proto_key_equal,
80 .equal = NULL,
81 .remove_callback = NULL
82};
83
84static size_t method_oper_key_hash(void *key)
85{
86 return *(int *)key;
87}
88
89static size_t method_oper_hash(const ht_link_t *item)
90{
91 method_oper_t *mo = hash_table_get_inst(item, method_oper_t, link);
92 return mo->method;
93}
94
95static bool method_oper_key_equal(void *key, const ht_link_t *item)
96{
97 method_oper_t *mo = hash_table_get_inst(item, method_oper_t, link);
98 return mo->method == *(int *)key;
99}
100
101static hash_table_ops_t method_oper_ops = {
102 .hash = method_oper_hash,
103 .key_hash = method_oper_key_hash,
104 .key_equal = method_oper_key_equal,
105 .equal = NULL,
106 .remove_callback = NULL
107};
108
109void proto_init(void)
110{
111 /* todo: check return value. */
112 bool ok = hash_table_create(&srv_proto, 0, 0, &srv_proto_ops);
113 assert(ok);
114}
115
116void proto_cleanup(void)
117{
118 hash_table_destroy(&srv_proto);
119}
120
121void proto_register(int srv, proto_t *proto)
122{
123 srv_proto_t *sp;
124
125 sp = malloc(sizeof(srv_proto_t));
126 sp->srv = srv;
127 sp->proto = proto;
128
129 hash_table_insert(&srv_proto, &sp->link);
130}
131
132proto_t *proto_get_by_srv(int srv)
133{
134 ht_link_t *item = hash_table_find(&srv_proto, &srv);
135 if (item == NULL)
136 return NULL;
137
138 srv_proto_t *sp = hash_table_get_inst(item, srv_proto_t, link);
139 return sp->proto;
140}
141
142static void proto_struct_init(proto_t *proto, const char *name)
143{
144 proto->name = name;
145 /* todo: check return value. */
146 bool ok = hash_table_create(&proto->method_oper, 0, 0, &method_oper_ops);
147 assert(ok);
148}
149
150proto_t *proto_new(const char *name)
151{
152 proto_t *p;
153
154 p = malloc(sizeof(proto_t));
155 proto_struct_init(p, name);
156
157 return p;
158}
159
160void proto_delete(proto_t *proto)
161{
162 free(proto);
163}
164
165void proto_add_oper(proto_t *proto, int method, oper_t *oper)
166{
167 method_oper_t *mo;
168
169 mo = malloc(sizeof(method_oper_t));
170 mo->method = method;
171 mo->oper = oper;
172
173 hash_table_insert(&proto->method_oper, &mo->link);
174}
175
176oper_t *proto_get_oper(proto_t *proto, int method)
177{
178 ht_link_t *item = hash_table_find(&proto->method_oper, &method);
179 if (item == NULL)
180 return NULL;
181
182 method_oper_t *mo = hash_table_get_inst(item, method_oper_t, link);
183 return mo->oper;
184}
185
186static void oper_struct_init(oper_t *oper, const char *name)
187{
188 oper->name = name;
189}
190
191oper_t *oper_new(const char *name, int argc, val_type_t *arg_types,
192 val_type_t rv_type, int respc, val_type_t *resp_types)
193{
194 oper_t *o;
195 int i;
196
197 o = malloc(sizeof(oper_t));
198 oper_struct_init(o, name);
199
200 o->argc = argc;
201 for (i = 0; i < argc; i++)
202 o->arg_type[i] = arg_types[i];
203
204 o->rv_type = rv_type;
205
206 o->respc = respc;
207 for (i = 0; i < respc; i++)
208 o->resp_type[i] = resp_types[i];
209
210 return o;
211}
212
213/** @}
214 */
Note: See TracBrowser for help on using the repository browser.