0,0 → 1,87 |
/** @addtogroup sctrace |
* @{ |
*/ |
/** @file |
*/ |
|
#include <stdio.h> |
#include <stdlib.h> |
#include <libadt/hash_table.h> |
|
#include "proto.h" |
|
#define SRV_PROTO_TABLE_CHAINS 32 |
|
hash_table_t srv_proto; |
|
typedef struct { |
int srv; |
proto_t *proto; |
link_t link; |
} srv_proto_t; |
|
hash_index_t srv_proto_hash(unsigned long key[]) |
{ |
return key[0] % SRV_PROTO_TABLE_CHAINS; |
} |
|
int srv_proto_compare(unsigned long key[], hash_count_t keys, |
link_t *item) |
{ |
srv_proto_t *sp; |
|
sp = hash_table_get_instance(item, srv_proto_t, link); |
|
return key[0] == sp->srv; |
} |
|
void srv_proto_remove_callback(link_t *item) |
{ |
} |
|
hash_table_operations_t srv_proto_ops = { |
.hash = srv_proto_hash, |
.compare = srv_proto_compare, |
.remove_callback = srv_proto_remove_callback |
}; |
|
void proto_init(void) |
{ |
hash_table_create(&srv_proto, SRV_PROTO_TABLE_CHAINS, 1, |
&srv_proto_ops); |
} |
|
void proto_cleanup(void) |
{ |
hash_table_destroy(&srv_proto); |
} |
|
void proto_register(int srv, proto_t *proto) |
{ |
srv_proto_t *sp; |
unsigned long key; |
|
sp = malloc(sizeof(srv_proto_t)); |
sp->srv = srv; |
sp->proto = proto; |
key = srv; |
|
hash_table_insert(&srv_proto, &key, &sp->link); |
} |
|
proto_t *proto_get_by_srv(int srv) |
{ |
unsigned long key; |
link_t *item; |
srv_proto_t *sp; |
|
key = srv; |
item = hash_table_find(&srv_proto, &key); |
if (item == NULL) return NULL; |
|
sp = hash_table_get_instance(item, srv_proto_t, link); |
return sp->proto; |
} |
|
/** @} |
*/ |