Subversion Repositories HelenOS

Rev

Rev 3438 | Rev 3452 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3438 svoboda 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 <ipc/ipc.h>
38
#include <libadt/hash_table.h>
39
 
40
#include "proto.h"
41
 
42
#define SRV_PROTO_TABLE_CHAINS 32
43
#define METHOD_OPER_TABLE_CHAINS 32
44
 
45
hash_table_t srv_proto;
46
 
47
typedef struct {
48
    int srv;
49
    proto_t *proto;
50
    link_t link;
51
} srv_proto_t;
52
 
53
typedef struct {
54
    ipcarg_t method;
55
    oper_t *oper;
56
    link_t link;
57
} method_oper_t;
58
 
59
static hash_index_t srv_proto_hash(unsigned long key[]);
60
static int srv_proto_compare(unsigned long key[], hash_count_t keys,
61
    link_t *item);
62
static void srv_proto_remove_callback(link_t *item);
63
 
64
hash_table_operations_t srv_proto_ops = {
65
    .hash = srv_proto_hash,
66
    .compare = srv_proto_compare,
67
    .remove_callback = srv_proto_remove_callback
68
};
69
 
70
static hash_index_t method_oper_hash(unsigned long key[]);
71
static int method_oper_compare(unsigned long key[], hash_count_t keys,
72
    link_t *item);
73
static void method_oper_remove_callback(link_t *item);
74
 
75
hash_table_operations_t method_oper_ops = {
76
    .hash = method_oper_hash,
77
    .compare = method_oper_compare,
78
    .remove_callback = method_oper_remove_callback
79
};
80
 
81
static hash_index_t srv_proto_hash(unsigned long key[])
82
{
83
    return key[0] % SRV_PROTO_TABLE_CHAINS;
84
}
85
 
86
static int srv_proto_compare(unsigned long key[], hash_count_t keys,
87
    link_t *item)
88
{
89
    srv_proto_t *sp;
90
 
91
    sp = hash_table_get_instance(item, srv_proto_t, link);
92
 
93
    return key[0] == sp->srv;
94
}
95
 
96
static void srv_proto_remove_callback(link_t *item)
97
{
98
}
99
 
100
static hash_index_t method_oper_hash(unsigned long key[])
101
{
102
    return key[0] % METHOD_OPER_TABLE_CHAINS;
103
}
104
 
105
static int method_oper_compare(unsigned long key[], hash_count_t keys,
106
    link_t *item)
107
{
108
    method_oper_t *mo;
109
 
110
    mo = hash_table_get_instance(item, method_oper_t, link);
111
 
112
    return key[0] == mo->method;
113
}
114
 
115
static void method_oper_remove_callback(link_t *item)
116
{
117
}
118
 
119
 
120
void proto_init(void)
121
{
122
    hash_table_create(&srv_proto, SRV_PROTO_TABLE_CHAINS, 1,
123
        &srv_proto_ops);
124
}
125
 
126
void proto_cleanup(void)
127
{
128
    hash_table_destroy(&srv_proto);
129
}
130
 
131
void proto_register(int srv, proto_t *proto)
132
{
133
    srv_proto_t *sp;
134
    unsigned long key;
135
 
136
    sp = malloc(sizeof(srv_proto_t));
137
    sp->srv = srv;
138
    sp->proto = proto;
139
    key = srv;
140
 
141
    hash_table_insert(&srv_proto, &key, &sp->link);
142
}
143
 
144
proto_t *proto_get_by_srv(int srv)
145
{
146
    unsigned long key;
147
    link_t *item;
148
    srv_proto_t *sp;
149
 
150
    key = srv;
151
    item = hash_table_find(&srv_proto, &key);
152
    if (item == NULL) return NULL;
153
 
154
    sp = hash_table_get_instance(item, srv_proto_t, link);
155
    return sp->proto;
156
}
157
 
158
static void proto_struct_init(proto_t *proto, char *name)
159
{
160
    proto->name = name;
161
    hash_table_create(&proto->method_oper, SRV_PROTO_TABLE_CHAINS, 1,
162
        &method_oper_ops);
163
}
164
 
165
proto_t *proto_new(char *name)
166
{
167
    proto_t *p;
168
 
169
    p = malloc(sizeof(proto_t));
170
    proto_struct_init(p, name);
171
 
172
    return p;
173
}
174
 
3443 svoboda 175
void proto_delete(proto_t *proto)
176
{
177
    free(proto);
178
}
179
 
3438 svoboda 180
void proto_add_oper(proto_t *proto, int method, oper_t *oper)
181
{
182
    method_oper_t *mo;
183
    unsigned long key;
184
 
185
    mo = malloc(sizeof(method_oper_t));
186
    mo->method = method;
187
    mo->oper = oper;
188
    key = method;
189
 
190
    hash_table_insert(&proto->method_oper, &key, &mo->link);   
191
}
192
 
193
oper_t *proto_get_oper(proto_t *proto, int method)
194
{
195
    unsigned long key;
196
    link_t *item;
197
    method_oper_t *mo;
198
 
199
    key = method;
200
    item = hash_table_find(&proto->method_oper, &key);
201
    if (item == NULL) return NULL;
202
 
203
    mo = hash_table_get_instance(item, method_oper_t, link);
204
    return mo->oper;
205
}
206
 
207
static void oper_struct_init(oper_t *oper, char *name)
208
{
209
    oper->name = name;
210
}
211
 
212
oper_t *oper_new(char *name)
213
{
214
    oper_t *o;
215
 
216
    o = malloc(sizeof(oper_t));
217
    oper_struct_init(o, name);
218
 
219
    return o;
220
}
221
 
222
/** @}
223
 */