Changeset bc216a0 in mainline for uspace/srv/fs/mfs/mfs_ops.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/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;
Note: See TracChangeset for help on using the changeset viewer.