Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/ns/service.c

    rb72efe8 r4e00f87  
    4040#include "ns.h"
    4141
    42 #define SERVICE_HASH_TABLE_CHAINS  20
    4342
    4443/** Service hash table item. */
    4544typedef struct {
    46         link_t link;
     45        ht_link_t link;
    4746        sysarg_t service;        /**< Service ID. */
    4847        sysarg_t phone;          /**< Phone registered with the service. */
     
    5049} hashed_service_t;
    5150
    52 /** Compute hash index into service hash table.
    53  *
    54  * @param key Pointer keys. However, only the first key (i.e. service number)
    55  *            is used to compute the hash index.
    56  *
    57  * @return Hash index corresponding to key[0].
    58  *
    59  */
    60 static hash_index_t service_hash(unsigned long key[])
     51
     52static size_t service_key_hash(void *key)
    6153{
    62         assert(key);
    63         return (key[0] % SERVICE_HASH_TABLE_CHAINS);
     54        return *(sysarg_t*)key;
    6455}
    6556
    66 /** Compare a key with hashed item.
    67  *
    68  * This compare function always ignores the third key.
    69  * It exists only to make it possible to remove records
    70  * originating from connection with key[1] in_phone_hash
    71  * value. Note that this is close to being classified
    72  * as a nasty hack.
    73  *
    74  * @param key  Array of keys.
    75  * @param keys Must be lesser or equal to 3.
    76  * @param item Pointer to a hash table item.
    77  *
    78  * @return Non-zero if the key matches the item, zero otherwise.
    79  *
    80  */
    81 static int service_compare(unsigned long key[], hash_count_t keys, link_t *item)
     57static size_t service_hash(const ht_link_t *item)
    8258{
    83         assert(key);
    84         assert(keys <= 3);
    85         assert(item);
    86        
    87         hashed_service_t *hs = hash_table_get_instance(item, hashed_service_t, link);
    88        
    89         if (keys == 2)
    90                 return ((key[0] == hs->service) && (key[1] == hs->in_phone_hash));
    91         else
    92                 return (key[0] == hs->service);
     59        hashed_service_t *hs = hash_table_get_inst(item, hashed_service_t, link);
     60        return hs->service;
    9361}
    9462
    95 /** Perform actions after removal of item from the hash table.
    96  *
    97  * @param item Item that was removed from the hash table.
    98  *
    99  */
    100 static void service_remove(link_t *item)
     63static bool service_key_equal(void *key, const ht_link_t *item)
    10164{
    102         assert(item);
    103         free(hash_table_get_instance(item, hashed_service_t, link));
     65        hashed_service_t *hs = hash_table_get_inst(item, hashed_service_t, link);
     66        return hs->service == *(sysarg_t*)key;
    10467}
    10568
    10669/** Operations for service hash table. */
    107 static hash_table_operations_t service_hash_table_ops = {
     70static hash_table_ops_t service_hash_table_ops = {
    10871        .hash = service_hash,
    109         .compare = service_compare,
    110         .remove_callback = service_remove
     72        .key_hash = service_key_hash,
     73        .key_equal = service_key_equal,
     74        .equal = NULL,
     75        .remove_callback = NULL
    11176};
    11277
     
    12792int service_init(void)
    12893{
    129         if (!hash_table_create(&service_hash_table, SERVICE_HASH_TABLE_CHAINS,
    130             3, &service_hash_table_ops)) {
     94        if (!hash_table_create(&service_hash_table, 0, 0, &service_hash_table_ops)) {
    13195                printf(NAME ": No memory available for services\n");
    13296                return ENOMEM;
     
    145109                pending_conn_t *pr = list_get_instance(cur, pending_conn_t, link);
    146110               
    147                 unsigned long keys[3] = {
    148                         pr->service,
    149                         0,
    150                         0
    151                 };
    152                
    153                 link_t *link = hash_table_find(&service_hash_table, keys);
     111                ht_link_t *link = hash_table_find(&service_hash_table, &pr->service);
    154112                if (!link)
    155113                        continue;
    156114               
    157                 hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link);
     115                hashed_service_t *hs = hash_table_get_inst(link, hashed_service_t, link);
    158116                (void) ipc_forward_fast(pr->callid, hs->phone, pr->arg2,
    159117                    pr->arg3, 0, IPC_FF_NONE);
     
    176134int register_service(sysarg_t service, sysarg_t phone, ipc_call_t *call)
    177135{
    178         unsigned long keys[3] = {
    179                 service,
    180                 call->in_phone_hash,
    181                 0
    182         };
    183        
    184         if (hash_table_find(&service_hash_table, keys))
     136        if (hash_table_find(&service_hash_table, &service))
    185137                return EEXISTS;
    186138       
     
    189141                return ENOMEM;
    190142       
    191         link_initialize(&hs->link);
    192143        hs->service = service;
    193144        hs->phone = phone;
    194145        hs->in_phone_hash = call->in_phone_hash;
    195         hash_table_insert(&service_hash_table, keys, &hs->link);
     146        hash_table_insert(&service_hash_table, &hs->link);
    196147       
    197148        return EOK;
     
    210161{
    211162        sysarg_t retval;
    212         unsigned long keys[3] = {
    213                 service,
    214                 0,
    215                 0
    216         };
    217163       
    218         link_t *link = hash_table_find(&service_hash_table, keys);
     164        ht_link_t *link = hash_table_find(&service_hash_table, &service);
    219165        if (!link) {
    220166                if (IPC_GET_ARG4(*call) & IPC_FLAG_BLOCKING) {
     
    239185        }
    240186       
    241         hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link);
     187        hashed_service_t *hs = hash_table_get_inst(link, hashed_service_t, link);
    242188        (void) ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call),
    243189            IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
Note: See TracChangeset for help on using the changeset viewer.