Changeset 0ca7286 in mainline for uspace/app/trace/proto.c


Ignore:
Timestamp:
2012-07-21T11:19:27Z (12 years ago)
Author:
Adam Hraska <adam.hraska+hos@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
2732c94
Parents:
1c1da4b
Message:

Added resizing to user space (single-threaded) hash_table. Resizes in a way to mitigate effects of bad hash functions. Change of interface affected many files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/trace/proto.c

    r1c1da4b r0ca7286  
    4040#include "proto.h"
    4141
    42 #define SRV_PROTO_TABLE_CHAINS 32
    43 #define METHOD_OPER_TABLE_CHAINS 32
    44 
    45 hash_table_t srv_proto;
     42
     43/* Maps service number to protocol */
     44static hash_table_t srv_proto;
    4645
    4746typedef struct {
     
    5756} method_oper_t;
    5857
    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 = {
     58
     59
     60
     61static size_t srv_proto_key_hash(unsigned long key[])
     62{
     63        return key[0];
     64}
     65
     66static size_t srv_proto_hash(const link_t *item)
     67{
     68        srv_proto_t *sp = hash_table_get_instance(item, srv_proto_t, link);
     69        unsigned long key = sp->srv;
     70        return srv_proto_key_hash(&key);       
     71}
     72
     73static bool srv_proto_match(unsigned long key[], size_t keys, const link_t *item)
     74{
     75        srv_proto_t *sp = hash_table_get_instance(item, srv_proto_t, link);
     76
     77        return key[0] == sp->srv;
     78}
     79
     80static hash_table_ops_t srv_proto_ops = {
    6581        .hash = srv_proto_hash,
    66         .compare = srv_proto_compare,
    67         .remove_callback = srv_proto_remove_callback
     82        .key_hash = srv_proto_key_hash,
     83        .match = srv_proto_match,
     84        .equal = 0,
     85        .remove_callback = 0
    6886};
    6987
    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 = {
     88
     89static size_t method_oper_key_hash(unsigned long key[])
     90{
     91        return key[0];
     92}
     93
     94static size_t method_oper_hash(const link_t *item)
     95{
     96        method_oper_t *mo = hash_table_get_instance(item, method_oper_t, link);
     97        unsigned long key = mo->method;
     98        return method_oper_key_hash(&key);
     99}
     100
     101static bool method_oper_match(unsigned long key[], size_t keys,
     102        const link_t *item)
     103{
     104        method_oper_t *mo = hash_table_get_instance(item, method_oper_t, link);
     105
     106        return key[0] == mo->method;
     107}
     108
     109static hash_table_ops_t method_oper_ops = {
    76110        .hash = method_oper_hash,
    77         .compare = method_oper_compare,
    78         .remove_callback = method_oper_remove_callback
     111        .key_hash = method_oper_key_hash,
     112        .match = method_oper_match,
     113        .equal = 0,
     114        .remove_callback = 0
    79115};
    80116
    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)
     117
     118void proto_init(void)
     119{
     120        hash_table_create(&srv_proto, 0, 1, &srv_proto_ops);
     121}
     122
     123void proto_cleanup(void)
     124{
     125        hash_table_destroy(&srv_proto);
     126}
     127
     128void proto_register(int srv, proto_t *proto)
    88129{
    89130        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;
    135131
    136132        sp = malloc(sizeof(srv_proto_t));
    137133        sp->srv = srv;
    138134        sp->proto = proto;
    139         key = srv;
    140 
    141         hash_table_insert(&srv_proto, &key, &sp->link);
     135
     136        hash_table_insert(&srv_proto, &sp->link);
    142137}
    143138
    144139proto_t *proto_get_by_srv(int srv)
    145140{
    146         unsigned long key;
    147141        link_t *item;
    148142        srv_proto_t *sp;
    149143
    150         key = srv;
     144        unsigned long key = srv;
    151145        item = hash_table_find(&srv_proto, &key);
    152146        if (item == NULL) return NULL;
     
    159153{
    160154        proto->name = name;
    161         hash_table_create(&proto->method_oper, SRV_PROTO_TABLE_CHAINS, 1,
    162             &method_oper_ops);
     155        hash_table_create(&proto->method_oper, 0, 1, &method_oper_ops);
    163156}
    164157
     
    181174{
    182175        method_oper_t *mo;
    183         unsigned long key;
    184176
    185177        mo = malloc(sizeof(method_oper_t));
    186178        mo->method = method;
    187179        mo->oper = oper;
    188         key = method;
    189 
    190         hash_table_insert(&proto->method_oper, &key, &mo->link);       
     180
     181        hash_table_insert(&proto->method_oper, &mo->link);     
    191182}
    192183
Note: See TracChangeset for help on using the changeset viewer.