Subversion Repositories HelenOS

Rev

Rev 2883 | Rev 2946 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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