source: mainline/uspace/app/trace/proto.c@ 02c6dcc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 02c6dcc 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
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
[062d900]42/* Maps service number to protocol */
43static hash_table_t srv_proto;
[9a1b20c]44
45typedef struct {
[062d900]46 int srv;
[9a1b20c]47 proto_t *proto;
[062d900]48 ht_link_t link;
[9a1b20c]49} srv_proto_t;
50
51typedef struct {
[062d900]52 int method;
[9a1b20c]53 oper_t *oper;
[062d900]54 ht_link_t link;
[9a1b20c]55} method_oper_t;
56
[062d900]57/* Hash table operations. */
[9a1b20c]58
[062d900]59static size_t srv_proto_key_hash(void *key)
[9a1b20c]60{
[062d900]61 return *(int *)key;
[9a1b20c]62}
63
[062d900]64static size_t srv_proto_hash(const ht_link_t *item)
[9a1b20c]65{
[062d900]66 srv_proto_t *sp = hash_table_get_inst(item, srv_proto_t, link);
67 return sp->srv;
[9a1b20c]68}
69
[062d900]70static bool srv_proto_key_equal(void *key, const ht_link_t *item)
[9a1b20c]71{
[062d900]72 srv_proto_t *sp = hash_table_get_inst(item, srv_proto_t, link);
73 return sp->srv == *(int *)key;
[9a1b20c]74}
75
[062d900]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,
[4e00f87]80 .equal = NULL,
81 .remove_callback = NULL
[062d900]82};
83
84static size_t method_oper_key_hash(void *key)
[9a1b20c]85{
[062d900]86 return *(int *)key;
[9a1b20c]87}
88
[062d900]89static size_t method_oper_hash(const ht_link_t *item)
[9a1b20c]90{
[062d900]91 method_oper_t *mo = hash_table_get_inst(item, method_oper_t, link);
92 return mo->method;
[9a1b20c]93}
94
[062d900]95static bool method_oper_key_equal(void *key, const ht_link_t *item)
[9a1b20c]96{
[062d900]97 method_oper_t *mo = hash_table_get_inst(item, method_oper_t, link);
98 return mo->method == *(int *)key;
[9a1b20c]99}
100
[062d900]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,
[4e00f87]105 .equal = NULL,
106 .remove_callback = NULL
[062d900]107};
108
[9a1b20c]109void proto_init(void)
110{
[062d900]111 /* todo: check return value. */
112 bool ok = hash_table_create(&srv_proto, 0, 0, &srv_proto_ops);
113 assert(ok);
[9a1b20c]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
[062d900]129 hash_table_insert(&srv_proto, &sp->link);
[9a1b20c]130}
131
132proto_t *proto_get_by_srv(int srv)
133{
[062d900]134 ht_link_t *item = hash_table_find(&srv_proto, &srv);
[1433ecda]135 if (item == NULL)
136 return NULL;
[9a1b20c]137
[062d900]138 srv_proto_t *sp = hash_table_get_inst(item, srv_proto_t, link);
[9a1b20c]139 return sp->proto;
140}
141
[a000878c]142static void proto_struct_init(proto_t *proto, const char *name)
[9a1b20c]143{
144 proto->name = name;
[062d900]145 /* todo: check return value. */
146 bool ok = hash_table_create(&proto->method_oper, 0, 0, &method_oper_ops);
147 assert(ok);
[9a1b20c]148}
149
[a000878c]150proto_t *proto_new(const char *name)
[9a1b20c]151{
152 proto_t *p;
153
154 p = malloc(sizeof(proto_t));
155 proto_struct_init(p, name);
156
157 return p;
158}
159
[8c125ad]160void proto_delete(proto_t *proto)
161{
162 free(proto);
163}
164
[9a1b20c]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
[1b20da0]173 hash_table_insert(&proto->method_oper, &mo->link);
[9a1b20c]174}
175
176oper_t *proto_get_oper(proto_t *proto, int method)
177{
[062d900]178 ht_link_t *item = hash_table_find(&proto->method_oper, &method);
[1433ecda]179 if (item == NULL)
180 return NULL;
[9a1b20c]181
[062d900]182 method_oper_t *mo = hash_table_get_inst(item, method_oper_t, link);
[9a1b20c]183 return mo->oper;
184}
185
[a000878c]186static void oper_struct_init(oper_t *oper, const char *name)
[9a1b20c]187{
188 oper->name = name;
189}
190
[a000878c]191oper_t *oper_new(const char *name, int argc, val_type_t *arg_types,
[abf3564]192 val_type_t rv_type, int respc, val_type_t *resp_types)
[9a1b20c]193{
194 oper_t *o;
[abf3564]195 int i;
[9a1b20c]196
197 o = malloc(sizeof(oper_t));
198 oper_struct_init(o, name);
199
[abf3564]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
[9a1b20c]210 return o;
211}
212
213/** @}
214 */
Note: See TracBrowser for help on using the repository browser.