source: mainline/uspace/app/trace/proto.c@ d92060f

Last change on this file since d92060f was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

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