Changeset bc216a0 in mainline for uspace/srv/vfs/vfs_node.c


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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.