Subversion Repositories HelenOS

Rev

Rev 2880 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2878 svoboda 1
/** @addtogroup sctrace
2
 * @{
3
 */
4
/** @file
5
 */
6
 
7
#include <stdio.h>
8
#include <stdlib.h>
9
#include <libadt/hash_table.h>
10
 
11
#include "proto.h"
12
 
13
#define SRV_PROTO_TABLE_CHAINS 32
14
 
15
hash_table_t srv_proto;
16
 
17
typedef struct {
18
    int srv;
19
    proto_t *proto;
20
    link_t link;
21
} srv_proto_t;
22
 
23
hash_index_t srv_proto_hash(unsigned long key[])
24
{
25
    return key[0] % SRV_PROTO_TABLE_CHAINS;
26
}
27
 
28
int srv_proto_compare(unsigned long key[], hash_count_t keys,
29
    link_t *item)
30
{
31
    srv_proto_t *sp;
32
 
33
    sp = hash_table_get_instance(item, srv_proto_t, link);
34
 
35
    return key[0] == sp->srv;
36
}
37
 
38
void srv_proto_remove_callback(link_t *item)
39
{
40
}
41
 
42
hash_table_operations_t srv_proto_ops = {
43
    .hash = srv_proto_hash,
44
    .compare = srv_proto_compare,
45
    .remove_callback = srv_proto_remove_callback
46
};
47
 
48
void proto_init(void)
49
{
50
    hash_table_create(&srv_proto, SRV_PROTO_TABLE_CHAINS, 1,
51
        &srv_proto_ops);
52
}
53
 
54
void proto_cleanup(void)
55
{
56
    hash_table_destroy(&srv_proto);
57
}
58
 
59
void proto_register(int srv, proto_t *proto)
60
{
61
    srv_proto_t *sp;
62
    unsigned long key;
63
 
64
    sp = malloc(sizeof(srv_proto_t));
65
    sp->srv = srv;
66
    sp->proto = proto;
67
    key = srv;
68
 
69
    hash_table_insert(&srv_proto, &key, &sp->link);
70
}
71
 
72
proto_t *proto_get_by_srv(int srv)
73
{
74
    unsigned long key;
75
    link_t *item;
76
    srv_proto_t *sp;
77
 
78
    key = srv;
79
    item = hash_table_find(&srv_proto, &key);
80
    if (item == NULL) return NULL;
81
 
82
    sp = hash_table_get_instance(item, srv_proto_t, link);
83
    return sp->proto;
84
}
85
 
86
/** @}
87
 */