Changeset 38fc00b in mainline for uspace/srv/fs/udf/udf_idx.c


Ignore:
Timestamp:
2012-10-09T20:42:32Z (12 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
97b199b1
Parents:
4e00f87
Message:

Convert UDF to use the new hash table.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/fs/udf/udf_idx.c

    r4e00f87 r38fc00b  
    4242#include <malloc.h>
    4343#include <adt/hash_table.h>
     44#include <adt/hash.h>
    4445#include <adt/list.h>
     46#include <bool.h>
    4547#include "udf_idx.h"
    4648#include "udf.h"
    4749
    48 #define UDF_IDX_KEYS  2
    49 #define UDF_IDX_KEY   1
    50 
    51 #define UDF_IDX_SERVICE_ID_KEY  0
    52 #define UDF_IDX_BUCKETS         1024
    53 
    5450static FIBRIL_MUTEX_INITIALIZE(udf_idx_lock);
    5551
    5652static hash_table_t udf_idx;
    5753
    58 /** Calculate value of hash by key
    59  *
    60  * @param Key for calculation of function
    61  *
    62  * @return Value of hash function
    63  *
    64  */
    65 static hash_index_t udf_idx_hash(unsigned long key[])
    66 {
    67         /* TODO: This is very simple and probably can be improved */
    68         return key[UDF_IDX_KEY] % UDF_IDX_BUCKETS;
    69 }
    70 
    71 /** Compare two items of hash table
    72  *
    73  * @param key  Key of hash table item. Include several items.
    74  * @param keys Number of parts of key.
    75  * @param item Item of hash table
    76  *
    77  * @return True if input value of key equivalent key of node from hash table
    78  *
    79  */
    80 static int udf_idx_compare(unsigned long key[], hash_count_t keys, link_t *item)
    81 {
    82         assert(keys > 0);
    83        
    84         udf_node_t *node = hash_table_get_instance(item, udf_node_t, link);
    85        
    86         if (node->instance->service_id !=
    87             ((service_id_t) key[UDF_IDX_SERVICE_ID_KEY]))
    88                 return false;
    89        
    90         assert(keys == 2);
    91         return (node->index == key[UDF_IDX_KEY]);
    92 }
    93 
    94 /** Remove callback
    95  *
    96  */
    97 static void udf_idx_remove_cb(link_t *link)
    98 {
    99         /* We don't use remove callback for this hash table */
    100 }
    101 
    102 static hash_table_operations_t udf_idx_ops = {
     54typedef struct {
     55        service_id_t service_id;
     56        fs_index_t index;
     57} udf_ht_key_t;
     58
     59static size_t udf_idx_hash(const ht_link_t *item)
     60{
     61        udf_node_t *node = hash_table_get_inst(item, udf_node_t, link);
     62        return hash_combine(node->instance->service_id, node->index);
     63}
     64
     65static size_t udf_idx_key_hash(void *k)
     66{
     67        udf_ht_key_t *key = (udf_ht_key_t *) k;
     68        return hash_combine(key->service_id, key->index);
     69}
     70
     71static bool udf_idx_key_equal(void *k, const ht_link_t *item)
     72{
     73        udf_ht_key_t *key = (udf_ht_key_t *) k;
     74        udf_node_t *node = hash_table_get_inst(item, udf_node_t, link);
     75
     76        return (key->service_id == node->instance->service_id) &&
     77            (key->index == node->index);
     78}
     79
     80static hash_table_ops_t udf_idx_ops = {
    10381        .hash = udf_idx_hash,
    104         .compare = udf_idx_compare,
    105         .remove_callback = udf_idx_remove_cb,
     82        .key_hash = udf_idx_key_hash,
     83        .key_equal = udf_idx_key_equal,
     84        .equal = NULL,
     85        .remove_callback = NULL
    10686};
    10787
     
    11393int udf_idx_init(void)
    11494{
    115         if (!hash_table_create(&udf_idx, UDF_IDX_BUCKETS, UDF_IDX_KEYS,
    116             &udf_idx_ops))
     95        if (!hash_table_create(&udf_idx, 0, 0, &udf_idx_ops))
    11796                return ENOMEM;
    11897       
     
    143122{
    144123        fibril_mutex_lock(&udf_idx_lock);
    145        
    146         unsigned long key[] = {
    147                 [UDF_IDX_SERVICE_ID_KEY] = instance->service_id,
    148                 [UDF_IDX_KEY] = index
     124
     125        udf_ht_key_t key = {
     126                .service_id = instance->service_id,
     127                .index = index
    149128        };
    150129       
    151         link_t *already_open = hash_table_find(&udf_idx, key);
     130        ht_link_t *already_open = hash_table_find(&udf_idx, &key);
    152131        if (already_open) {
    153                 udf_node_t *node = hash_table_get_instance(already_open,
     132                udf_node_t *node = hash_table_get_inst(already_open,
    154133                    udf_node_t, link);
    155134                node->ref_cnt++;
     
    202181       
    203182        fibril_mutex_initialize(&udf_node->lock);
    204         link_initialize(&udf_node->link);
    205183        fs_node->data = udf_node;
    206184       
    207         unsigned long key[] = {
    208                 [UDF_IDX_SERVICE_ID_KEY] = instance->service_id,
    209                 [UDF_IDX_KEY] = index
    210         };
    211        
    212         hash_table_insert(&udf_idx, key, &udf_node->link);
     185        hash_table_insert(&udf_idx, &udf_node->link);
    213186        instance->open_nodes_count++;
    214187       
     
    232205        fibril_mutex_lock(&udf_idx_lock);
    233206       
    234         unsigned long key[] = {
    235                 [UDF_IDX_SERVICE_ID_KEY] = node->instance->service_id,
    236                 [UDF_IDX_KEY] = node->index
    237         };
    238        
    239         hash_table_remove(&udf_idx, key, UDF_IDX_KEYS);
     207        hash_table_remove_item(&udf_idx, &node->link);
    240208       
    241209        assert(node->instance->open_nodes_count > 0);
Note: See TracChangeset for help on using the changeset viewer.