Changeset bc216a0 in mainline for uspace/srv


Ignore:
Timestamp:
2012-08-07T22:13:44Z (13 years ago)
Author:
Adam Hraska <adam.hraska+hos@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
da68871a
Parents:
b17518e
Message:

Refactored any users of hash_table to use opaque void* keys instead of the cumbersome unsigned long[] keys. Switched from the ad hoc computations of hashes of multiple values to hash_combine().

Location:
uspace/srv
Files:
20 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/devman/devman.c

    rb17518e rbc216a0  
    6666/* hash table operations */
    6767
    68 static size_t devices_key_hash(unsigned long key[])
    69 {
    70         return key[0];
    71 }
    72 
    73 static size_t devman_devices_hash(const link_t *item)
    74 {
    75         dev_node_t *dev = hash_table_get_instance(item, dev_node_t, devman_dev);
    76         unsigned long key = dev->handle;
    77         return devices_key_hash(&key);
    78 }
    79 
    80 static size_t devman_functions_hash(const link_t *item)
    81 {
    82         fun_node_t *fun = hash_table_get_instance(item, fun_node_t, devman_fun);
    83         unsigned long key = fun->handle;
    84         return devices_key_hash(&key);
    85 }
    86 
    87 static size_t loc_functions_hash(const link_t *item)
    88 {
    89         fun_node_t *fun = hash_table_get_instance(item, fun_node_t, loc_fun);
    90         unsigned long key = fun->service_id;
    91         return devices_key_hash(&key);
    92 }
    93 
    94 static bool devman_devices_match(unsigned long key[], size_t keys,
    95     const link_t *item)
    96 {
    97         dev_node_t *dev = hash_table_get_instance(item, dev_node_t, devman_dev);
    98         return (dev->handle == (devman_handle_t) key[0]);
    99 }
    100 
    101 static bool devman_functions_match(unsigned long key[], size_t keys,
    102     const link_t *item)
    103 {
    104         fun_node_t *fun = hash_table_get_instance(item, fun_node_t, devman_fun);
    105         return (fun->handle == (devman_handle_t) key[0]);
    106 }
    107 
    108 static bool loc_functions_match(unsigned long key[], size_t keys,
    109     const link_t *item)
    110 {
    111         fun_node_t *fun = hash_table_get_instance(item, fun_node_t, loc_fun);
    112         return (fun->service_id == (service_id_t) key[0]);
     68static inline size_t handle_key_hash(void *key)
     69{
     70        devman_handle_t handle = *(devman_handle_t*)key;
     71        return handle;
     72}
     73
     74static size_t devman_devices_hash(const ht_link_t *item)
     75{
     76        dev_node_t *dev = hash_table_get_inst(item, dev_node_t, devman_dev);
     77        return handle_key_hash(&dev->handle);
     78}
     79
     80static size_t devman_functions_hash(const ht_link_t *item)
     81{
     82        fun_node_t *fun = hash_table_get_inst(item, fun_node_t, devman_fun);
     83        return handle_key_hash(&fun->handle);
     84}
     85
     86static bool devman_devices_key_equal(void *key, const ht_link_t *item)
     87{
     88        devman_handle_t handle = *(devman_handle_t*)key;
     89        dev_node_t *dev = hash_table_get_inst(item, dev_node_t, devman_dev);
     90        return dev->handle == handle;
     91}
     92
     93static bool devman_functions_key_equal(void *key, const ht_link_t *item)
     94{
     95        devman_handle_t handle = *(devman_handle_t*)key;
     96        fun_node_t *fun = hash_table_get_inst(item, fun_node_t, devman_fun);
     97        return fun->handle == handle;
     98}
     99
     100static inline size_t service_id_key_hash(void *key)
     101{
     102        service_id_t service_id = *(service_id_t*)key;
     103        return service_id;
     104}
     105
     106static size_t loc_functions_hash(const ht_link_t *item)
     107{
     108        fun_node_t *fun = hash_table_get_inst(item, fun_node_t, loc_fun);
     109        return service_id_key_hash(&fun->service_id);
     110}
     111
     112static bool loc_functions_key_equal(void *key, const ht_link_t *item)
     113{
     114        service_id_t service_id = *(service_id_t*)key;
     115        fun_node_t *fun = hash_table_get_inst(item, fun_node_t, loc_fun);
     116        return fun->service_id == service_id;
    113117}
    114118
     
    116120static hash_table_ops_t devman_devices_ops = {
    117121        .hash = devman_devices_hash,
    118         .key_hash = devices_key_hash,
    119         .match = devman_devices_match,
     122        .key_hash = handle_key_hash,
     123        .key_equal = devman_devices_key_equal,
    120124        .equal = 0,
    121125        .remove_callback = 0
     
    124128static hash_table_ops_t devman_functions_ops = {
    125129        .hash = devman_functions_hash,
    126         .key_hash = devices_key_hash,
    127         .match = devman_functions_match,
     130        .key_hash = handle_key_hash,
     131        .key_equal = devman_functions_key_equal,
    128132        .equal = 0,
    129133        .remove_callback = 0
     
    132136static hash_table_ops_t loc_devices_ops = {
    133137        .hash = loc_functions_hash,
    134         .key_hash = devices_key_hash,
    135         .match = loc_functions_match,
     138        .key_hash = service_id_key_hash,
     139        .key_equal = loc_functions_key_equal,
    136140        .equal = 0,
    137141        .remove_callback = 0
     
    9981002        tree->current_handle = 0;
    9991003       
    1000         hash_table_create(&tree->devman_devices, 0, 1, &devman_devices_ops);
    1001         hash_table_create(&tree->devman_functions, 0, 1, &devman_functions_ops);
    1002         hash_table_create(&tree->loc_functions, 0, 1, &loc_devices_ops);
     1004        hash_table_create(&tree->devman_devices, 0, 0, &devman_devices_ops);
     1005        hash_table_create(&tree->devman_functions, 0, 0, &devman_functions_ops);
     1006        hash_table_create(&tree->loc_functions, 0, 0, &loc_devices_ops);
    10031007       
    10041008        fibril_rwlock_initialize(&tree->rwlock);
     
    10341038        list_initialize(&dev->functions);
    10351039        link_initialize(&dev->driver_devices);
    1036         link_initialize(&dev->devman_dev);
    10371040       
    10381041        return dev;
     
    10821085dev_node_t *find_dev_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
    10831086{
    1084         unsigned long key = handle;
    1085         link_t *link;
    1086        
    10871087        assert(fibril_rwlock_is_locked(&tree->rwlock));
    10881088       
    1089         link = hash_table_find(&tree->devman_devices, &key);
     1089        ht_link_t *link = hash_table_find(&tree->devman_devices, &handle);
    10901090        if (link == NULL)
    10911091                return NULL;
    10921092       
    1093         return hash_table_get_instance(link, dev_node_t, devman_dev);
     1093        return hash_table_get_inst(link, dev_node_t, devman_dev);
    10941094}
    10951095
     
    11651165        link_initialize(&fun->dev_functions);
    11661166        list_initialize(&fun->match_ids.ids);
    1167         link_initialize(&fun->devman_fun);
    1168         link_initialize(&fun->loc_fun);
    11691167       
    11701168        return fun;
     
    12151213fun_node_t *find_fun_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
    12161214{
    1217         unsigned long key = handle;
    1218         link_t *link;
    12191215        fun_node_t *fun;
    12201216       
    12211217        assert(fibril_rwlock_is_locked(&tree->rwlock));
    12221218       
    1223         link = hash_table_find(&tree->devman_functions, &key);
     1219        ht_link_t *link = hash_table_find(&tree->devman_functions, &handle);
    12241220        if (link == NULL)
    12251221                return NULL;
    12261222       
    1227         fun = hash_table_get_instance(link, fun_node_t, devman_fun);
     1223        fun = hash_table_get_inst(link, fun_node_t, devman_fun);
    12281224       
    12291225        return fun;
     
    13241320       
    13251321        /* Remove node from the handle-to-node map. */
    1326         unsigned long key = dev->handle;
    1327         hash_table_remove(&tree->devman_devices, &key, 1);
     1322        hash_table_remove(&tree->devman_devices, &dev->handle);
    13281323       
    13291324        /* Unlink from parent function. */
     
    13861381       
    13871382        /* Remove the node from the handle-to-node map. */
    1388         unsigned long key = fun->handle;
    1389         hash_table_remove(&tree->devman_functions, &key, 1);
     1383        hash_table_remove(&tree->devman_functions, &fun->handle);
    13901384       
    13911385        /* Remove the node from the list of its parent's children. */
     
    15001494{
    15011495        fun_node_t *fun = NULL;
    1502         link_t *link;
    1503         unsigned long key = (unsigned long) service_id;
    15041496       
    15051497        fibril_rwlock_read_lock(&tree->rwlock);
    1506         link = hash_table_find(&tree->loc_functions, &key);
     1498        ht_link_t *link = hash_table_find(&tree->loc_functions, &service_id);
    15071499        if (link != NULL) {
    1508                 fun = hash_table_get_instance(link, fun_node_t, loc_fun);
     1500                fun = hash_table_get_inst(link, fun_node_t, loc_fun);
    15091501                fun_add_ref(fun);
    15101502        }
  • uspace/srv/devman/devman.h

    rb17518e rbc216a0  
    150150         * Used by the hash table of devices indexed by devman device handles.
    151151         */
    152         link_t devman_dev;
     152        ht_link_t devman_dev;
    153153       
    154154        /**
     
    201201         * Used by the hash table of functions indexed by devman device handles.
    202202         */
    203         link_t devman_fun;
     203        ht_link_t devman_fun;
    204204       
    205205        /**
    206206         * Used by the hash table of functions indexed by service IDs.
    207207         */
    208         link_t loc_fun;
     208        ht_link_t loc_fun;
    209209};
    210210
  • uspace/srv/fs/cdfs/cdfs_ops.c

    rb17518e rbc216a0  
    3737 */
    3838
     39#include "cdfs_ops.h"
    3940#include <bool.h>
    4041#include <adt/hash_table.h>
     42#include <adt/hash.h>
    4143#include <malloc.h>
    4244#include <mem.h>
     
    5052#include "cdfs.h"
    5153#include "cdfs_endian.h"
    52 #include "cdfs_ops.h"
    5354
    5455/** Standard CD-ROM block size */
    5556#define BLOCK_SIZE  2048
    5657
    57 /** Implicit node cache size
    58  *
    59  * More nodes can be actually cached if the files remain
    60  * opended.
    61  *
    62  */
    63 #define NODE_CACHE_SIZE  200
    64 
    65 #define NODES_KEY_SRVC   0
    66 #define NODES_KEY_INDEX  1
     58#define NODE_CACHE_SIZE 200
    6759
    6860/** All root nodes have index 0 */
     
    203195        service_id_t service_id;  /**< Service ID of block device */
    204196       
    205         link_t nh_link;           /**< Nodes hash table link */
     197        ht_link_t nh_link;        /**< Nodes hash table link */
    206198        cdfs_dentry_type_t type;  /**< Dentry type */
    207199       
     
    224216static hash_table_t nodes;
    225217
    226 static size_t nodes_key_hash(unsigned long key[])
    227 {
    228         return key[NODES_KEY_INDEX];
    229 }
    230 
    231 static size_t nodes_hash(const link_t *item)
    232 {
    233         cdfs_node_t *node = hash_table_get_instance(item, cdfs_node_t, nh_link);
    234        
    235         unsigned long key[] = {
    236                 [NODES_KEY_INDEX] = node->index
    237         };
    238        
    239         return nodes_key_hash(key);
    240 }
    241 
    242 static bool nodes_match(unsigned long key[], size_t keys, const link_t *item)
    243 {
    244         cdfs_node_t *node = hash_table_get_instance(item, cdfs_node_t, nh_link);
    245        
    246         if (keys == 1) {
    247                 return (node->service_id == key[NODES_KEY_SRVC]);
    248         } else {
    249                 assert(keys == 2);
    250                 return ((node->service_id == key[NODES_KEY_SRVC]) &&
    251                     (node->index == key[NODES_KEY_INDEX]));
    252         }
    253 }
    254 
    255 static bool nodes_equal(const link_t *item1, const link_t *item2)
    256 {
    257         cdfs_node_t *node1 = hash_table_get_instance(item1, cdfs_node_t, nh_link);
    258         cdfs_node_t *node2 = hash_table_get_instance(item2, cdfs_node_t, nh_link);
    259        
    260         return node1->service_id == node2->service_id
    261                 && node1->index == node2->index;
    262 }
    263 
    264 static void nodes_remove_callback(link_t *item)
    265 {
    266         cdfs_node_t *node = hash_table_get_instance(item, cdfs_node_t, nh_link);
     218/*
     219 * Hash table support functions.
     220 */
     221
     222typedef struct {
     223        service_id_t service_id;
     224    fs_index_t index;
     225} ht_key_t;
     226
     227static size_t nodes_key_hash(void *k)
     228{
     229        ht_key_t *key = (ht_key_t*)k;
     230        return hash_combine(key->service_id, key->index);
     231}
     232
     233static size_t nodes_hash(const ht_link_t *item)
     234{
     235        cdfs_node_t *node = hash_table_get_inst(item, cdfs_node_t, nh_link);
     236        return hash_combine(node->service_id, node->index);
     237}
     238
     239static bool nodes_key_equal(void *k, const ht_link_t *item)
     240{
     241        cdfs_node_t *node = hash_table_get_inst(item, cdfs_node_t, nh_link);
     242        ht_key_t *key = (ht_key_t*)k;
     243       
     244        return key->service_id == node->service_id && key->index == node->index;
     245}
     246
     247static void nodes_remove_callback(ht_link_t *item)
     248{
     249        cdfs_node_t *node = hash_table_get_inst(item, cdfs_node_t, nh_link);
    267250       
    268251        assert(node->type == CDFS_DIRECTORY);
     
    283266        .hash = nodes_hash,
    284267        .key_hash = nodes_key_hash,
    285         .match = nodes_match,
    286         .equal = nodes_equal,
     268        .key_equal = nodes_key_equal,
     269        .equal = 0,
    287270        .remove_callback = nodes_remove_callback
    288271};
     
    291274    fs_index_t index)
    292275{
    293         unsigned long key[] = {
    294                 [NODES_KEY_SRVC] = service_id,
    295                 [NODES_KEY_INDEX] = index
     276        ht_key_t key = {
     277                .index = index,
     278                .service_id = service_id
    296279        };
    297280       
    298         link_t *link = hash_table_find(&nodes, key);
     281        ht_link_t *link = hash_table_find(&nodes, &key);
    299282        if (link) {
    300283                cdfs_node_t *node =
    301                     hash_table_get_instance(link, cdfs_node_t, nh_link);
     284                    hash_table_get_inst(link, cdfs_node_t, nh_link);
    302285               
    303286                *rfn = FS_NODE(node);
     
    325308        node->opened = 0;
    326309       
    327         link_initialize(&node->nh_link);
    328310        list_initialize(&node->cs_list);
    329311}
     
    517499static fs_node_t *get_cached_node(service_id_t service_id, fs_index_t index)
    518500{
    519         unsigned long key[] = {
    520                 [NODES_KEY_SRVC] = service_id,
    521                 [NODES_KEY_INDEX] = index
     501        ht_key_t key = {
     502                .index = index,
     503                .service_id = service_id
    522504        };
    523505       
    524         link_t *link = hash_table_find(&nodes, key);
     506        ht_link_t *link = hash_table_find(&nodes, &key);
    525507        if (link) {
    526508                cdfs_node_t *node =
    527                     hash_table_get_instance(link, cdfs_node_t, nh_link);
     509                    hash_table_get_inst(link, cdfs_node_t, nh_link);
    528510                return FS_NODE(node);
    529511        }
     
    811793}
    812794
     795static bool rm_service_id_nodes(ht_link_t *item, void *arg)
     796{
     797        service_id_t service_id = *(service_id_t*)arg;
     798        cdfs_node_t *node = hash_table_get_inst(item, cdfs_node_t, nh_link);
     799       
     800        if (node->service_id == service_id) {
     801                hash_table_remove_item(&nodes, &node->nh_link);
     802        }
     803       
     804        return true;
     805}
     806
    813807static void cdfs_instance_done(service_id_t service_id)
    814808{
    815         unsigned long key[] = {
    816                 [NODES_KEY_SRVC] = service_id
    817         };
    818        
    819         hash_table_remove(&nodes, key, 1);
     809        hash_table_apply(&nodes, rm_service_id_nodes, &service_id);
    820810        block_cache_fini(service_id);
    821811        block_fini(service_id);
     
    831821    size_t *rbytes)
    832822{
    833         unsigned long key[] = {
    834                 [NODES_KEY_SRVC] = service_id,
    835                 [NODES_KEY_INDEX] = index
     823        ht_key_t key = {
     824                .index = index,
     825                .service_id = service_id
    836826        };
    837827       
    838         link_t *link = hash_table_find(&nodes, key);
     828        ht_link_t *link = hash_table_find(&nodes, &key);
    839829        if (link == NULL)
    840830                return ENOENT;
    841831       
    842832        cdfs_node_t *node =
    843             hash_table_get_instance(link, cdfs_node_t, nh_link);
     833            hash_table_get_inst(link, cdfs_node_t, nh_link);
    844834       
    845835        if (!node->processed) {
     
    921911}
    922912
    923 static bool cache_remove_closed(link_t *item, void *arg)
     913static bool cache_remove_closed(ht_link_t *item, void *arg)
    924914{
    925915        size_t *premove_cnt = (size_t*)arg;
     
    927917        /* Some nodes were requested to be removed from the cache. */
    928918        if (0 < *premove_cnt) {
    929                 cdfs_node_t *node =     hash_table_get_instance(item, cdfs_node_t, nh_link);
     919                cdfs_node_t *node =     hash_table_get_inst(item, cdfs_node_t, nh_link);
    930920
    931921                if (!node->opened) {
     
    957947                return EOK;
    958948       
    959         unsigned long key[] = {
    960                 [NODES_KEY_SRVC] = service_id,
    961                 [NODES_KEY_INDEX] = index
     949        ht_key_t key = {
     950                .index = index,
     951                .service_id = service_id
    962952        };
    963953       
    964         link_t *link = hash_table_find(&nodes, key);
     954        ht_link_t *link = hash_table_find(&nodes, &key);
    965955        if (link == 0)
    966956                return ENOENT;
    967957       
    968958        cdfs_node_t *node =
    969             hash_table_get_instance(link, cdfs_node_t, nh_link);
     959            hash_table_get_inst(link, cdfs_node_t, nh_link);
    970960       
    971961        assert(node->opened > 0);
     
    10131003bool cdfs_init(void)
    10141004{
    1015         if (!hash_table_create(&nodes, 0, 2, &nodes_ops))
     1005        if (!hash_table_create(&nodes, 0, 0, &nodes_ops))
    10161006                return false;
    10171007       
  • uspace/srv/fs/exfat/exfat.h

    rb17518e rbc216a0  
    106106typedef struct {
    107107        /** Used indices (position) hash table link. */
    108         link_t          uph_link;
     108        ht_link_t               uph_link;
    109109        /** Used indices (index) hash table link. */
    110         link_t          uih_link;
     110        ht_link_t               uih_link;
    111111
    112112        fibril_mutex_t  lock;
  • uspace/srv/fs/exfat/exfat_idx.c

    rb17518e rbc216a0  
    4141#include <str.h>
    4242#include <adt/hash_table.h>
     43#include <adt/hash.h>
    4344#include <adt/list.h>
    4445#include <assert.h>
     
    9192        if (lock)
    9293                fibril_mutex_lock(&unused_lock);
     94
    9395        list_foreach(unused_list, l) {
    9496                u = list_get_instance(l, unused_t, link);
     
    112114static hash_table_t up_hash;
    113115
    114 #define UPH_SID_KEY     0
    115 #define UPH_PFC_KEY     1
    116 #define UPH_PDI_KEY     2
    117 
    118 static size_t pos_key_hash(unsigned long key[])
    119 {
    120         /* Inspired by Effective Java, 2nd edition. */
    121         size_t hash = 17;
    122        
    123         hash = 31 * hash + key[UPH_PFC_KEY];
    124         hash = 31 * hash + key[UPH_PDI_KEY];
    125         hash = 31 * hash + key[UPH_SID_KEY];
    126        
    127         return hash;
    128 }
    129 
    130 static size_t pos_hash(const link_t *item)
    131 {
    132         exfat_idx_t *fidx = list_get_instance(item, exfat_idx_t, uph_link);
    133        
    134         unsigned long pkey[] = {
    135                 [UPH_SID_KEY] = fidx->service_id,
    136                 [UPH_PFC_KEY] = fidx->pfc,
    137                 [UPH_PDI_KEY] = fidx->pdi,
    138         };
    139        
    140         return pos_key_hash(pkey);
    141 }
    142 
    143 static bool pos_match(unsigned long key[], size_t keys, const link_t *item)
    144 {
    145         service_id_t service_id = (service_id_t)key[UPH_SID_KEY];
     116typedef struct {
     117        service_id_t service_id;
    146118        exfat_cluster_t pfc;
    147119        unsigned pdi;
    148         exfat_idx_t *fidx = list_get_instance(item, exfat_idx_t, uph_link);
    149 
    150         switch (keys) {
    151         case 1:
    152                 return (service_id == fidx->service_id);
    153         case 3:
    154                 pfc = (exfat_cluster_t) key[UPH_PFC_KEY];
    155                 pdi = (unsigned) key[UPH_PDI_KEY];
    156                 return (service_id == fidx->service_id) && (pfc == fidx->pfc) &&
    157                     (pdi == fidx->pdi);
    158         default:
    159                 assert((keys == 1) || (keys == 3));
    160         }
    161 
    162         return 0;
     120} pos_key_t;
     121
     122static inline size_t pos_key_hash(void *key)
     123{
     124        pos_key_t *pos = (pos_key_t*)key;
     125       
     126        size_t hash = 0;
     127        hash = hash_combine(pos->pfc, pos->pdi);
     128        return hash_combine(hash, pos->service_id);
     129}
     130
     131static size_t pos_hash(const ht_link_t *item)
     132{
     133        exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uph_link);
     134       
     135        pos_key_t pkey = {
     136                .service_id = fidx->service_id,
     137                .pfc = fidx->pfc,
     138                .pdi = fidx->pdi,
     139        };
     140       
     141        return pos_key_hash(&pkey);
     142}
     143
     144static bool pos_key_equal(void *key, const ht_link_t *item)
     145{
     146        pos_key_t *pos = (pos_key_t*)key;
     147        exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uph_link);
     148       
     149        return pos->service_id == fidx->service_id
     150                && pos->pdi == fidx->pdi
     151                && pos->pfc == fidx->pfc;
    163152}
    164153
     
    166155        .hash = pos_hash,
    167156        .key_hash = pos_key_hash,
    168         .match = pos_match,
     157        .key_equal = pos_key_equal,
    169158        .equal = 0,
    170159        .remove_callback = 0,
     
    177166static hash_table_t ui_hash;
    178167
    179 #define UIH_SID_KEY     0
    180 #define UIH_INDEX_KEY   1
    181 
    182 static size_t idx_key_hash(unsigned long key[])
    183 {
    184         service_id_t service_id = (service_id_t)key[UIH_SID_KEY];
    185         fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY];
    186 
    187         /*
    188          * Compute a simple hash unlimited by specific table size as per:
    189          * Effective Java, 2nd edition.
    190          */
    191         size_t hash = 17;
    192         hash = 31 * hash + (size_t)service_id;
    193         hash = 31 * hash + (size_t)index;
    194         return hash;
    195 }
    196 
    197 static size_t idx_hash(const link_t *item)
    198 {
    199         exfat_idx_t *fidx = list_get_instance(item, exfat_idx_t, uih_link);
    200        
    201         unsigned long ikey[] = {
    202                 [UIH_SID_KEY] = fidx->service_id,
    203                 [UIH_INDEX_KEY] = fidx->index,
    204         };
    205 
    206         return idx_key_hash(ikey);
    207 }
    208 
    209 static bool idx_match(unsigned long key[], size_t keys, const link_t *item)
    210 {
    211         service_id_t service_id = (service_id_t)key[UIH_SID_KEY];
     168typedef struct {
     169        service_id_t service_id;
    212170        fs_index_t index;
    213         exfat_idx_t *fidx = list_get_instance(item, exfat_idx_t, uih_link);
    214 
    215         switch (keys) {
    216         case 1:
    217                 return (service_id == fidx->service_id);
    218         case 2:
    219                 index = (fs_index_t) key[UIH_INDEX_KEY];
    220                 return (service_id == fidx->service_id) &&
    221                     (index == fidx->index);
    222         default:
    223                 assert((keys == 1) || (keys == 2));
    224         }
    225 
    226         return 0;
    227 }
    228 
    229 static void idx_remove_callback(link_t *item)
    230 {
    231         exfat_idx_t *fidx = list_get_instance(item, exfat_idx_t, uih_link);
     171} idx_key_t;
     172
     173static size_t idx_key_hash(void *key_arg)
     174{
     175        idx_key_t *key = (idx_key_t*)key_arg;
     176        return hash_combine(key->service_id, key->index);
     177}
     178
     179static size_t idx_hash(const ht_link_t *item)
     180{
     181        exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uih_link);
     182        return hash_combine(fidx->service_id, fidx->index);
     183}
     184
     185static bool idx_key_equal(void *key_arg, const ht_link_t *item)
     186{
     187        exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uih_link);
     188        idx_key_t *key = (idx_key_t*)key_arg;
     189       
     190        return key->index == fidx->index && key->service_id == fidx->service_id;
     191}
     192
     193static void idx_remove_callback(ht_link_t *item)
     194{
     195        exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uih_link);
    232196
    233197        free(fidx);
     
    237201        .hash = idx_hash,
    238202        .key_hash = idx_key_hash,
    239         .match = idx_match,
     203        .key_equal = idx_key_equal,
    240204        .equal = 0,
    241205        .remove_callback = idx_remove_callback,
     
    379343        }
    380344               
    381         link_initialize(&fidx->uph_link);
    382         link_initialize(&fidx->uih_link);
    383345        fibril_mutex_initialize(&fidx->lock);
    384346        fidx->service_id = service_id;
     
    415377{
    416378        exfat_idx_t *fidx;
    417         link_t *l;
    418         unsigned long pkey[] = {
    419                 [UPH_SID_KEY] = service_id,
    420                 [UPH_PFC_KEY] = pfc,
    421                 [UPH_PDI_KEY] = pdi,
     379       
     380        pos_key_t pos_key = {
     381                .service_id = service_id,
     382                .pfc = pfc,
     383                .pdi = pdi,
    422384        };
    423385
    424386        fibril_mutex_lock(&used_lock);
    425         l = hash_table_find(&up_hash, pkey);
     387        ht_link_t *l = hash_table_find(&up_hash, &pos_key);
    426388        if (l) {
    427                 fidx = hash_table_get_instance(l, exfat_idx_t, uph_link);
     389                fidx = hash_table_get_inst(l, exfat_idx_t, uph_link);
    428390        } else {
    429391                int rc;
     
    456418void exfat_idx_hashout(exfat_idx_t *idx)
    457419{
    458         unsigned long pkey[] = {
    459                 [UPH_SID_KEY] = idx->service_id,
    460                 [UPH_PFC_KEY] = idx->pfc,
    461                 [UPH_PDI_KEY] = idx->pdi,
    462         };
    463 
    464         fibril_mutex_lock(&used_lock);
    465         hash_table_remove(&up_hash, pkey, 3);
     420        fibril_mutex_lock(&used_lock);
     421        hash_table_remove_item(&up_hash, &idx->uph_link);
    466422        fibril_mutex_unlock(&used_lock);
    467423}
     
    471427{
    472428        exfat_idx_t *fidx = NULL;
    473         link_t *l;
    474         unsigned long ikey[] = {
    475                 [UIH_SID_KEY] = service_id,
    476                 [UIH_INDEX_KEY] = index,
     429
     430        idx_key_t idx_key = {
     431                .service_id = service_id,
     432                .index = index,
    477433        };
    478434
    479435        fibril_mutex_lock(&used_lock);
    480         l = hash_table_find(&ui_hash, ikey);
     436        ht_link_t *l = hash_table_find(&ui_hash, &idx_key);
    481437        if (l) {
    482                 fidx = hash_table_get_instance(l, exfat_idx_t, uih_link);
     438                fidx = hash_table_get_inst(l, exfat_idx_t, uih_link);
    483439                fibril_mutex_lock(&fidx->lock);
    484440        }
     
    494450void exfat_idx_destroy(exfat_idx_t *idx)
    495451{
    496         unsigned long ikey[] = {
    497                 [UIH_SID_KEY] = idx->service_id,
    498                 [UIH_INDEX_KEY] = idx->index,
     452        idx_key_t idx_key = {
     453                .service_id = idx->service_id,
     454                .index = idx->index,
    499455        };
    500         service_id_t service_id = idx->service_id;
    501         fs_index_t index = idx->index;
    502456
    503457        /* TODO: assert(idx->pfc == FAT_CLST_RES0); */
     
    510464         * the index hash only.
    511465         */
    512         hash_table_remove(&ui_hash, ikey, 2);
     466        hash_table_remove(&ui_hash, &idx_key);
    513467        fibril_mutex_unlock(&used_lock);
    514468        /* Release the VFS index. */
    515         exfat_index_free(service_id, index);
     469        exfat_index_free(idx_key.service_id, idx_key.index);
    516470        /* The index structure itself is freed in idx_remove_callback(). */
    517471}
     
    519473int exfat_idx_init(void)
    520474{
    521         if (!hash_table_create(&up_hash, 0, 3, &uph_ops))
     475        if (!hash_table_create(&up_hash, 0, 0, &uph_ops))
    522476                return ENOMEM;
    523         if (!hash_table_create(&ui_hash, 0, 2, &uih_ops)) {
     477        if (!hash_table_create(&ui_hash, 0, 0, &uih_ops)) {
    524478                hash_table_destroy(&up_hash);
    525479                return ENOMEM;
     
    531485{
    532486        /* We assume the hash tables are empty. */
     487        assert(hash_table_empty(&up_hash) && hash_table_empty(&ui_hash));
    533488        hash_table_destroy(&up_hash);
    534489        hash_table_destroy(&ui_hash);
     
    555510}
    556511
     512static bool rm_pos_service_id(ht_link_t *item, void *arg)
     513{
     514        service_id_t service_id = *(service_id_t*)arg;
     515        exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uph_link);
     516
     517        if (fidx->service_id == service_id) {
     518                hash_table_remove_item(&up_hash, item);
     519        }
     520       
     521        return true;
     522}
     523
     524static bool rm_idx_service_id(ht_link_t *item, void *arg)
     525{
     526        service_id_t service_id = *(service_id_t*)arg;
     527        exfat_idx_t *fidx = hash_table_get_inst(item, exfat_idx_t, uih_link);
     528
     529        if (fidx->service_id == service_id) {
     530                hash_table_remove_item(&ui_hash, item);
     531        }
     532       
     533        return true;
     534}
     535
    557536void exfat_idx_fini_by_service_id(service_id_t service_id)
    558537{
    559         unsigned long ikey[] = {
    560                 [UIH_SID_KEY] = service_id
    561         };
    562         unsigned long pkey[] = {
    563                 [UPH_SID_KEY] = service_id
    564         };
    565 
    566538        /*
    567539         * Remove this instance's index structure from up_hash and ui_hash.
     
    570542         */
    571543        fibril_mutex_lock(&used_lock);
    572         hash_table_remove(&up_hash, pkey, 1);
    573         hash_table_remove(&ui_hash, ikey, 1);
     544        hash_table_apply(&up_hash, rm_pos_service_id, &service_id);
     545        hash_table_apply(&ui_hash, rm_idx_service_id, &service_id);
    574546        fibril_mutex_unlock(&used_lock);
    575547
  • uspace/srv/fs/exfat/exfat_ops.c

    rb17518e rbc216a0  
    5454#include <byteorder.h>
    5555#include <adt/hash_table.h>
     56#include <adt/hash.h>
    5657#include <adt/list.h>
    5758#include <assert.h>
  • uspace/srv/fs/ext2fs/ext2fs_ops.c

    rb17518e rbc216a0  
    4949#include <byteorder.h>
    5050#include <adt/hash_table.h>
     51#include <adt/hash.h>
    5152#include <adt/list.h>
    5253#include <assert.h>
     
    6263#define EXT2FS_NODE(node)       ((node) ? (ext2fs_node_t *) (node)->data : NULL)
    6364#define EXT2FS_DBG(format, ...) {if (false) printf("ext2fs: %s: " format "\n", __FUNCTION__, ##__VA_ARGS__);}
    64 #define OPEN_NODES_KEYS 2
    65 #define OPEN_NODES_DEV_HANDLE_KEY 0
    66 #define OPEN_NODES_INODE_KEY 1
    6765
    6866typedef struct ext2fs_instance {
     
    7775        ext2_inode_ref_t *inode_ref;
    7876        fs_node_t *fs_node;
    79         link_t link;
     77        ht_link_t link;
    8078        unsigned int references;
    8179} ext2fs_node_t;
     
    121119static FIBRIL_MUTEX_INITIALIZE(open_nodes_lock);
    122120
    123 /* Hash table interface for open nodes hash table */
    124 static size_t open_nodes_key_hash(unsigned long key[])
    125 {
    126         /* Hash construction recommended in Effective Java, 2nd Edition. */
    127         size_t hash = 17;
    128         hash = 31 * hash + key[OPEN_NODES_DEV_HANDLE_KEY];
    129         hash = 31 * hash + key[OPEN_NODES_INODE_KEY];
    130         return hash;
    131 }
    132 
    133 static size_t open_nodes_hash(const link_t *item)
    134 {
    135         ext2fs_node_t *enode = hash_table_get_instance(item, ext2fs_node_t, link);
     121/*
     122 * Hash table interface for open nodes hash table
     123 */
     124
     125typedef struct {
     126        service_id_t service_id;
     127        fs_index_t index;
     128} node_key_t;
     129
     130static size_t open_nodes_key_hash(void *key)
     131{
     132        node_key_t *node_key = (node_key_t*)key;
     133        return hash_combine(node_key->service_id, node_key->index);
     134}
     135
     136static size_t open_nodes_hash(const ht_link_t *item)
     137{
     138        ext2fs_node_t *enode = hash_table_get_inst(item, ext2fs_node_t, link);
    136139
    137140        assert(enode->instance);
    138141        assert(enode->inode_ref);
    139142       
    140         unsigned long key[] = {
    141                 [OPEN_NODES_DEV_HANDLE_KEY] = enode->instance->service_id,
    142                 [OPEN_NODES_INODE_KEY] = enode->inode_ref->index,
    143         };
    144        
    145         return open_nodes_key_hash(key);
    146 }
    147 
    148 static bool open_nodes_match(unsigned long key[], size_t keys,
    149     const link_t *item)
    150 {
    151         ext2fs_node_t *enode = hash_table_get_instance(item, ext2fs_node_t, link);
    152         assert(keys > 0);
    153         if (enode->instance->service_id !=
    154             ((service_id_t) key[OPEN_NODES_DEV_HANDLE_KEY])) {
    155                 return false;
    156         }
    157         if (keys == 1) {
    158                 return true;
    159         }
    160         assert(keys == 2);
    161         return (enode->inode_ref->index == key[OPEN_NODES_INODE_KEY]);
     143        return hash_combine(enode->instance->service_id, enode->inode_ref->index);
     144}
     145
     146static bool open_nodes_key_equal(void *key, const ht_link_t *item)
     147{
     148        node_key_t *node_key = (node_key_t*)key;
     149        ext2fs_node_t *enode = hash_table_get_inst(item, ext2fs_node_t, link);
     150       
     151        return node_key->service_id == enode->instance->service_id
     152                && node_key->index == enode->inode_ref->index;
    162153}
    163154
     
    165156        .hash = open_nodes_hash,
    166157        .key_hash = open_nodes_key_hash,
    167         .match = open_nodes_match,
     158        .key_equal = open_nodes_key_equal,
    168159        .equal = 0,
    169160        .remove_callback = 0,
     
    175166int ext2fs_global_init(void)
    176167{
    177         if (!hash_table_create(&open_nodes, 0, OPEN_NODES_KEYS, &open_nodes_ops)) {
     168        if (!hash_table_create(&open_nodes, 0, 0, &open_nodes_ops)) {
    178169                return ENOMEM;
    179170        }
     
    329320       
    330321        /* Check if the node is not already open */
    331         unsigned long key[] = {
    332                 [OPEN_NODES_DEV_HANDLE_KEY] = inst->service_id,
    333                 [OPEN_NODES_INODE_KEY] = index,
     322        node_key_t key = {
     323                .service_id = inst->service_id,
     324                .index = index
    334325        };
    335         link_t *already_open = hash_table_find(&open_nodes, key);
     326        ht_link_t *already_open = hash_table_find(&open_nodes, &key);
    336327
    337328        if (already_open) {
    338                 enode = hash_table_get_instance(already_open, ext2fs_node_t, link);
     329                enode = hash_table_get_inst(already_open, ext2fs_node_t, link);
    339330                *rfn = enode->fs_node;
    340331                enode->references++;
     
    370361        enode->references = 1;
    371362        enode->fs_node = node;
    372         link_initialize(&enode->link);
    373363       
    374364        node->data = enode;
     
    421411int ext2fs_node_put_core(ext2fs_node_t *enode)
    422412{
    423         int rc;
    424 
    425         unsigned long key[] = {
    426                 [OPEN_NODES_DEV_HANDLE_KEY] = enode->instance->service_id,
    427                 [OPEN_NODES_INODE_KEY] = enode->inode_ref->index,
     413        node_key_t key = {
     414                .service_id = enode->instance->service_id,
     415                .index = enode->inode_ref->index
    428416        };
    429         hash_table_remove(&open_nodes, key, OPEN_NODES_KEYS);
     417
     418        hash_table_remove(&open_nodes, &key);
     419       
    430420        assert(enode->instance->open_nodes_count > 0);
    431421        enode->instance->open_nodes_count--;
    432422
    433         rc = ext2_filesystem_put_inode_ref(enode->inode_ref);
     423        int rc = ext2_filesystem_put_inode_ref(enode->inode_ref);
    434424        if (rc != EOK) {
    435425                EXT2FS_DBG("ext2_filesystem_put_inode_ref failed");
  • uspace/srv/fs/fat/fat.h

    rb17518e rbc216a0  
    190190typedef struct {
    191191        /** Used indices (position) hash table link. */
    192         link_t          uph_link;
     192        ht_link_t               uph_link;
    193193        /** Used indices (index) hash table link. */
    194         link_t          uih_link;
     194        ht_link_t               uih_link;
    195195
    196196        fibril_mutex_t  lock;
  • uspace/srv/fs/fat/fat_idx.c

    rb17518e rbc216a0  
    4141#include <str.h>
    4242#include <adt/hash_table.h>
     43#include <adt/hash.h>
    4344#include <adt/list.h>
    4445#include <assert.h>
    4546#include <fibril_synch.h>
    4647#include <malloc.h>
    47 
    4848
    4949/** Each instance of this type describes one interval of freed VFS indices. */
     
    5959 */
    6060typedef struct {
    61         link_t          link;
    62         service_id_t    service_id;
     61        link_t link;
     62        service_id_t service_id;
    6363
    6464        /** Next unassigned index. */
     
    9898                        return u;
    9999        }
    100        
     100
    101101        if (lock)
    102102                fibril_mutex_unlock(&unused_lock);
     
    114114static hash_table_t up_hash;
    115115
    116 #define UPH_SID_KEY     0
    117 #define UPH_PFC_KEY     1
    118 #define UPH_PDI_KEY     2
    119 
    120 static size_t pos_key_hash(unsigned long key[])
    121 {
    122         /* Inspired by Effective Java, 2nd edition. */
    123         size_t hash = 17;
    124        
    125         hash = 31 * hash + key[UPH_PFC_KEY];
    126         hash = 31 * hash + key[UPH_PDI_KEY];
    127         hash = 31 * hash + key[UPH_SID_KEY];
    128        
    129         return hash;
    130 }
    131 
    132 static size_t pos_hash(const link_t *item)
    133 {
    134         fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uph_link);
    135        
    136         unsigned long pkey[] = {
    137                 [UPH_SID_KEY] = fidx->service_id,
    138                 [UPH_PFC_KEY] = fidx->pfc,
    139                 [UPH_PDI_KEY] = fidx->pdi,
    140         };
    141        
    142         return pos_key_hash(pkey);
    143 }
    144 
    145 static bool pos_match(unsigned long key[], size_t keys, const link_t *item)
    146 {
    147         service_id_t service_id = (service_id_t)key[UPH_SID_KEY];
     116typedef struct {
     117        service_id_t service_id;
    148118        fat_cluster_t pfc;
    149119        unsigned pdi;
    150         fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uph_link);
    151 
    152         switch (keys) {
    153         case 1:
    154                 return (service_id == fidx->service_id);
    155         case 3:
    156                 pfc = (fat_cluster_t) key[UPH_PFC_KEY];
    157                 pdi = (unsigned) key[UPH_PDI_KEY];
    158                 return (service_id == fidx->service_id) && (pfc == fidx->pfc) &&
    159                     (pdi == fidx->pdi);
    160         default:
    161                 assert((keys == 1) || (keys == 3));
    162         }
    163 
    164         return 0;
     120} pos_key_t;
     121
     122static inline size_t pos_key_hash(void *key)
     123{
     124        pos_key_t *pos = (pos_key_t*)key;
     125       
     126        size_t hash = 0;
     127        hash = hash_combine(pos->pfc, pos->pdi);
     128        return hash_combine(hash, pos->service_id);
     129}
     130
     131static size_t pos_hash(const ht_link_t *item)
     132{
     133        fat_idx_t *fidx = hash_table_get_inst(item, fat_idx_t, uph_link);
     134       
     135        pos_key_t pkey = {
     136                .service_id = fidx->service_id,
     137                .pfc = fidx->pfc,
     138                .pdi = fidx->pdi,
     139        };
     140       
     141        return pos_key_hash(&pkey);
     142}
     143
     144static bool pos_key_equal(void *key, const ht_link_t *item)
     145{
     146        pos_key_t *pos = (pos_key_t*)key;
     147        fat_idx_t *fidx = hash_table_get_inst(item, fat_idx_t, uph_link);
     148       
     149        return pos->service_id == fidx->service_id
     150                && pos->pdi == fidx->pdi
     151                && pos->pfc == fidx->pfc;
    165152}
    166153
     
    168155        .hash = pos_hash,
    169156        .key_hash = pos_key_hash,
    170         .match = pos_match,
     157        .key_equal = pos_key_equal,
    171158        .equal = 0,
    172159        .remove_callback = 0,
     
    179166static hash_table_t ui_hash;
    180167
    181 #define UIH_SID_KEY     0
    182 #define UIH_INDEX_KEY   1
    183 
    184 static size_t idx_key_hash(unsigned long key[])
    185 {
    186         /*
    187          * Compute a simple hash unlimited by specific table size as per:
    188          * Effective Java, 2nd edition.
    189          */
    190         size_t hash = 17;
    191         hash = 31 * hash + key[UIH_SID_KEY];
    192         hash = 31 * hash + key[UIH_INDEX_KEY];
    193         return hash;
    194 }
    195 
    196 static size_t idx_hash(const link_t *item)
    197 {
    198         fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link);
    199        
    200         unsigned long ikey[] = {
    201                 [UIH_SID_KEY] = fidx->service_id,
    202                 [UIH_INDEX_KEY] = fidx->index,
    203         };
    204 
    205         return idx_key_hash(ikey);
    206 }
    207 
    208 static bool idx_match(unsigned long key[], size_t keys, const link_t *item)
    209 {
    210         service_id_t service_id = (service_id_t)key[UIH_SID_KEY];
     168typedef struct {
     169        service_id_t service_id;
    211170        fs_index_t index;
    212         fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link);
    213 
    214         switch (keys) {
    215         case 1:
    216                 return (service_id == fidx->service_id);
    217         case 2:
    218                 index = (fs_index_t) key[UIH_INDEX_KEY];
    219                 return (service_id == fidx->service_id) &&
    220                     (index == fidx->index);
    221         default:
    222                 assert((keys == 1) || (keys == 2));
    223         }
    224 
    225         return 0;
    226 }
    227 
    228 static void idx_remove_callback(link_t *item)
    229 {
    230         fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link);
     171} idx_key_t;
     172
     173static size_t idx_key_hash(void *key_arg)
     174{
     175        idx_key_t *key = (idx_key_t*)key_arg;
     176        return hash_combine(key->service_id, key->index);
     177}
     178
     179static size_t idx_hash(const ht_link_t *item)
     180{
     181        fat_idx_t *fidx = hash_table_get_inst(item, fat_idx_t, uih_link);
     182        return hash_combine(fidx->service_id, fidx->index);
     183}
     184
     185static bool idx_key_equal(void *key_arg, const ht_link_t *item)
     186{
     187        fat_idx_t *fidx = hash_table_get_inst(item, fat_idx_t, uih_link);
     188        idx_key_t *key = (idx_key_t*)key_arg;
     189       
     190        return key->index == fidx->index && key->service_id == fidx->service_id;
     191}
     192
     193static void idx_remove_callback(ht_link_t *item)
     194{
     195        fat_idx_t *fidx = hash_table_get_inst(item, fat_idx_t, uih_link);
    231196
    232197        free(fidx);
     
    236201        .hash = idx_hash,
    237202        .key_hash = idx_key_hash,
    238         .match = idx_match,
     203        .key_equal = idx_key_equal,
    239204        .equal = 0,
    240205        .remove_callback = idx_remove_callback,
     
    378343        }
    379344               
    380         link_initialize(&fidx->uph_link);
    381         link_initialize(&fidx->uih_link);
    382345        fibril_mutex_initialize(&fidx->lock);
    383346        fidx->service_id = service_id;
     
    414377{
    415378        fat_idx_t *fidx;
    416         link_t *l;
    417         unsigned long pkey[] = {
    418                 [UPH_SID_KEY] = service_id,
    419                 [UPH_PFC_KEY] = pfc,
    420                 [UPH_PDI_KEY] = pdi,
     379
     380        pos_key_t pos_key = {
     381                .service_id = service_id,
     382                .pfc = pfc,
     383                .pdi = pdi,
    421384        };
    422385
    423386        fibril_mutex_lock(&used_lock);
    424         l = hash_table_find(&up_hash, pkey);
     387        ht_link_t *l = hash_table_find(&up_hash, &pos_key);
    425388        if (l) {
    426                 fidx = hash_table_get_instance(l, fat_idx_t, uph_link);
     389                fidx = hash_table_get_inst(l, fat_idx_t, uph_link);
    427390        } else {
    428391                int rc;
     
    464427{
    465428        fat_idx_t *fidx = NULL;
    466         link_t *l;
    467         unsigned long ikey[] = {
    468                 [UIH_SID_KEY] = service_id,
    469                 [UIH_INDEX_KEY] = index,
     429
     430        idx_key_t idx_key = {
     431                .service_id = service_id,
     432                .index = index,
    470433        };
    471434
    472435        fibril_mutex_lock(&used_lock);
    473         l = hash_table_find(&ui_hash, ikey);
     436        ht_link_t *l = hash_table_find(&ui_hash, &idx_key);
    474437        if (l) {
    475                 fidx = hash_table_get_instance(l, fat_idx_t, uih_link);
     438                fidx = hash_table_get_inst(l, fat_idx_t, uih_link);
    476439                fibril_mutex_lock(&fidx->lock);
    477440        }
     
    487450void fat_idx_destroy(fat_idx_t *idx)
    488451{
    489         unsigned long ikey[] = {
    490                 [UIH_SID_KEY] = idx->service_id,
    491                 [UIH_INDEX_KEY] = idx->index,
     452        idx_key_t idx_key = {
     453                .service_id = idx->service_id,
     454                .index = idx->index,
    492455        };
    493         service_id_t service_id = idx->service_id;
    494         fs_index_t index = idx->index;
    495456
    496457        assert(idx->pfc == FAT_CLST_RES0);
     
    502463         * the index hash only.
    503464         */
    504         hash_table_remove(&ui_hash, ikey, 2);
     465        hash_table_remove(&ui_hash, &idx_key);
    505466        fibril_mutex_unlock(&used_lock);
    506467        /* Release the VFS index. */
    507         fat_index_free(service_id, index);
     468        fat_index_free(idx_key.service_id, idx_key.index);
    508469        /* The index structure itself is freed in idx_remove_callback(). */
    509470}
     
    511472int fat_idx_init(void)
    512473{
    513         if (!hash_table_create(&up_hash, 0, 3, &uph_ops))
     474        if (!hash_table_create(&up_hash, 0, 0, &uph_ops))
    514475                return ENOMEM;
    515         if (!hash_table_create(&ui_hash, 0, 2, &uih_ops)) {
     476        if (!hash_table_create(&ui_hash, 0, 0, &uih_ops)) {
    516477                hash_table_destroy(&up_hash);
    517478                return ENOMEM;
     
    523484{
    524485        /* We assume the hash tables are empty. */
     486        assert(hash_table_empty(&up_hash) && hash_table_empty(&ui_hash));
    525487        hash_table_destroy(&up_hash);
    526488        hash_table_destroy(&ui_hash);
     
    547509}
    548510
     511static bool rm_pos_service_id(ht_link_t *item, void *arg)
     512{
     513        service_id_t service_id = *(service_id_t*)arg;
     514        fat_idx_t *fidx = hash_table_get_inst(item, fat_idx_t, uph_link);
     515
     516        if (fidx->service_id == service_id) {
     517                hash_table_remove_item(&up_hash, item);
     518        }
     519       
     520        return true;
     521}
     522
     523static bool rm_idx_service_id(ht_link_t *item, void *arg)
     524{
     525        service_id_t service_id = *(service_id_t*)arg;
     526        fat_idx_t *fidx = hash_table_get_inst(item, fat_idx_t, uih_link);
     527
     528        if (fidx->service_id == service_id) {
     529                hash_table_remove_item(&ui_hash, item);
     530        }
     531       
     532        return true;
     533}
     534
    549535void fat_idx_fini_by_service_id(service_id_t service_id)
    550536{
    551         unsigned long ikey[] = {
    552                 [UIH_SID_KEY] = service_id
    553         };
    554         unsigned long pkey[] = {
    555                 [UPH_SID_KEY] = service_id
    556         };
    557 
    558537        /*
    559538         * Remove this instance's index structure from up_hash and ui_hash.
     
    562541         */
    563542        fibril_mutex_lock(&used_lock);
    564         hash_table_remove(&up_hash, pkey, 1);
    565         hash_table_remove(&ui_hash, ikey, 1);
     543        hash_table_apply(&up_hash, rm_pos_service_id, &service_id);
     544        hash_table_apply(&ui_hash, rm_idx_service_id, &service_id);
    566545        fibril_mutex_unlock(&used_lock);
    567546
  • uspace/srv/fs/locfs/locfs_ops.c

    rb17518e rbc216a0  
    6161        async_sess_t *sess;       /**< If NULL, the structure is incomplete. */
    6262        size_t refcount;
    63         link_t link;
     63        ht_link_t link;
    6464        fibril_condvar_t cv;      /**< Broadcast when completed. */
    6565} service_t;
     
    7171static FIBRIL_MUTEX_INITIALIZE(services_mutex);
    7272
    73 #define SERVICES_KEYS        1
    74 #define SERVICES_KEY_HANDLE  0
    75 
    7673/* Implementation of hash table interface for the nodes hash table. */
    7774
    78 static size_t services_key_hash(unsigned long key[])
    79 {
    80         return key[SERVICES_KEY_HANDLE];
    81 }
    82 
    83 static size_t services_hash(const link_t *item)
    84 {
    85         service_t *dev = hash_table_get_instance(item, service_t, link);
    86         unsigned long key[] = {
    87                 [SERVICES_KEY_HANDLE] = dev->service_id
    88         };
    89        
    90         return services_key_hash(key);
    91 }
    92 
    93 static bool services_match(unsigned long key[], size_t keys, const link_t *item)
    94 {
    95         assert(keys == 1);
    96         service_t *dev = hash_table_get_instance(item, service_t, link);
    97         return (dev->service_id == (service_id_t) key[SERVICES_KEY_HANDLE]);
    98 }
    99 
    100 static void services_remove_callback(link_t *item)
    101 {
    102         free(hash_table_get_instance(item, service_t, link));
     75static size_t services_key_hash(void *key)
     76{
     77        return *(service_id_t*)key;
     78}
     79
     80static size_t services_hash(const ht_link_t *item)
     81{
     82        service_t *dev = hash_table_get_inst(item, service_t, link);
     83        return dev->service_id;
     84}
     85
     86static bool services_key_equal(void *key, const ht_link_t *item)
     87{
     88        service_t *dev = hash_table_get_inst(item, service_t, link);
     89        return (dev->service_id == *(service_id_t*)key);
     90}
     91
     92static void services_remove_callback(ht_link_t *item)
     93{
     94        free(hash_table_get_inst(item, service_t, link));
    10395}
    10496
     
    10698        .hash = services_hash,
    10799        .key_hash = services_key_hash,
    108         .match = services_match,
     100        .key_equal = services_key_equal,
    109101        .equal = 0,
    110102        .remove_callback = services_remove_callback
     
    242234                /* Device node */
    243235               
    244                 unsigned long key[] = {
    245                         [SERVICES_KEY_HANDLE] = (unsigned long) node->service_id
    246                 };
    247                 link_t *lnk;
    248                
    249236                fibril_mutex_lock(&services_mutex);
     237                ht_link_t *lnk;
    250238restart:
    251                 lnk = hash_table_find(&services, key);
     239                lnk = hash_table_find(&services, &node->service_id);
    252240                if (lnk == NULL) {
    253241                        service_t *dev = (service_t *) malloc(sizeof(service_t));
     
    292280                                 * entry and free the device structure.
    293281                                 */
    294                                 hash_table_remove(&services, key, SERVICES_KEYS);
     282                                hash_table_remove(&services, &node->service_id);
    295283                                fibril_mutex_unlock(&services_mutex);
    296284                               
     
    301289                        dev->sess = sess;
    302290                } else {
    303                         service_t *dev = hash_table_get_instance(lnk, service_t, link);
     291                        service_t *dev = hash_table_get_inst(lnk, service_t, link);
    304292                       
    305293                        if (!dev->sess) {
     
    463451bool locfs_init(void)
    464452{
    465         if (!hash_table_create(&services, 0,  SERVICES_KEYS, &services_ops))
     453        if (!hash_table_create(&services, 0,  0, &services_ops))
    466454                return false;
    467455       
     
    567555                /* Device node */
    568556               
    569                 unsigned long key[] = {
    570                         [SERVICES_KEY_HANDLE] = (unsigned long) index
    571                 };
    572                
    573557                fibril_mutex_lock(&services_mutex);
    574                 link_t *lnk = hash_table_find(&services, key);
     558                ht_link_t *lnk = hash_table_find(&services, &index);
    575559                if (lnk == NULL) {
    576560                        fibril_mutex_unlock(&services_mutex);
     
    578562                }
    579563               
    580                 service_t *dev = hash_table_get_instance(lnk, service_t, link);
     564                service_t *dev = hash_table_get_inst(lnk, service_t, link);
    581565                assert(dev->sess);
    582566               
     
    633617        if (type == LOC_OBJECT_SERVICE) {
    634618                /* Device node */
    635                 unsigned long key[] = {
    636                         [SERVICES_KEY_HANDLE] = (unsigned long) index
    637                 };
    638619               
    639620                fibril_mutex_lock(&services_mutex);
    640                 link_t *lnk = hash_table_find(&services, key);
     621                ht_link_t *lnk = hash_table_find(&services, &index);
    641622                if (lnk == NULL) {
    642623                        fibril_mutex_unlock(&services_mutex);
     
    644625                }
    645626               
    646                 service_t *dev = hash_table_get_instance(lnk, service_t, link);
     627                service_t *dev = hash_table_get_inst(lnk, service_t, link);
    647628                assert(dev->sess);
    648629               
     
    703684       
    704685        if (type == LOC_OBJECT_SERVICE) {
    705                 unsigned long key[] = {
    706                         [SERVICES_KEY_HANDLE] = (unsigned long) index
    707                 };
    708686               
    709687                fibril_mutex_lock(&services_mutex);
    710                 link_t *lnk = hash_table_find(&services, key);
     688                ht_link_t *lnk = hash_table_find(&services, &index);
    711689                if (lnk == NULL) {
    712690                        fibril_mutex_unlock(&services_mutex);
     
    714692                }
    715693               
    716                 service_t *dev = hash_table_get_instance(lnk, service_t, link);
     694                service_t *dev = hash_table_get_inst(lnk, service_t, link);
    717695                assert(dev->sess);
    718696                dev->refcount--;
     
    720698                if (dev->refcount == 0) {
    721699                        async_hangup(dev->sess);
    722                         hash_table_remove(&services, key, SERVICES_KEYS);
     700                        hash_table_remove(&services, &index);
    723701                }
    724702               
     
    744722       
    745723        if (type == LOC_OBJECT_SERVICE) {
    746                 unsigned long key[] = {
    747                         [SERVICES_KEY_HANDLE] = (unsigned long) index
    748                 };
    749                
     724
    750725                fibril_mutex_lock(&services_mutex);
    751                 link_t *lnk = hash_table_find(&services, key);
     726                ht_link_t *lnk = hash_table_find(&services, &index);
    752727                if (lnk == NULL) {
    753728                        fibril_mutex_unlock(&services_mutex);
     
    755730                }
    756731               
    757                 service_t *dev = hash_table_get_instance(lnk, service_t, link);
     732                service_t *dev = hash_table_get_inst(lnk, service_t, link);
    758733                assert(dev->sess);
    759734               
  • uspace/srv/fs/mfs/mfs.h

    rb17518e rbc216a0  
    142142        unsigned refcnt;
    143143        fs_node_t *fsnode;
    144         link_t link;
     144        ht_link_t link;
    145145};
    146146
  • uspace/srv/fs/mfs/mfs_ops.c

    rb17518e rbc216a0  
    3535#include <align.h>
    3636#include <adt/hash_table.h>
     37#include <adt/hash.h>
    3738#include "mfs.h"
    3839
    39 #define OPEN_NODES_KEYS 2
    40 #define OPEN_NODES_SERVICE_KEY 0
    41 #define OPEN_NODES_INODE_KEY 1
    4240
    4341static bool check_magic_number(uint16_t magic, bool *native,
     
    6058static int mfs_unlink(fs_node_t *, fs_node_t *, const char *name);
    6159static int mfs_destroy_node(fs_node_t *fn);
    62 static size_t open_nodes_hash(const link_t *item);
    63 static size_t open_nodes_key_hash(unsigned long key[]);
    64 static bool open_nodes_match(unsigned long key[], size_t keys,
    65     const link_t *item);
    6660static int mfs_node_get(fs_node_t **rfn, service_id_t service_id,
    6761    fs_index_t index);
     
    9589/* Hash table interface for open nodes hash table */
    9690
     91typedef struct {
     92        service_id_t service_id;
     93        fs_index_t index;
     94} node_key_t;
     95
    9796static size_t
    98 open_nodes_key_hash(unsigned long key[])
    99 {
    100         /* As recommended by Effective Java, 2nd Edition. */
    101         size_t hash = 17;
    102         hash = 37 * hash + key[OPEN_NODES_SERVICE_KEY];
    103         hash = 37 * hash + key[OPEN_NODES_INODE_KEY];
    104         return hash;
     97open_nodes_key_hash(void *key)
     98{
     99        node_key_t *node_key = (node_key_t*)key;
     100        return hash_combine(node_key->service_id, node_key->index);
    105101}
    106102
    107103static size_t
    108 open_nodes_hash(const link_t *item)
    109 {
    110         struct mfs_node *m = hash_table_get_instance(item, struct mfs_node, link);
    111        
    112         unsigned long key[] = {
    113                 [OPEN_NODES_SERVICE_KEY] = m->instance->service_id,
    114                 [OPEN_NODES_INODE_KEY] = m->ino_i->index,
    115         };
    116        
    117         return open_nodes_key_hash(key);
     104open_nodes_hash(const ht_link_t *item)
     105{
     106        struct mfs_node *m = hash_table_get_inst(item, struct mfs_node, link);
     107        return hash_combine(m->instance->service_id, m->ino_i->index);
    118108}
    119109
    120110static bool
    121 open_nodes_match(unsigned long key[], size_t keys, const link_t *item)
    122 {
    123         assert(keys == 2);
    124         struct mfs_node *mnode = hash_table_get_instance(item, struct mfs_node, link);
    125        
    126         service_id_t service_id = ((service_id_t) key[OPEN_NODES_SERVICE_KEY]);
    127        
    128         return mnode->instance->service_id == service_id
    129                 && mnode->ino_i->index == key[OPEN_NODES_INODE_KEY];
    130 }
    131 
     111open_nodes_key_equal(void *key, const ht_link_t *item)
     112{
     113        node_key_t *node_key = (node_key_t*)key;
     114        struct mfs_node *mnode = hash_table_get_inst(item, struct mfs_node, link);
     115
     116        return node_key->service_id == mnode->instance->service_id
     117                && node_key->index == mnode->ino_i->index;
     118}
    132119
    133120static hash_table_ops_t open_nodes_ops = {
    134121        .hash = open_nodes_hash,
    135122        .key_hash = open_nodes_key_hash,
    136         .match = open_nodes_match,
     123        .key_equal = open_nodes_key_equal,
    137124        .equal = 0,
    138125        .remove_callback = 0,
     
    142129mfs_global_init(void)
    143130{
    144         if (!hash_table_create(&open_nodes, 0, OPEN_NODES_KEYS, &open_nodes_ops)) {
     131        if (!hash_table_create(&open_nodes, 0, 0, &open_nodes_ops)) {
    145132                return ENOMEM;
    146133        }
     
    413400        mnode->refcnt = 1;
    414401
    415         link_initialize(&mnode->link);
    416 
    417402        fibril_mutex_lock(&open_nodes_lock);
    418403        hash_table_insert(&open_nodes, &mnode->link);
     
    515500        mnode->refcnt--;
    516501        if (mnode->refcnt == 0) {
    517                 unsigned long key[] = {
    518                         [OPEN_NODES_SERVICE_KEY] = mnode->instance->service_id,
    519                         [OPEN_NODES_INODE_KEY] = mnode->ino_i->index
    520                 };
    521                 hash_table_remove(&open_nodes, key, OPEN_NODES_KEYS);
     502                hash_table_remove_item(&open_nodes, &mnode->link);
    522503                assert(mnode->instance->open_nodes_cnt > 0);
    523504                mnode->instance->open_nodes_cnt--;
     
    578559
    579560        /* Check if the node is not already open */
    580         unsigned long key[] = {
    581                 [OPEN_NODES_SERVICE_KEY] = inst->service_id,
    582                 [OPEN_NODES_INODE_KEY] = index,
     561        node_key_t key = {
     562                .service_id = inst->service_id,
     563                .index = index
    583564        };
    584         link_t *already_open = hash_table_find(&open_nodes, key);
     565       
     566        ht_link_t *already_open = hash_table_find(&open_nodes, &key);
    585567
    586568        if (already_open) {
    587                 mnode = hash_table_get_instance(already_open, struct mfs_node, link);
     569                mnode = hash_table_get_inst(already_open, struct mfs_node, link);
    588570                *rfn = mnode->fsnode;
    589571                mnode->refcnt++;
     
    616598        mnode->ino_i = ino_i;
    617599        mnode->refcnt = 1;
    618         link_initialize(&mnode->link);
    619600
    620601        mnode->instance = inst;
  • uspace/srv/fs/tmpfs/tmpfs.h

    rb17518e rbc216a0  
    6262        fs_index_t index;       /**< TMPFS node index. */
    6363        service_id_t service_id;/**< Service ID of block device. */
    64         link_t nh_link;         /**< Nodes hash table link. */
     64        ht_link_t nh_link;              /**< Nodes hash table link. */
    6565        tmpfs_dentry_type_t type;
    6666        unsigned lnkcnt;        /**< Link count. */
  • uspace/srv/fs/tmpfs/tmpfs_ops.c

    rb17518e rbc216a0  
    5050#include <sys/types.h>
    5151#include <adt/hash_table.h>
     52#include <adt/hash.h>
    5253#include <as.h>
    5354#include <libfs.h>
     
    140141hash_table_t nodes;
    141142
    142 #define NODES_KEY_DEV   0       
    143 #define NODES_KEY_INDEX 1
    144 
    145 /* Implementation of hash table interface for the nodes hash table. */
    146 static size_t nodes_key_hash(unsigned long key[])
    147 {
    148         /* Based on Effective Java, 2nd Edition. */
    149         size_t hash = 17;
    150         hash = 37 * hash + key[NODES_KEY_DEV];
    151         hash = 37 * hash + key[NODES_KEY_INDEX];
    152         return hash;
    153 }
    154 
    155 static size_t nodes_hash(const link_t *item)
    156 {
    157         tmpfs_node_t *nodep = hash_table_get_instance(item, tmpfs_node_t, nh_link);
    158        
    159         unsigned long key[] = {
    160                 [NODES_KEY_DEV] = nodep->service_id,
    161                 [NODES_KEY_INDEX] = nodep->index
    162         };
    163        
    164         return nodes_key_hash(key);
    165 }
    166 
    167 static bool nodes_match(unsigned long key[], size_t keys, const link_t *item)
    168 {
    169         tmpfs_node_t *nodep = hash_table_get_instance(item, tmpfs_node_t,
    170             nh_link);
    171        
    172         switch (keys) {
    173         case 1:
    174                 return (nodep->service_id == key[NODES_KEY_DEV]);
    175         case 2:
    176                 return ((nodep->service_id == key[NODES_KEY_DEV]) &&
    177                     (nodep->index == key[NODES_KEY_INDEX]));
    178         default:
    179                 assert((keys == 1) || (keys == 2));
    180         }
    181 
    182         return 0;
    183 }
    184 
    185 static void nodes_remove_callback(link_t *item)
    186 {
    187         tmpfs_node_t *nodep = hash_table_get_instance(item, tmpfs_node_t,
    188             nh_link);
     143/*
     144 * Implementation of hash table interface for the nodes hash table.
     145 */
     146
     147typedef struct {
     148        service_id_t service_id;
     149        fs_index_t index;
     150} node_key_t;
     151
     152static size_t nodes_key_hash(void *k)
     153{
     154        node_key_t *key = (node_key_t *)k;
     155        return hash_combine(key->service_id, key->index);
     156}
     157
     158static size_t nodes_hash(const ht_link_t *item)
     159{
     160        tmpfs_node_t *nodep = hash_table_get_inst(item, tmpfs_node_t, nh_link);
     161        return hash_combine(nodep->service_id, nodep->index);
     162}
     163
     164static bool nodes_key_equal(void *key_arg, const ht_link_t *item)
     165{
     166        tmpfs_node_t *node = hash_table_get_inst(item, tmpfs_node_t, nh_link);
     167        node_key_t *key = (node_key_t *)key_arg;
     168       
     169        return key->service_id == node->service_id && key->index == node->index;
     170}
     171
     172static void nodes_remove_callback(ht_link_t *item)
     173{
     174        tmpfs_node_t *nodep = hash_table_get_inst(item, tmpfs_node_t, nh_link);
    189175
    190176        while (!list_empty(&nodep->cs_list)) {
     
    209195        .hash = nodes_hash,
    210196        .key_hash = nodes_key_hash,
    211         .match = nodes_match,
     197        .key_equal = nodes_key_equal,
    212198        .equal = 0,
    213199        .remove_callback = nodes_remove_callback
     
    223209        nodep->size = 0;
    224210        nodep->data = NULL;
    225         link_initialize(&nodep->nh_link);
    226211        list_initialize(&nodep->cs_list);
    227212}
     
    236221bool tmpfs_init(void)
    237222{
    238         if (!hash_table_create(&nodes, 0, 2, &nodes_ops))
     223        if (!hash_table_create(&nodes, 0, 0, &nodes_ops))
    239224                return false;
    240225       
     
    254239}
    255240
     241static bool rm_service_id_nodes(ht_link_t *item, void *arg)
     242{
     243        service_id_t sid = *(service_id_t*)arg;
     244        tmpfs_node_t *node = hash_table_get_inst(item, tmpfs_node_t, nh_link);
     245       
     246        if (node->service_id == sid) {
     247                hash_table_remove_item(&nodes, &node->nh_link);
     248        }
     249        return true;
     250}
     251
    256252static void tmpfs_instance_done(service_id_t service_id)
    257 {
    258         unsigned long key[] = {
    259                 [NODES_KEY_DEV] = service_id
    260         };
    261         /*
    262          * Here we are making use of one special feature of our hash table
    263          * implementation, which allows to remove more items based on a partial
    264          * key match. In the following, we are going to remove all nodes
    265          * matching our device handle. The nodes_remove_callback() function will
    266          * take care of resource deallocation.
    267          */
    268         hash_table_remove(&nodes, key, 1);
     253{       
     254        hash_table_apply(&nodes, rm_service_id_nodes, &service_id);
    269255}
    270256
     
    288274int tmpfs_node_get(fs_node_t **rfn, service_id_t service_id, fs_index_t index)
    289275{
    290         unsigned long key[] = {
    291                 [NODES_KEY_DEV] = service_id,
    292                 [NODES_KEY_INDEX] = index
     276        node_key_t key = {
     277                .service_id = service_id,
     278                .index = index
    293279        };
    294         link_t *lnk = hash_table_find(&nodes, key);
     280       
     281        ht_link_t *lnk = hash_table_find(&nodes, &key);
     282       
    295283        if (lnk) {
    296284                tmpfs_node_t *nodep;
    297                 nodep = hash_table_get_instance(lnk, tmpfs_node_t, nh_link);
     285                nodep = hash_table_get_inst(lnk, tmpfs_node_t, nh_link);
    298286                *rfn = FS_NODE(nodep);
    299287        } else {
     
    358346        assert(!nodep->lnkcnt);
    359347        assert(list_empty(&nodep->cs_list));
    360 
    361         unsigned long key[] = {
    362                 [NODES_KEY_DEV] = nodep->service_id,
    363                 [NODES_KEY_INDEX] = nodep->index
    364         };
    365         hash_table_remove(&nodes, key, 2);
     348       
     349        hash_table_remove_item(&nodes, &nodep->nh_link);
    366350
    367351        /*
     
    488472         * Lookup the respective TMPFS node.
    489473         */
    490         link_t *hlp;
    491         unsigned long key[] = {
    492                 [NODES_KEY_DEV] = service_id,
    493                 [NODES_KEY_INDEX] = index
     474        node_key_t key = {
     475                .service_id = service_id,
     476                .index = index
    494477        };
    495         hlp = hash_table_find(&nodes, key);
     478       
     479        ht_link_t *hlp = hash_table_find(&nodes, &key);
    496480        if (!hlp)
    497481                return ENOENT;
    498         tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
    499             nh_link);
     482       
     483        tmpfs_node_t *nodep = hash_table_get_inst(hlp, tmpfs_node_t, nh_link);
    500484       
    501485        /*
     
    550534         * Lookup the respective TMPFS node.
    551535         */
    552         link_t *hlp;
    553         unsigned long key[] = {
    554                 [NODES_KEY_DEV] = service_id,
    555                 [NODES_KEY_INDEX] = index
     536        node_key_t key = {
     537                .service_id = service_id,
     538                .index = index
    556539        };
    557         hlp = hash_table_find(&nodes, key);
     540       
     541        ht_link_t *hlp = hash_table_find(&nodes, &key);
     542       
    558543        if (!hlp)
    559544                return ENOENT;
    560         tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
    561             nh_link);
     545       
     546        tmpfs_node_t *nodep = hash_table_get_inst(hlp, tmpfs_node_t, nh_link);
    562547
    563548        /*
     
    612597         * Lookup the respective TMPFS node.
    613598         */
    614         unsigned long key[] = {
    615                 [NODES_KEY_DEV] = service_id,
    616                 [NODES_KEY_INDEX] = index
     599        node_key_t key = {
     600                .service_id = service_id,
     601                .index = index
    617602        };
    618         link_t *hlp = hash_table_find(&nodes, key);
     603       
     604        ht_link_t *hlp = hash_table_find(&nodes, &key);
     605       
    619606        if (!hlp)
    620607                return ENOENT;
    621         tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, nh_link);
     608        tmpfs_node_t *nodep = hash_table_get_inst(hlp, tmpfs_node_t, nh_link);
    622609       
    623610        if (size == nodep->size)
     
    648635static int tmpfs_destroy(service_id_t service_id, fs_index_t index)
    649636{
    650         link_t *hlp;
    651         unsigned long key[] = {
    652                 [NODES_KEY_DEV] = service_id,
    653                 [NODES_KEY_INDEX] = index
     637        node_key_t key = {
     638                .service_id = service_id,
     639                .index = index
    654640        };
    655         hlp = hash_table_find(&nodes, key);
     641       
     642        ht_link_t *hlp = hash_table_find(&nodes, &key);
    656643        if (!hlp)
    657644                return ENOENT;
    658         tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
     645        tmpfs_node_t *nodep = hash_table_get_inst(hlp, tmpfs_node_t,
    659646            nh_link);
    660647        return tmpfs_destroy_node(FS_NODE(nodep));
  • uspace/srv/hid/input/generic/gsp.c

    rb17518e rbc216a0  
    5151#include <gsp.h>
    5252#include <adt/hash_table.h>
     53#include <adt/hash.h>
    5354#include <stdlib.h>
    5455#include <stdio.h>
    5556
    5657/*
    57  * Hash table operations for the transition function.
    58  */
    59 
    60 static size_t trans_op_hash(const link_t *item);
    61 static size_t trans_op_key_hash(unsigned long key[]);
    62 static bool trans_op_match(unsigned long key[], size_t keys, const link_t *item);
     58 * Transition function hash table operations.
     59 */
     60typedef struct {
     61        int old_state;
     62        int input;
     63} trans_key_t;
     64
     65static size_t trans_key_hash(void *key)
     66{
     67        trans_key_t *trans_key = (trans_key_t *)key;
     68        return hash_combine(trans_key->input, trans_key->old_state);
     69}
     70
     71static size_t trans_hash(const ht_link_t *item)
     72{
     73        gsp_trans_t *t = hash_table_get_inst(item, gsp_trans_t, link);
     74        return hash_combine(t->input, t->old_state);
     75}
     76
     77static bool trans_key_equal(void *key, const ht_link_t *item)
     78{
     79        trans_key_t *trans_key = (trans_key_t *)key;
     80        gsp_trans_t *t = hash_table_get_inst(item, gsp_trans_t, link);
     81       
     82        return trans_key->input == t->input && trans_key->old_state == t->old_state;
     83}
    6384
    6485static hash_table_ops_t trans_ops = {
    65         .hash = trans_op_hash,
    66         .key_hash = trans_op_key_hash,
    67         .match = trans_op_match,
     86        .hash = trans_hash,
     87        .key_hash = trans_key_hash,
     88        .key_equal = trans_key_equal,
    6889        .equal = 0,
    6990        .remove_callback = 0
    7091};
    7192
     93
    7294static gsp_trans_t *trans_lookup(gsp_t *p, int state, int input);
    7395static void trans_insert(gsp_t *p, gsp_trans_t *t);
     
    78100{
    79101        p->states = 1;
    80         hash_table_create(&p->trans, 0, 2, &trans_ops);
     102        hash_table_create(&p->trans, 0, 0, &trans_ops);
    81103}
    82104
     
    222244static gsp_trans_t *trans_lookup(gsp_t *p, int state, int input)
    223245{
    224         link_t *item;
    225         unsigned long key[2];
    226 
    227         key[0] = state;
    228         key[1] = input;
    229 
    230         item = hash_table_find(&p->trans, key);
     246        ht_link_t *item;
     247       
     248        trans_key_t key = {
     249                .input = input,
     250                .old_state = state
     251        };
     252
     253        item = hash_table_find(&p->trans, &key);
    231254        if (item == NULL) return NULL;
    232255
    233         return hash_table_get_instance(item, gsp_trans_t, link);
     256        return hash_table_get_inst(item, gsp_trans_t, link);
    234257}
    235258
     
    258281}
    259282
    260 /*
    261  * Transition function hash table operations.
    262  */
    263 
    264 static size_t trans_op_key_hash(unsigned long key[])
    265 {
    266         return (key[0] * 17 + key[1]);
    267 }
    268 
    269 static size_t trans_op_hash(const link_t *item)
    270 {
    271         gsp_trans_t *t = hash_table_get_instance(item, gsp_trans_t, link);
    272         unsigned long key[] = {
    273                 t->old_state,
    274                 t->input
    275         };
    276        
    277         return trans_op_key_hash(key);
    278 }
    279 
    280 static bool trans_op_match(unsigned long key[], size_t keys, const link_t *item)
    281 {
    282         gsp_trans_t *t = hash_table_get_instance(item, gsp_trans_t, link);
    283         return ((key[0] == (unsigned long) t->old_state)
    284             && (key[1] == (unsigned long) t->input));
    285 }
    286283
    287284/**
  • uspace/srv/hid/input/include/gsp.h

    rb17518e rbc216a0  
    5656/** Scancode parser transition. */
    5757typedef struct {
    58         link_t link;            /**< Link to hash table in @c gsp_t */
     58        ht_link_t link;         /**< Link to hash table in @c gsp_t */
    5959
    6060        /* Preconditions */
  • uspace/srv/ns/service.c

    rb17518e rbc216a0  
    4343/** Service hash table item. */
    4444typedef struct {
    45         link_t link;
     45        ht_link_t link;
    4646        sysarg_t service;        /**< Service ID. */
    4747        sysarg_t phone;          /**< Phone registered with the service. */
     
    4949} hashed_service_t;
    5050
    51 /** Compute hash index into service hash table.
    52  *
    53  * @param key Pointer keys. However, only the first key (i.e. service number)
    54  *            is used to compute the hash index.
    55  *
    56  * @return Hash index corresponding to key[0].
    57  *
    58  */
    59 static size_t service_key_hash(unsigned long key[])
     51
     52static size_t service_key_hash(void *key)
    6053{
    61         assert(key);
    62         return key[0];
     54        return *(sysarg_t*)key;
    6355}
    6456
    65 static size_t service_hash(const link_t *item)
     57static size_t service_hash(const ht_link_t *item)
    6658{
    67         hashed_service_t *hs = hash_table_get_instance(item, hashed_service_t, link);
    68         unsigned long key = hs->service;
    69         return service_key_hash(&key);
     59        hashed_service_t *hs = hash_table_get_inst(item, hashed_service_t, link);
     60        return hs->service;
    7061}
    7162
    72 /** Compare a key with hashed item.
    73  *
    74  * This compare function always ignores the third key.
    75  * It exists only to make it possible to remove records
    76  * originating from connection with key[1] in_phone_hash
    77  * value. Note that this is close to being classified
    78  * as a nasty hack.
    79  *
    80  * @param key  Array of keys.
    81  * @param keys Must be lesser or equal to 3.
    82  * @param item Pointer to a hash table item.
    83  *
    84  * @return Non-zero if the key matches the item, zero otherwise.
    85  *
    86  */
    87 static bool service_match(unsigned long key[], size_t keys, const link_t *item)
     63static bool service_key_equal(void *key, const ht_link_t *item)
    8864{
    89         assert(key);
    90         assert(keys <= 3);
    91         assert(item);
    92        
    93         hashed_service_t *hs = hash_table_get_instance(item, hashed_service_t, link);
    94        
    95         if (keys == 2)
    96                 return ((key[0] == hs->service) && (key[1] == hs->in_phone_hash));
    97         else
    98                 return (key[0] == hs->service);
    99 }
    100 
    101 /** Perform actions after removal of item from the hash table.
    102  *
    103  * @param item Item that was removed from the hash table.
    104  *
    105  */
    106 static void service_remove(link_t *item)
    107 {
    108         assert(item);
    109         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;
    11067}
    11168
     
    11471        .hash = service_hash,
    11572        .key_hash = service_key_hash,
    116         .match = service_match,
     73        .key_equal = service_key_equal,
    11774        .equal = 0,
    118         .remove_callback = service_remove
     75        .remove_callback = 0
    11976};
    12077
     
    13592int service_init(void)
    13693{
    137         if (!hash_table_create(&service_hash_table, 0, 3, &service_hash_table_ops)) {
     94        if (!hash_table_create(&service_hash_table, 0, 0, &service_hash_table_ops)) {
    13895                printf(NAME ": No memory available for services\n");
    13996                return ENOMEM;
     
    152109                pending_conn_t *pr = list_get_instance(cur, pending_conn_t, link);
    153110               
    154                 unsigned long keys[3] = {
    155                         pr->service,
    156                         0,
    157                         0
    158                 };
    159                
    160                 link_t *link = hash_table_find(&service_hash_table, keys);
     111                ht_link_t *link = hash_table_find(&service_hash_table, &pr->service);
    161112                if (!link)
    162113                        continue;
    163114               
    164                 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);
    165116                (void) ipc_forward_fast(pr->callid, hs->phone, pr->arg2,
    166117                    pr->arg3, 0, IPC_FF_NONE);
     
    183134int register_service(sysarg_t service, sysarg_t phone, ipc_call_t *call)
    184135{
    185         unsigned long keys[3] = {
    186                 service,
    187                 call->in_phone_hash,
    188                 0
    189         };
    190        
    191         if (hash_table_find(&service_hash_table, keys))
     136        if (hash_table_find(&service_hash_table, &service))
    192137                return EEXISTS;
    193138       
     
    196141                return ENOMEM;
    197142       
    198         link_initialize(&hs->link);
    199143        hs->service = service;
    200144        hs->phone = phone;
     
    217161{
    218162        sysarg_t retval;
    219         unsigned long keys[3] = {
    220                 service,
    221                 0,
    222                 0
    223         };
    224163       
    225         link_t *link = hash_table_find(&service_hash_table, keys);
     164        ht_link_t *link = hash_table_find(&service_hash_table, &service);
    226165        if (!link) {
    227166                if (IPC_GET_ARG4(*call) & IPC_FLAG_BLOCKING) {
     
    246185        }
    247186       
    248         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);
    249188        (void) ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call),
    250189            IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
  • uspace/srv/ns/task.c

    rb17518e rbc216a0  
    5353/** Task hash table item. */
    5454typedef struct {
    55         link_t link;
     55        ht_link_t link;
    5656       
    5757        task_id_t id;    /**< Task ID. */
     
    6161} hashed_task_t;
    6262
    63 /** Compute hash index into task hash table.
    64  *
    65  * @param key Pointer keys. However, only the first key (i.e. truncated task
    66  *            number) is used to compute the hash index.
    67  *
    68  * @return Hash index corresponding to key[0].
    69  *
    70  */
    71 static size_t task_key_hash(unsigned long key[])
    72 {
    73         size_t hash = 17;
    74         hash = 37 * hash + key[1];
    75         hash = 37 * hash + key[0];
    76         return hash;
    77 }
    78 
    79 static size_t task_hash(const link_t *item)
    80 {
    81         hashed_task_t *ht = hash_table_get_instance(item, hashed_task_t, link);
    82 
    83         unsigned long key[] = {
    84                 LOWER32(ht->id),
    85                 UPPER32(ht->id)
    86         };
    87        
    88         return task_key_hash(key);
    89 }
    90 
    91 /** Compare a key with hashed item.
    92  *
    93  * @param key  Array of keys.
    94  * @param keys Must be less than or equal to 2.
    95  * @param item Pointer to a hash table item.
    96  *
    97  * @return Non-zero if the key matches the item, zero otherwise.
    98  *
    99  */
    100 static bool task_match(unsigned long key[], size_t keys, const link_t *item)
    101 {
    102         assert(key);
    103         assert(keys == 2);
    104         assert(item);
    105        
    106         hashed_task_t *ht = hash_table_get_instance(item, hashed_task_t, link);
    107        
    108         return (key[0] == LOWER32(ht->id))
    109                 && (key[1] == UPPER32(ht->id));
    110 }
    111 
    112 /** Perform actions after removal of item from the hash table.
    113  *
    114  * @param item Item that was removed from the hash table.
    115  *
    116  */
    117 static void task_remove(link_t *item)
    118 {
    119         assert(item);
    120         free(hash_table_get_instance(item, hashed_task_t, link));
     63
     64static size_t task_key_hash(void *key)
     65{
     66        return *(task_id_t*)key;
     67}
     68
     69static size_t task_hash(const ht_link_t  *item)
     70{
     71        hashed_task_t *ht = hash_table_get_inst(item, hashed_task_t, link);
     72        return ht->id;
     73}
     74
     75static bool task_key_equal(void *key, const ht_link_t *item)
     76{
     77        hashed_task_t *ht = hash_table_get_inst(item, hashed_task_t, link);
     78        return ht->id == *(task_id_t*)key;
     79}
     80
     81/** Perform actions after removal of item from the hash table. */
     82static void task_remove(ht_link_t *item)
     83{
     84        free(hash_table_get_inst(item, hashed_task_t, link));
    12185}
    12286
     
    12589        .hash = task_hash,
    12690        .key_hash = task_key_hash,
    127         .match = task_match,
     91        .key_equal = task_key_equal,
    12892        .equal = 0,
    12993        .remove_callback = task_remove
     
    13498
    13599typedef struct {
    136         link_t link;
     100        ht_link_t link;
    137101        sysarg_t in_phone_hash;  /**< Incoming phone hash. */
    138102        task_id_t id;            /**< Task ID. */
    139103} p2i_entry_t;
    140104
    141 /** Compute hash index into task hash table.
    142  *
    143  * @param key Array of keys.
    144  *
    145  * @return Hash index corresponding to key[0].
    146  *
    147  */
    148 static size_t p2i_key_hash(unsigned long key[])
    149 {
    150         assert(key);
    151         return key[0];
    152 }
    153 
    154 static size_t p2i_hash(const link_t *item)
    155 {
    156         p2i_entry_t *entry = hash_table_get_instance(item, p2i_entry_t, link);
    157         unsigned long key = entry->in_phone_hash;
    158         return p2i_key_hash(&key);
    159 }
    160 
    161 /** Compare a key with hashed item.
    162  *
    163  * @param key  Array of keys.
    164  * @param keys Must be less than or equal to 1.
    165  * @param item Pointer to a hash table item.
    166  *
    167  * @return Non-zero if the key matches the item, zero otherwise.
    168  *
    169  */
    170 static bool p2i_match(unsigned long key[], size_t keys, const link_t *item)
    171 {
    172         assert(key);
    173         assert(keys == 1);
     105/* phone-to-id hash table operations */
     106
     107static size_t p2i_key_hash(void *key)
     108{
     109        sysarg_t in_phone_hash = *(sysarg_t*)key;
     110        return in_phone_hash;
     111}
     112
     113static size_t p2i_hash(const ht_link_t *item)
     114{
     115        p2i_entry_t *entry = hash_table_get_inst(item, p2i_entry_t, link);
     116        return entry->in_phone_hash;
     117}
     118
     119static bool p2i_key_equal(void *key, const ht_link_t *item)
     120{
     121        sysarg_t in_phone_hash = *(sysarg_t*)key;
     122        p2i_entry_t *entry = hash_table_get_inst(item, p2i_entry_t, link);
     123       
     124        return (in_phone_hash == entry->in_phone_hash);
     125}
     126
     127/** Perform actions after removal of item from the hash table.
     128 *
     129 * @param item Item that was removed from the hash table.
     130 *
     131 */
     132static void p2i_remove(ht_link_t *item)
     133{
    174134        assert(item);
    175        
    176         p2i_entry_t *entry = hash_table_get_instance(item, p2i_entry_t, link);
    177        
    178         return (key[0] == entry->in_phone_hash);
    179 }
    180 
    181 /** Perform actions after removal of item from the hash table.
    182  *
    183  * @param item Item that was removed from the hash table.
    184  *
    185  */
    186 static void p2i_remove(link_t *item)
    187 {
    188         assert(item);
    189         free(hash_table_get_instance(item, p2i_entry_t, link));
     135        free(hash_table_get_inst(item, p2i_entry_t, link));
    190136}
    191137
     
    194140        .hash = p2i_hash,
    195141        .key_hash = p2i_key_hash,
    196         .match = p2i_match,
     142        .key_equal = p2i_key_equal,
    197143        .equal = 0,
    198144        .remove_callback = p2i_remove
     
    213159int task_init(void)
    214160{
    215         if (!hash_table_create(&task_hash_table, 0, 2, &task_hash_table_ops)) {
     161        if (!hash_table_create(&task_hash_table, 0, 0, &task_hash_table_ops)) {
    216162                printf(NAME ": No memory available for tasks\n");
    217163                return ENOMEM;
    218164        }
    219165       
    220         if (!hash_table_create(&phone_to_id, 0, 1, &p2i_ops)) {
     166        if (!hash_table_create(&phone_to_id, 0, 0, &p2i_ops)) {
    221167                printf(NAME ": No memory available for tasks\n");
    222168                return ENOMEM;
     
    236182                pending_wait_t *pr = list_get_instance(cur, pending_wait_t, link);
    237183               
    238                 unsigned long keys[2] = {
    239                         LOWER32(pr->id),
    240                         UPPER32(pr->id)
    241                 };
    242                
    243                 link_t *link = hash_table_find(&task_hash_table, keys);
     184                ht_link_t *link = hash_table_find(&task_hash_table, &pr->id);
    244185                if (!link)
    245186                        continue;
    246187               
    247                 hashed_task_t *ht = hash_table_get_instance(link, hashed_task_t, link);
     188                hashed_task_t *ht = hash_table_get_inst(link, hashed_task_t, link);
    248189                if (!ht->finished)
    249190                        continue;
     
    256197                }
    257198               
    258                 hash_table_remove(&task_hash_table, keys, 2);
     199                hash_table_remove(&task_hash_table, &pr->id);
    259200                list_remove(cur);
    260201                free(pr);
     
    268209        task_exit_t texit;
    269210       
    270         unsigned long keys[2] = {
    271                 LOWER32(id),
    272                 UPPER32(id)
    273         };
    274        
    275         link_t *link = hash_table_find(&task_hash_table, keys);
     211        ht_link_t *link = hash_table_find(&task_hash_table, &id);
    276212        hashed_task_t *ht = (link != NULL) ?
    277             hash_table_get_instance(link, hashed_task_t, link) : NULL;
     213            hash_table_get_inst(link, hashed_task_t, link) : NULL;
    278214       
    279215        if (ht == NULL) {
     
    299235        }
    300236       
    301         hash_table_remove(&task_hash_table, keys, 2);
     237        hash_table_remove_item(&task_hash_table, link);
    302238        retval = EOK;
    303239       
     
    314250        task_id_t id = MERGE_LOUP32(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call));
    315251
    316         unsigned long keys[] = { call->in_phone_hash };
    317        
    318         link_t *link = hash_table_find(&phone_to_id, keys);
     252        ht_link_t *link = hash_table_find(&phone_to_id, &call->in_phone_hash);
    319253        if (link != NULL)
    320254                return EEXISTS;
     
    332266         */
    333267       
    334         link_initialize(&entry->link);
    335268        entry->in_phone_hash = call->in_phone_hash;
    336269        entry->id = id;
     
    341274         */
    342275       
    343         link_initialize(&ht->link);
    344276        ht->id = id;
    345277        ht->finished = false;
     
    353285static int get_id_by_phone(sysarg_t phone_hash, task_id_t *id)
    354286{
    355         unsigned long keys[1] = {phone_hash};
    356        
    357         link_t *link = hash_table_find(&phone_to_id, keys);
     287        ht_link_t *link = hash_table_find(&phone_to_id, &phone_hash);
    358288        if (link == NULL)
    359289                return ENOENT;
    360290       
    361         p2i_entry_t *entry = hash_table_get_instance(link, p2i_entry_t, link);
     291        p2i_entry_t *entry = hash_table_get_inst(link, p2i_entry_t, link);
    362292        *id = entry->id;
    363293       
     
    372302                return rc;
    373303       
    374         unsigned long keys[2] = {
    375                 LOWER32(id),
    376                 UPPER32(id)
    377         };
    378        
    379         link_t *link = hash_table_find(&task_hash_table, keys);
     304        ht_link_t *link = hash_table_find(&task_hash_table, &id);
    380305        hashed_task_t *ht = (link != NULL) ?
    381             hash_table_get_instance(link, hashed_task_t, link) : NULL;
     306            hash_table_get_inst(link, hashed_task_t, link) : NULL;
    382307       
    383308        if ((ht == NULL) || (ht->finished))
     
    393318int ns_task_disconnect(ipc_call_t *call)
    394319{
    395         unsigned long keys[2];
    396        
    397320        task_id_t id;
    398321        int rc = get_id_by_phone(call->in_phone_hash, &id);
     
    401324       
    402325        /* Delete from phone-to-id map. */
    403         keys[0] = call->in_phone_hash;
    404         hash_table_remove(&phone_to_id, keys, 1);
     326        hash_table_remove(&phone_to_id, &call->in_phone_hash);
    405327       
    406328        /* Mark task as finished. */
    407         keys[0] = LOWER32(id);
    408         keys[1] = UPPER32(id);
    409        
    410         link_t *link = hash_table_find(&task_hash_table, keys);
     329        ht_link_t *link = hash_table_find(&task_hash_table, &id);
    411330        if (link == NULL)
    412331                return EOK;
    413332
    414         hashed_task_t *ht =
    415             hash_table_get_instance(link, hashed_task_t, link);
     333        hashed_task_t *ht = hash_table_get_inst(link, hashed_task_t, link);
    416334       
    417335        ht->finished = true;
  • uspace/srv/vfs/vfs.h

    rb17518e rbc216a0  
    3636#include <async.h>
    3737#include <adt/list.h>
     38#include <adt/hash_table.h>
    3839#include <fibril_synch.h>
    3940#include <sys/types.h>
     
    112113        unsigned lnkcnt;
    113114
    114         link_t nh_link;         /**< Node hash-table link. */
     115        ht_link_t nh_link;              /**< Node hash-table link. */
    115116
    116117        vfs_node_type_t type;   /**< Partial info about the node type. */
  • uspace/srv/vfs/vfs_node.c

    rb17518e rbc216a0  
    4141#include <fibril_synch.h>
    4242#include <adt/hash_table.h>
     43#include <adt/hash.h>
    4344#include <assert.h>
    4445#include <async.h>
     
    5859#define KEY_INDEX       2
    5960
    60 static size_t nodes_key_hash(unsigned long []);
    61 static size_t nodes_hash(const link_t *);
    62 static bool nodes_match(unsigned long [], size_t, const link_t *);
     61static size_t nodes_key_hash(void *);
     62static size_t nodes_hash(const ht_link_t *);
     63static bool nodes_key_equal(void *, const ht_link_t *);
     64static vfs_triplet_t node_triplet(vfs_node_t *node);
    6365
    6466/** VFS node hash table operations. */
     
    6668        .hash = nodes_hash,
    6769        .key_hash = nodes_key_hash,
    68         .match = nodes_match,
     70        .key_equal = nodes_key_equal,
    6971        .equal = 0,
    7072        .remove_callback = 0,
     
    7779bool vfs_nodes_init(void)
    7880{
    79         return hash_table_create(&nodes, 0, 3, &nodes_ops);
     81        return hash_table_create(&nodes, 0, 0, &nodes_ops);
    8082}
    8183
     
    116118                 */
    117119               
    118                 unsigned long key[] = {
    119                         [KEY_FS_HANDLE] = node->fs_handle,
    120                         [KEY_DEV_HANDLE] = node->service_id,
    121                         [KEY_INDEX] = node->index
    122                 };
    123                
    124                 hash_table_remove(&nodes, key, 3);
     120                hash_table_remove_item(&nodes, &node->nh_link);
    125121                free_vfs_node = true;
    126122               
     
    160156{
    161157        fibril_mutex_lock(&nodes_mutex);
    162         unsigned long key[] = {
    163                 [KEY_FS_HANDLE] = node->fs_handle,
    164                 [KEY_DEV_HANDLE] = node->service_id,
    165                 [KEY_INDEX] = node->index
    166         };
    167         hash_table_remove(&nodes, key, 3);
     158        hash_table_remove_item(&nodes, &node->nh_link);
    168159        fibril_mutex_unlock(&nodes_mutex);
    169160        free(node);
     
    184175vfs_node_t *vfs_node_get(vfs_lookup_res_t *result)
    185176{
    186         unsigned long key[] = {
    187                 [KEY_FS_HANDLE] = result->triplet.fs_handle,
    188                 [KEY_DEV_HANDLE] = result->triplet.service_id,
    189                 [KEY_INDEX] = result->triplet.index
    190         };
    191         link_t *tmp;
    192177        vfs_node_t *node;
    193178
    194179        fibril_mutex_lock(&nodes_mutex);
    195         tmp = hash_table_find(&nodes, key);
     180        ht_link_t *tmp = hash_table_find(&nodes, &result->triplet);
    196181        if (!tmp) {
    197182                node = (vfs_node_t *) malloc(sizeof(vfs_node_t));
     
    207192                node->lnkcnt = result->lnkcnt;
    208193                node->type = result->type;
    209                 link_initialize(&node->nh_link);
    210194                fibril_rwlock_initialize(&node->contents_rwlock);
    211195                hash_table_insert(&nodes, &node->nh_link);
    212196        } else {
    213                 node = hash_table_get_instance(tmp, vfs_node_t, nh_link);
     197                node = hash_table_get_inst(tmp, vfs_node_t, nh_link);
    214198                if (node->type == VFS_NODE_UNKNOWN &&
    215199                    result->type != VFS_NODE_UNKNOWN) {
     
    242226}
    243227
    244 size_t nodes_key_hash(unsigned long key[])
    245 {
    246         /* Combine into a hash like they do in Effective Java, 2nd edition. */
    247         size_t hash = 17;
    248         hash = 37 * hash + key[KEY_FS_HANDLE];
    249         hash = 37 * hash + key[KEY_DEV_HANDLE];
    250         hash = 37 * hash + key[KEY_INDEX];
    251         return hash;
    252 }
    253 
    254 size_t nodes_hash(const link_t *item)
    255 {
    256         vfs_node_t *node = hash_table_get_instance(item, vfs_node_t, nh_link);
    257        
    258         unsigned long key[] = {
    259                 [KEY_FS_HANDLE] = node->fs_handle,
    260                 [KEY_DEV_HANDLE] = node->service_id,
    261                 [KEY_INDEX] = node->index
    262         };
    263        
    264         return nodes_key_hash(key);
    265 }
    266 
    267 
    268 bool nodes_match(unsigned long key[], size_t keys, const link_t *item)
    269 {
    270         vfs_node_t *node = hash_table_get_instance(item, vfs_node_t, nh_link);
    271         return (node->fs_handle == (fs_handle_t) key[KEY_FS_HANDLE]) &&
    272             (node->service_id == key[KEY_DEV_HANDLE]) &&
    273             (node->index == key[KEY_INDEX]);
    274 }
    275 
    276 
    277228struct refcnt_data {
    278229        /** Sum of all reference counts for this file system instance. */
     
    282233};
    283234
    284 static bool refcnt_visitor(link_t *item, void *arg)
    285 {
    286         vfs_node_t *node = hash_table_get_instance(item, vfs_node_t, nh_link);
     235static bool refcnt_visitor(ht_link_t *item, void *arg)
     236{
     237        vfs_node_t *node = hash_table_get_inst(item, vfs_node_t, nh_link);
    287238        struct refcnt_data *rd = (void *) arg;
    288239
     
    332283}
    333284
     285
     286static size_t nodes_key_hash(void *key)
     287{
     288        vfs_triplet_t *tri = key;
     289        size_t hash = hash_combine(tri->fs_handle, tri->index);
     290        return hash_combine(hash, tri->service_id);
     291}
     292
     293static size_t nodes_hash(const ht_link_t *item)
     294{
     295        vfs_node_t *node = hash_table_get_inst(item, vfs_node_t, nh_link);
     296        vfs_triplet_t tri = node_triplet(node);
     297        return nodes_key_hash(&tri);
     298}
     299
     300static bool nodes_key_equal(void *key, const ht_link_t *item)
     301{
     302        vfs_triplet_t *tri = key;
     303        vfs_node_t *node = hash_table_get_inst(item, vfs_node_t, nh_link);
     304        return node->fs_handle == tri->fs_handle
     305                && node->service_id == tri->service_id
     306                && node->index == tri->index;
     307}
     308
     309static inline vfs_triplet_t node_triplet(vfs_node_t *node)
     310{
     311        vfs_triplet_t tri = {
     312                .fs_handle = node->fs_handle,
     313                .service_id = node->service_id,
     314                .index = node->index
     315        };
     316       
     317        return tri;
     318}
     319
    334320/**
    335321 * @}
Note: See TracChangeset for help on using the changeset viewer.